SEARCH.H 34 KB

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