simDictionary.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #ifndef _SIMDICTIONARY_H_
  23. #define _SIMDICTIONARY_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. #ifndef _STRINGTABLE_H_
  28. #include "core/stringTable.h"
  29. #endif
  30. #ifndef _PLATFORMMUTEX_H_
  31. #include "platform/threads/mutex.h"
  32. #endif
  33. #include "torqueConfig.h"
  34. class SimObject;
  35. #ifdef USE_NEW_SIMDICTIONARY
  36. #include <string>
  37. #include <unordered_map>
  38. #ifndef _SIM_H_
  39. #include "console/sim.h"
  40. #endif
  41. #include "core/strings/stringFunctions.h"
  42. struct StringTableEntryHash
  43. {
  44. inline size_t operator()(StringTableEntry val) const
  45. {
  46. return (size_t)val;
  47. }
  48. };
  49. struct StringTableEntryEq
  50. {
  51. inline bool operator()(StringTableEntry s1, StringTableEntry s2) const
  52. {
  53. return s1 == s2;
  54. }
  55. };
  56. typedef std::unordered_map<StringTableEntry, SimObject*, StringTableEntryHash, StringTableEntryEq> StringDictDef;
  57. typedef std::unordered_map<SimObjectId, SimObject*> SimObjectIdDictDef;
  58. #endif
  59. //----------------------------------------------------------------------------
  60. /// Map of names to SimObjects
  61. ///
  62. /// Provides fast lookup for name->object and
  63. /// for fast removal of an object given object*
  64. class SimNameDictionary
  65. {
  66. #ifndef USE_NEW_SIMDICTIONARY
  67. enum
  68. {
  69. DefaultTableSize = 29
  70. };
  71. SimObject **hashTable; // hash the pointers of the names...
  72. S32 hashTableSize;
  73. S32 hashEntryCount;
  74. #else
  75. StringDictDef root;
  76. #endif
  77. void *mutex;
  78. public:
  79. void insert(SimObject* obj);
  80. void remove(SimObject* obj);
  81. SimObject* find(StringTableEntry name);
  82. SimNameDictionary();
  83. ~SimNameDictionary();
  84. };
  85. class SimManagerNameDictionary
  86. {
  87. #ifndef USE_NEW_SIMDICTIONARY
  88. enum
  89. {
  90. DefaultTableSize = 29
  91. };
  92. SimObject **hashTable; // hash the pointers of the names...
  93. S32 hashTableSize;
  94. S32 hashEntryCount;
  95. #else
  96. StringDictDef root;
  97. #endif
  98. void *mutex;
  99. public:
  100. void insert(SimObject* obj);
  101. void remove(SimObject* obj);
  102. SimObject* find(StringTableEntry name);
  103. SimManagerNameDictionary();
  104. ~SimManagerNameDictionary();
  105. };
  106. //----------------------------------------------------------------------------
  107. /// Map of ID's to SimObjects.
  108. ///
  109. /// Provides fast lookup for ID->object and
  110. /// for fast removal of an object given object*
  111. class SimIdDictionary
  112. {
  113. #ifndef USE_NEW_SIMDICTIONARY
  114. enum
  115. {
  116. DefaultTableSize = 4096,
  117. TableBitMask = 4095
  118. };
  119. SimObject *table[DefaultTableSize];
  120. #else
  121. SimObjectIdDictDef root;
  122. #endif
  123. void *mutex;
  124. public:
  125. void insert(SimObject* obj);
  126. void remove(SimObject* obj);
  127. SimObject* find(S32 id);
  128. SimIdDictionary();
  129. ~SimIdDictionary();
  130. };
  131. #endif //_SIMDICTIONARY_H_