stringTable.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. _StringTable *_gStringTable = NULL;
  25. const U32 _StringTable::csm_stInitSize = 29;
  26. //---------------------------------------------------------------
  27. //
  28. // StringTable functions
  29. //
  30. //---------------------------------------------------------------
  31. namespace {
  32. bool sgInitTable = true;
  33. U8 sgHashTable[256];
  34. void initTolowerTable()
  35. {
  36. for (U32 i = 0; i < 256; i++) {
  37. U8 c = dTolower(i);
  38. sgHashTable[i] = c * c;
  39. }
  40. sgInitTable = false;
  41. }
  42. } // namespace {}
  43. U32 _StringTable::hashString(const char* str)
  44. {
  45. if (sgInitTable)
  46. initTolowerTable();
  47. if(!str) return -1;
  48. U32 ret = 0;
  49. char c;
  50. while((c = *str++) != 0) {
  51. ret <<= 1;
  52. ret ^= sgHashTable[static_cast<U8>(c)];
  53. }
  54. return ret;
  55. }
  56. U32 _StringTable::hashStringn(const char* str, S32 len)
  57. {
  58. if (sgInitTable)
  59. initTolowerTable();
  60. U32 ret = 0;
  61. char c;
  62. while((c = *str++) != 0 && len--) {
  63. ret <<= 1;
  64. ret ^= sgHashTable[static_cast<U8>(c)];
  65. }
  66. return ret;
  67. }
  68. //--------------------------------------
  69. _StringTable::_StringTable()
  70. {
  71. buckets = (Node **) dMalloc(csm_stInitSize * sizeof(Node *));
  72. for(U32 i = 0; i < csm_stInitSize; i++) {
  73. buckets[i] = 0;
  74. }
  75. numBuckets = csm_stInitSize;
  76. itemCount = 0;
  77. }
  78. //--------------------------------------
  79. _StringTable::~_StringTable()
  80. {
  81. dFree(buckets);
  82. }
  83. //--------------------------------------
  84. void _StringTable::create()
  85. {
  86. //AssertFatal(_gStringTable == NULL, "StringTable::create: StringTable already exists.");
  87. if(!_gStringTable)
  88. {
  89. _gStringTable = new _StringTable;
  90. _gStringTable->_EmptyString = _gStringTable->insert("");
  91. }
  92. }
  93. //--------------------------------------
  94. void _StringTable::destroy()
  95. {
  96. AssertFatal(StringTable != NULL, "StringTable::destroy: StringTable does not exist.");
  97. delete _gStringTable;
  98. _gStringTable = NULL;
  99. }
  100. //--------------------------------------
  101. StringTableEntry _StringTable::insert(const char* _val, const bool caseSens)
  102. {
  103. // Added 3/29/2007 -- If this is undesirable behavior, let me know -patw
  104. const char *val = _val;
  105. if( val == NULL )
  106. val = "";
  107. //-
  108. Node **walk, *temp;
  109. U32 key = hashString(val);
  110. walk = &buckets[key % numBuckets];
  111. while((temp = *walk) != NULL) {
  112. if(caseSens && !dStrcmp(temp->val, val))
  113. return temp->val;
  114. else if(!caseSens && !dStricmp(temp->val, val))
  115. return temp->val;
  116. walk = &(temp->next);
  117. }
  118. char *ret = 0;
  119. if(!*walk) {
  120. *walk = (Node *) mempool.alloc(sizeof(Node));
  121. (*walk)->next = 0;
  122. (*walk)->val = (char *) mempool.alloc(dStrlen(val) + 1);
  123. dStrcpy((*walk)->val, val);
  124. ret = (*walk)->val;
  125. itemCount ++;
  126. }
  127. if(itemCount > 2 * numBuckets) {
  128. resize(4 * numBuckets - 1);
  129. }
  130. return ret;
  131. }
  132. //--------------------------------------
  133. StringTableEntry _StringTable::insertn(const char* src, S32 len, const bool caseSens)
  134. {
  135. char val[256];
  136. AssertFatal(len < 255, "Invalid string to insertn");
  137. dStrncpy(val, src, len);
  138. val[len] = 0;
  139. return insert(val, caseSens);
  140. }
  141. //--------------------------------------
  142. StringTableEntry _StringTable::lookup(const char* val, const bool caseSens)
  143. {
  144. Node **walk, *temp;
  145. U32 key = hashString(val);
  146. walk = &buckets[key % numBuckets];
  147. while((temp = *walk) != NULL) {
  148. if(caseSens && !dStrcmp(temp->val, val))
  149. return temp->val;
  150. else if(!caseSens && !dStricmp(temp->val, val))
  151. return temp->val;
  152. walk = &(temp->next);
  153. }
  154. return NULL;
  155. }
  156. //--------------------------------------
  157. StringTableEntry _StringTable::lookupn(const char* val, S32 len, const bool caseSens)
  158. {
  159. Node **walk, *temp;
  160. U32 key = hashStringn(val, len);
  161. walk = &buckets[key % numBuckets];
  162. while((temp = *walk) != NULL) {
  163. if(caseSens && !dStrncmp(temp->val, val, len) && temp->val[len] == 0)
  164. return temp->val;
  165. else if(!caseSens && !dStrnicmp(temp->val, val, len) && temp->val[len] == 0)
  166. return temp->val;
  167. walk = &(temp->next);
  168. }
  169. return NULL;
  170. }
  171. //--------------------------------------
  172. void _StringTable::resize(const U32 _newSize)
  173. {
  174. /// avoid a possible 0 division
  175. const U32 newSize = _newSize ? _newSize : 1;
  176. Node *head = NULL, *walk, *temp;
  177. U32 i;
  178. // reverse individual bucket lists
  179. // we do this because new strings are added at the end of bucket
  180. // lists so that case sens strings are always after their
  181. // corresponding case insens strings
  182. for(i = 0; i < numBuckets; i++) {
  183. walk = buckets[i];
  184. while(walk)
  185. {
  186. temp = walk->next;
  187. walk->next = head;
  188. head = walk;
  189. walk = temp;
  190. }
  191. }
  192. buckets = (Node **) dRealloc(buckets, newSize * sizeof(Node));
  193. for(i = 0; i < newSize; i++) {
  194. buckets[i] = 0;
  195. }
  196. numBuckets = newSize;
  197. walk = head;
  198. while(walk) {
  199. U32 key;
  200. Node *temp = walk;
  201. walk = walk->next;
  202. key = hashString(temp->val);
  203. temp->next = buckets[key % newSize];
  204. buckets[key % newSize] = temp;
  205. }
  206. }