HashBase.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Allocator.h"
  25. #include "Swap.h"
  26. /// Pointer hash function
  27. template <class T> unsigned MakeHash(T* value)
  28. {
  29. return (unsigned)value;
  30. }
  31. /// Const pointer hash function
  32. template <class T> unsigned MakeHash(const T* value)
  33. {
  34. return (unsigned)value;
  35. }
  36. /// Generic hash function
  37. template <class T> unsigned MakeHash(const T& value)
  38. {
  39. return value.ToHash();
  40. }
  41. /// Int hash function
  42. template<> inline unsigned MakeHash(const int& value)
  43. {
  44. return value;
  45. }
  46. /// Unsigned hash function
  47. template<> inline unsigned MakeHash(const unsigned& value)
  48. {
  49. return value;
  50. }
  51. /// Short hash function
  52. template<> inline unsigned MakeHash(const short& value)
  53. {
  54. return value;
  55. }
  56. /// Unsigned short hash function
  57. template<> inline unsigned MakeHash(const unsigned short& value)
  58. {
  59. return value;
  60. }
  61. /// Char hash function
  62. template<> inline unsigned MakeHash(const char& value)
  63. {
  64. return value;
  65. }
  66. /// Unsigned char hash function
  67. template<> inline unsigned MakeHash(const unsigned char& value)
  68. {
  69. return value;
  70. }
  71. /// Hash node base
  72. struct HashNodeBase
  73. {
  74. /// Construct
  75. HashNodeBase() :
  76. prev_(0),
  77. next_(0),
  78. down_(0)
  79. {
  80. }
  81. /// Previous node
  82. HashNodeBase* prev_;
  83. /// Next node
  84. HashNodeBase* next_;
  85. /// Next node in the bucket
  86. HashNodeBase* down_;
  87. };
  88. /// Hash iterator base class
  89. class HashIteratorBase
  90. {
  91. public:
  92. /// Construct
  93. explicit HashIteratorBase(HashNodeBase* ptr) :
  94. ptr_(ptr)
  95. {
  96. }
  97. /// Test for equality with another iterator
  98. bool operator == (const HashIteratorBase& rhs) const { return ptr_ == rhs.ptr_; }
  99. /// Test for inequality with another iterator
  100. bool operator != (const HashIteratorBase& rhs) const { return ptr_ != rhs.ptr_; }
  101. /// Go to the next node
  102. void GotoNext()
  103. {
  104. if (ptr_)
  105. ptr_ = ptr_->next_;
  106. }
  107. /// Go to the previous node
  108. void GotoPrev()
  109. {
  110. if (ptr_)
  111. ptr_ = ptr_->prev_;
  112. }
  113. /// Node pointer
  114. HashNodeBase* ptr_;
  115. };
  116. /// Hash set/map base class
  117. class HashBase
  118. {
  119. public:
  120. /// Initial amount of buckets
  121. static const unsigned MIN_BUCKETS = 8;
  122. /// Maximum load factor
  123. static const unsigned MAX_LOAD_FACTOR = 4;
  124. /// Construct
  125. HashBase() :
  126. ptrs_(0),
  127. allocator_(0),
  128. size_(0),
  129. numBuckets_(0)
  130. {
  131. }
  132. /// Swap with another hash set or map
  133. void Swap(HashBase& rhs)
  134. {
  135. ::Swap(head_, rhs.head_);
  136. ::Swap(tail_, rhs.tail_);
  137. ::Swap(ptrs_, rhs.ptrs_);
  138. ::Swap(allocator_, rhs.allocator_);
  139. ::Swap(size_, rhs.size_);
  140. ::Swap(numBuckets_, rhs.numBuckets_);
  141. }
  142. protected:
  143. /// List head node pointer
  144. HashNodeBase* head_;
  145. /// List tail node pointer
  146. HashNodeBase* tail_;
  147. /// Bucket head pointers
  148. HashNodeBase** ptrs_;
  149. /// Node allocator
  150. AllocatorBlock* allocator_;
  151. /// Number of nodes
  152. unsigned size_;
  153. /// Number of buckets
  154. unsigned numBuckets_;
  155. };