HashBase.h 4.3 KB

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