simDictionary.h 3.8 KB

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