simDictionary.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 "sim/simDictionary.h"
  23. #include "sim/simBase.h"
  24. //----------------------------------------------------------------------------
  25. //----------------------------------------------------------------------------
  26. extern U32 HashPointer(StringTableEntry e);
  27. SimNameDictionary::SimNameDictionary()
  28. {
  29. hashTable = NULL;
  30. mutex = Mutex::createMutex();
  31. }
  32. SimNameDictionary::~SimNameDictionary()
  33. {
  34. delete[] hashTable;
  35. Mutex::destroyMutex(mutex);
  36. }
  37. void SimNameDictionary::insert(SimObject* obj)
  38. {
  39. if(!obj->objectName)
  40. return;
  41. Mutex::lockMutex(mutex);
  42. if(!hashTable)
  43. {
  44. hashTable = new SimObject *[DefaultTableSize];
  45. hashTableSize = DefaultTableSize;
  46. hashEntryCount = 0;
  47. S32 i;
  48. for(i = 0; i < hashTableSize; i++)
  49. hashTable[i] = NULL;
  50. }
  51. S32 idx = HashPointer(obj->objectName) % hashTableSize;
  52. obj->nextNameObject = hashTable[idx];
  53. hashTable[idx] = obj;
  54. hashEntryCount++;
  55. if(hashEntryCount > hashTableSize)
  56. {
  57. // resize the hash table
  58. S32 i;
  59. SimObject *head = NULL, *walk, *temp;
  60. for(i = 0; i < hashTableSize; i++) {
  61. walk = hashTable[i];
  62. while(walk)
  63. {
  64. temp = walk->nextNameObject;
  65. walk->nextNameObject = head;
  66. head = walk;
  67. walk = temp;
  68. }
  69. }
  70. delete[] hashTable;
  71. hashTableSize = hashTableSize * 2 + 1;
  72. hashTable = new SimObject *[hashTableSize];
  73. for(i = 0; i < hashTableSize;i++)
  74. hashTable[i] = NULL;
  75. while(head)
  76. {
  77. temp = head->nextNameObject;
  78. idx = HashPointer(head->objectName) % hashTableSize;
  79. head->nextNameObject = hashTable[idx];
  80. hashTable[idx] = head;
  81. head = temp;
  82. }
  83. }
  84. Mutex::unlockMutex(mutex);
  85. }
  86. SimObject* SimNameDictionary::find(StringTableEntry name)
  87. {
  88. // NULL is a valid lookup - it will always return NULL
  89. if(!hashTable)
  90. return NULL;
  91. Mutex::lockMutex(mutex);
  92. S32 idx = HashPointer(name) % hashTableSize;
  93. SimObject *walk = hashTable[idx];
  94. while(walk)
  95. {
  96. if(walk->objectName == name)
  97. {
  98. Mutex::unlockMutex(mutex);
  99. return walk;
  100. }
  101. walk = walk->nextNameObject;
  102. }
  103. Mutex::unlockMutex(mutex);
  104. return NULL;
  105. }
  106. void SimNameDictionary::remove(SimObject* obj)
  107. {
  108. if(!obj->objectName)
  109. return;
  110. Mutex::lockMutex(mutex);
  111. SimObject **walk = &hashTable[HashPointer(obj->objectName) % hashTableSize];
  112. while(*walk)
  113. {
  114. if(*walk == obj)
  115. {
  116. *walk = obj->nextNameObject;
  117. obj->nextNameObject = (SimObject*)-1;
  118. hashEntryCount--;
  119. Mutex::unlockMutex(mutex);
  120. return;
  121. }
  122. walk = &((*walk)->nextNameObject);
  123. }
  124. Mutex::unlockMutex(mutex);
  125. }
  126. //----------------------------------------------------------------------------
  127. SimManagerNameDictionary::SimManagerNameDictionary()
  128. {
  129. hashTable = new SimObject *[DefaultTableSize];
  130. hashTableSize = DefaultTableSize;
  131. hashEntryCount = 0;
  132. S32 i;
  133. for(i = 0; i < hashTableSize; i++)
  134. hashTable[i] = NULL;
  135. mutex = Mutex::createMutex();
  136. }
  137. SimManagerNameDictionary::~SimManagerNameDictionary()
  138. {
  139. delete[] hashTable;
  140. Mutex::destroyMutex(mutex);
  141. }
  142. void SimManagerNameDictionary::insert(SimObject* obj)
  143. {
  144. if(!obj->objectName)
  145. return;
  146. Mutex::lockMutex(mutex);
  147. S32 idx = HashPointer(obj->objectName) % hashTableSize;
  148. obj->nextManagerNameObject = hashTable[idx];
  149. hashTable[idx] = obj;
  150. hashEntryCount++;
  151. if(hashEntryCount > hashTableSize)
  152. {
  153. // resize the hash table
  154. S32 i;
  155. SimObject *head = NULL, *walk, *temp;
  156. for(i = 0; i < hashTableSize; i++) {
  157. walk = hashTable[i];
  158. while(walk)
  159. {
  160. temp = walk->nextManagerNameObject;
  161. walk->nextManagerNameObject = head;
  162. head = walk;
  163. walk = temp;
  164. }
  165. }
  166. delete[] hashTable;
  167. hashTableSize = hashTableSize * 2 + 1;
  168. hashTable = new SimObject *[hashTableSize];
  169. for(i = 0; i < hashTableSize;i++)
  170. hashTable[i] = NULL;
  171. while(head)
  172. {
  173. temp = head->nextManagerNameObject;
  174. idx = HashPointer(head->objectName) % hashTableSize;
  175. head->nextManagerNameObject = hashTable[idx];
  176. hashTable[idx] = head;
  177. head = temp;
  178. }
  179. }
  180. Mutex::unlockMutex(mutex);
  181. }
  182. SimObject* SimManagerNameDictionary::find(StringTableEntry name)
  183. {
  184. // NULL is a valid lookup - it will always return NULL
  185. Mutex::lockMutex(mutex);
  186. S32 idx = HashPointer(name) % hashTableSize;
  187. SimObject *walk = hashTable[idx];
  188. while(walk)
  189. {
  190. if(walk->objectName == name)
  191. {
  192. Mutex::unlockMutex(mutex);
  193. return walk;
  194. }
  195. walk = walk->nextManagerNameObject;
  196. }
  197. Mutex::unlockMutex(mutex);
  198. return NULL;
  199. }
  200. void SimManagerNameDictionary::remove(SimObject* obj)
  201. {
  202. if(!obj->objectName)
  203. return;
  204. Mutex::lockMutex(mutex);
  205. SimObject **walk = &hashTable[HashPointer(obj->objectName) % hashTableSize];
  206. while(*walk)
  207. {
  208. if(*walk == obj)
  209. {
  210. *walk = obj->nextManagerNameObject;
  211. obj->nextManagerNameObject = (SimObject*)-1;
  212. hashEntryCount--;
  213. Mutex::unlockMutex(mutex);
  214. return;
  215. }
  216. walk = &((*walk)->nextManagerNameObject);
  217. }
  218. Mutex::unlockMutex(mutex);
  219. }
  220. //---------------------------------------------------------------------------
  221. //---------------------------------------------------------------------------
  222. SimIdDictionary::SimIdDictionary()
  223. {
  224. for(S32 i = 0; i < DefaultTableSize; i++)
  225. table[i] = NULL;
  226. mutex = Mutex::createMutex();
  227. }
  228. SimIdDictionary::~SimIdDictionary()
  229. {
  230. Mutex::destroyMutex(mutex);
  231. }
  232. void SimIdDictionary::insert(SimObject* obj)
  233. {
  234. Mutex::lockMutex(mutex);
  235. S32 idx = obj->getId() & TableBitMask;
  236. obj->nextIdObject = table[idx];
  237. AssertFatal( obj->nextIdObject != obj, "SimIdDictionary::insert - Creating Infinite Loop linking to self!" );
  238. table[idx] = obj;
  239. Mutex::unlockMutex(mutex);
  240. }
  241. SimObject* SimIdDictionary::find(S32 id)
  242. {
  243. Mutex::lockMutex(mutex);
  244. S32 idx = id & TableBitMask;
  245. SimObject *walk = table[idx];
  246. while(walk)
  247. {
  248. if(walk->getId() == U32(id))
  249. {
  250. Mutex::unlockMutex(mutex);
  251. return walk;
  252. }
  253. walk = walk->nextIdObject;
  254. }
  255. Mutex::unlockMutex(mutex);
  256. return NULL;
  257. }
  258. void SimIdDictionary::remove(SimObject* obj)
  259. {
  260. Mutex::lockMutex(mutex);
  261. SimObject **walk = &table[obj->getId() & TableBitMask];
  262. while(*walk && *walk != obj)
  263. walk = &((*walk)->nextIdObject);
  264. if(*walk)
  265. *walk = obj->nextIdObject;
  266. Mutex::unlockMutex(mutex);
  267. }
  268. //---------------------------------------------------------------------------
  269. //---------------------------------------------------------------------------