gim_hash_table.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. #ifndef GIM_HASH_TABLE_H_INCLUDED
  2. #define GIM_HASH_TABLE_H_INCLUDED
  3. /*! \file gim_trimesh_data.h
  4. \author Francisco Leon Najera
  5. */
  6. /*
  7. -----------------------------------------------------------------------------
  8. This source file is part of GIMPACT Library.
  9. For the latest info, see http://gimpact.sourceforge.net/
  10. Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
  11. email: [email protected]
  12. This library is free software; you can redistribute it and/or
  13. modify it under the terms of EITHER:
  14. (1) The GNU Lesser General Public License as published by the Free
  15. Software Foundation; either version 2.1 of the License, or (at
  16. your option) any later version. The text of the GNU Lesser
  17. General Public License is included with this library in the
  18. file GIMPACT-LICENSE-LGPL.TXT.
  19. (2) The BSD-style license that is included with this library in
  20. the file GIMPACT-LICENSE-BSD.TXT.
  21. (3) The zlib/libpng license that is included with this library in
  22. the file GIMPACT-LICENSE-ZLIB.TXT.
  23. This library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
  26. GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
  27. -----------------------------------------------------------------------------
  28. */
  29. #include "gim_radixsort.h"
  30. #define GIM_INVALID_HASH 0xffffffff //!< A very very high value
  31. #define GIM_DEFAULT_HASH_TABLE_SIZE 380
  32. #define GIM_DEFAULT_HASH_TABLE_NODE_SIZE 4
  33. #define GIM_HASH_TABLE_GROW_FACTOR 2
  34. #define GIM_MIN_RADIX_SORT_SIZE 860 //!< calibrated on a PIII
  35. template <typename T>
  36. struct GIM_HASH_TABLE_NODE
  37. {
  38. GUINT m_key;
  39. T m_data;
  40. GIM_HASH_TABLE_NODE()
  41. {
  42. }
  43. GIM_HASH_TABLE_NODE(const GIM_HASH_TABLE_NODE& value)
  44. {
  45. m_key = value.m_key;
  46. m_data = value.m_data;
  47. }
  48. GIM_HASH_TABLE_NODE(GUINT key, const T& data)
  49. {
  50. m_key = key;
  51. m_data = data;
  52. }
  53. bool operator<(const GIM_HASH_TABLE_NODE<T>& other) const
  54. {
  55. ///inverse order, further objects are first
  56. if (m_key < other.m_key) return true;
  57. return false;
  58. }
  59. bool operator>(const GIM_HASH_TABLE_NODE<T>& other) const
  60. {
  61. ///inverse order, further objects are first
  62. if (m_key > other.m_key) return true;
  63. return false;
  64. }
  65. bool operator==(const GIM_HASH_TABLE_NODE<T>& other) const
  66. {
  67. ///inverse order, further objects are first
  68. if (m_key == other.m_key) return true;
  69. return false;
  70. }
  71. };
  72. ///Macro for getting the key
  73. class GIM_HASH_NODE_GET_KEY
  74. {
  75. public:
  76. template <class T>
  77. inline GUINT operator()(const T& a)
  78. {
  79. return a.m_key;
  80. }
  81. };
  82. ///Macro for comparing the key and the element
  83. class GIM_HASH_NODE_CMP_KEY_MACRO
  84. {
  85. public:
  86. template <class T>
  87. inline int operator()(const T& a, GUINT key)
  88. {
  89. return ((int)(a.m_key - key));
  90. }
  91. };
  92. ///Macro for comparing Hash nodes
  93. class GIM_HASH_NODE_CMP_MACRO
  94. {
  95. public:
  96. template <class T>
  97. inline int operator()(const T& a, const T& b)
  98. {
  99. return ((int)(a.m_key - b.m_key));
  100. }
  101. };
  102. //! Sorting for hash table
  103. /*!
  104. switch automatically between quicksort and radixsort
  105. */
  106. template <typename T>
  107. void gim_sort_hash_node_array(T* array, GUINT array_count)
  108. {
  109. if (array_count < GIM_MIN_RADIX_SORT_SIZE)
  110. {
  111. gim_heap_sort(array, array_count, GIM_HASH_NODE_CMP_MACRO());
  112. }
  113. else
  114. {
  115. memcopy_elements_func cmpfunc;
  116. gim_radix_sort(array, array_count, GIM_HASH_NODE_GET_KEY(), cmpfunc);
  117. }
  118. }
  119. // Note: assumes long is at least 32 bits.
  120. #define GIM_NUM_PRIME 28
  121. static const GUINT gim_prime_list[GIM_NUM_PRIME] =
  122. {
  123. 53ul, 97ul, 193ul, 389ul, 769ul,
  124. 1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
  125. 49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
  126. 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
  127. 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
  128. 1610612741ul, 3221225473ul, 4294967291ul};
  129. inline GUINT gim_next_prime(GUINT number)
  130. {
  131. //Find nearest upper prime
  132. GUINT result_ind = 0;
  133. gim_binary_search(gim_prime_list, 0, (GIM_NUM_PRIME - 2), number, result_ind);
  134. // inv: result_ind < 28
  135. return gim_prime_list[result_ind];
  136. }
  137. //! A compact hash table implementation
  138. /*!
  139. A memory aligned compact hash table that coud be treated as an array.
  140. It could be a simple sorted array without the overhead of the hash key bucked, or could
  141. be a formely hash table with an array of keys.
  142. You can use switch_to_hashtable() and switch_to_sorted_array for saving space or increase speed.
  143. </br>
  144. <ul>
  145. <li> if node_size = 0, then this container becomes a simple sorted array allocator. reserve_size is used for reserve memory in m_nodes.
  146. When the array size reaches the size equivalent to 'min_hash_table_size', then it becomes a hash table by calling check_for_switching_to_hashtable.
  147. <li> If node_size != 0, then this container becomes a hash table for ever
  148. </ul>
  149. */
  150. template <class T>
  151. class gim_hash_table
  152. {
  153. protected:
  154. typedef GIM_HASH_TABLE_NODE<T> _node_type;
  155. //!The nodes
  156. //array< _node_type, SuperAllocator<_node_type> > m_nodes;
  157. gim_array<_node_type> m_nodes;
  158. //SuperBufferedArray< _node_type > m_nodes;
  159. bool m_sorted;
  160. ///Hash table data management. The hash table has the indices to the corresponding m_nodes array
  161. GUINT* m_hash_table; //!<
  162. GUINT m_table_size; //!<
  163. GUINT m_node_size; //!<
  164. GUINT m_min_hash_table_size;
  165. //! Returns the cell index
  166. inline GUINT _find_cell(GUINT hashkey)
  167. {
  168. _node_type* nodesptr = m_nodes.pointer();
  169. GUINT start_index = (hashkey % m_table_size) * m_node_size;
  170. GUINT end_index = start_index + m_node_size;
  171. while (start_index < end_index)
  172. {
  173. GUINT value = m_hash_table[start_index];
  174. if (value != GIM_INVALID_HASH)
  175. {
  176. if (nodesptr[value].m_key == hashkey) return start_index;
  177. }
  178. start_index++;
  179. }
  180. return GIM_INVALID_HASH;
  181. }
  182. //! Find the avaliable cell for the hashkey, and return an existing cell if it has the same hash key
  183. inline GUINT _find_avaliable_cell(GUINT hashkey)
  184. {
  185. _node_type* nodesptr = m_nodes.pointer();
  186. GUINT avaliable_index = GIM_INVALID_HASH;
  187. GUINT start_index = (hashkey % m_table_size) * m_node_size;
  188. GUINT end_index = start_index + m_node_size;
  189. while (start_index < end_index)
  190. {
  191. GUINT value = m_hash_table[start_index];
  192. if (value == GIM_INVALID_HASH)
  193. {
  194. if (avaliable_index == GIM_INVALID_HASH)
  195. {
  196. avaliable_index = start_index;
  197. }
  198. }
  199. else if (nodesptr[value].m_key == hashkey)
  200. {
  201. return start_index;
  202. }
  203. start_index++;
  204. }
  205. return avaliable_index;
  206. }
  207. //! reserves the memory for the hash table.
  208. /*!
  209. \pre hash table must be empty
  210. \post reserves the memory for the hash table, an initializes all elements to GIM_INVALID_HASH.
  211. */
  212. inline void _reserve_table_memory(GUINT newtablesize)
  213. {
  214. if (newtablesize == 0) return;
  215. if (m_node_size == 0) return;
  216. //Get a Prime size
  217. m_table_size = gim_next_prime(newtablesize);
  218. GUINT datasize = m_table_size * m_node_size;
  219. //Alloc the data buffer
  220. m_hash_table = (GUINT*)gim_alloc(datasize * sizeof(GUINT));
  221. }
  222. inline void _invalidate_keys()
  223. {
  224. GUINT datasize = m_table_size * m_node_size;
  225. for (GUINT i = 0; i < datasize; i++)
  226. {
  227. m_hash_table[i] = GIM_INVALID_HASH; // invalidate keys
  228. }
  229. }
  230. //! Clear all memory for the hash table
  231. inline void _clear_table_memory()
  232. {
  233. if (m_hash_table == NULL) return;
  234. gim_free(m_hash_table);
  235. m_hash_table = NULL;
  236. m_table_size = 0;
  237. }
  238. //! Invalidates the keys (Assigning GIM_INVALID_HASH to all) Reorders the hash keys
  239. inline void _rehash()
  240. {
  241. _invalidate_keys();
  242. _node_type* nodesptr = m_nodes.pointer();
  243. for (GUINT i = 0; i < (GUINT)m_nodes.size(); i++)
  244. {
  245. GUINT nodekey = nodesptr[i].m_key;
  246. if (nodekey != GIM_INVALID_HASH)
  247. {
  248. //Search for the avaliable cell in buffer
  249. GUINT index = _find_avaliable_cell(nodekey);
  250. if (m_hash_table[index] != GIM_INVALID_HASH)
  251. { //The new index is alreade used... discard this new incomming object, repeated key
  252. btAssert(m_hash_table[index] == nodekey);
  253. nodesptr[i].m_key = GIM_INVALID_HASH;
  254. }
  255. else
  256. {
  257. //;
  258. //Assign the value for alloc
  259. m_hash_table[index] = i;
  260. }
  261. }
  262. }
  263. }
  264. //! Resize hash table indices
  265. inline void _resize_table(GUINT newsize)
  266. {
  267. //Clear memory
  268. _clear_table_memory();
  269. //Alloc the data
  270. _reserve_table_memory(newsize);
  271. //Invalidate keys and rehash
  272. _rehash();
  273. }
  274. //! Destroy hash table memory
  275. inline void _destroy()
  276. {
  277. if (m_hash_table == NULL) return;
  278. _clear_table_memory();
  279. }
  280. //! Finds an avaliable hash table cell, and resizes the table if there isn't space
  281. inline GUINT _assign_hash_table_cell(GUINT hashkey)
  282. {
  283. GUINT cell_index = _find_avaliable_cell(hashkey);
  284. if (cell_index == GIM_INVALID_HASH)
  285. {
  286. //rehashing
  287. _resize_table(m_table_size + 1);
  288. GUINT cell_index = _find_avaliable_cell(hashkey);
  289. btAssert(cell_index != GIM_INVALID_HASH);
  290. }
  291. return cell_index;
  292. }
  293. //! erase by index in hash table
  294. inline bool _erase_by_index_hash_table(GUINT index)
  295. {
  296. if (index >= m_nodes.size()) return false;
  297. if (m_nodes[index].m_key != GIM_INVALID_HASH)
  298. {
  299. //Search for the avaliable cell in buffer
  300. GUINT cell_index = _find_cell(m_nodes[index].m_key);
  301. btAssert(cell_index != GIM_INVALID_HASH);
  302. btAssert(m_hash_table[cell_index] == index);
  303. m_hash_table[cell_index] = GIM_INVALID_HASH;
  304. }
  305. return this->_erase_unsorted(index);
  306. }
  307. //! erase by key in hash table
  308. inline bool _erase_hash_table(GUINT hashkey)
  309. {
  310. if (hashkey == GIM_INVALID_HASH) return false;
  311. //Search for the avaliable cell in buffer
  312. GUINT cell_index = _find_cell(hashkey);
  313. if (cell_index == GIM_INVALID_HASH) return false;
  314. GUINT index = m_hash_table[cell_index];
  315. m_hash_table[cell_index] = GIM_INVALID_HASH;
  316. return this->_erase_unsorted(index);
  317. }
  318. //! insert an element in hash table
  319. /*!
  320. If the element exists, this won't insert the element
  321. \return the index in the array of the existing element,or GIM_INVALID_HASH if the element has been inserted
  322. If so, the element has been inserted at the last position of the array.
  323. */
  324. inline GUINT _insert_hash_table(GUINT hashkey, const T& value)
  325. {
  326. if (hashkey == GIM_INVALID_HASH)
  327. {
  328. //Insert anyway
  329. _insert_unsorted(hashkey, value);
  330. return GIM_INVALID_HASH;
  331. }
  332. GUINT cell_index = _assign_hash_table_cell(hashkey);
  333. GUINT value_key = m_hash_table[cell_index];
  334. if (value_key != GIM_INVALID_HASH) return value_key; // Not overrited
  335. m_hash_table[cell_index] = m_nodes.size();
  336. _insert_unsorted(hashkey, value);
  337. return GIM_INVALID_HASH;
  338. }
  339. //! insert an element in hash table.
  340. /*!
  341. If the element exists, this replaces the element.
  342. \return the index in the array of the existing element,or GIM_INVALID_HASH if the element has been inserted
  343. If so, the element has been inserted at the last position of the array.
  344. */
  345. inline GUINT _insert_hash_table_replace(GUINT hashkey, const T& value)
  346. {
  347. if (hashkey == GIM_INVALID_HASH)
  348. {
  349. //Insert anyway
  350. _insert_unsorted(hashkey, value);
  351. return GIM_INVALID_HASH;
  352. }
  353. GUINT cell_index = _assign_hash_table_cell(hashkey);
  354. GUINT value_key = m_hash_table[cell_index];
  355. if (value_key != GIM_INVALID_HASH)
  356. { //replaces the existing
  357. m_nodes[value_key] = _node_type(hashkey, value);
  358. return value_key; // index of the replaced element
  359. }
  360. m_hash_table[cell_index] = m_nodes.size();
  361. _insert_unsorted(hashkey, value);
  362. return GIM_INVALID_HASH;
  363. }
  364. ///Sorted array data management. The hash table has the indices to the corresponding m_nodes array
  365. inline bool _erase_sorted(GUINT index)
  366. {
  367. if (index >= (GUINT)m_nodes.size()) return false;
  368. m_nodes.erase_sorted(index);
  369. if (m_nodes.size() < 2) m_sorted = false;
  370. return true;
  371. }
  372. //! faster, but unsorted
  373. inline bool _erase_unsorted(GUINT index)
  374. {
  375. if (index >= m_nodes.size()) return false;
  376. GUINT lastindex = m_nodes.size() - 1;
  377. if (index < lastindex && m_hash_table != 0)
  378. {
  379. GUINT hashkey = m_nodes[lastindex].m_key;
  380. if (hashkey != GIM_INVALID_HASH)
  381. {
  382. //update the new position of the last element
  383. GUINT cell_index = _find_cell(hashkey);
  384. btAssert(cell_index != GIM_INVALID_HASH);
  385. //new position of the last element which will be swaped
  386. m_hash_table[cell_index] = index;
  387. }
  388. }
  389. m_nodes.erase(index);
  390. m_sorted = false;
  391. return true;
  392. }
  393. //! Insert in position ordered
  394. /*!
  395. Also checks if it is needed to transform this container to a hash table, by calling check_for_switching_to_hashtable
  396. */
  397. inline void _insert_in_pos(GUINT hashkey, const T& value, GUINT pos)
  398. {
  399. m_nodes.insert(_node_type(hashkey, value), pos);
  400. this->check_for_switching_to_hashtable();
  401. }
  402. //! Insert an element in an ordered array
  403. inline GUINT _insert_sorted(GUINT hashkey, const T& value)
  404. {
  405. if (hashkey == GIM_INVALID_HASH || size() == 0)
  406. {
  407. m_nodes.push_back(_node_type(hashkey, value));
  408. return GIM_INVALID_HASH;
  409. }
  410. //Insert at last position
  411. //Sort element
  412. GUINT result_ind = 0;
  413. GUINT last_index = m_nodes.size() - 1;
  414. _node_type* ptr = m_nodes.pointer();
  415. bool found = gim_binary_search_ex(
  416. ptr, 0, last_index, result_ind, hashkey, GIM_HASH_NODE_CMP_KEY_MACRO());
  417. //Insert before found index
  418. if (found)
  419. {
  420. return result_ind;
  421. }
  422. else
  423. {
  424. _insert_in_pos(hashkey, value, result_ind);
  425. }
  426. return GIM_INVALID_HASH;
  427. }
  428. inline GUINT _insert_sorted_replace(GUINT hashkey, const T& value)
  429. {
  430. if (hashkey == GIM_INVALID_HASH || size() == 0)
  431. {
  432. m_nodes.push_back(_node_type(hashkey, value));
  433. return GIM_INVALID_HASH;
  434. }
  435. //Insert at last position
  436. //Sort element
  437. GUINT result_ind;
  438. GUINT last_index = m_nodes.size() - 1;
  439. _node_type* ptr = m_nodes.pointer();
  440. bool found = gim_binary_search_ex(
  441. ptr, 0, last_index, result_ind, hashkey, GIM_HASH_NODE_CMP_KEY_MACRO());
  442. //Insert before found index
  443. if (found)
  444. {
  445. m_nodes[result_ind] = _node_type(hashkey, value);
  446. }
  447. else
  448. {
  449. _insert_in_pos(hashkey, value, result_ind);
  450. }
  451. return result_ind;
  452. }
  453. //! Fast insertion in m_nodes array
  454. inline GUINT _insert_unsorted(GUINT hashkey, const T& value)
  455. {
  456. m_nodes.push_back(_node_type(hashkey, value));
  457. m_sorted = false;
  458. return GIM_INVALID_HASH;
  459. }
  460. public:
  461. /*!
  462. <li> if node_size = 0, then this container becomes a simple sorted array allocator. reserve_size is used for reserve memory in m_nodes.
  463. When the array size reaches the size equivalent to 'min_hash_table_size', then it becomes a hash table by calling check_for_switching_to_hashtable.
  464. <li> If node_size != 0, then this container becomes a hash table for ever
  465. </ul>
  466. */
  467. gim_hash_table(GUINT reserve_size = GIM_DEFAULT_HASH_TABLE_SIZE,
  468. GUINT node_size = GIM_DEFAULT_HASH_TABLE_NODE_SIZE,
  469. GUINT min_hash_table_size = GIM_INVALID_HASH)
  470. {
  471. m_hash_table = NULL;
  472. m_table_size = 0;
  473. m_sorted = false;
  474. m_node_size = node_size;
  475. m_min_hash_table_size = min_hash_table_size;
  476. if (m_node_size != 0)
  477. {
  478. if (reserve_size != 0)
  479. {
  480. m_nodes.reserve(reserve_size);
  481. _reserve_table_memory(reserve_size);
  482. _invalidate_keys();
  483. }
  484. else
  485. {
  486. m_nodes.reserve(GIM_DEFAULT_HASH_TABLE_SIZE);
  487. _reserve_table_memory(GIM_DEFAULT_HASH_TABLE_SIZE);
  488. _invalidate_keys();
  489. }
  490. }
  491. else if (reserve_size != 0)
  492. {
  493. m_nodes.reserve(reserve_size);
  494. }
  495. }
  496. ~gim_hash_table()
  497. {
  498. _destroy();
  499. }
  500. inline bool is_hash_table()
  501. {
  502. if (m_hash_table) return true;
  503. return false;
  504. }
  505. inline bool is_sorted()
  506. {
  507. if (size() < 2) return true;
  508. return m_sorted;
  509. }
  510. bool sort()
  511. {
  512. if (is_sorted()) return true;
  513. if (m_nodes.size() < 2) return false;
  514. _node_type* ptr = m_nodes.pointer();
  515. GUINT siz = m_nodes.size();
  516. gim_sort_hash_node_array(ptr, siz);
  517. m_sorted = true;
  518. if (m_hash_table)
  519. {
  520. _rehash();
  521. }
  522. return true;
  523. }
  524. bool switch_to_hashtable()
  525. {
  526. if (m_hash_table) return false;
  527. if (m_node_size == 0) m_node_size = GIM_DEFAULT_HASH_TABLE_NODE_SIZE;
  528. if (m_nodes.size() < GIM_DEFAULT_HASH_TABLE_SIZE)
  529. {
  530. _resize_table(GIM_DEFAULT_HASH_TABLE_SIZE);
  531. }
  532. else
  533. {
  534. _resize_table(m_nodes.size() + 1);
  535. }
  536. return true;
  537. }
  538. bool switch_to_sorted_array()
  539. {
  540. if (m_hash_table == NULL) return true;
  541. _clear_table_memory();
  542. return sort();
  543. }
  544. //!If the container reaches the
  545. bool check_for_switching_to_hashtable()
  546. {
  547. if (this->m_hash_table) return true;
  548. if (!(m_nodes.size() < m_min_hash_table_size))
  549. {
  550. if (m_node_size == 0)
  551. {
  552. m_node_size = GIM_DEFAULT_HASH_TABLE_NODE_SIZE;
  553. }
  554. _resize_table(m_nodes.size() + 1);
  555. return true;
  556. }
  557. return false;
  558. }
  559. inline void set_sorted(bool value)
  560. {
  561. m_sorted = value;
  562. }
  563. //! Retrieves the amount of keys.
  564. inline GUINT size() const
  565. {
  566. return m_nodes.size();
  567. }
  568. //! Retrieves the hash key.
  569. inline GUINT get_key(GUINT index) const
  570. {
  571. return m_nodes[index].m_key;
  572. }
  573. //! Retrieves the value by index
  574. /*!
  575. */
  576. inline T* get_value_by_index(GUINT index)
  577. {
  578. return &m_nodes[index].m_data;
  579. }
  580. inline const T& operator[](GUINT index) const
  581. {
  582. return m_nodes[index].m_data;
  583. }
  584. inline T& operator[](GUINT index)
  585. {
  586. return m_nodes[index].m_data;
  587. }
  588. //! Finds the index of the element with the key
  589. /*!
  590. \return the index in the array of the existing element,or GIM_INVALID_HASH if the element has been inserted
  591. If so, the element has been inserted at the last position of the array.
  592. */
  593. inline GUINT find(GUINT hashkey)
  594. {
  595. if (m_hash_table)
  596. {
  597. GUINT cell_index = _find_cell(hashkey);
  598. if (cell_index == GIM_INVALID_HASH) return GIM_INVALID_HASH;
  599. return m_hash_table[cell_index];
  600. }
  601. GUINT last_index = m_nodes.size();
  602. if (last_index < 2)
  603. {
  604. if (last_index == 0) return GIM_INVALID_HASH;
  605. if (m_nodes[0].m_key == hashkey) return 0;
  606. return GIM_INVALID_HASH;
  607. }
  608. else if (m_sorted)
  609. {
  610. //Binary search
  611. GUINT result_ind = 0;
  612. last_index--;
  613. _node_type* ptr = m_nodes.pointer();
  614. bool found = gim_binary_search_ex(ptr, 0, last_index, result_ind, hashkey, GIM_HASH_NODE_CMP_KEY_MACRO());
  615. if (found) return result_ind;
  616. }
  617. return GIM_INVALID_HASH;
  618. }
  619. //! Retrieves the value associated with the index
  620. /*!
  621. \return the found element, or null
  622. */
  623. inline T* get_value(GUINT hashkey)
  624. {
  625. GUINT index = find(hashkey);
  626. if (index == GIM_INVALID_HASH) return NULL;
  627. return &m_nodes[index].m_data;
  628. }
  629. /*!
  630. */
  631. inline bool erase_by_index(GUINT index)
  632. {
  633. if (index > m_nodes.size()) return false;
  634. if (m_hash_table == NULL)
  635. {
  636. if (is_sorted())
  637. {
  638. return this->_erase_sorted(index);
  639. }
  640. else
  641. {
  642. return this->_erase_unsorted(index);
  643. }
  644. }
  645. else
  646. {
  647. return this->_erase_by_index_hash_table(index);
  648. }
  649. return false;
  650. }
  651. inline bool erase_by_index_unsorted(GUINT index)
  652. {
  653. if (index > m_nodes.size()) return false;
  654. if (m_hash_table == NULL)
  655. {
  656. return this->_erase_unsorted(index);
  657. }
  658. else
  659. {
  660. return this->_erase_by_index_hash_table(index);
  661. }
  662. return false;
  663. }
  664. /*!
  665. */
  666. inline bool erase_by_key(GUINT hashkey)
  667. {
  668. if (size() == 0) return false;
  669. if (m_hash_table)
  670. {
  671. return this->_erase_hash_table(hashkey);
  672. }
  673. //Binary search
  674. if (is_sorted() == false) return false;
  675. GUINT result_ind = find(hashkey);
  676. if (result_ind != GIM_INVALID_HASH)
  677. {
  678. return this->_erase_sorted(result_ind);
  679. }
  680. return false;
  681. }
  682. void clear()
  683. {
  684. m_nodes.clear();
  685. if (m_hash_table == NULL) return;
  686. GUINT datasize = m_table_size * m_node_size;
  687. //Initialize the hashkeys.
  688. GUINT i;
  689. for (i = 0; i < datasize; i++)
  690. {
  691. m_hash_table[i] = GIM_INVALID_HASH; // invalidate keys
  692. }
  693. m_sorted = false;
  694. }
  695. //! Insert an element into the hash
  696. /*!
  697. \return If GIM_INVALID_HASH, the object has been inserted succesfully. Else it returns the position
  698. of the existing element.
  699. */
  700. inline GUINT insert(GUINT hashkey, const T& element)
  701. {
  702. if (m_hash_table)
  703. {
  704. return this->_insert_hash_table(hashkey, element);
  705. }
  706. if (this->is_sorted())
  707. {
  708. return this->_insert_sorted(hashkey, element);
  709. }
  710. return this->_insert_unsorted(hashkey, element);
  711. }
  712. //! Insert an element into the hash, and could overrite an existing object with the same hash.
  713. /*!
  714. \return If GIM_INVALID_HASH, the object has been inserted succesfully. Else it returns the position
  715. of the replaced element.
  716. */
  717. inline GUINT insert_override(GUINT hashkey, const T& element)
  718. {
  719. if (m_hash_table)
  720. {
  721. return this->_insert_hash_table_replace(hashkey, element);
  722. }
  723. if (this->is_sorted())
  724. {
  725. return this->_insert_sorted_replace(hashkey, element);
  726. }
  727. this->_insert_unsorted(hashkey, element);
  728. return m_nodes.size();
  729. }
  730. //! Insert an element into the hash,But if this container is a sorted array, this inserts it unsorted
  731. /*!
  732. */
  733. inline GUINT insert_unsorted(GUINT hashkey, const T& element)
  734. {
  735. if (m_hash_table)
  736. {
  737. return this->_insert_hash_table(hashkey, element);
  738. }
  739. return this->_insert_unsorted(hashkey, element);
  740. }
  741. };
  742. #endif // GIM_CONTAINERS_H_INCLUDED