tSparseArray.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 _TSPARSEARRAY_H_
  23. #define _TSPARSEARRAY_H_
  24. //Includes
  25. #ifndef _PLATFORM_H_
  26. #include "platform/platform.h"
  27. #endif
  28. #ifndef _PLATFORMASSERT_H_
  29. #include "platform/platformAssert.h"
  30. #endif
  31. template <class T>
  32. class SparseArray
  33. {
  34. protected:
  35. struct Node {
  36. T* pObject;
  37. U32 key;
  38. Node* next;
  39. };
  40. protected:
  41. U32 mModulus;
  42. Node* mSentryTables;
  43. public:
  44. SparseArray(const U32 modulusSize = 64);
  45. ~SparseArray();
  46. void insert(T* pObject, U32 key);
  47. T* remove(U32 key);
  48. T* retreive(U32 key);
  49. void clearTables(); // Note: _deletes_ the objects!
  50. };
  51. template <class T>
  52. inline SparseArray<T>::SparseArray(const U32 modulusSize)
  53. {
  54. AssertFatal(modulusSize > 0, "Error, modulus must be > 0");
  55. mModulus = modulusSize;
  56. mSentryTables = new Node[mModulus];
  57. for (U32 i = 0; i < mModulus; i++)
  58. mSentryTables[i].next = NULL;
  59. }
  60. template <class T>
  61. inline SparseArray<T>::~SparseArray()
  62. {
  63. clearTables();
  64. }
  65. template <class T>
  66. inline void SparseArray<T>::clearTables()
  67. {
  68. for (U32 i = 0; i < mModulus; i++) {
  69. Node* pProbe = mSentryTables[i].next;
  70. while (pProbe != NULL) {
  71. Node* pNext = pProbe->next;
  72. delete pProbe->pObject;
  73. delete pProbe;
  74. pProbe = pNext;
  75. }
  76. }
  77. delete [] mSentryTables;
  78. mSentryTables = NULL;
  79. mModulus = 0;
  80. }
  81. template <class T>
  82. inline void SparseArray<T>::insert(T* pObject, U32 key)
  83. {
  84. U32 insert = key % mModulus;
  85. Node* pNew = new Node;
  86. pNew->pObject = pObject;
  87. pNew->key = key;
  88. pNew->next = mSentryTables[insert].next;
  89. mSentryTables[insert].next = pNew;
  90. #ifdef TORQUE_DEBUG
  91. Node* probe = pNew->next;
  92. while (probe != NULL) {
  93. AssertFatal(probe->key != key, "error, duplicate keys in sparse array!");
  94. probe = probe->next;
  95. }
  96. #endif
  97. }
  98. template <class T>
  99. inline T* SparseArray<T>::remove(U32 key)
  100. {
  101. U32 sentryID = key % mModulus;
  102. Node* probe = &mSentryTables[sentryID];
  103. while (probe->next != NULL) {
  104. if (probe->next->key == key) {
  105. Node* nextProbe = probe->next;
  106. T* pReturn = nextProbe->pObject;
  107. probe->next = nextProbe->next;
  108. delete nextProbe;
  109. return pReturn;
  110. }
  111. probe = probe->next;
  112. }
  113. // [tom, 8/19/2006] This assert is also utterly, utterly useless
  114. // AssertFatal(false, "Key didn't exist in the array!");
  115. return NULL;
  116. }
  117. template <class T>
  118. inline T* SparseArray<T>::retreive(U32 key)
  119. {
  120. U32 retrieve = key % mModulus;
  121. Node* probe = &mSentryTables[retrieve];
  122. while (probe->next != NULL) {
  123. if (probe->next->key == key) {
  124. return probe->next->pObject;
  125. }
  126. probe = probe->next;
  127. }
  128. // [tom, 11/16/2005] This assert is utterly, utterly useless
  129. // AssertFatal(false, "Key didn't exist in the array!");
  130. return NULL;
  131. }
  132. #endif //_TSPARSEARRAY_H_