HashTableKey_String.cpp
Go to the documentation of this file.00001 //*** HashTableKey_String.cpp *** 00002 00003 #include "HashTableKey_String.h" 00004 #include "StandardLibrary.h" 00005 00006 00007 00008 HashTableKey_String::HashTableKey_String(): 00009 string_(0), 00010 hash_(0) 00011 { 00012 } 00013 00014 HashTableKey_String::HashTableKey_String(const HashTableKey_String& string): 00015 string_(0), 00016 hash_(0) 00017 { 00018 if (string.string_) 00019 { 00020 string_=StrDup(string.string_); 00021 } 00022 hash_=string.hash_; 00023 } 00024 00025 const HashTableKey_String& HashTableKey_String::operator =(const HashTableKey_String& string ) 00026 { 00027 // Get rid of the copy of the string 00028 if (string_) 00029 { 00030 Free(string_); 00031 string_=0; 00032 } 00033 if (string.string_) 00034 { 00035 string_=StrDup(string.string_); 00036 } 00037 hash_=string.hash_; 00038 return *this; 00039 } 00040 00041 00042 //*** Constructor *** 00043 00044 HashTableKey_String::HashTableKey_String(const char* string): 00045 string_(0), 00046 hash_(0) 00047 { 00048 00049 // Keep a copy of the string 00050 if (string) 00051 { 00052 string_=StrDup(string); 00053 } 00054 00055 // Calculate the hash value for the string 00056 hash_=CalculateHash(string); 00057 } 00058 00059 00060 //*** Destructor *** 00061 00062 HashTableKey_String::~HashTableKey_String() 00063 { 00064 // Get rid of the copy of the string 00065 if (string_) 00066 { 00067 Free(string_); 00068 string_=0; 00069 } 00070 } 00071 00072 00073 //*** GetHash *** 00074 00075 unsigned int HashTableKey_String::GetHash() const 00076 { 00077 return hash_; 00078 } 00079 00080 00081 //*** GetString *** 00082 00083 const char* HashTableKey_String::GetString() const 00084 { 00085 return string_; 00086 } 00087 00088 //*** Compare *** 00089 00090 bool HashTableKey_String::Compare(const HashTableKey* key) const 00091 { 00092 // Make sure key is of the correct type 00093 if (key->GetType()!=GetType()) 00094 { 00095 return false; 00096 } 00097 00098 if (string_==0 || (static_cast<const HashTableKey_String*>(key))->string_==0) 00099 { 00100 if (string_==(static_cast<const HashTableKey_String*>(key))->string_) 00101 { 00102 return true; 00103 } 00104 return false; 00105 } 00106 // The keys are only the same if the two strings are the same (two 00107 // different strings might have the same hash value) 00108 return (StrICmp(string_,(static_cast<const HashTableKey_String*>(key))->string_)==0); 00109 } 00110 00111 00112 //*** CalculateHash *** 00113 00114 unsigned int HashTableKey_String::CalculateHash(const char* key) const 00115 { 00116 unsigned long hash = 5381; // Seed value 00117 00118 // Modify hash for each character in the string 00119 const char* stringData=key; 00120 while (*stringData) 00121 { 00122 // A little bit-manipulation magic to get a nice distribution of values 00123 hash = ((hash << 5) + hash) ^ ToUpper(*stringData); 00124 stringData++; 00125 } 00126 00127 // Return the final hash value 00128 return hash; 00129 00130 } 00131 00132 00133 //*** GetType *** 00134 00135 StringId HashTableKey_String::GetType() const 00136 { 00137 static StringId type("HashTableKey_String"); 00138 return type; 00139 } 00140
Reproduction/republishing of any material on this site without permission is strictly prohibited.
