very basic hashable string implementation, compatible with b3HashMap
| 22 | |
| 23 | ///very basic hashable string implementation, compatible with b3HashMap |
| 24 | struct b3HashString |
| 25 | { |
| 26 | std::string m_string; |
| 27 | unsigned int m_hash; |
| 28 | |
| 29 | B3_FORCE_INLINE unsigned int getHash() const |
| 30 | { |
| 31 | return m_hash; |
| 32 | } |
| 33 | |
| 34 | b3HashString(const char* name) |
| 35 | : m_string(name) |
| 36 | { |
| 37 | /* magic numbers from http://www.isthe.com/chongo/tech/comp/fnv/ */ |
| 38 | static const unsigned int InitialFNV = 2166136261u; |
| 39 | static const unsigned int FNVMultiple = 16777619u; |
| 40 | |
| 41 | /* Fowler / Noll / Vo (FNV) Hash */ |
| 42 | unsigned int hash = InitialFNV; |
| 43 | int len = m_string.length(); |
| 44 | for (int i = 0; i < len; i++) |
| 45 | { |
| 46 | hash = hash ^ (m_string[i]); /* xor the low 8 bits */ |
| 47 | hash = hash * FNVMultiple; /* multiply by the magic number */ |
| 48 | } |
| 49 | m_hash = hash; |
| 50 | } |
| 51 | |
| 52 | int portableStringCompare(const char* src, const char* dst) const |
| 53 | { |
| 54 | int ret = 0; |
| 55 | |
| 56 | while (!(ret = *(unsigned char*)src - *(unsigned char*)dst) && *dst) |
| 57 | ++src, ++dst; |
| 58 | |
| 59 | if (ret < 0) |
| 60 | ret = -1; |
| 61 | else if (ret > 0) |
| 62 | ret = 1; |
| 63 | |
| 64 | return (ret); |
| 65 | } |
| 66 | |
| 67 | bool equals(const b3HashString& other) const |
| 68 | { |
| 69 | return (m_string == other.m_string); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | const int B3_HASH_NULL = 0xffffffff; |
| 74 |