SEARCH.H 34 KB

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