HashBase.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "Hash.h"
  26. #include "Swap.h"
  27. /// Hash set/map node base.
  28. struct HashNodeBase
  29. {
  30. /// Construct.
  31. HashNodeBase() :
  32. prev_(0),
  33. next_(0),
  34. down_(0)
  35. {
  36. }
  37. /// Previous node.
  38. HashNodeBase* prev_;
  39. /// Next node.
  40. HashNodeBase* next_;
  41. /// Next node in the bucket.
  42. HashNodeBase* down_;
  43. };
  44. /// Hash set/map iterator base class.
  45. class HashIteratorBase
  46. {
  47. public:
  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. class HashBase
  79. {
  80. public:
  81. /// Initial amount of buckets.
  82. static const unsigned MIN_BUCKETS = 8;
  83. /// Maximum load factor.
  84. static const unsigned MAX_LOAD_FACTOR = 4;
  85. /// Construct.
  86. HashBase() :
  87. ptrs_(0),
  88. allocator_(0),
  89. size_(0),
  90. numBuckets_(0)
  91. {
  92. }
  93. /// Swap with another hash set or map.
  94. void Swap(HashBase& rhs)
  95. {
  96. ::Swap(head_, rhs.head_);
  97. ::Swap(tail_, rhs.tail_);
  98. ::Swap(ptrs_, rhs.ptrs_);
  99. ::Swap(allocator_, rhs.allocator_);
  100. ::Swap(size_, rhs.size_);
  101. ::Swap(numBuckets_, rhs.numBuckets_);
  102. }
  103. protected:
  104. /// List head node pointer.
  105. HashNodeBase* head_;
  106. /// List tail node pointer.
  107. HashNodeBase* tail_;
  108. /// Bucket head pointers.
  109. HashNodeBase** ptrs_;
  110. /// Node allocator.
  111. AllocatorBlock* allocator_;
  112. /// Number of nodes.
  113. unsigned size_;
  114. /// Number of buckets.
  115. unsigned numBuckets_;
  116. };