index.h 35 KB

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