diff --git a/src/LinearMath/btHashMap.h b/src/LinearMath/btHashMap.h index f883e0e48..98f587c0b 100644 --- a/src/LinearMath/btHashMap.h +++ b/src/LinearMath/btHashMap.h @@ -3,6 +3,59 @@ #include "btAlignedObjectArray.h" +///very basic hashable string implementation, compatible with btHashMap +struct btHashString +{ + const char* m_string; + unsigned int m_hash; + + SIMD_FORCE_INLINE unsigned int getHash()const + { + return m_hash; + } + + btHashString(const char* name) + :m_string(name) + { + /* magic numbers from http://www.isthe.com/chongo/tech/comp/fnv/ */ + static const unsigned int InitialFNV = 2166136261; + static const unsigned int FNVMultiple = 16777619; + + /* Fowler / Noll / Vo (FNV) Hash */ + unsigned int hash = InitialFNV; + + for(int i = 0; m_string[i]; i++) + { + hash = hash ^ (m_string[i]); /* xor the low 8 bits */ + hash = hash * FNVMultiple; /* multiply by the magic number */ + } + m_hash = hash; + } + + int portableStringCompare(const char* src, const char* dst) const + { + int ret = 0 ; + + while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst) + ++src, ++dst; + + if ( ret < 0 ) + ret = -1 ; + else if ( ret > 0 ) + ret = 1 ; + + return( ret ); + } + + const bool equals(const btHashString& other) const + { + return (m_string == other.m_string) || + (0==portableStringCompare(m_string,other.m_string)); + + } + +}; + const int BT_HASH_NULL=0xffffffff; template @@ -16,11 +69,15 @@ public: { } - int getUid() const + int getUid1() const { return m_uid; } + bool equals(const btHashKey& other) const + { + return getUid1() == other.getUid1(); + } //to our success SIMD_FORCE_INLINE unsigned int getHash()const { @@ -35,10 +92,7 @@ public: return key; } - btHashKey getKey(const Value& value) const - { - return btHashKey(value.getUid()); - } + }; @@ -53,11 +107,16 @@ public: { } - int getUid() const + int getUid1() const { return m_uid; } + bool equals(const btHashKeyPtr& other) const + { + return getUid1() == other.getUid1(); + } + //to our success SIMD_FORCE_INLINE unsigned int getHash()const { @@ -72,10 +131,7 @@ public: return key; } - btHashKeyPtr getKey(const Value& value) const - { - return btHashKeyPtr(value->getUid()); - } + }; ///The btHashMap template class implements a generic and lightweight hashmap. @@ -86,9 +142,9 @@ class btHashMap btAlignedObjectArray m_hashTable; btAlignedObjectArray m_next; + btAlignedObjectArray m_valueArray; - - + btAlignedObjectArray m_keyArray; void growTables(const Key& key) { @@ -116,8 +172,9 @@ class btHashMap for(i=0;i= m_hashTable.size()) + if (hash >= (unsigned int)m_hashTable.size()) { return BT_HASH_NULL; } int index = m_hashTable[hash]; - while ((index != BT_HASH_NULL) && (key.getUid() == key.getKey(m_valueArray[index]).getUid()) == false) + while ((index != BT_HASH_NULL) && key.equals(m_keyArray[index]) == false) { index = m_next[index]; } @@ -296,6 +362,7 @@ class btHashMap m_hashTable.clear(); m_next.clear(); m_valueArray.clear(); + m_keyArray.clear(); } };