HashTable.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Allocator.h"
  24. #include "Vector.h"
  25. namespace Urho3D
  26. {
  27. /// Hash table with fixed bucket count. Does not support iteration. Should only be used when performance is critical, as HashMap is much more user-friendly.
  28. template <class T, unsigned U> class HashTable
  29. {
  30. public:
  31. /// Hash table node.
  32. struct Node
  33. {
  34. /// Construct.
  35. Node(unsigned hash, const T& value) :
  36. hash_(hash),
  37. value_(value),
  38. down_(0)
  39. {
  40. }
  41. /// Hash value.
  42. unsigned hash_;
  43. /// Node value.
  44. T value_;
  45. /// Next node in bucket.
  46. Node* down_;
  47. };
  48. /// Construct empty.
  49. HashTable() :
  50. allocator_(0)
  51. {
  52. for (unsigned i = 0; i < U; ++i)
  53. ptrs_[i] = 0;
  54. }
  55. /// Destruct.
  56. ~HashTable()
  57. {
  58. Clear();
  59. AllocatorUninitialize(allocator_);
  60. }
  61. /// Insert by hash value. If value with same hash already exists, it is replaced.
  62. void Insert(unsigned hash, const T& value)
  63. {
  64. unsigned bucket = hash & (U - 1);
  65. if (!allocator_)
  66. allocator_ = AllocatorInitialize(sizeof(Node));
  67. Node* ptr = ptrs_[bucket];
  68. while (ptr)
  69. {
  70. if (ptr->hash_ == hash)
  71. {
  72. ptr->value_ = value;
  73. return;
  74. }
  75. ptr = ptr->down_;
  76. }
  77. Node* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
  78. new(newNode) Node(hash, value);
  79. ptr = ptrs_[bucket];
  80. if (ptr)
  81. {
  82. ptrs_[bucket] = newNode;
  83. newNode->down_ = ptr;
  84. }
  85. else
  86. ptrs_[bucket] = newNode;
  87. return;
  88. }
  89. /// Remove by hash value. Return true if was found and removed.
  90. bool Erase(unsigned hash)
  91. {
  92. unsigned bucket = hash & (U - 1);
  93. Node* ptr = ptrs_[bucket];
  94. while (ptr)
  95. {
  96. if (ptr->hash_ == hash)
  97. {
  98. (ptr)->~Node();
  99. AllocatorFree(allocator_, ptr);
  100. return true;
  101. }
  102. else
  103. ptr = ptr->down_;
  104. }
  105. return false;
  106. }
  107. /// Remove all values.
  108. void Clear()
  109. {
  110. for (unsigned i = 0; i < U; ++i)
  111. {
  112. Node* ptr = ptrs_[i];
  113. while (ptr)
  114. {
  115. Node* next = ptr->down_;
  116. (ptr)->~Node();
  117. AllocatorFree(allocator_, ptr);
  118. ptr = next;
  119. }
  120. }
  121. }
  122. /// Find by hash value. Return pointer if was found or null if not found.
  123. T* Find(unsigned hash) const
  124. {
  125. unsigned bucket = hash & (U - 1);
  126. Node* ptr = ptrs_[bucket];
  127. while (ptr)
  128. {
  129. if (ptr->hash_ == hash)
  130. return &ptr->value_;
  131. else
  132. ptr = ptr->down_;
  133. }
  134. return 0;
  135. }
  136. /// Return pointers to all values.
  137. PODVector<T*> Values() const
  138. {
  139. PODVector<T*> ret;
  140. for (unsigned i = 0; i < U; ++i)
  141. {
  142. Node* ptr = ptrs_[i];
  143. while (ptr)
  144. {
  145. ret.Push(&ptr->value_);
  146. ptr = ptr->down_;
  147. }
  148. }
  149. return ret;
  150. }
  151. private:
  152. /// Allocator.
  153. AllocatorBlock* allocator_;
  154. /// Bucket pointers, fixed size.
  155. Node* ptrs_[U];
  156. };
  157. }