stringTable.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 && !dStrcmp(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. *walk = (Node *) mempool.alloc(sizeof(Node));
  123. (*walk)->next = 0;
  124. (*walk)->val = (char *) mempool.alloc(dStrlen(val) + 1);
  125. dStrcpy((*walk)->val, val, dStrlen(val) + 1);
  126. ret = (*walk)->val;
  127. itemCount ++;
  128. }
  129. if(itemCount > 2 * numBuckets) {
  130. resize(4 * numBuckets - 1);
  131. }
  132. return ret;
  133. }
  134. //--------------------------------------
  135. StringTableEntry _StringTable::insertn(const char* src, S32 len, const bool caseSens)
  136. {
  137. char val[256];
  138. AssertFatal(len < 255, "Invalid string to insertn");
  139. dStrncpy(val, src, len);
  140. val[len] = 0;
  141. return insert(val, caseSens);
  142. }
  143. //--------------------------------------
  144. StringTableEntry _StringTable::lookup(const char* val, const bool caseSens)
  145. {
  146. PROFILE_SCOPE(StringTableLookup);
  147. Node **walk, *temp;
  148. U32 key = hashString(val);
  149. walk = &buckets[key % numBuckets];
  150. while((temp = *walk) != NULL) {
  151. if(caseSens && !dStrcmp(temp->val, val))
  152. return temp->val;
  153. else if(!caseSens && !dStricmp(temp->val, val))
  154. return temp->val;
  155. walk = &(temp->next);
  156. }
  157. return NULL;
  158. }
  159. //--------------------------------------
  160. StringTableEntry _StringTable::lookupn(const char* val, S32 len, const bool caseSens)
  161. {
  162. PROFILE_SCOPE(StringTableLookupN);
  163. Node **walk, *temp;
  164. U32 key = hashStringn(val, len);
  165. walk = &buckets[key % numBuckets];
  166. while((temp = *walk) != NULL) {
  167. if(caseSens && !dStrncmp(temp->val, val, len) && temp->val[len] == 0)
  168. return temp->val;
  169. else if(!caseSens && !dStrnicmp(temp->val, val, len) && temp->val[len] == 0)
  170. return temp->val;
  171. walk = &(temp->next);
  172. }
  173. return NULL;
  174. }
  175. //--------------------------------------
  176. void _StringTable::resize(const U32 _newSize)
  177. {
  178. /// avoid a possible 0 division
  179. const U32 newSize = _newSize ? _newSize : 1;
  180. Node *head = NULL, *walk, *temp;
  181. U32 i;
  182. // reverse individual bucket lists
  183. // we do this because new strings are added at the end of bucket
  184. // lists so that case sens strings are always after their
  185. // corresponding case insens strings
  186. for(i = 0; i < numBuckets; i++) {
  187. walk = buckets[i];
  188. while(walk)
  189. {
  190. temp = walk->next;
  191. walk->next = head;
  192. head = walk;
  193. walk = temp;
  194. }
  195. }
  196. buckets = (Node **) dRealloc(buckets, newSize * sizeof(Node));
  197. for(i = 0; i < newSize; i++) {
  198. buckets[i] = 0;
  199. }
  200. numBuckets = newSize;
  201. walk = head;
  202. while(walk) {
  203. U32 key;
  204. Node *temp = walk;
  205. walk = walk->next;
  206. key = hashString(temp->val);
  207. temp->next = buckets[key % newSize];
  208. buckets[key % newSize] = temp;
  209. }
  210. }