Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
bin.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//
20// Bin.cpp
21//
22
23#include "stdAfx.h"
24#include "bin.h"
25#include "assert.h"
26#include "list.h"
27
28Bin::Bin ( int size )
29{
30 assert ( size > 0 );
31 num_buckets = size;
32 sh_item = NULL;
33
34 bucket = new List[size];
35
36}
37
39{
40 Clear ();
41
42 delete [] bucket;
43
44}
45
46
47
48void Bin::Clear ( void )
49{
50 int count = num_buckets;
51 sh_item = NULL;
52 while ( count-- )
53 {
54 List *head = &bucket[count];
55 BinItem *item;
56
57 while ( ( item = (BinItem *) head->Next ()))
58 {
59 Remove ( item );
60 }
61 }
62
63}
64
65void* Bin::Get ( OLECHAR *text1, OLECHAR *text2 )
66{
67 BinItem *item;
68
69 if ( ( item = GetBinItem ( text1, text2 )) )
70 {
71 return item->Item();
72 }
73
74 return NULL;
75}
76
77void* Bin::GetNext ( void )
78{
79 BinItem *item;
80
81 if ( ( item = GetNextBinItem ( )) )
82 {
83 return item->Item();
84 }
85
86 return NULL;
87}
88
89void Bin::Add ( void *data, OLECHAR *text1, OLECHAR *text2 )
90{
91 BinItem *item;
92 List *list;
93 int hash;
94
95 sh_item = NULL;
96
97 hash = calc_hash ( text1 );
98 item = new BinItem ( data, hash, text1, text2 );
99
100 list = &bucket[hash%num_buckets];
101
102 list->AddToTail ( (ListNode *) item );
103
104}
105
106BinItem* Bin::GetBinItem ( OLECHAR *text1, OLECHAR *text2)
107{
108
109 sh_size1 = sh_size2 = 0;
110 sh_text1 = text1;
111 sh_text2 = text2;
112
113 sh_hash = calc_hash ( text1 );
114
115 if ( sh_text1 )
116 {
117 sh_size1 = wcslen ( sh_text1 );
118 }
119
120 if ( sh_text2 )
121 {
122 sh_size2 = wcslen ( sh_text2 );
123 }
124
125 sh_item = (BinItem *) &bucket[sh_hash%num_buckets];
126
127
128 return GetNextBinItem ();
129}
130
132{
133 if ( sh_item )
134 {
135 sh_item = (BinItem *) sh_item->Next ();
136 }
137
138 while ( sh_item )
139 {
140 if ( sh_item->Same ( sh_hash, sh_text1, sh_size1, sh_text2, sh_size2 ))
141 {
142 break;
143 }
144 sh_item = (BinItem *) sh_item->Next ();
145 }
146
147 return sh_item;
148}
149
151{
152 BinItem *bitem = NULL;
153 int i;
154
155
156 for ( i=0; i< num_buckets; i++)
157 {
158
159 if ( ( bitem = (BinItem *) bucket[i].Find ( item )))
160 {
161 break;
162 }
163
164 }
165
166 return bitem;
167
168}
169
170void Bin::Remove ( void *item )
171{
172 BinItem *bitem;
173
174 if ( ( bitem = GetBinItem ( item ) ))
175 {
176 Remove ( bitem );
177 }
178
179}
180
181void Bin::Remove ( OLECHAR *text1, OLECHAR *text2 )
182{
183 BinItem *bitem;
184
185 if ( ( bitem = GetBinItem ( text1, text2 ) ))
186 {
187 Remove ( bitem );
188 }
189
190}
191
192void Bin::Remove ( BinItem *item )
193{
194 sh_item = NULL;
195 item->Remove ();
196 delete item ;
197
198}
199
200
201BinItem::BinItem ( void *data, int new_hash, OLECHAR *new_text1, OLECHAR *new_text2 )
202{
203 SetItem ( data );
204 hash = new_hash;
205
206 if ( (text1 = new_text1) )
207 {
208 text1size = wcslen ( text1 );
209 }
210 else
211 {
212 text1size = 0;
213 }
214
215 if ( (text2 = new_text2) )
216 {
217 text2size = wcslen ( text2 );
218 }
219 else
220 {
221 text2size = 0;
222 }
223}
224
225int BinItem::Same ( int chash, OLECHAR *ctext1, int size1, OLECHAR *ctext2, int size2 )
226{
227 if ( hash != chash || text1size != size1 || text2size != size2 )
228 {
229 return FALSE;
230 }
231
232 if ( wcsicmp ( text1, ctext1 ))
233 {
234 return FALSE;
235 }
236
237 if ( text2 && ctext2 && wcsicmp ( text2, ctext2 ))
238 {
239 return FALSE;
240 }
241
242 return TRUE;
243
244
245}
246int Bin::calc_hash ( OLECHAR *text )
247{
248 int hash = 0;
249
250 while ( *text )
251 {
252 hash += *text++;
253 }
254
255 return hash;
256
257}
258
259// Bin ID code
260
261BinID::BinID ( int size )
262{
263 assert ( size > 0 );
264 num_buckets = size;
265
266 bucket = new List[size];
267
268}
269
271{
272 Clear ();
273
274 delete [] bucket;
275
276}
277
278
279
280void BinID::Clear ( void )
281{
282 int count = num_buckets;
283
284 while ( count-- )
285 {
286 List *head = &bucket[count];
287 BinIDItem *item;
288
289 while ( ( item = (BinIDItem *) head->Next ()))
290 {
291 Remove ( item );
292 }
293 }
294
295}
296
297void* BinID::Get ( int id)
298{
299 BinIDItem *item;
300
301 if ( ( item = GetBinIDItem ( id )) )
302 {
303 return item->Item();
304 }
305
306 return NULL;
307}
308
309void BinID::Add ( void *data, int id )
310{
311 BinIDItem *item;
312 List *list;
313
314
315 item = new BinIDItem ( data, id );
316
317 list = &bucket[id%num_buckets];
318
319 list->AddToTail ( (ListNode *) item );
320
321}
322
324{
325 BinIDItem *item;
326
327
328 item = (BinIDItem *) bucket[id%num_buckets].Next();
329
330 while ( item )
331 {
332 if ( item->Same ( id ))
333 {
334 break;
335 }
336 item = (BinIDItem *) item->Next ();
337 }
338
339 return item ;
340}
341
342
344{
345 BinIDItem *bitem = NULL;
346 int i;
347
348
349 for ( i=0; i< num_buckets; i++)
350 {
351
352 if ( ( bitem = (BinIDItem *) bucket[i].Find ( item )))
353 {
354 break;
355 }
356
357 }
358
359 return bitem;
360
361}
362
363void BinID::Remove ( void *item )
364{
365 BinIDItem *bitem;
366
367 if ( ( bitem = GetBinIDItem ( item ) ))
368 {
369 Remove ( bitem );
370 }
371
372}
373
374void BinID::Remove ( int id )
375{
376 BinIDItem *bitem;
377
378 if ( ( bitem = GetBinIDItem ( id ) ))
379 {
380 Remove ( bitem );
381 }
382
383}
384
386{
387 item->Remove ();
388 delete item ;
389
390}
391
392
393BinIDItem::BinIDItem ( void *data, int new_id )
394{
395 SetItem ( data );
396 id = new_id;
397
398}
399
400int BinIDItem::Same ( int compare_id )
401{
402 return id == compare_id;
403}
404
#define NULL
Definition BaseType.h:92
#define TRUE
Definition BaseType.h:109
#define FALSE
Definition BaseType.h:113
void Add(void *item, OLECHAR *text1, OLECHAR *text2=NULL)
Definition bin.cpp:89
void Clear(void)
Definition bin.cpp:48
Bin(int size=256)
Definition bin.cpp:28
void * Get(OLECHAR *text1, OLECHAR *text2=NULL)
Definition bin.cpp:65
~Bin()
Definition bin.cpp:38
void * GetNext(void)
Definition bin.cpp:77
BinItem * GetNextBinItem(void)
Definition bin.cpp:131
void Remove(void *item)
Definition bin.cpp:170
BinItem * GetBinItem(OLECHAR *text1, OLECHAR *text2=NULL)
Definition bin.cpp:106
BinIDItem * GetBinIDItem(int id)
Definition bin.cpp:323
void * Get(int id)
Definition bin.cpp:297
void Add(void *item, int id)
Definition bin.cpp:309
~BinID()
Definition bin.cpp:270
BinID(int size=256)
Definition bin.cpp:261
void Remove(void *item)
Definition bin.cpp:363
void Clear(void)
Definition bin.cpp:280
int Same(int id)
Definition bin.cpp:400
BinIDItem(void *data, int id)
Definition bin.cpp:393
Definition bin.h:31
int Same(int chash, OLECHAR *ctext1, int size1, OLECHAR *ctext2, int size2)
Definition bin.cpp:225
BinItem(void *data, int hash, OLECHAR *text1, OLECHAR *text2)
Definition bin.cpp:201
Definition list.h:60
void AddToTail(ListNode *node)
Definition list.cpp:172
void SetItem(void *item)
Definition list.cpp:134
void Remove(void)
Definition list.cpp:63
ListNode * Next(void)
Definition list.cpp:70
void * Item(void)
Definition list.cpp:125