Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
search.h
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 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
21 ***********************************************************************************************
22 * *
23 * Project Name : Command & Conquer *
24 * *
25 * $Archive:: /Commando/Library/SEARCH.H $*
26 * *
27 * $Author:: Greg_h $*
28 * *
29 * $Modtime:: 7/22/97 11:37a $*
30 * *
31 * $Revision:: 1 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * IndexClass<T>::Add_Index -- Add element to index tracking system. *
36 * IndexClass<T>::Clear -- Clear index handler to empty state. *
37 * IndexClass<T>::Count -- Fetch the number of index entries recorded. *
38 * IndexClass<T>::Fetch_Index -- Fetch data from specified index. *
39 * IndexClass<T>::Increase_Table_Size -- Increase the internal index table capacity. *
40 * IndexClass<T>::IndexClass -- Constructor for index handler. *
41 * IndexClass<T>::Invalidate_Archive -- Invalidate the archive pointer. *
42 * IndexClass<T>::Is_Archive_Same -- Checks to see if archive pointer is same as index. *
43 * IndexClass<T>::Is_Present -- Checks for presense of index entry. *
44 * IndexClass<T>::Remove_Index -- Find matching index and remove it from system. *
45 * IndexClass<T>::Search_For_Node -- Perform a search for the specified node ID *
46 * IndexClass<T>::Set_Archive -- Records the node pointer into the archive. *
47 * IndexClass<T>::Sort_Nodes -- Sorts nodes in preparation for a binary search. *
48 * IndexClass<T>::~IndexClass -- Destructor for index handler object. *
49 * compfunc -- Support function for bsearch and bsort. *
50 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
51
52#ifndef SEARCH_H
53#define SEARCH_H
54
55/*
56** The "bool" integral type was defined by the C++ comittee in
57** November of '94. Until the compiler supports this, use the following
58** definition.
59*/
60#include "bool.h"
61
62
63#if !defined(__BORLANDC__) || !defined(_USERENTRY)
64#define _USERENTRY
65#endif
66
67/*
68** This class is used to create and maintain an index. It does this by assigning a unique
69** identifier number to the type objects that it is indexing. The regular binary sort and search
70** function are used for speedy index retrieval. Typical use of this would be to index pointers to
71** normal data objects, but it can be used to hold the data objects themselves. Keep in mind that
72** the data object "T" is copied around by this routine in the internal tables so not only must
73** it have a valid copy constructor, it must also be efficient. The internal algorithm will
74** create an arbitrary number of default constructed objects of type "T". Make sure it has a
75** default constructor that is efficient and trivial. The constructor need not perform any actual
76** initialization since this class has prior knowledge about the legality of these temporary
77** objects and doesn't use them until after the copy constructor is used to initialize them.
78** This means that the default constructed object of type "T" can be technically in an unusable
79** state since it won't ever be used by this routine and won't ever be returned by any of
80** this template's methods.
81**
82** This should properly be called a "map container class" since it is a container with a
83** mapping of an identifier with the element in the container.
84*/
85
86template<class T>
87class IndexClass
88{
89 public:
92
93 /*
94 ** Add element to index table.
95 */
96 bool Add_Index(int id, T data);
97
98 /*
99 ** Removes an index entry from the index table.
100 */
101 bool Remove_Index(int id);
102
103 /*
104 ** Check to see if index is present.
105 */
106 bool Is_Present(int id) const;
107
108 /*
109 ** Fetch number of indexes in the table.
110 */
111 int Count(void) const;
112
113 /*
114 ** Actually a fetch an index data element from the table.
115 */
116 T Fetch_Index(int id) const;
117
118 /*
119 ** Clear out the index table to null (empty) state.
120 */
121 void Clear(void);
122
123 private:
124 /*
125 ** This node object is used to keep track of the connection between the data
126 ** object and the index identifier number.
127 */
128 struct NodeElement {
129 int ID; // ID number (must be first element in this structure).
130 T Data; // Data element assigned to this ID number.
131 };
132
133 /*
134 ** This is the pointer to the allocated index table. It contains all valid nodes in
135 ** a sorted order.
136 */
137 NodeElement * IndexTable;
138
139 /*
140 ** This records the number of valid nodes within the index table.
141 */
142 int IndexCount;
143
144 /*
145 ** The total size (in nodes) of the index table is recorded here. If adding a node
146 ** would cause the index count to exceed this value, the index table must be resized
147 ** to make room.
148 */
149 int IndexSize;
150
151 /*
152 ** If the index table is sorted and ready for searching, this flag will be true. Sorting
153 ** of the table only occurs when absolutely necessary.
154 */
155 bool IsSorted;
156
157 /*
158 ** This records a pointer to the last element found by the Is_Present() function. Using
159 ** this last recorded value can allow quick fetches of data whenever possible.
160 */
161 NodeElement const * Archive;
162
163 //-------------------------------------------------------------------------------------
164 IndexClass(IndexClass const & rvalue);
165 IndexClass * operator = (IndexClass const & rvalue);
166
167 /*
168 ** Increase size of internal index table by amount specified.
169 */
170 bool Increase_Table_Size(int amount);
171
172 /*
173 ** Check if archive pointer is the same as that requested.
174 */
175 bool Is_Archive_Same(int id) const;
176
177 /*
178 ** Invalidate the archive pointer.
179 */
180 void Invalidate_Archive(void);
181
182 /*
183 ** Set archive to specified value.
184 */
185 void Set_Archive(NodeElement const * node);
186
187 /*
188 ** Search for the node in the index table.
189 */
190 NodeElement const * Search_For_Node(int id) const;
191
192 static int _USERENTRY search_compfunc(void const * ptr, void const * ptr2);
193};
194
195
196/***********************************************************************************************
197 * IndexClass<T>::IndexClass -- Constructor for index handler. *
198 * *
199 * This constructs an empty index handler. *
200 * *
201 * INPUT: none *
202 * *
203 * OUTPUT: none *
204 * *
205 * WARNINGS: none *
206 * *
207 * HISTORY: *
208 * 11/02/1996 JLB : Created. *
209 *=============================================================================================*/
210template<class T>
212 IndexTable(0),
213 IndexCount(0),
214 IndexSize(0),
215 IsSorted(false),
216 Archive(0)
217{
218 Invalidate_Archive();
219}
220
221
222/***********************************************************************************************
223 * IndexClass<T>::~IndexClass -- Destructor for index handler object. *
224 * *
225 * This will destruct and free any resources managed by this index handler. *
226 * *
227 * INPUT: none *
228 * *
229 * OUTPUT: none *
230 * *
231 * WARNINGS: none *
232 * *
233 * HISTORY: *
234 * 11/02/1996 JLB : Created. *
235 *=============================================================================================*/
236template<class T>
238{
239 Clear();
240}
241
242
243/***********************************************************************************************
244 * IndexClass<T>::Clear -- Clear index handler to empty state. *
245 * *
246 * This routine will free all internal resources and bring the index handler into a *
247 * known empty state. After this routine, the index handler is free to be reused. *
248 * *
249 * INPUT: none *
250 * *
251 * OUTPUT: none *
252 * *
253 * WARNINGS: none *
254 * *
255 * HISTORY: *
256 * 11/02/1996 JLB : Created. *
257 *=============================================================================================*/
258template<class T>
259void IndexClass<T>::Clear(void)
260{
261 delete [] IndexTable;
262 IndexTable = 0;
263 IndexCount = 0;
264 IndexSize = 0;
265 IsSorted = false;
266 Invalidate_Archive();
267}
268
269
270/***********************************************************************************************
271 * IndexClass<T>::Increase_Table_Size -- Increase the internal index table capacity. *
272 * *
273 * This helper function will increase the capacity of the internal index table without *
274 * performing any alterations to the index data. Use this routine prior to adding a new *
275 * element if the existing capacity is insufficient. *
276 * *
277 * INPUT: amount -- The number of index element spaces to add to its capacity. *
278 * *
279 * OUTPUT: bool; Was the internal capacity increased without error? *
280 * *
281 * WARNINGS: If there is insufficient RAM, then this routine will fail. *
282 * *
283 * HISTORY: *
284 * 11/02/1996 JLB : Created. *
285 *=============================================================================================*/
286template<class T>
287bool IndexClass<T>::Increase_Table_Size(int amount)
288{
289 /*
290 ** Check size increase parameter for legality.
291 */
292 if (amount < 0) return(false);
293
294 NodeElement * table = W3DNEWARRAY NodeElement[IndexSize + amount];
295 if (table != NULL) {
296
297 /*
298 ** Copy all valid nodes into the new table.
299 */
300 for (int index = 0; index < IndexCount; index++) {
301 table[index] = IndexTable[index];
302 }
303
304 /*
305 ** Make the new table the current one (and delete the old one).
306 */
307 delete [] IndexTable;
308 IndexTable = table;
309 IndexSize += amount;
310 Invalidate_Archive();
311
312 /*
313 ** Return with success flag.
314 */
315 return(true);
316 }
317
318 /*
319 ** Failure to allocate the memory results in a failure to increase
320 ** the size of the index table.
321 */
322 return(false);
323}
324
325
326/***********************************************************************************************
327 * IndexClass<T>::Count -- Fetch the number of index entries recorded. *
328 * *
329 * This will return the quantity of index entries that have been recored by this index *
330 * handler. *
331 * *
332 * INPUT: none *
333 * *
334 * OUTPUT: Returns with number of recored indecies present. *
335 * *
336 * WARNINGS: none *
337 * *
338 * HISTORY: *
339 * 11/02/1996 JLB : Created. *
340 *=============================================================================================*/
341template<class T>
342int IndexClass<T>::Count(void) const
343{
344 return(IndexCount);
345}
346
347
348/***********************************************************************************************
349 * IndexClass<T>::Is_Present -- Checks for presense of index entry. *
350 * *
351 * This routine will scan for the specified index entry. If it was found, then 'true' is *
352 * returned. *
353 * *
354 * INPUT: id -- The index ID to search for. *
355 * *
356 * OUTPUT: bool; Was the index entry found in this index handler object? *
357 * *
358 * WARNINGS: none *
359 * *
360 * HISTORY: *
361 * 11/02/1996 JLB : Created. *
362 *=============================================================================================*/
363template<class T>
365{
366 /*
367 ** If there are no data elements in the index table, then it can
368 ** never find the specified index. Check for and return failure
369 ** in this case.
370 */
371 if (IndexCount == 0) {
372 return(false);
373 }
374
375 /*
376 ** Check to see if this same index element was previously searched for. If
377 ** so and it was previously found, then there is no need to search for it
378 ** again -- just return true.
379 */
380 if (Is_Archive_Same(id)) {
381 return(true);
382 }
383
384 /*
385 ** Perform a binary search on the index nodes in order to look for a
386 ** matching index value.
387 */
388 NodeElement const * nodeptr = Search_For_Node(id);
389
390 /*
391 ** If a matching index was found, then record it for future reference and return success.
392 */
393 if (nodeptr != 0) {
394 ((IndexClass<T> *)this)->Set_Archive(nodeptr);
395 return(true);
396 }
397
398 /*
399 ** Could not find element so return failure condition.
400 */
401 return(false);
402}
403
404
405/***********************************************************************************************
406 * IndexClass<T>::Fetch_Index -- Fetch data from specified index. *
407 * *
408 * This routine will find the specified index and return the data value associated with it. *
409 * *
410 * INPUT: id -- The index ID to search for. *
411 * *
412 * OUTPUT: Returns with the data value associated with the index value. *
413 * *
414 * WARNINGS: This routine presumes that the index exists. If it doesn't exist, then the *
415 * default constructed object "T" is returned instead. To avoid this problem, *
416 * always verfiy the existance of the index by calling Is_Present() first. *
417 * *
418 * HISTORY: *
419 * 11/02/1996 JLB : Created. *
420 *=============================================================================================*/
421#ifdef __BORLANDC__
422#pragma warn -def
423#endif
424template<class T>
426{
427 if (Is_Present(id)) {
428
429 /*
430 ** Count on the fact that the archive pointer is always valid after a call to Is_Present
431 ** that returns "true".
432 */
433 return(Archive->Data);
434 }
435 static T x;
436 return(x);
437}
438#ifdef __BORLANDC__
439#pragma warn .def
440#endif
441
442
443/***********************************************************************************************
444 * IndexClass<T>::Is_Archive_Same -- Checks to see if archive pointer is same as index. *
445 * *
446 * This routine compares the specified index ID with the previously recorded index archive *
447 * pointer in order to determine if they match. *
448 * *
449 * INPUT: id -- The index ID to compare to the archive index object pointer. *
450 * *
451 * OUTPUT: bool; Does the specified index match the archive pointer? *
452 * *
453 * WARNINGS: none *
454 * *
455 * HISTORY: *
456 * 11/02/1996 JLB : Created. *
457 *=============================================================================================*/
458template<class T>
459bool IndexClass<T>::Is_Archive_Same(int id) const
460{
461 if (Archive != 0 && Archive->ID == id) {
462 return(true);
463 }
464 return(false);
465}
466
467
468/***********************************************************************************************
469 * IndexClass<T>::Invalidate_Archive -- Invalidate the archive pointer. *
470 * *
471 * This routine will set the archive pointer to an invalid state. This must be performed *
472 * if ever the archive pointer would become illegal (such as when the element it refers to *
473 * is deleted). *
474 * *
475 * INPUT: none *
476 * *
477 * OUTPUT: none *
478 * *
479 * WARNINGS: none *
480 * *
481 * HISTORY: *
482 * 11/02/1996 JLB : Created. *
483 *=============================================================================================*/
484template<class T>
485void IndexClass<T>::Invalidate_Archive(void)
486{
487 Archive = 0;
488}
489
490
491/***********************************************************************************************
492 * IndexClass<T>::Set_Archive -- Records the node pointer into the archive. *
493 * *
494 * This routine records the specified node pointer. Use this routine when there is a *
495 * good chance that the specified node will be requested in the immediate future. *
496 * *
497 * INPUT: node -- Pointer to the node to assign to the archive. *
498 * *
499 * OUTPUT: none *
500 * *
501 * WARNINGS: none *
502 * *
503 * HISTORY: *
504 * 11/02/1996 JLB : Created. *
505 *=============================================================================================*/
506template<class T>
507void IndexClass<T>::Set_Archive(NodeElement const * node)
508{
509 Archive = node;
510}
511
512
513/***********************************************************************************************
514 * IndexClass<T>::Add_Index -- Add element to index tracking system. *
515 * *
516 * This routine will record the index information into this index manager object. It *
517 * associates the index number with the data specified. The data is copied to an internal *
518 * storage location. *
519 * *
520 * INPUT: id -- The ID number to associate with the data. *
521 * *
522 * data -- The data to store. *
523 * *
524 * OUTPUT: bool; Was the element added without error? Failure indicates that RAM has been *
525 * exhausted. *
526 * *
527 * WARNINGS: The data is COPIED to internal storage. This means that the data object must *
528 * have a functional and efficient copy constructor and assignment operator. *
529 * *
530 * HISTORY: *
531 * 11/02/1996 JLB : Created. *
532 *=============================================================================================*/
533template<class T>
534bool IndexClass<T>::Add_Index(int id, T data)
535{
536 /*
537 ** Ensure that there is enough room to add this index. If not, then increase the
538 ** capacity of the internal index table.
539 */
540 if (IndexCount + 1 > IndexSize) {
541 if (!Increase_Table_Size(IndexSize == 0 ? 10 : IndexSize)) {
542
543 /*
544 ** Failure to increase the size of the index table means failure to add
545 ** the index element.
546 */
547 return(false);
548 }
549 }
550
551 /*
552 ** Add the data to the end of the index data and then sort the index table.
553 */
554 IndexTable[IndexCount].ID = id;
555 IndexTable[IndexCount].Data = data;
556 IndexCount++;
557 IsSorted = false;
558
559 return(true);
560}
561
562
563/***********************************************************************************************
564 * IndexClass<T>::Remove_Index -- Find matching index and remove it from system. *
565 * *
566 * This will scan through the previously added index elements and if a match was found, it *
567 * will be removed. *
568 * *
569 * INPUT: id -- The index ID to search for and remove. *
570 * *
571 * OUTPUT: bool; Was the index element found and removed? *
572 * *
573 * WARNINGS: none *
574 * *
575 * HISTORY: *
576 * 11/02/1996 JLB : Created. *
577 *=============================================================================================*/
578template<class T>
580{
581 /*
582 ** Find the array index into the table that matches the specified id value.
583 */
584 int found_index = -1;
585 for (int index = 0; index < IndexCount; index++) {
586 if (IndexTable[index].ID == id) {
587 found_index = index;
588 break;
589 }
590 }
591
592 /*
593 ** If the array index was found, then copy all higher index entries
594 ** downward to fill the vacated location. We cannot use memcpy here because the type
595 ** object may not support raw copies. C++ defines the assignment operator to deal
596 ** with this, so that is what we use.
597 */
598 if (found_index != -1) {
599
600 for (int index = found_index+1; index < IndexCount; index++) {
601 IndexTable[index-1] = IndexTable[index];
602 }
603 IndexCount--;
604
605 NodeElement fake;
606 fake.ID = 0;
607 fake.Data = T();
608 IndexTable[IndexCount] = fake; // zap last (now unused) element
609
610 Invalidate_Archive();
611 return(true);
612 }
613
614 return(false);
615}
616
617
618/***********************************************************************************************
619 * compfunc -- Support function for bsearch and bsort. *
620 * *
621 * This compare function presumes that its parameters are pointing to NodeElements and that *
622 * the first "int" in the node is the index ID number to be used for comparison. *
623 * *
624 * INPUT: ptr1 -- Pointer to first node. *
625 * *
626 * ptr2 -- Pointer to second node. *
627 * *
628 * OUTPUT: Returns with the comparision value between the two nodes. *
629 * *
630 * WARNINGS: This is highly dependant upon the layout of the NodeElement structure. *
631 * *
632 * HISTORY: *
633 * 11/02/1996 JLB : Created. *
634 *=============================================================================================*/
635template<class T>
636int _USERENTRY IndexClass<T>::search_compfunc(void const * ptr1, void const * ptr2)
637{
638 if (*(int const *)ptr1 == *(int const *)ptr2) {
639 return(0);
640 }
641 if (*(int const *)ptr1 < *(int const *)ptr2) {
642 return(-1);
643 }
644 return(1);
645}
646
647
648/***********************************************************************************************
649 * IndexClass<T>::Search_For_Node -- Perform a search for the specified node ID *
650 * *
651 * This routine will perform a binary search on the previously recorded index values and *
652 * if a match was found, it will return a pointer to the NodeElement. *
653 * *
654 * INPUT: id -- The index ID to search for. *
655 * *
656 * OUTPUT: Returns with a pointer to the NodeElement that matches the index ID specified. If *
657 * no matching index could be found, then NULL is returned. *
658 * *
659 * WARNINGS: none *
660 * *
661 * HISTORY: *
662 * 11/02/1996 JLB : Created. *
663 *=============================================================================================*/
664template<class T>
665#ifdef __BORLANDC__
666NodeElement const * IndexClass<T>::Search_For_Node(int id) const
667#else
668IndexClass<T>::NodeElement const * IndexClass<T>::Search_For_Node(int id) const
669#endif
670{
671 /*
672 ** If there are no elements in the list, then it certainly can't find any matches.
673 */
674 if (IndexCount == 0) {
675 return(0);
676 }
677
678 /*
679 ** If the list has not yet been sorted, then do so now. Binary searching requires
680 ** the list to be sorted.
681 */
682 if (!IsSorted) {
683 qsort(&IndexTable[0], IndexCount, sizeof(IndexTable[0]), search_compfunc);
684 ((IndexClass<T> *)this)->Invalidate_Archive();
685 ((IndexClass<T> *)this)->IsSorted = true;
686 }
687
688 /*
689 ** This list is sorted and ready to perform a binary search upon it.
690 */
691 NodeElement node;
692 node.ID = id;
693 return((NodeElement const *)bsearch(&node, &IndexTable[0], IndexCount, sizeof(IndexTable[0]), search_compfunc));
694}
695
696
697#endif
698
699
#define NULL
Definition BaseType.h:92
#define _USERENTRY
Definition INDEX.H:61
#define W3DNEWARRAY
Definition always.h:110
@ false
Definition bool.h:59
T Fetch_Index(int id) const
Definition search.h:425
void Clear(void)
int Count(void) const
Definition INDEX.H:347
~IndexClass(void)
Definition INDEX.H:242
bool Remove_Index(int id)
Definition search.h:579
bool Add_Index(INDEX const &id, T const &data)
Definition INDEX.H:548
~IndexClass(void)
bool Is_Present(INDEX const &id) const
Definition INDEX.H:369
bool Remove_Index(INDEX const &id)
Definition INDEX.H:603
IndexClass(void)
Definition INDEX.H:216
bool Is_Present(int id) const
Definition search.h:364
void Clear(void)
Definition INDEX.H:264
IndexClass(void)
int Count(void) const
bool Add_Index(int id, T data)
Definition search.h:534