btHashMap.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef BT_HASH_MAP_H
  14. #define BT_HASH_MAP_H
  15. #include <string>
  16. #include "btAlignedObjectArray.h"
  17. ///very basic hashable string implementation, compatible with btHashMap
  18. struct btHashString
  19. {
  20. std::string m_string1;
  21. unsigned int m_hash;
  22. SIMD_FORCE_INLINE unsigned int getHash() const
  23. {
  24. return m_hash;
  25. }
  26. btHashString()
  27. {
  28. m_string1 = "";
  29. m_hash = 0;
  30. }
  31. btHashString(const char* name)
  32. : m_string1(name)
  33. {
  34. /* magic numbers from http://www.isthe.com/chongo/tech/comp/fnv/ */
  35. static const unsigned int InitialFNV = 2166136261u;
  36. static const unsigned int FNVMultiple = 16777619u;
  37. /* Fowler / Noll / Vo (FNV) Hash */
  38. unsigned int hash = InitialFNV;
  39. for (int i = 0; m_string1.c_str()[i]; i++)
  40. {
  41. hash = hash ^ (m_string1.c_str()[i]); /* xor the low 8 bits */
  42. hash = hash * FNVMultiple; /* multiply by the magic number */
  43. }
  44. m_hash = hash;
  45. }
  46. bool equals(const btHashString& other) const
  47. {
  48. return (m_string1 == other.m_string1);
  49. }
  50. };
  51. const int BT_HASH_NULL = 0xffffffff;
  52. class btHashInt
  53. {
  54. int m_uid;
  55. public:
  56. btHashInt()
  57. {
  58. }
  59. btHashInt(int uid) : m_uid(uid)
  60. {
  61. }
  62. int getUid1() const
  63. {
  64. return m_uid;
  65. }
  66. void setUid1(int uid)
  67. {
  68. m_uid = uid;
  69. }
  70. bool equals(const btHashInt& other) const
  71. {
  72. return getUid1() == other.getUid1();
  73. }
  74. //to our success
  75. SIMD_FORCE_INLINE unsigned int getHash() const
  76. {
  77. unsigned int key = m_uid;
  78. // Thomas Wang's hash
  79. key += ~(key << 15);
  80. key ^= (key >> 10);
  81. key += (key << 3);
  82. key ^= (key >> 6);
  83. key += ~(key << 11);
  84. key ^= (key >> 16);
  85. return key;
  86. }
  87. };
  88. class btHashPtr
  89. {
  90. union {
  91. const void* m_pointer;
  92. unsigned int m_hashValues[2];
  93. };
  94. public:
  95. btHashPtr(const void* ptr)
  96. : m_pointer(ptr)
  97. {
  98. }
  99. const void* getPointer() const
  100. {
  101. return m_pointer;
  102. }
  103. bool equals(const btHashPtr& other) const
  104. {
  105. return getPointer() == other.getPointer();
  106. }
  107. //to our success
  108. SIMD_FORCE_INLINE unsigned int getHash() const
  109. {
  110. const bool VOID_IS_8 = ((sizeof(void*) == 8));
  111. unsigned int key = VOID_IS_8 ? m_hashValues[0] + m_hashValues[1] : m_hashValues[0];
  112. // Thomas Wang's hash
  113. key += ~(key << 15);
  114. key ^= (key >> 10);
  115. key += (key << 3);
  116. key ^= (key >> 6);
  117. key += ~(key << 11);
  118. key ^= (key >> 16);
  119. return key;
  120. }
  121. };
  122. template <class Value>
  123. class btHashKeyPtr
  124. {
  125. int m_uid;
  126. public:
  127. btHashKeyPtr(int uid) : m_uid(uid)
  128. {
  129. }
  130. int getUid1() const
  131. {
  132. return m_uid;
  133. }
  134. bool equals(const btHashKeyPtr<Value>& other) const
  135. {
  136. return getUid1() == other.getUid1();
  137. }
  138. //to our success
  139. SIMD_FORCE_INLINE unsigned int getHash() const
  140. {
  141. unsigned int key = m_uid;
  142. // Thomas Wang's hash
  143. key += ~(key << 15);
  144. key ^= (key >> 10);
  145. key += (key << 3);
  146. key ^= (key >> 6);
  147. key += ~(key << 11);
  148. key ^= (key >> 16);
  149. return key;
  150. }
  151. };
  152. template <class Value>
  153. class btHashKey
  154. {
  155. int m_uid;
  156. public:
  157. btHashKey(int uid) : m_uid(uid)
  158. {
  159. }
  160. int getUid1() const
  161. {
  162. return m_uid;
  163. }
  164. bool equals(const btHashKey<Value>& other) const
  165. {
  166. return getUid1() == other.getUid1();
  167. }
  168. //to our success
  169. SIMD_FORCE_INLINE unsigned int getHash() const
  170. {
  171. unsigned int key = m_uid;
  172. // Thomas Wang's hash
  173. key += ~(key << 15);
  174. key ^= (key >> 10);
  175. key += (key << 3);
  176. key ^= (key >> 6);
  177. key += ~(key << 11);
  178. key ^= (key >> 16);
  179. return key;
  180. }
  181. };
  182. ///The btHashMap template class implements a generic and lightweight hashmap.
  183. ///A basic sample of how to use btHashMap is located in Demos\BasicDemo\main.cpp
  184. template <class Key, class Value>
  185. class btHashMap
  186. {
  187. protected:
  188. btAlignedObjectArray<int> m_hashTable;
  189. btAlignedObjectArray<int> m_next;
  190. btAlignedObjectArray<Value> m_valueArray;
  191. btAlignedObjectArray<Key> m_keyArray;
  192. void growTables(const Key& /*key*/)
  193. {
  194. int newCapacity = m_valueArray.capacity();
  195. if (m_hashTable.size() < newCapacity)
  196. {
  197. //grow hashtable and next table
  198. int curHashtableSize = m_hashTable.size();
  199. m_hashTable.resize(newCapacity);
  200. m_next.resize(newCapacity);
  201. int i;
  202. for (i = 0; i < newCapacity; ++i)
  203. {
  204. m_hashTable[i] = BT_HASH_NULL;
  205. }
  206. for (i = 0; i < newCapacity; ++i)
  207. {
  208. m_next[i] = BT_HASH_NULL;
  209. }
  210. for (i = 0; i < curHashtableSize; i++)
  211. {
  212. //const Value& value = m_valueArray[i];
  213. //const Key& key = m_keyArray[i];
  214. int hashValue = m_keyArray[i].getHash() & (m_valueArray.capacity() - 1); // New hash value with new mask
  215. m_next[i] = m_hashTable[hashValue];
  216. m_hashTable[hashValue] = i;
  217. }
  218. }
  219. }
  220. public:
  221. void insert(const Key& key, const Value& value)
  222. {
  223. int hash = key.getHash() & (m_valueArray.capacity() - 1);
  224. //replace value if the key is already there
  225. int index = findIndex(key);
  226. if (index != BT_HASH_NULL)
  227. {
  228. m_valueArray[index] = value;
  229. return;
  230. }
  231. int count = m_valueArray.size();
  232. int oldCapacity = m_valueArray.capacity();
  233. m_valueArray.push_back(value);
  234. m_keyArray.push_back(key);
  235. int newCapacity = m_valueArray.capacity();
  236. if (oldCapacity < newCapacity)
  237. {
  238. growTables(key);
  239. //hash with new capacity
  240. hash = key.getHash() & (m_valueArray.capacity() - 1);
  241. }
  242. m_next[count] = m_hashTable[hash];
  243. m_hashTable[hash] = count;
  244. }
  245. void remove(const Key& key)
  246. {
  247. int hash = key.getHash() & (m_valueArray.capacity() - 1);
  248. int pairIndex = findIndex(key);
  249. if (pairIndex == BT_HASH_NULL)
  250. {
  251. return;
  252. }
  253. // Remove the pair from the hash table.
  254. int index = m_hashTable[hash];
  255. btAssert(index != BT_HASH_NULL);
  256. int previous = BT_HASH_NULL;
  257. while (index != pairIndex)
  258. {
  259. previous = index;
  260. index = m_next[index];
  261. }
  262. if (previous != BT_HASH_NULL)
  263. {
  264. btAssert(m_next[previous] == pairIndex);
  265. m_next[previous] = m_next[pairIndex];
  266. }
  267. else
  268. {
  269. m_hashTable[hash] = m_next[pairIndex];
  270. }
  271. // We now move the last pair into spot of the
  272. // pair being removed. We need to fix the hash
  273. // table indices to support the move.
  274. int lastPairIndex = m_valueArray.size() - 1;
  275. // If the removed pair is the last pair, we are done.
  276. if (lastPairIndex == pairIndex)
  277. {
  278. m_valueArray.pop_back();
  279. m_keyArray.pop_back();
  280. return;
  281. }
  282. // Remove the last pair from the hash table.
  283. int lastHash = m_keyArray[lastPairIndex].getHash() & (m_valueArray.capacity() - 1);
  284. index = m_hashTable[lastHash];
  285. btAssert(index != BT_HASH_NULL);
  286. previous = BT_HASH_NULL;
  287. while (index != lastPairIndex)
  288. {
  289. previous = index;
  290. index = m_next[index];
  291. }
  292. if (previous != BT_HASH_NULL)
  293. {
  294. btAssert(m_next[previous] == lastPairIndex);
  295. m_next[previous] = m_next[lastPairIndex];
  296. }
  297. else
  298. {
  299. m_hashTable[lastHash] = m_next[lastPairIndex];
  300. }
  301. // Copy the last pair into the remove pair's spot.
  302. m_valueArray[pairIndex] = m_valueArray[lastPairIndex];
  303. m_keyArray[pairIndex] = m_keyArray[lastPairIndex];
  304. // Insert the last pair into the hash table
  305. m_next[pairIndex] = m_hashTable[lastHash];
  306. m_hashTable[lastHash] = pairIndex;
  307. m_valueArray.pop_back();
  308. m_keyArray.pop_back();
  309. }
  310. int size() const
  311. {
  312. return m_valueArray.size();
  313. }
  314. const Value* getAtIndex(int index) const
  315. {
  316. btAssert(index < m_valueArray.size());
  317. btAssert(index >= 0);
  318. if (index >= 0 && index < m_valueArray.size())
  319. {
  320. return &m_valueArray[index];
  321. }
  322. return 0;
  323. }
  324. Value* getAtIndex(int index)
  325. {
  326. btAssert(index < m_valueArray.size());
  327. btAssert(index >= 0);
  328. if (index >= 0 && index < m_valueArray.size())
  329. {
  330. return &m_valueArray[index];
  331. }
  332. return 0;
  333. }
  334. Key getKeyAtIndex(int index)
  335. {
  336. btAssert(index < m_keyArray.size());
  337. btAssert(index >= 0);
  338. return m_keyArray[index];
  339. }
  340. const Key getKeyAtIndex(int index) const
  341. {
  342. btAssert(index < m_keyArray.size());
  343. btAssert(index >= 0);
  344. return m_keyArray[index];
  345. }
  346. Value* operator[](const Key& key)
  347. {
  348. return find(key);
  349. }
  350. const Value* operator[](const Key& key) const
  351. {
  352. return find(key);
  353. }
  354. const Value* find(const Key& key) const
  355. {
  356. int index = findIndex(key);
  357. if (index == BT_HASH_NULL)
  358. {
  359. return NULL;
  360. }
  361. return &m_valueArray[index];
  362. }
  363. Value* find(const Key& key)
  364. {
  365. int index = findIndex(key);
  366. if (index == BT_HASH_NULL)
  367. {
  368. return NULL;
  369. }
  370. return &m_valueArray[index];
  371. }
  372. int findIndex(const Key& key) const
  373. {
  374. unsigned int hash = key.getHash() & (m_valueArray.capacity() - 1);
  375. if (hash >= (unsigned int)m_hashTable.size())
  376. {
  377. return BT_HASH_NULL;
  378. }
  379. int index = m_hashTable[hash];
  380. while ((index != BT_HASH_NULL) && key.equals(m_keyArray[index]) == false)
  381. {
  382. index = m_next[index];
  383. }
  384. return index;
  385. }
  386. void clear()
  387. {
  388. m_hashTable.clear();
  389. m_next.clear();
  390. m_valueArray.clear();
  391. m_keyArray.clear();
  392. }
  393. };
  394. #endif //BT_HASH_MAP_H