Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
hash.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/* $Header: /VSS_Sync/wwlib/hash.cpp 3 10/17/00 4:48p Vss_sync $ */
20/***********************************************************************************************
21 *** Confidential - Westwood Studios ***
22 ***********************************************************************************************
23 * *
24 * Project Name : Commando / G 3D Library *
25 * *
26 * $Archive:: /VSS_Sync/wwlib/hash.cpp $*
27 * *
28 * Author:: Greg_h *
29 * *
30 * $Modtime:: 10/16/00 11:42a $*
31 * *
32 * $Revision:: 3 $*
33 * *
34 *---------------------------------------------------------------------------------------------*
35 * Functions: *
36 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
37
38#include "hash.h"
39#include "wwdebug.h"
40#include "realcrc.h"
41#ifdef _UNIX
42#include "osdep.h"
43#endif
44
45#include <string.h>
46
47/*
48** HashTableClass
49*/
51 HashTableSize( size )
52{
53 // Assert HashTableSize is a power of 2
54 WWASSERT( (HashTableSize & (HashTableSize-1)) == 0 );
55
56 // Allocate and clear the table
57 HashTable = W3DNEWARRAY HashableClass * [ HashTableSize ];
58 Reset();
59}
60
62{
63 // If we need to, free the hash table
64 if ( HashTable != NULL) {
65 delete [] HashTable;
66 HashTable = NULL;
67 }
68}
69
71{
72 for ( int i = 0; i < HashTableSize; i++ ) {
73 HashTable[i] = NULL;
74 }
75}
76
78{
79 WWASSERT( entry != NULL);
80
81 int index = Hash( entry->Get_Key() );
82 WWASSERT( entry->NextHash == NULL );
83 entry->NextHash = HashTable[ index ];
84 HashTable[ index ] = entry;
85}
86
88{
89 WWASSERT(entry != NULL);
90
91 // Find in the hash table.
92 const char *key = entry->Get_Key();
93 int index = Hash( key );
94
95 if ( HashTable[ index ] != NULL ) {
96
97 // Special check for first entry
98 if ( HashTable[ index ] == entry ) {
99 HashTable[ index ] = entry->NextHash;
100 return true;
101 }
102
103 // Search the list for the entry, and remove it
104 HashableClass * node = HashTable[ index ];
105 while ( node->NextHash != NULL ) {
106 if ( node->NextHash == entry ) {
107 node->NextHash = entry->NextHash;
108 return true;
109 }
110 node = node->NextHash;
111 }
112 }
113
114 return false;
115}
116
118{
119 // Find in the hash table.
120 int index = Hash( key );
121 for ( HashableClass * node = HashTable[ index ]; node != NULL; node = node->NextHash ) {
122 if ( ::stricmp( node->Get_Key(), key ) == 0 ) {
123 return node;
124 }
125 }
126 return NULL;
127}
128
129int HashTableClass::Hash( const char * key )
130{
131 return CRC_Stringi( key ) & (HashTableSize-1);
132}
133
134
135/*
136**
137*/
139{
140 Index = 0;
141 NextEntry = Table.HashTable[ Index ];
142 Advance_Next();
143 Next(); // Accept the next we found, and go to the next next
144}
145
147{
148 CurrentEntry = NextEntry;
149 if ( NextEntry != NULL ) {
150 NextEntry = NextEntry->NextHash;
151 Advance_Next();
152 }
153}
154
155void HashTableIteratorClass::Advance_Next(void)
156{
157 while ( NextEntry == NULL ) {
158 Index++;
159 if ( Index >= Table.HashTableSize ) {
160 return; // Done!
161 }
162 NextEntry = Table.HashTable[ Index ];
163 }
164}
165
166
#define NULL
Definition BaseType.h:92
#define WWASSERT
#define W3DNEWARRAY
Definition always.h:110
unsigned long CRC_Stringi(const char *string, unsigned long crc)
Definition realcrc.cpp:168
void Add(HashableClass *entry)
Definition hash.cpp:77
~HashTableClass(void)
Definition hash.cpp:61
bool Remove(HashableClass *entry)
Definition hash.cpp:87
HashableClass * Find(const char *key)
Definition hash.cpp:117
void Reset(void)
Definition hash.cpp:70
HashTableClass(int size)
Definition hash.cpp:50
virtual const char * Get_Key(void)=0