HashTable.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // Copyright (c) 2008-2014 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, Node* next) :
  36. hash_(hash),
  37. value_(value),
  38. next_(next)
  39. {
  40. }
  41. /// Hash value.
  42. unsigned hash_;
  43. /// Node value.
  44. T value_;
  45. /// Next node in bucket.
  46. Node* next_;
  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->next_;
  76. }
  77. Node* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
  78. // Insert at the top of the bucket, connect to the previous top node if exists
  79. new(newNode) Node(hash, value, ptrs_[bucket]);
  80. ptrs_[bucket] = newNode;
  81. }
  82. /// Remove by hash value. Return true if was found and removed.
  83. bool Erase(unsigned hash)
  84. {
  85. unsigned bucket = hash & (U - 1);
  86. Node* ptr = ptrs_[bucket];
  87. while (ptr)
  88. {
  89. if (ptr->hash_ == hash)
  90. {
  91. (ptr)->~Node();
  92. AllocatorFree(allocator_, ptr);
  93. return true;
  94. }
  95. else
  96. ptr = ptr->next_;
  97. }
  98. return false;
  99. }
  100. /// Remove all values.
  101. void Clear()
  102. {
  103. for (unsigned i = 0; i < U; ++i)
  104. {
  105. Node* ptr = ptrs_[i];
  106. while (ptr)
  107. {
  108. Node* next = ptr->next_;
  109. (ptr)->~Node();
  110. AllocatorFree(allocator_, ptr);
  111. ptr = next;
  112. }
  113. }
  114. }
  115. /// Find by hash value. Return pointer if was found or null if not found.
  116. T* Find(unsigned hash) const
  117. {
  118. unsigned bucket = hash & (U - 1);
  119. Node* ptr = ptrs_[bucket];
  120. while (ptr)
  121. {
  122. if (ptr->hash_ == hash)
  123. return &ptr->value_;
  124. else
  125. ptr = ptr->next_;
  126. }
  127. return 0;
  128. }
  129. /// Return pointers to all values.
  130. PODVector<T*> Values() const
  131. {
  132. PODVector<T*> ret;
  133. for (unsigned i = 0; i < U; ++i)
  134. {
  135. Node* ptr = ptrs_[i];
  136. while (ptr)
  137. {
  138. ret.Push(&ptr->value_);
  139. ptr = ptr->next_;
  140. }
  141. }
  142. return ret;
  143. }
  144. private:
  145. /// Allocator.
  146. AllocatorBlock* allocator_;
  147. /// Bucket pointers, fixed size.
  148. Node* ptrs_[U];
  149. };
  150. }