HashBase.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "Hash.h"
  26. #include "Swap.h"
  27. /// Hash set/map node base class.
  28. struct HashNodeBase
  29. {
  30. /// Construct.
  31. HashNodeBase() :
  32. down_(0),
  33. prev_(0),
  34. next_(0)
  35. {
  36. }
  37. /// Next node in the bucket.
  38. HashNodeBase* down_;
  39. /// Previous node.
  40. HashNodeBase* prev_;
  41. /// Next node.
  42. HashNodeBase* next_;
  43. };
  44. /// Hash set/map iterator base class.
  45. struct HashIteratorBase
  46. {
  47. /// Construct.
  48. HashIteratorBase() :
  49. ptr_(0)
  50. {
  51. }
  52. /// Construct with a node pointer.
  53. explicit HashIteratorBase(HashNodeBase* ptr) :
  54. ptr_(ptr)
  55. {
  56. }
  57. /// Test for equality with another iterator.
  58. bool operator == (const HashIteratorBase& rhs) const { return ptr_ == rhs.ptr_; }
  59. /// Test for inequality with another iterator.
  60. bool operator != (const HashIteratorBase& rhs) const { return ptr_ != rhs.ptr_; }
  61. /// Go to the next node.
  62. void GotoNext()
  63. {
  64. if (ptr_)
  65. ptr_ = ptr_->next_;
  66. }
  67. /// Go to the previous node.
  68. void GotoPrev()
  69. {
  70. if (ptr_)
  71. ptr_ = ptr_->prev_;
  72. }
  73. /// Node pointer.
  74. HashNodeBase* ptr_;
  75. };
  76. /// Hash set/map base class.
  77. class HashBase
  78. {
  79. public:
  80. /// Initial amount of buckets.
  81. static const unsigned MIN_BUCKETS = 8;
  82. /// Maximum load factor.
  83. static const unsigned MAX_LOAD_FACTOR = 4;
  84. /// Construct.
  85. HashBase() :
  86. ptrs_(0),
  87. allocator_(0),
  88. size_(0),
  89. numBuckets_(0)
  90. {
  91. }
  92. /// Swap with another hash set or map.
  93. void Swap(HashBase& rhs)
  94. {
  95. ::Swap(head_, rhs.head_);
  96. ::Swap(tail_, rhs.tail_);
  97. ::Swap(ptrs_, rhs.ptrs_);
  98. ::Swap(allocator_, rhs.allocator_);
  99. ::Swap(size_, rhs.size_);
  100. ::Swap(numBuckets_, rhs.numBuckets_);
  101. }
  102. protected:
  103. /// List head node pointer.
  104. HashNodeBase* head_;
  105. /// List tail node pointer.
  106. HashNodeBase* tail_;
  107. /// Bucket head pointers.
  108. HashNodeBase** ptrs_;
  109. /// Node allocator.
  110. AllocatorBlock* allocator_;
  111. /// Number of nodes.
  112. unsigned size_;
  113. /// Number of buckets.
  114. unsigned numBuckets_;
  115. };