PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/llsimplehash.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 155 lines | 119 code | 9 blank | 27 comment | 10 complexity | 303a298c59745b98a766257b720adc83 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llsimplehash.h
  3. *
  4. * $LicenseInfo:firstyear=2003&license=viewerlgpl$
  5. * Second Life Viewer Source Code
  6. * Copyright (C) 2010, Linden Research, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License only.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  23. * $/LicenseInfo$
  24. */
  25. #ifndef LL_LLSIMPLEHASH_H
  26. #define LL_LLSIMPLEHASH_H
  27. #include "llstl.h"
  28. template <typename HASH_KEY_TYPE>
  29. class LLSimpleHashEntry
  30. {
  31. protected:
  32. HASH_KEY_TYPE mHashKey;
  33. LLSimpleHashEntry<HASH_KEY_TYPE>* mNextEntry;
  34. public:
  35. LLSimpleHashEntry(HASH_KEY_TYPE key) :
  36. mHashKey(key),
  37. mNextEntry(0)
  38. {
  39. }
  40. virtual ~LLSimpleHashEntry()
  41. {
  42. }
  43. HASH_KEY_TYPE getHashKey() const
  44. {
  45. return mHashKey;
  46. }
  47. LLSimpleHashEntry<HASH_KEY_TYPE>* getNextEntry() const
  48. {
  49. return mNextEntry;
  50. }
  51. void setNextEntry(LLSimpleHashEntry<HASH_KEY_TYPE>* next)
  52. {
  53. mNextEntry = next;
  54. }
  55. };
  56. template <typename HASH_KEY_TYPE, int TABLE_SIZE>
  57. class LL_COMMON_API LLSimpleHash
  58. {
  59. public:
  60. LLSimpleHash()
  61. {
  62. llassert(TABLE_SIZE);
  63. llassert((TABLE_SIZE ^ (TABLE_SIZE-1)) == (TABLE_SIZE | (TABLE_SIZE-1))); // power of 2
  64. memset(mEntryTable, 0, sizeof(mEntryTable));
  65. }
  66. virtual ~LLSimpleHash()
  67. {
  68. }
  69. virtual int getIndex(HASH_KEY_TYPE key)
  70. {
  71. return key & (TABLE_SIZE-1);
  72. }
  73. bool insert(LLSimpleHashEntry<HASH_KEY_TYPE>* entry)
  74. {
  75. llassert(entry->getNextEntry() == 0);
  76. int index = getIndex(entry->getHashKey());
  77. entry->setNextEntry(mEntryTable[index]);
  78. mEntryTable[index] = entry;
  79. return true;
  80. }
  81. LLSimpleHashEntry<HASH_KEY_TYPE>* find(HASH_KEY_TYPE key)
  82. {
  83. int index = getIndex(key);
  84. LLSimpleHashEntry<HASH_KEY_TYPE>* res = mEntryTable[index];
  85. while(res && (res->getHashKey() != key))
  86. {
  87. res = res->getNextEntry();
  88. }
  89. return res;
  90. }
  91. bool erase(LLSimpleHashEntry<HASH_KEY_TYPE>* entry)
  92. {
  93. return erase(entry->getHashKey());
  94. }
  95. bool erase(HASH_KEY_TYPE key)
  96. {
  97. int index = getIndex(key);
  98. LLSimpleHashEntry<HASH_KEY_TYPE>* prev = 0;
  99. LLSimpleHashEntry<HASH_KEY_TYPE>* res = mEntryTable[index];
  100. while(res && (res->getHashKey() != key))
  101. {
  102. prev = res;
  103. res = res->getNextEntry();
  104. }
  105. if (res)
  106. {
  107. LLSimpleHashEntry<HASH_KEY_TYPE>* next = res->getNextEntry();
  108. if (prev)
  109. {
  110. prev->setNextEntry(next);
  111. }
  112. else
  113. {
  114. mEntryTable[index] = next;
  115. }
  116. return true;
  117. }
  118. else
  119. {
  120. return false;
  121. }
  122. }
  123. // Removes and returns an arbitrary ("first") element from the table
  124. // Used for deleting the entire table.
  125. LLSimpleHashEntry<HASH_KEY_TYPE>* pop_element()
  126. {
  127. for (int i=0; i<TABLE_SIZE; i++)
  128. {
  129. LLSimpleHashEntry<HASH_KEY_TYPE>* entry = mEntryTable[i];
  130. if (entry)
  131. {
  132. mEntryTable[i] = entry->getNextEntry();
  133. return entry;
  134. }
  135. }
  136. return 0;
  137. }
  138. // debugging
  139. LLSimpleHashEntry<HASH_KEY_TYPE>* get_element_at_index(S32 index) const
  140. {
  141. return mEntryTable[index];
  142. }
  143. private:
  144. LLSimpleHashEntry<HASH_KEY_TYPE>* mEntryTable[TABLE_SIZE];
  145. };
  146. #endif // LL_LLSIMPLEHASH_H