HashBase.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "../Urho3D.h"
  24. #include "../Container/Allocator.h"
  25. #include "../Container/Hash.h"
  26. #include "../Container/Swap.h"
  27. namespace Urho3D
  28. {
  29. /// Hash set/map node base class.
  30. struct HashNodeBase
  31. {
  32. /// Construct.
  33. HashNodeBase() :
  34. down_(0),
  35. prev_(0),
  36. next_(0)
  37. {
  38. }
  39. /// Next node in the bucket.
  40. HashNodeBase* down_;
  41. /// Previous node.
  42. HashNodeBase* prev_;
  43. /// Next node.
  44. HashNodeBase* next_;
  45. };
  46. /// Hash set/map iterator base class.
  47. struct HashIteratorBase
  48. {
  49. /// Construct.
  50. HashIteratorBase() :
  51. ptr_(0)
  52. {
  53. }
  54. /// Construct with a node pointer.
  55. explicit HashIteratorBase(HashNodeBase* ptr) :
  56. ptr_(ptr)
  57. {
  58. }
  59. /// Test for equality with another iterator.
  60. bool operator == (const HashIteratorBase& rhs) const { return ptr_ == rhs.ptr_; }
  61. /// Test for inequality with another iterator.
  62. bool operator != (const HashIteratorBase& rhs) const { return ptr_ != rhs.ptr_; }
  63. /// Go to the next node.
  64. void GotoNext()
  65. {
  66. if (ptr_)
  67. ptr_ = ptr_->next_;
  68. }
  69. /// Go to the previous node.
  70. void GotoPrev()
  71. {
  72. if (ptr_)
  73. ptr_ = ptr_->prev_;
  74. }
  75. /// Node pointer.
  76. HashNodeBase* ptr_;
  77. };
  78. /// Hash set/map base class.
  79. /** Note that to prevent extra memory use due to vtable pointer, %HashBase intentionally does not declare a virtual destructor
  80. and therefore %HashBase pointers should never be used.
  81. */
  82. class URHO3D_API HashBase
  83. {
  84. public:
  85. /// Initial amount of buckets.
  86. static const unsigned MIN_BUCKETS = 8;
  87. /// Maximum load factor.
  88. static const unsigned MAX_LOAD_FACTOR = 4;
  89. /// Construct.
  90. HashBase() :
  91. ptrs_(0),
  92. allocator_(0)
  93. {
  94. }
  95. /// Swap with another hash set or map.
  96. void Swap(HashBase& rhs)
  97. {
  98. Urho3D::Swap(head_, rhs.head_);
  99. Urho3D::Swap(tail_, rhs.tail_);
  100. Urho3D::Swap(ptrs_, rhs.ptrs_);
  101. Urho3D::Swap(allocator_, rhs.allocator_);
  102. }
  103. /// Return number of elements.
  104. unsigned Size() const { return ptrs_ ? (reinterpret_cast<unsigned*>(ptrs_))[0] : 0; }
  105. /// Return number of buckets.
  106. unsigned NumBuckets() const { return ptrs_ ? (reinterpret_cast<unsigned*>(ptrs_))[1] : 0; }
  107. /// Return whether has no elements.
  108. bool Empty() const { return Size() == 0; }
  109. protected:
  110. /// Allocate bucket head pointers + room for size and bucket count variables.
  111. void AllocateBuckets(unsigned size, unsigned numBuckets);
  112. /// Reset bucket head pointers.
  113. void ResetPtrs();
  114. /// Set new size.
  115. void SetSize(unsigned size) { if (ptrs_) (reinterpret_cast<unsigned*>(ptrs_))[0] = size; }
  116. /// Return bucket head pointers.
  117. HashNodeBase** Ptrs() const { return ptrs_ ? ptrs_ + 2 : 0; }
  118. /// List head node pointer.
  119. HashNodeBase* head_;
  120. /// List tail node pointer.
  121. HashNodeBase* tail_;
  122. /// Bucket head pointers.
  123. HashNodeBase** ptrs_;
  124. /// Node allocator.
  125. AllocatorBlock* allocator_;
  126. };
  127. }