stringTable.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 *StringTable = 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. AssertFatal(StringTable == NULL, "StringTable::create: StringTable already exists.");
  89. StringTable = new _StringTable;
  90. }
  91. //--------------------------------------
  92. void _StringTable::destroy()
  93. {
  94. AssertFatal(StringTable != NULL, "StringTable::destroy: StringTable does not exist.");
  95. delete StringTable;
  96. StringTable = NULL;
  97. }
  98. //--------------------------------------
  99. StringTableEntry _StringTable::insert(const char* val, const bool caseSens)
  100. {
  101. if ( val == NULL )
  102. return StringTable->EmptyString;
  103. MutexHandle mutex;
  104. mutex.lock(&mMutex, true);
  105. Node **walk, *temp;
  106. U32 key = hashString(val);
  107. walk = &buckets[key % numBuckets];
  108. while((temp = *walk) != NULL) {
  109. if(caseSens && !dStrcmp(temp->val, val))
  110. return temp->val;
  111. else if(!caseSens && !dStricmp(temp->val, val))
  112. return temp->val;
  113. walk = &(temp->next);
  114. }
  115. char *ret = 0;
  116. if(!*walk) {
  117. *walk = (Node *) mempool.alloc(sizeof(Node));
  118. (*walk)->next = 0;
  119. (*walk)->val = (char *) mempool.alloc(dStrlen(val) + 1);
  120. dStrcpy((*walk)->val, val);
  121. ret = (*walk)->val;
  122. itemCount ++;
  123. }
  124. if(itemCount > 2 * numBuckets) {
  125. resize(4 * numBuckets - 1);
  126. }
  127. return ret;
  128. }
  129. //--------------------------------------
  130. StringTableEntry _StringTable::insertn(const char* src, S32 len, const bool caseSens)
  131. {
  132. if ( src == NULL )
  133. return StringTable->EmptyString;
  134. MutexHandle mutex;
  135. mutex.lock(&mMutex, true);
  136. char val[1024];
  137. AssertFatal(len < sizeof(val), "Invalid string to insertn");
  138. dStrncpy(val, src, len);
  139. val[len] = 0;
  140. return insert(val, caseSens);
  141. }
  142. //--------------------------------------
  143. StringTableEntry _StringTable::lookup(const char* val, const bool caseSens)
  144. {
  145. if ( val == NULL )
  146. return StringTable->EmptyString;
  147. MutexHandle mutex;
  148. mutex.lock(&mMutex, true);
  149. Node **walk, *temp;
  150. U32 key = hashString(val);
  151. walk = &buckets[key % numBuckets];
  152. while((temp = *walk) != NULL) {
  153. if(caseSens && !dStrcmp(temp->val, val))
  154. return temp->val;
  155. else if(!caseSens && !dStricmp(temp->val, val))
  156. return temp->val;
  157. walk = &(temp->next);
  158. }
  159. return NULL;
  160. }
  161. //--------------------------------------
  162. StringTableEntry _StringTable::lookupn(const char* val, S32 len, const bool caseSens)
  163. {
  164. if ( val == NULL )
  165. return StringTable->EmptyString;
  166. Node **walk, *temp;
  167. U32 key = hashStringn(val, len);
  168. walk = &buckets[key % numBuckets];
  169. while((temp = *walk) != NULL) {
  170. if(caseSens && !dStrncmp(temp->val, val, len) && temp->val[len] == 0)
  171. return temp->val;
  172. else if(!caseSens && !dStrnicmp(temp->val, val, len) && temp->val[len] == 0)
  173. return temp->val;
  174. walk = &(temp->next);
  175. }
  176. return NULL;
  177. }
  178. //--------------------------------------
  179. void _StringTable::resize(const U32 newSize)
  180. {
  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. Node *temp = walk;
  206. walk = walk->next;
  207. key = hashString(temp->val);
  208. temp->next = buckets[key % newSize];
  209. buckets[key % newSize] = temp;
  210. }
  211. }