stringTable.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "platform/platform.h"
  23. #include "stringTable.h"
  24. _StringTable *_gStringTable = NULL;
  25. const U32 _StringTable::csm_stInitSize = 29;
  26. StringTableEntry _StringTable::EmptyString;
  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. U32 ret = 0;
  49. char c;
  50. while((c = *str++) != 0) {
  51. ret <<= 1;
  52. ret ^= sgHashTable[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[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. // Insert empty string.
  78. EmptyString = insert("");
  79. }
  80. //--------------------------------------
  81. _StringTable::~_StringTable()
  82. {
  83. dFree(buckets);
  84. }
  85. //--------------------------------------
  86. void _StringTable::create()
  87. {
  88. if(!_gStringTable)
  89. {
  90. _gStringTable = new _StringTable;
  91. }
  92. }
  93. //--------------------------------------
  94. void _StringTable::destroy()
  95. {
  96. AssertFatal(StringTable != NULL, "StringTable::destroy: StringTable does not exist.");
  97. delete StringTable;
  98. _gStringTable = NULL;
  99. }
  100. //--------------------------------------
  101. StringTableEntry _StringTable::insert(const char* val, const bool caseSens)
  102. {
  103. if ( val == NULL )
  104. return StringTable->EmptyString;
  105. MutexHandle mutex;
  106. mutex.lock(&mMutex, true);
  107. Node **walk, *temp;
  108. U32 key = hashString(val);
  109. walk = &buckets[key % numBuckets];
  110. while((temp = *walk) != NULL) {
  111. if(caseSens && !dStrcmp(temp->val, val))
  112. return temp->val;
  113. else if(!caseSens && !dStricmp(temp->val, val))
  114. return temp->val;
  115. walk = &(temp->next);
  116. }
  117. char *ret = 0;
  118. if(!*walk) {
  119. *walk = (Node *) mempool.alloc(sizeof(Node));
  120. (*walk)->next = 0;
  121. (*walk)->val = (char *) mempool.alloc(dStrlen(val) + 1);
  122. dStrcpy((*walk)->val, val);
  123. ret = (*walk)->val;
  124. itemCount ++;
  125. }
  126. if(itemCount > 2 * numBuckets) {
  127. resize(4 * numBuckets - 1);
  128. }
  129. return ret;
  130. }
  131. //--------------------------------------
  132. StringTableEntry _StringTable::insertn(const char* src, S32 len, const bool caseSens)
  133. {
  134. if ( src == NULL )
  135. return StringTable->EmptyString;
  136. MutexHandle mutex;
  137. mutex.lock(&mMutex, true);
  138. char val[1024];
  139. AssertFatal(len < sizeof(val), "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. if ( val == NULL )
  148. return StringTable->EmptyString;
  149. MutexHandle mutex;
  150. mutex.lock(&mMutex, true);
  151. Node **walk, *temp;
  152. U32 key = hashString(val);
  153. walk = &buckets[key % numBuckets];
  154. while((temp = *walk) != NULL) {
  155. if(caseSens && !dStrcmp(temp->val, val))
  156. return temp->val;
  157. else if(!caseSens && !dStricmp(temp->val, val))
  158. return temp->val;
  159. walk = &(temp->next);
  160. }
  161. return NULL;
  162. }
  163. //--------------------------------------
  164. StringTableEntry _StringTable::lookupn(const char* val, S32 len, const bool caseSens)
  165. {
  166. if ( val == NULL )
  167. return StringTable->EmptyString;
  168. Node **walk, *temp;
  169. U32 key = hashStringn(val, len);
  170. walk = &buckets[key % numBuckets];
  171. while((temp = *walk) != NULL) {
  172. if(caseSens && !dStrncmp(temp->val, val, len) && temp->val[len] == 0)
  173. return temp->val;
  174. else if(!caseSens && !dStrnicmp(temp->val, val, len) && temp->val[len] == 0)
  175. return temp->val;
  176. walk = &(temp->next);
  177. }
  178. return NULL;
  179. }
  180. //--------------------------------------
  181. void _StringTable::resize(const U32 newSize)
  182. {
  183. Node *head = NULL, *walk, *temp;
  184. U32 i;
  185. // reverse individual bucket lists
  186. // we do this because new strings are added at the end of bucket
  187. // lists so that case sens strings are always after their
  188. // corresponding case insens strings
  189. for(i = 0; i < numBuckets; i++) {
  190. walk = buckets[i];
  191. while(walk)
  192. {
  193. temp = walk->next;
  194. walk->next = head;
  195. head = walk;
  196. walk = temp;
  197. }
  198. }
  199. buckets = (Node **) dRealloc(buckets, newSize * sizeof(Node));
  200. for(i = 0; i < newSize; i++) {
  201. buckets[i] = 0;
  202. }
  203. numBuckets = newSize;
  204. walk = head;
  205. while(walk) {
  206. U32 key;
  207. Node *temp = walk;
  208. walk = walk->next;
  209. key = hashString(temp->val);
  210. temp->next = buckets[key % newSize];
  211. buckets[key % newSize] = temp;
  212. }
  213. }