stringTable.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "core/strings/stringFunctions.h"
  23. #include "core/stringTable.h"
  24. #include "platform/profiler.h"
  25. _StringTable *_gStringTable = NULL;
  26. const U32 _StringTable::csm_stInitSize = 29;
  27. //---------------------------------------------------------------
  28. //
  29. // StringTable functions
  30. //
  31. //---------------------------------------------------------------
  32. namespace {
  33. bool sgInitTable = true;
  34. U8 sgHashTable[256];
  35. void initTolowerTable()
  36. {
  37. for (U32 i = 0; i < 256; i++) {
  38. U8 c = dTolower(i);
  39. sgHashTable[i] = c * c;
  40. }
  41. sgInitTable = false;
  42. }
  43. } // namespace {}
  44. U32 _StringTable::hashString(const char* str)
  45. {
  46. if (sgInitTable)
  47. initTolowerTable();
  48. if(!str) return -1;
  49. U32 ret = 0;
  50. char c;
  51. while((c = *str++) != 0) {
  52. ret <<= 1;
  53. ret ^= sgHashTable[static_cast<U8>(c)];
  54. }
  55. return ret;
  56. }
  57. U32 _StringTable::hashStringn(const char* str, S32 len)
  58. {
  59. if (sgInitTable)
  60. initTolowerTable();
  61. U32 ret = 0;
  62. char c;
  63. while((c = *str++) != 0 && len--) {
  64. ret <<= 1;
  65. ret ^= sgHashTable[static_cast<U8>(c)];
  66. }
  67. return ret;
  68. }
  69. //--------------------------------------
  70. _StringTable::_StringTable()
  71. {
  72. buckets = (Node **) dMalloc(csm_stInitSize * sizeof(Node *));
  73. for(U32 i = 0; i < csm_stInitSize; i++) {
  74. buckets[i] = 0;
  75. }
  76. numBuckets = csm_stInitSize;
  77. itemCount = 0;
  78. }
  79. //--------------------------------------
  80. _StringTable::~_StringTable()
  81. {
  82. dFree(buckets);
  83. }
  84. //--------------------------------------
  85. void _StringTable::create()
  86. {
  87. //AssertFatal(_gStringTable == NULL, "StringTable::create: StringTable already exists.");
  88. if(!_gStringTable)
  89. {
  90. _gStringTable = new _StringTable;
  91. _gStringTable->_EmptyString = _gStringTable->insert("");
  92. }
  93. }
  94. //--------------------------------------
  95. void _StringTable::destroy()
  96. {
  97. AssertFatal(StringTable != NULL, "StringTable::destroy: StringTable does not exist.");
  98. delete _gStringTable;
  99. _gStringTable = NULL;
  100. }
  101. //--------------------------------------
  102. StringTableEntry _StringTable::insert(const char* _val, const bool caseSens)
  103. {
  104. PROFILE_SCOPE(StringTableInsert);
  105. // Added 3/29/2007 -- If this is undesirable behavior, let me know -patw
  106. const char *val = _val;
  107. if( val == NULL )
  108. val = "";
  109. //-
  110. Node **walk, *temp;
  111. U32 key = hashString(val);
  112. walk = &buckets[key % numBuckets];
  113. while((temp = *walk) != NULL) {
  114. if(caseSens && !String::compare(temp->val, val))
  115. return temp->val;
  116. else if(!caseSens && !dStricmp(temp->val, val))
  117. return temp->val;
  118. walk = &(temp->next);
  119. }
  120. char *ret = 0;
  121. if(!*walk) {
  122. dsize_t valLen = dStrlen(val) + 1;
  123. *walk = (Node *) mempool.alloc(sizeof(Node));
  124. (*walk)->next = 0;
  125. (*walk)->val = (char *) mempool.alloc(valLen);
  126. dStrcpy((*walk)->val, val, valLen);
  127. ret = (*walk)->val;
  128. itemCount ++;
  129. }
  130. if(itemCount > 2 * numBuckets) {
  131. resize(4 * numBuckets - 1);
  132. }
  133. return ret;
  134. }
  135. //--------------------------------------
  136. StringTableEntry _StringTable::insertn(const char* src, S32 len, const bool caseSens)
  137. {
  138. char val[256];
  139. AssertFatal(len < 255, "Invalid string to insertn");
  140. dStrncpy(val, src, len);
  141. val[len] = 0;
  142. return insert(val, caseSens);
  143. }
  144. //--------------------------------------
  145. StringTableEntry _StringTable::lookup(const char* val, const bool caseSens)
  146. {
  147. PROFILE_SCOPE(StringTableLookup);
  148. Node **walk, *temp;
  149. U32 key = hashString(val);
  150. walk = &buckets[key % numBuckets];
  151. while((temp = *walk) != NULL) {
  152. if(caseSens && !String::compare(temp->val, val))
  153. return temp->val;
  154. else if(!caseSens && !dStricmp(temp->val, val))
  155. return temp->val;
  156. walk = &(temp->next);
  157. }
  158. return NULL;
  159. }
  160. //--------------------------------------
  161. StringTableEntry _StringTable::lookupn(const char* val, S32 len, const bool caseSens)
  162. {
  163. PROFILE_SCOPE(StringTableLookupN);
  164. Node **walk, *temp;
  165. U32 key = hashStringn(val, len);
  166. walk = &buckets[key % numBuckets];
  167. while((temp = *walk) != NULL) {
  168. if(caseSens && !dStrncmp(temp->val, val, len) && temp->val[len] == 0)
  169. return temp->val;
  170. else if(!caseSens && !dStrnicmp(temp->val, val, len) && temp->val[len] == 0)
  171. return temp->val;
  172. walk = &(temp->next);
  173. }
  174. return NULL;
  175. }
  176. //--------------------------------------
  177. void _StringTable::resize(const U32 _newSize)
  178. {
  179. /// avoid a possible 0 division
  180. const U32 newSize = _newSize ? _newSize : 1;
  181. Node *head = NULL, *walk, *temp;
  182. U32 i;
  183. // reverse individual bucket lists
  184. // we do this because new strings are added at the end of bucket
  185. // lists so that case sens strings are always after their
  186. // corresponding case insens strings
  187. for(i = 0; i < numBuckets; i++) {
  188. walk = buckets[i];
  189. while(walk)
  190. {
  191. temp = walk->next;
  192. walk->next = head;
  193. head = walk;
  194. walk = temp;
  195. }
  196. }
  197. buckets = (Node **) dRealloc(buckets, newSize * sizeof(Node));
  198. for(i = 0; i < newSize; i++) {
  199. buckets[i] = 0;
  200. }
  201. numBuckets = newSize;
  202. walk = head;
  203. while(walk) {
  204. U32 key;
  205. temp = walk;
  206. walk = walk->next;
  207. key = hashString(temp->val);
  208. temp->next = buckets[key % newSize];
  209. buckets[key % newSize] = temp;
  210. }
  211. }