/* Bullet Continuous Collision Detection and Physics Library Copyright (c) 2003-2013 Erwin Coumans http://bulletphysics.org This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 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. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ #ifndef B3_HASH_MAP_H #define B3_HASH_MAP_H #include "b3AlignedObjectArray.h" #include ///very basic hashable string implementation, compatible with b3HashMap struct b3HashString { std::string m_string; unsigned int m_hash; B3_FORCE_INLINE unsigned int getHash()const { return m_hash; } b3HashString(const char* name) :m_string(name) { /* magic numbers from http://www.isthe.com/chongo/tech/comp/fnv/ */ static const unsigned int InitialFNV = 2166136261u; static const unsigned int FNVMultiple = 16777619u; /* Fowler / Noll / Vo (FNV) Hash */ unsigned int hash = InitialFNV; int len = m_string.length(); for(int i = 0; i 0 ) ret = 1 ; return( ret ); } bool equals(const b3HashString& other) const { return (m_string == other.m_string); } }; const int B3_HASH_NULL=0xffffffff; class b3HashInt { int m_uid; public: b3HashInt(int uid) :m_uid(uid) { } int getUid1() const { return m_uid; } void setUid1(int uid) { m_uid = uid; } bool equals(const b3HashInt& other) const { return getUid1() == other.getUid1(); } //to our success B3_FORCE_INLINE unsigned int getHash()const { int key = m_uid; // Thomas Wang's hash key += ~(key << 15); key ^= (key >> 10); key += (key << 3); key ^= (key >> 6); key += ~(key << 11); key ^= (key >> 16); return key; } }; class b3HashPtr { union { const void* m_pointer; int m_hashValues[2]; }; public: b3HashPtr(const void* ptr) :m_pointer(ptr) { } const void* getPointer() const { return m_pointer; } bool equals(const b3HashPtr& other) const { return getPointer() == other.getPointer(); } //to our success B3_FORCE_INLINE unsigned int getHash()const { const bool VOID_IS_8 = ((sizeof(void*)==8)); int key = VOID_IS_8? m_hashValues[0]+m_hashValues[1] : m_hashValues[0]; // Thomas Wang's hash key += ~(key << 15); key ^= (key >> 10); key += (key << 3); key ^= (key >> 6); key += ~(key << 11); key ^= (key >> 16); return key; } }; template class b3HashKeyPtr { int m_uid; public: b3HashKeyPtr(int uid) :m_uid(uid) { } int getUid1() const { return m_uid; } bool equals(const b3HashKeyPtr& other) const { return getUid1() == other.getUid1(); } //to our success B3_FORCE_INLINE unsigned int getHash()const { int key = m_uid; // Thomas Wang's hash key += ~(key << 15); key ^= (key >> 10); key += (key << 3); key ^= (key >> 6); key += ~(key << 11); key ^= (key >> 16); return key; } }; template class b3HashKey { int m_uid; public: b3HashKey(int uid) :m_uid(uid) { } int getUid1() const { return m_uid; } bool equals(const b3HashKey& other) const { return getUid1() == other.getUid1(); } //to our success B3_FORCE_INLINE unsigned int getHash()const { int key = m_uid; // Thomas Wang's hash key += ~(key << 15); key ^= (key >> 10); key += (key << 3); key ^= (key >> 6); key += ~(key << 11); key ^= (key >> 16); return key; } }; ///The b3HashMap template class implements a generic and lightweight hashmap. ///A basic sample of how to use b3HashMap is located in Demos\BasicDemo\main.cpp template class b3HashMap { protected: b3AlignedObjectArray m_hashTable; b3AlignedObjectArray m_next; b3AlignedObjectArray m_valueArray; b3AlignedObjectArray m_keyArray; void growTables(const Key& /*key*/) { int newCapacity = m_valueArray.capacity(); if (m_hashTable.size() < newCapacity) { //grow hashtable and next table int curHashtableSize = m_hashTable.size(); m_hashTable.resize(newCapacity); m_next.resize(newCapacity); int i; for (i= 0; i < newCapacity; ++i) { m_hashTable[i] = B3_HASH_NULL; } for (i = 0; i < newCapacity; ++i) { m_next[i] = B3_HASH_NULL; } for(i=0;i= (unsigned int)m_hashTable.size()) { return B3_HASH_NULL; } int index = m_hashTable[hash]; while ((index != B3_HASH_NULL) && key.equals(m_keyArray[index]) == false) { index = m_next[index]; } return index; } void clear() { m_hashTable.clear(); m_next.clear(); m_valueArray.clear(); m_keyArray.clear(); } }; #endif //B3_HASH_MAP_H