HashBase.h 4.2 KB

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