hash_map.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*************************************************************************/
  2. /* hash_map.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef HASH_MAP_H
  30. #define HASH_MAP_H
  31. #include "hashfuncs.h"
  32. #include "error_macros.h"
  33. #include "ustring.h"
  34. #include "os/memory.h"
  35. #include "list.h"
  36. class HashMapHahserDefault {
  37. public:
  38. static _FORCE_INLINE_ uint32_t hash(const String &p_string) { return p_string.hash(); }
  39. static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); }
  40. static _FORCE_INLINE_ uint32_t hash(const uint64_t p_int) {
  41. uint64_t v=p_int;
  42. v = (~v) + (v << 18); // v = (v << 18) - v - 1;
  43. v = v ^ (v >> 31);
  44. v = v * 21; // v = (v + (v << 2)) + (v << 4);
  45. v = v ^ (v >> 11);
  46. v = v + (v << 6);
  47. v = v ^ (v >> 22);
  48. return (int) v;
  49. }
  50. static _FORCE_INLINE_ uint32_t hash(const int64_t p_int) { return hash(uint64_t(p_int)); }
  51. static _FORCE_INLINE_ uint32_t hash(const uint32_t p_int) { return p_int; }
  52. static _FORCE_INLINE_ uint32_t hash(const int32_t p_int) { return (uint32_t)p_int; }
  53. static _FORCE_INLINE_ uint32_t hash(const uint16_t p_int) { return p_int; }
  54. static _FORCE_INLINE_ uint32_t hash(const int16_t p_int) { return (uint32_t)p_int; }
  55. static _FORCE_INLINE_ uint32_t hash(const uint8_t p_int) { return p_int; }
  56. static _FORCE_INLINE_ uint32_t hash(const int8_t p_int) { return (uint32_t)p_int; }
  57. static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return (uint32_t)p_wchar; }
  58. // static _FORCE_INLINE_ uint32_t hash(const void* p_ptr) { return uint32_t(uint64_t(p_ptr))*(0x9e3779b1L); }
  59. };
  60. /**
  61. * @class HashMap
  62. * @author Juan Linietsky <[email protected]>
  63. *
  64. * Implementation of a standard Hashing HashMap, for quick lookups of Data associated with a Key.
  65. * The implementation provides hashers for the default types, if you need a special kind of hasher, provide
  66. * your own.
  67. * @param TKey Key, search is based on it, needs to be hasheable. It is unique in this container.
  68. * @param TData Data, data associated with the key
  69. * @param Hasher Hasher object, needs to provide a valid static hash function for TKey
  70. * @param MIN_HASH_TABLE_POWER Miminum size of the hash table, as a power of two. You rarely need to change this parameter.
  71. * @param RELATIONSHIP Relationship at which the hash table is resized. if amount of elements is RELATIONSHIP
  72. * times bigger than the hash table, table is resized to solve this condition. if RELATIONSHIP is zero, table is always MIN_HASH_TABLE_POWER.
  73. *
  74. */
  75. template<class TKey, class TData, class Hasher=HashMapHahserDefault,uint8_t MIN_HASH_TABLE_POWER=3,uint8_t RELATIONSHIP=8>
  76. class HashMap {
  77. public:
  78. struct Pair {
  79. TKey key;
  80. TData data;
  81. Pair() {}
  82. Pair(const TKey& p_key, const TData& p_data) { key=p_key; data=p_data; }
  83. };
  84. private:
  85. struct Entry {
  86. uint32_t hash;
  87. Entry *next;
  88. Pair pair;
  89. Entry() { next=0; }
  90. };
  91. Entry **hash_table;
  92. uint8_t hash_table_power;
  93. uint32_t elements;
  94. void make_hash_table() {
  95. ERR_FAIL_COND( hash_table );
  96. hash_table = memnew_arr( Entry*, (1<<MIN_HASH_TABLE_POWER) );
  97. hash_table_power = MIN_HASH_TABLE_POWER;
  98. elements=0;
  99. for (int i=0;i<(1<<MIN_HASH_TABLE_POWER);i++)
  100. hash_table[i]=0;
  101. }
  102. void erase_hash_table() {
  103. ERR_FAIL_COND(elements);
  104. memdelete_arr( hash_table );
  105. hash_table=0;
  106. hash_table_power=0;
  107. elements=0;
  108. }
  109. void check_hash_table() {
  110. int new_hash_table_power=-1;
  111. if ((int)elements > ( (1<<hash_table_power) * RELATIONSHIP ) ) {
  112. /* rehash up */
  113. new_hash_table_power=hash_table_power+1;
  114. while( (int)elements > ( (1<<new_hash_table_power) * RELATIONSHIP ) ) {
  115. new_hash_table_power++;
  116. }
  117. } else if ( (hash_table_power>(int)MIN_HASH_TABLE_POWER) && ((int)elements < ( (1<<(hash_table_power-1)) * RELATIONSHIP ) ) ) {
  118. /* rehash down */
  119. new_hash_table_power=hash_table_power-1;
  120. while( (int)elements < ( (1<<(new_hash_table_power-1)) * RELATIONSHIP ) ) {
  121. new_hash_table_power--;
  122. }
  123. if (new_hash_table_power<(int)MIN_HASH_TABLE_POWER)
  124. new_hash_table_power=MIN_HASH_TABLE_POWER;
  125. }
  126. if (new_hash_table_power==-1)
  127. return;
  128. Entry ** new_hash_table = memnew_arr( Entry*, (1<<new_hash_table_power) );
  129. if (!new_hash_table) {
  130. ERR_PRINT("Out of Memory");
  131. return;
  132. }
  133. for (int i=0;i<(1<<new_hash_table_power);i++) {
  134. new_hash_table[i]=0;
  135. }
  136. for (int i=0;i<(1<<hash_table_power);i++) {
  137. while( hash_table[i] ) {
  138. Entry *se=hash_table[i];
  139. hash_table[i]=se->next;
  140. int new_pos = se->hash & ((1<<new_hash_table_power)-1);
  141. se->next=new_hash_table[new_pos];
  142. new_hash_table[new_pos]=se;
  143. }
  144. }
  145. if (hash_table)
  146. memdelete_arr( hash_table );
  147. hash_table=new_hash_table;
  148. hash_table_power=new_hash_table_power;
  149. }
  150. /* I want to have only one function.. */
  151. _FORCE_INLINE_ const Entry * get_entry( const TKey& p_key ) const {
  152. uint32_t hash = Hasher::hash( p_key );
  153. uint32_t index = hash&((1<<hash_table_power)-1);
  154. Entry *e = hash_table[index];
  155. while (e) {
  156. /* checking hash first avoids comparing key, which may take longer */
  157. if (e->hash == hash && e->pair.key == p_key ) {
  158. /* the pair exists in this hashtable, so just update data */
  159. return e;
  160. }
  161. e=e->next;
  162. }
  163. return NULL;
  164. }
  165. Entry * create_entry(const TKey& p_key) {
  166. /* if entry doesn't exist, create it */
  167. Entry *e = memnew( Entry );
  168. ERR_FAIL_COND_V(!e,NULL); /* out of memory */
  169. uint32_t hash = Hasher::hash( p_key );
  170. uint32_t index = hash&((1<<hash_table_power)-1);
  171. e->next = hash_table[index];
  172. e->hash = hash;
  173. e->pair.key=p_key;
  174. hash_table[index]=e;
  175. elements++;
  176. return e;
  177. }
  178. void copy_from(const HashMap& p_t) {
  179. if (&p_t==this)
  180. return; /* much less bother with that */
  181. clear();
  182. if (!p_t.hash_table || p_t.hash_table_power==0)
  183. return; /* not copying from empty table */
  184. hash_table = memnew_arr(Entry*,1<<p_t.hash_table_power);
  185. hash_table_power=p_t.hash_table_power;
  186. elements=p_t.elements;
  187. for (int i=0;i<( 1<<p_t.hash_table_power );i++) {
  188. hash_table[i]=NULL;
  189. /* elements will be in the reverse order, but it doesn't matter */
  190. const Entry *e = p_t.hash_table[i];
  191. while(e) {
  192. Entry *le = memnew( Entry ); /* local entry */
  193. *le=*e; /* copy data */
  194. /* add to list and reassign pointers */
  195. le->next=hash_table[i];
  196. hash_table[i]=le;
  197. e=e->next;
  198. }
  199. }
  200. }
  201. public:
  202. void set( const TKey& p_key, const TData& p_data ) {
  203. set( Pair( p_key, p_data ) );
  204. }
  205. void set( const Pair& p_pair ) {
  206. if (!hash_table)
  207. make_hash_table(); // if no table, make one
  208. else
  209. check_hash_table(); // perform mantenience routine
  210. /* As said, i want to have only one get_entry */
  211. Entry *e = const_cast<Entry*>( get_entry(p_pair.key) );
  212. /* if we made it up to here, the pair doesn't exist, create and assign */
  213. if (!e) {
  214. e=create_entry(p_pair.key);
  215. if (!e)
  216. return;
  217. }
  218. e->pair.data = p_pair.data;
  219. }
  220. bool has( const TKey& p_key ) const {
  221. return getptr(p_key)!=NULL;
  222. }
  223. /**
  224. * Get a key from data, return a const reference.
  225. * WARNING: this doesn't check errors, use either getptr and check NULL, or check
  226. * first with has(key)
  227. */
  228. const TData& get( const TKey& p_key ) const {
  229. const TData* res = getptr(p_key);
  230. ERR_FAIL_COND_V(!res,*res);
  231. return *res;
  232. }
  233. TData& get( const TKey& p_key ) {
  234. TData* res = getptr(p_key);
  235. ERR_FAIL_COND_V(!res,*res);
  236. return *res;
  237. }
  238. /**
  239. * Same as get, except it can return NULL when item was not found.
  240. * This is mainly used for speed purposes.
  241. */
  242. _FORCE_INLINE_ TData* getptr( const TKey& p_key ) {
  243. if (!hash_table)
  244. return NULL;
  245. Entry *e=const_cast<Entry*>(get_entry(p_key ));
  246. if (e)
  247. return &e->pair.data;
  248. return NULL;
  249. }
  250. _FORCE_INLINE_ const TData* getptr( const TKey& p_key ) const {
  251. if (!hash_table)
  252. return NULL;
  253. const Entry *e=const_cast<Entry*>(get_entry(p_key ));
  254. if (e)
  255. return &e->pair.data;
  256. return NULL;
  257. }
  258. /**
  259. * Same as get, except it can return NULL when item was not found.
  260. * This version is custom, will take a hash and a custom key (that should support operator==()
  261. */
  262. template<class C>
  263. _FORCE_INLINE_ TData* custom_getptr( C p_custom_key,uint32_t p_custom_hash ) {
  264. if (!hash_table)
  265. return NULL;
  266. uint32_t hash = p_custom_hash;
  267. uint32_t index = hash&((1<<hash_table_power)-1);
  268. Entry *e = hash_table[index];
  269. while (e) {
  270. /* checking hash first avoids comparing key, which may take longer */
  271. if (e->hash == hash && e->pair.key == p_custom_key ) {
  272. /* the pair exists in this hashtable, so just update data */
  273. return &e->pair.data;
  274. }
  275. e=e->next;
  276. }
  277. return NULL;
  278. }
  279. template<class C>
  280. _FORCE_INLINE_ const TData* custom_getptr( C p_custom_key,uint32_t p_custom_hash ) const {
  281. if (!hash_table)
  282. return NULL;
  283. uint32_t hash = p_custom_hash;
  284. uint32_t index = hash&((1<<hash_table_power)-1);
  285. const Entry *e = hash_table[index];
  286. while (e) {
  287. /* checking hash first avoids comparing key, which may take longer */
  288. if (e->hash == hash && e->pair.key == p_custom_key ) {
  289. /* the pair exists in this hashtable, so just update data */
  290. return &e->pair.data;
  291. }
  292. e=e->next;
  293. }
  294. return NULL;
  295. }
  296. /**
  297. * Erase an item, return true if erasing was succesful
  298. */
  299. bool erase( const TKey& p_key ) {
  300. if (!hash_table)
  301. return false;
  302. uint32_t hash = Hasher::hash( p_key );
  303. uint32_t index = hash&((1<<hash_table_power)-1);
  304. Entry *e = hash_table[index];
  305. Entry *p=NULL;
  306. while (e) {
  307. /* checking hash first avoids comparing key, which may take longer */
  308. if (e->hash == hash && e->pair.key == p_key ) {
  309. if (p) {
  310. p->next=e->next;
  311. } else {
  312. //begin of list
  313. hash_table[index]=e->next;
  314. }
  315. memdelete(e);
  316. elements--;
  317. if (elements==0)
  318. erase_hash_table();
  319. else
  320. check_hash_table();
  321. return true;
  322. }
  323. p=e;
  324. e=e->next;
  325. }
  326. return false;
  327. }
  328. inline const TData& operator[](const TKey& p_key) const { //constref
  329. return get(p_key);
  330. }
  331. inline TData& operator[](const TKey& p_key ) { //assignment
  332. if (!hash_table)
  333. make_hash_table(); // if no table, make one
  334. else
  335. check_hash_table(); // perform mantenience routine
  336. Entry *e = const_cast<Entry*>( get_entry(p_key) );
  337. /* if we made it up to here, the pair doesn't exist, create */
  338. if (!e) {
  339. e=create_entry(p_key);
  340. if (!e)
  341. return *(TData*)NULL; /* panic! */
  342. }
  343. return e->pair.data;
  344. }
  345. /**
  346. * Get the next key to p_key, and the first key if p_key is null.
  347. * Returns a pointer to the next key if found, NULL otherwise.
  348. * Adding/Removing elements while iterating will, of course, have unexpected results, don't do it.
  349. *
  350. * Example:
  351. *
  352. * const TKey *k=NULL;
  353. *
  354. * while( (k=table.next(k)) ) {
  355. *
  356. * print( *k );
  357. * }
  358. *
  359. */
  360. const TKey* next(const TKey* p_key) const {
  361. if (!hash_table) return NULL;
  362. if (!p_key) { /* get the first key */
  363. for (int i=0;i<(1<<hash_table_power);i++) {
  364. if (hash_table[i]) {
  365. return &hash_table[i]->pair.key;
  366. }
  367. }
  368. } else { /* get the next key */
  369. const Entry *e = get_entry( *p_key );
  370. ERR_FAIL_COND_V( !e, NULL ); /* invalid key supplied */
  371. if (e->next) {
  372. /* if there is a "next" in the list, return that */
  373. return &e->next->pair.key;
  374. } else {
  375. /* go to next entries */
  376. uint32_t index = e->hash&((1<<hash_table_power)-1);
  377. index++;
  378. for (int i=index;i<(1<<hash_table_power);i++) {
  379. if (hash_table[i]) {
  380. return &hash_table[i]->pair.key;
  381. }
  382. }
  383. }
  384. /* nothing found, was at end */
  385. }
  386. return NULL; /* nothing found */
  387. }
  388. inline unsigned int size() const {
  389. return elements;
  390. }
  391. inline bool empty() const {
  392. return elements==0;
  393. }
  394. void clear() {
  395. /* clean up */
  396. if (hash_table) {
  397. for (int i=0;i<(1<<hash_table_power);i++) {
  398. while (hash_table[i]) {
  399. Entry *e=hash_table[i];
  400. hash_table[i]=e->next;
  401. memdelete( e );
  402. }
  403. }
  404. memdelete_arr( hash_table );
  405. }
  406. hash_table=0;
  407. hash_table_power=0;
  408. elements=0;
  409. }
  410. void operator=(const HashMap& p_table) {
  411. copy_from(p_table);
  412. }
  413. HashMap() {
  414. hash_table=NULL;
  415. elements=0;
  416. hash_table_power=0;
  417. }
  418. void get_key_list(List<TKey> *p_keys) const {
  419. if (!hash_table)
  420. return;
  421. for(int i=0;i<(1<<hash_table_power);i++) {
  422. Entry *e=hash_table[i];
  423. while(e) {
  424. p_keys->push_back(e->pair.key);
  425. e=e->next;
  426. }
  427. }
  428. }
  429. HashMap(const HashMap& p_table) {
  430. hash_table=NULL;
  431. elements=0;
  432. hash_table_power=0;
  433. copy_from(p_table);
  434. }
  435. ~HashMap() {
  436. clear();
  437. }
  438. };
  439. #endif