1 /* 2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef STRING_KEY_H 6 #define STRING_KEY_H 7 8 9 #include "String.h" 10 11 12 class StringKey { 13 public: 14 StringKey(const ::String& string) 15 : 16 fString(string.Data()), 17 fHash(string.Hash()) 18 { 19 } 20 21 explicit StringKey(const char* string) 22 : 23 fString(string), 24 fHash(hash_hash_string(string)) 25 { 26 } 27 28 const char* String() const 29 { 30 return fString; 31 } 32 33 uint32 Hash() const 34 { 35 return fHash; 36 } 37 38 inline int Compare(const ::StringKey& other) const 39 { 40 if (fHash == other.fHash) { 41 if (fString == other.fString) 42 return 0; 43 return strcmp(fString, other.fString); 44 } 45 return (fHash < other.fHash) ? -1 : 1; 46 } 47 48 bool operator==(const ::String& other) const 49 { 50 return Compare(StringKey(other)) == 0; 51 } 52 53 bool operator!=(const ::String& other) const 54 { 55 return !(*this == other); 56 } 57 58 private: 59 const char* fString; 60 uint32 fHash; 61 }; 62 63 64 #endif // STRING_KEY_H 65