search.h 34 KB

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