gim_hash_table.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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: projectileman@yahoo.com
  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. };
  130. inline GUINT gim_next_prime(GUINT number)
  131. {
  132. //Find nearest upper prime
  133. GUINT result_ind = 0;
  134. gim_binary_search(gim_prime_list,0,(GIM_NUM_PRIME-2),number,result_ind);
  135. // inv: result_ind < 28
  136. return gim_prime_list[result_ind];
  137. }
  138. //! A compact hash table implementation
  139. /*!
  140. A memory aligned compact hash table that coud be treated as an array.
  141. It could be a simple sorted array without the overhead of the hash key bucked, or could
  142. be a formely hash table with an array of keys.
  143. You can use switch_to_hashtable() and switch_to_sorted_array for saving space or increase speed.
  144. </br>
  145. <ul>
  146. <li> if node_size = 0, then this container becomes a simple sorted array allocator. reserve_size is used for reserve memory in m_nodes.
  147. 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.
  148. <li> If node_size != 0, then this container becomes a hash table for ever
  149. </ul>
  150. */
  151. template<class T>
  152. class gim_hash_table
  153. {
  154. protected:
  155. typedef GIM_HASH_TABLE_NODE<T> _node_type;
  156. //!The nodes
  157. //array< _node_type, SuperAllocator<_node_type> > m_nodes;
  158. gim_array< _node_type > m_nodes;
  159. //SuperBufferedArray< _node_type > m_nodes;
  160. bool m_sorted;
  161. ///Hash table data management. The hash table has the indices to the corresponding m_nodes array
  162. GUINT * m_hash_table;//!<
  163. GUINT m_table_size;//!<
  164. GUINT m_node_size;//!<
  165. GUINT m_min_hash_table_size;
  166. //! Returns the cell index
  167. inline GUINT _find_cell(GUINT hashkey)
  168. {
  169. _node_type * nodesptr = m_nodes.pointer();
  170. GUINT start_index = (hashkey%m_table_size)*m_node_size;
  171. GUINT end_index = start_index + m_node_size;
  172. while(start_index<end_index)
  173. {
  174. GUINT value = m_hash_table[start_index];
  175. if(value != GIM_INVALID_HASH)
  176. {
  177. if(nodesptr[value].m_key == hashkey) return start_index;
  178. }
  179. start_index++;
  180. }
  181. return GIM_INVALID_HASH;
  182. }
  183. //! Find the avaliable cell for the hashkey, and return an existing cell if it has the same hash key
  184. inline GUINT _find_avaliable_cell(GUINT hashkey)
  185. {
  186. _node_type * nodesptr = m_nodes.pointer();
  187. GUINT avaliable_index = GIM_INVALID_HASH;
  188. GUINT start_index = (hashkey%m_table_size)*m_node_size;
  189. GUINT end_index = start_index + m_node_size;
  190. while(start_index<end_index)
  191. {
  192. GUINT value = m_hash_table[start_index];
  193. if(value == GIM_INVALID_HASH)
  194. {
  195. if(avaliable_index==GIM_INVALID_HASH)
  196. {
  197. avaliable_index = start_index;
  198. }
  199. }
  200. else if(nodesptr[value].m_key == hashkey)
  201. {
  202. return start_index;
  203. }
  204. start_index++;
  205. }
  206. return avaliable_index;
  207. }
  208. //! reserves the memory for the hash table.
  209. /*!
  210. \pre hash table must be empty
  211. \post reserves the memory for the hash table, an initializes all elements to GIM_INVALID_HASH.
  212. */
  213. inline void _reserve_table_memory(GUINT newtablesize)
  214. {
  215. if(newtablesize==0) return;
  216. if(m_node_size==0) return;
  217. //Get a Prime size
  218. m_table_size = gim_next_prime(newtablesize);
  219. GUINT datasize = m_table_size*m_node_size;
  220. //Alloc the data buffer
  221. m_hash_table = (GUINT *)gim_alloc(datasize*sizeof(GUINT));
  222. }
  223. inline void _invalidate_keys()
  224. {
  225. GUINT datasize = m_table_size*m_node_size;
  226. for(GUINT i=0;i<datasize;i++)
  227. {
  228. m_hash_table[i] = GIM_INVALID_HASH;// invalidate keys
  229. }
  230. }
  231. //! Clear all memory for the hash table
  232. inline void _clear_table_memory()
  233. {
  234. if(m_hash_table==NULL) return;
  235. gim_free(m_hash_table);
  236. m_hash_table = NULL;
  237. m_table_size = 0;
  238. }
  239. //! Invalidates the keys (Assigning GIM_INVALID_HASH to all) Reorders the hash keys
  240. inline void _rehash()
  241. {
  242. _invalidate_keys();
  243. _node_type * nodesptr = m_nodes.pointer();
  244. for(GUINT i=0;i<(GUINT)m_nodes.size();i++)
  245. {
  246. GUINT nodekey = nodesptr[i].m_key;
  247. if(nodekey != GIM_INVALID_HASH)
  248. {
  249. //Search for the avaliable cell in buffer
  250. GUINT index = _find_avaliable_cell(nodekey);
  251. if(m_hash_table[index]!=GIM_INVALID_HASH)
  252. {//The new index is alreade used... discard this new incomming object, repeated key
  253. btAssert(m_hash_table[index]==nodekey);
  254. nodesptr[i].m_key = GIM_INVALID_HASH;
  255. }
  256. else
  257. {
  258. //;
  259. //Assign the value for alloc
  260. m_hash_table[index] = i;
  261. }
  262. }
  263. }
  264. }
  265. //! Resize hash table indices
  266. inline void _resize_table(GUINT newsize)
  267. {
  268. //Clear memory
  269. _clear_table_memory();
  270. //Alloc the data
  271. _reserve_table_memory(newsize);
  272. //Invalidate keys and rehash
  273. _rehash();
  274. }
  275. //! Destroy hash table memory
  276. inline void _destroy()
  277. {
  278. if(m_hash_table==NULL) return;
  279. _clear_table_memory();
  280. }
  281. //! Finds an avaliable hash table cell, and resizes the table if there isn't space
  282. inline GUINT _assign_hash_table_cell(GUINT hashkey)
  283. {
  284. GUINT cell_index = _find_avaliable_cell(hashkey);
  285. if(cell_index==GIM_INVALID_HASH)
  286. {
  287. //rehashing
  288. _resize_table(m_table_size+1);
  289. GUINT cell_index = _find_avaliable_cell(hashkey);
  290. btAssert(cell_index!=GIM_INVALID_HASH);
  291. }
  292. return cell_index;
  293. }
  294. //! erase by index in hash table
  295. inline bool _erase_by_index_hash_table(GUINT index)
  296. {
  297. if(index >= m_nodes.size()) return false;
  298. if(m_nodes[index].m_key != GIM_INVALID_HASH)
  299. {
  300. //Search for the avaliable cell in buffer
  301. GUINT cell_index = _find_cell(m_nodes[index].m_key);
  302. btAssert(cell_index!=GIM_INVALID_HASH);
  303. btAssert(m_hash_table[cell_index]==index);
  304. m_hash_table[cell_index] = GIM_INVALID_HASH;
  305. }
  306. return this->_erase_unsorted(index);
  307. }
  308. //! erase by key in hash table
  309. inline bool _erase_hash_table(GUINT hashkey)
  310. {
  311. if(hashkey == GIM_INVALID_HASH) return false;
  312. //Search for the avaliable cell in buffer
  313. GUINT cell_index = _find_cell(hashkey);
  314. if(cell_index ==GIM_INVALID_HASH) return false;
  315. GUINT index = m_hash_table[cell_index];
  316. m_hash_table[cell_index] = GIM_INVALID_HASH;
  317. return this->_erase_unsorted(index);
  318. }
  319. //! insert an element in hash table
  320. /*!
  321. If the element exists, this won't insert the element
  322. \return the index in the array of the existing element,or GIM_INVALID_HASH if the element has been inserted
  323. If so, the element has been inserted at the last position of the array.
  324. */
  325. inline GUINT _insert_hash_table(GUINT hashkey, const T & value)
  326. {
  327. if(hashkey==GIM_INVALID_HASH)
  328. {
  329. //Insert anyway
  330. _insert_unsorted(hashkey,value);
  331. return GIM_INVALID_HASH;
  332. }
  333. GUINT cell_index = _assign_hash_table_cell(hashkey);
  334. GUINT value_key = m_hash_table[cell_index];
  335. if(value_key!= GIM_INVALID_HASH) return value_key;// Not overrited
  336. m_hash_table[cell_index] = m_nodes.size();
  337. _insert_unsorted(hashkey,value);
  338. return GIM_INVALID_HASH;
  339. }
  340. //! insert an element in hash table.
  341. /*!
  342. If the element exists, this replaces the element.
  343. \return the index in the array of the existing element,or GIM_INVALID_HASH if the element has been inserted
  344. If so, the element has been inserted at the last position of the array.
  345. */
  346. inline GUINT _insert_hash_table_replace(GUINT hashkey, const T & value)
  347. {
  348. if(hashkey==GIM_INVALID_HASH)
  349. {
  350. //Insert anyway
  351. _insert_unsorted(hashkey,value);
  352. return GIM_INVALID_HASH;
  353. }
  354. GUINT cell_index = _assign_hash_table_cell(hashkey);
  355. GUINT value_key = m_hash_table[cell_index];
  356. if(value_key!= GIM_INVALID_HASH)
  357. {//replaces the existing
  358. m_nodes[value_key] = _node_type(hashkey,value);
  359. return value_key;// index of the replaced element
  360. }
  361. m_hash_table[cell_index] = m_nodes.size();
  362. _insert_unsorted(hashkey,value);
  363. return GIM_INVALID_HASH;
  364. }
  365. ///Sorted array data management. The hash table has the indices to the corresponding m_nodes array
  366. inline bool _erase_sorted(GUINT index)
  367. {
  368. if(index>=(GUINT)m_nodes.size()) return false;
  369. m_nodes.erase_sorted(index);
  370. if(m_nodes.size()<2) m_sorted = false;
  371. return true;
  372. }
  373. //! faster, but unsorted
  374. inline bool _erase_unsorted(GUINT index)
  375. {
  376. if(index>=m_nodes.size()) return false;
  377. GUINT lastindex = m_nodes.size()-1;
  378. if(index<lastindex && m_hash_table!=0)
  379. {
  380. GUINT hashkey = m_nodes[lastindex].m_key;
  381. if(hashkey!=GIM_INVALID_HASH)
  382. {
  383. //update the new position of the last element
  384. GUINT cell_index = _find_cell(hashkey);
  385. btAssert(cell_index!=GIM_INVALID_HASH);
  386. //new position of the last element which will be swaped
  387. m_hash_table[cell_index] = index;
  388. }
  389. }
  390. m_nodes.erase(index);
  391. m_sorted = false;
  392. return true;
  393. }
  394. //! Insert in position ordered
  395. /*!
  396. Also checks if it is needed to transform this container to a hash table, by calling check_for_switching_to_hashtable
  397. */
  398. inline void _insert_in_pos(GUINT hashkey, const T & value, GUINT pos)
  399. {
  400. m_nodes.insert(_node_type(hashkey,value),pos);
  401. this->check_for_switching_to_hashtable();
  402. }
  403. //! Insert an element in an ordered array
  404. inline GUINT _insert_sorted(GUINT hashkey, const T & value)
  405. {
  406. if(hashkey==GIM_INVALID_HASH || size()==0)
  407. {
  408. m_nodes.push_back(_node_type(hashkey,value));
  409. return GIM_INVALID_HASH;
  410. }
  411. //Insert at last position
  412. //Sort element
  413. GUINT result_ind=0;
  414. GUINT last_index = m_nodes.size()-1;
  415. _node_type * ptr = m_nodes.pointer();
  416. bool found = gim_binary_search_ex(
  417. ptr,0,last_index,result_ind,hashkey,GIM_HASH_NODE_CMP_KEY_MACRO());
  418. //Insert before found index
  419. if(found)
  420. {
  421. return result_ind;
  422. }
  423. else
  424. {
  425. _insert_in_pos(hashkey, value, result_ind);
  426. }
  427. return GIM_INVALID_HASH;
  428. }
  429. inline GUINT _insert_sorted_replace(GUINT hashkey, const T & value)
  430. {
  431. if(hashkey==GIM_INVALID_HASH || size()==0)
  432. {
  433. m_nodes.push_back(_node_type(hashkey,value));
  434. return GIM_INVALID_HASH;
  435. }
  436. //Insert at last position
  437. //Sort element
  438. GUINT result_ind;
  439. GUINT last_index = m_nodes.size()-1;
  440. _node_type * ptr = m_nodes.pointer();
  441. bool found = gim_binary_search_ex(
  442. ptr,0,last_index,result_ind,hashkey,GIM_HASH_NODE_CMP_KEY_MACRO());
  443. //Insert before found index
  444. if(found)
  445. {
  446. m_nodes[result_ind] = _node_type(hashkey,value);
  447. }
  448. else
  449. {
  450. _insert_in_pos(hashkey, value, result_ind);
  451. }
  452. return result_ind;
  453. }
  454. //! Fast insertion in m_nodes array
  455. inline GUINT _insert_unsorted(GUINT hashkey, const T & value)
  456. {
  457. m_nodes.push_back(_node_type(hashkey,value));
  458. m_sorted = false;
  459. return GIM_INVALID_HASH;
  460. }
  461. public:
  462. /*!
  463. <li> if node_size = 0, then this container becomes a simple sorted array allocator. reserve_size is used for reserve memory in m_nodes.
  464. 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.
  465. <li> If node_size != 0, then this container becomes a hash table for ever
  466. </ul>
  467. */
  468. gim_hash_table(GUINT reserve_size = GIM_DEFAULT_HASH_TABLE_SIZE,
  469. GUINT node_size = GIM_DEFAULT_HASH_TABLE_NODE_SIZE,
  470. GUINT min_hash_table_size = GIM_INVALID_HASH)
  471. {
  472. m_hash_table = NULL;
  473. m_table_size = 0;
  474. m_sorted = false;
  475. m_node_size = node_size;
  476. m_min_hash_table_size = min_hash_table_size;
  477. if(m_node_size!=0)
  478. {
  479. if(reserve_size!=0)
  480. {
  481. m_nodes.reserve(reserve_size);
  482. _reserve_table_memory(reserve_size);
  483. _invalidate_keys();
  484. }
  485. else
  486. {
  487. m_nodes.reserve(GIM_DEFAULT_HASH_TABLE_SIZE);
  488. _reserve_table_memory(GIM_DEFAULT_HASH_TABLE_SIZE);
  489. _invalidate_keys();
  490. }
  491. }
  492. else if(reserve_size!=0)
  493. {
  494. m_nodes.reserve(reserve_size);
  495. }
  496. }
  497. ~gim_hash_table()
  498. {
  499. _destroy();
  500. }
  501. inline bool is_hash_table()
  502. {
  503. if(m_hash_table) return true;
  504. return false;
  505. }
  506. inline bool is_sorted()
  507. {
  508. if(size()<2) return true;
  509. return m_sorted;
  510. }
  511. bool sort()
  512. {
  513. if(is_sorted()) return true;
  514. if(m_nodes.size()<2) return false;
  515. _node_type * ptr = m_nodes.pointer();
  516. GUINT siz = m_nodes.size();
  517. gim_sort_hash_node_array(ptr,siz);
  518. m_sorted=true;
  519. if(m_hash_table)
  520. {
  521. _rehash();
  522. }
  523. return true;
  524. }
  525. bool switch_to_hashtable()
  526. {
  527. if(m_hash_table) return false;
  528. if(m_node_size==0) m_node_size = GIM_DEFAULT_HASH_TABLE_NODE_SIZE;
  529. if(m_nodes.size()<GIM_DEFAULT_HASH_TABLE_SIZE)
  530. {
  531. _resize_table(GIM_DEFAULT_HASH_TABLE_SIZE);
  532. }
  533. else
  534. {
  535. _resize_table(m_nodes.size()+1);
  536. }
  537. return true;
  538. }
  539. bool switch_to_sorted_array()
  540. {
  541. if(m_hash_table==NULL) return true;
  542. _clear_table_memory();
  543. return sort();
  544. }
  545. //!If the container reaches the
  546. bool check_for_switching_to_hashtable()
  547. {
  548. if(this->m_hash_table) return true;
  549. if(!(m_nodes.size()< m_min_hash_table_size))
  550. {
  551. if(m_node_size == 0)
  552. {
  553. m_node_size = GIM_DEFAULT_HASH_TABLE_NODE_SIZE;
  554. }
  555. _resize_table(m_nodes.size()+1);
  556. return true;
  557. }
  558. return false;
  559. }
  560. inline void set_sorted(bool value)
  561. {
  562. m_sorted = value;
  563. }
  564. //! Retrieves the amount of keys.
  565. inline GUINT size() const
  566. {
  567. return m_nodes.size();
  568. }
  569. //! Retrieves the hash key.
  570. inline GUINT get_key(GUINT index) const
  571. {
  572. return m_nodes[index].m_key;
  573. }
  574. //! Retrieves the value by index
  575. /*!
  576. */
  577. inline T * get_value_by_index(GUINT index)
  578. {
  579. return &m_nodes[index].m_data;
  580. }
  581. inline const T& operator[](GUINT index) const
  582. {
  583. return m_nodes[index].m_data;
  584. }
  585. inline T& operator[](GUINT index)
  586. {
  587. return m_nodes[index].m_data;
  588. }
  589. //! Finds the index of the element with the key
  590. /*!
  591. \return the index in the array of the existing element,or GIM_INVALID_HASH if the element has been inserted
  592. If so, the element has been inserted at the last position of the array.
  593. */
  594. inline GUINT find(GUINT hashkey)
  595. {
  596. if(m_hash_table)
  597. {
  598. GUINT cell_index = _find_cell(hashkey);
  599. if(cell_index==GIM_INVALID_HASH) return GIM_INVALID_HASH;
  600. return m_hash_table[cell_index];
  601. }
  602. GUINT last_index = m_nodes.size();
  603. if(last_index<2)
  604. {
  605. if(last_index==0) return GIM_INVALID_HASH;
  606. if(m_nodes[0].m_key == hashkey) return 0;
  607. return GIM_INVALID_HASH;
  608. }
  609. else if(m_sorted)
  610. {
  611. //Binary search
  612. GUINT result_ind = 0;
  613. last_index--;
  614. _node_type * ptr = m_nodes.pointer();
  615. bool found = gim_binary_search_ex(ptr,0,last_index,result_ind,hashkey,GIM_HASH_NODE_CMP_KEY_MACRO());
  616. if(found) return result_ind;
  617. }
  618. return GIM_INVALID_HASH;
  619. }
  620. //! Retrieves the value associated with the index
  621. /*!
  622. \return the found element, or null
  623. */
  624. inline T * get_value(GUINT hashkey)
  625. {
  626. GUINT index = find(hashkey);
  627. if(index == GIM_INVALID_HASH) return NULL;
  628. return &m_nodes[index].m_data;
  629. }
  630. /*!
  631. */
  632. inline bool erase_by_index(GUINT index)
  633. {
  634. if(index > m_nodes.size()) return false;
  635. if(m_hash_table == NULL)
  636. {
  637. if(is_sorted())
  638. {
  639. return this->_erase_sorted(index);
  640. }
  641. else
  642. {
  643. return this->_erase_unsorted(index);
  644. }
  645. }
  646. else
  647. {
  648. return this->_erase_by_index_hash_table(index);
  649. }
  650. return false;
  651. }
  652. inline bool erase_by_index_unsorted(GUINT index)
  653. {
  654. if(index > m_nodes.size()) return false;
  655. if(m_hash_table == NULL)
  656. {
  657. return this->_erase_unsorted(index);
  658. }
  659. else
  660. {
  661. return this->_erase_by_index_hash_table(index);
  662. }
  663. return false;
  664. }
  665. /*!
  666. */
  667. inline bool erase_by_key(GUINT hashkey)
  668. {
  669. if(size()==0) return false;
  670. if(m_hash_table)
  671. {
  672. return this->_erase_hash_table(hashkey);
  673. }
  674. //Binary search
  675. if(is_sorted()==false) return false;
  676. GUINT result_ind = find(hashkey);
  677. if(result_ind!= GIM_INVALID_HASH)
  678. {
  679. return this->_erase_sorted(result_ind);
  680. }
  681. return false;
  682. }
  683. void clear()
  684. {
  685. m_nodes.clear();
  686. if(m_hash_table==NULL) return;
  687. GUINT datasize = m_table_size*m_node_size;
  688. //Initialize the hashkeys.
  689. GUINT i;
  690. for(i=0;i<datasize;i++)
  691. {
  692. m_hash_table[i] = GIM_INVALID_HASH;// invalidate keys
  693. }
  694. m_sorted = false;
  695. }
  696. //! Insert an element into the hash
  697. /*!
  698. \return If GIM_INVALID_HASH, the object has been inserted succesfully. Else it returns the position
  699. of the existing element.
  700. */
  701. inline GUINT insert(GUINT hashkey, const T & element)
  702. {
  703. if(m_hash_table)
  704. {
  705. return this->_insert_hash_table(hashkey,element);
  706. }
  707. if(this->is_sorted())
  708. {
  709. return this->_insert_sorted(hashkey,element);
  710. }
  711. return this->_insert_unsorted(hashkey,element);
  712. }
  713. //! Insert an element into the hash, and could overrite an existing object with the same hash.
  714. /*!
  715. \return If GIM_INVALID_HASH, the object has been inserted succesfully. Else it returns the position
  716. of the replaced element.
  717. */
  718. inline GUINT insert_override(GUINT hashkey, const T & element)
  719. {
  720. if(m_hash_table)
  721. {
  722. return this->_insert_hash_table_replace(hashkey,element);
  723. }
  724. if(this->is_sorted())
  725. {
  726. return this->_insert_sorted_replace(hashkey,element);
  727. }
  728. this->_insert_unsorted(hashkey,element);
  729. return m_nodes.size();
  730. }
  731. //! Insert an element into the hash,But if this container is a sorted array, this inserts it unsorted
  732. /*!
  733. */
  734. inline GUINT insert_unsorted(GUINT hashkey,const T & element)
  735. {
  736. if(m_hash_table)
  737. {
  738. return this->_insert_hash_table(hashkey,element);
  739. }
  740. return this->_insert_unsorted(hashkey,element);
  741. }
  742. };
  743. #endif // GIM_CONTAINERS_H_INCLUDED