sharded_set-inl.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #ifndef SHARDED_SET_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include sharded_set.h"
  3. // For the sake of sane code completion.
  4. #include "sharded_set.h"
  5. #endif
  6. #include <library/cpp/yt/assert/assert.h>
  7. namespace NYT {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. template <class T, int N, class F, class S>
  10. class TShardedSet<T, N, F, S>::const_iterator
  11. {
  12. private:
  13. friend class TShardedSet<T, N, F, S>;
  14. using TOwner = TShardedSet<T, N, F, S>;
  15. using TShardIterator = typename S::const_iterator;
  16. const TOwner* const Owner_;
  17. int ShardIndex_;
  18. TShardIterator ShardIterator_;
  19. const_iterator(
  20. const TOwner* owner,
  21. int shardIndex,
  22. TShardIterator shardIterator)
  23. : Owner_(owner)
  24. , ShardIndex_(shardIndex)
  25. , ShardIterator_(shardIterator)
  26. { }
  27. bool IsValid() const
  28. {
  29. return ShardIterator_ != Owner_->Shards_[ShardIndex_].end();
  30. }
  31. void FastForward()
  32. {
  33. while (ShardIndex_ != N - 1 && !IsValid()) {
  34. ++ShardIndex_;
  35. ShardIterator_ = Owner_->Shards_[ShardIndex_].begin();
  36. }
  37. }
  38. public:
  39. using difference_type = typename std::iterator_traits<TShardIterator>::difference_type;
  40. using value_type = typename std::iterator_traits<TShardIterator>::value_type;
  41. using pointer = typename std::iterator_traits<TShardIterator>::pointer;
  42. using reference = typename std::iterator_traits<TShardIterator>::reference;
  43. using iterator_category = std::forward_iterator_tag;
  44. const_iterator& operator++()
  45. {
  46. ++ShardIterator_;
  47. FastForward();
  48. return *this;
  49. }
  50. const_iterator operator++(int)
  51. {
  52. auto result = *this;
  53. ++ShardIterator_;
  54. FastForward();
  55. return result;
  56. }
  57. bool operator==(const const_iterator& rhs) const
  58. {
  59. return
  60. ShardIndex_ == rhs.ShardIndex_ &&
  61. ShardIterator_ == rhs.ShardIterator_;
  62. }
  63. bool operator!=(const const_iterator& rhs) const
  64. {
  65. return !(*this == rhs);
  66. }
  67. const T& operator*() const
  68. {
  69. return *ShardIterator_;
  70. }
  71. const T* operator->() const
  72. {
  73. return &operator*();
  74. }
  75. };
  76. ////////////////////////////////////////////////////////////////////////////////
  77. template <class T, int N, class F, class S>
  78. TShardedSet<T, N, F, S>::TShardedSet(F elementToShard)
  79. : ElementToShard_(elementToShard)
  80. { }
  81. template <class T, int N, class F, class S>
  82. bool TShardedSet<T, N, F, S>::empty() const
  83. {
  84. return size() == 0;
  85. }
  86. template <class T, int N, class F, class S>
  87. typename TShardedSet<T, N, F, S>::size_type TShardedSet<T, N, F, S>::size() const
  88. {
  89. size_type result = 0;
  90. for (const auto& shard : Shards_) {
  91. result += shard.size();
  92. }
  93. return result;
  94. }
  95. template <class T, int N, class F, class S>
  96. const T& TShardedSet<T, N, F, S>::front() const
  97. {
  98. return *begin();
  99. }
  100. template <class T, int N, class F, class S>
  101. typename TShardedSet<T, N, F, S>::size_type TShardedSet<T, N, F, S>::count(const T& value) const
  102. {
  103. return GetShard(value).count(value);
  104. }
  105. template <class T, int N, class F, class S>
  106. bool TShardedSet<T, N, F, S>::contains(const T& value) const
  107. {
  108. return GetShard(value).contains(value);
  109. }
  110. template <class T, int N, class F, class S>
  111. std::pair<typename TShardedSet<T, N, F, S>::const_iterator, bool> TShardedSet<T, N, F, S>::insert(const T& value)
  112. {
  113. auto shardIndex = ElementToShard_(value);
  114. auto& shard = Shards_[shardIndex];
  115. auto [shardIterator, inserted] = shard.insert(value);
  116. const_iterator iterator(this, shardIndex, shardIterator);
  117. return {iterator, inserted};
  118. }
  119. template <class T, int N, class F, class S>
  120. bool TShardedSet<T, N, F, S>::erase(const T& value)
  121. {
  122. return GetShard(value).erase(value);
  123. }
  124. template <class T, int N, class F, class S>
  125. void TShardedSet<T, N, F, S>::clear()
  126. {
  127. for (auto& shard : Shards_) {
  128. shard.clear();
  129. }
  130. }
  131. template <class T, int N, class F, class S>
  132. typename TShardedSet<T, N, F, S>::const_iterator TShardedSet<T, N, F, S>::begin() const
  133. {
  134. const_iterator iterator(this, /*shardIndex*/ 0, /*shardIterator*/ Shards_[0].begin());
  135. iterator.FastForward();
  136. return iterator;
  137. }
  138. template <class T, int N, class F, class S>
  139. typename TShardedSet<T, N, F, S>::const_iterator TShardedSet<T, N, F, S>::cbegin() const
  140. {
  141. return begin();
  142. }
  143. template <class T, int N, class F, class S>
  144. typename TShardedSet<T, N, F, S>::const_iterator TShardedSet<T, N, F, S>::end() const
  145. {
  146. return const_iterator(this, /*shardIndex*/ N - 1, /*shardIterator*/ Shards_[N - 1].end());
  147. }
  148. template <class T, int N, class F, class S>
  149. typename TShardedSet<T, N, F, S>::const_iterator TShardedSet<T, N, F, S>::cend() const
  150. {
  151. return end();
  152. }
  153. template <class T, int N, class F, class S>
  154. const S& TShardedSet<T, N, F, S>::Shard(int shardIndex) const
  155. {
  156. return Shards_[shardIndex];
  157. }
  158. template <class T, int N, class F, class S>
  159. S& TShardedSet<T, N, F, S>::MutableShard(int shardIndex)
  160. {
  161. return Shards_[shardIndex];
  162. }
  163. template <class T, int N, class F, class S>
  164. S& TShardedSet<T, N, F, S>::GetShard(const T& value)
  165. {
  166. return Shards_[ElementToShard_(value)];
  167. }
  168. template <class T, int N, class F, class S>
  169. const S& TShardedSet<T, N, F, S>::GetShard(const T& value) const
  170. {
  171. return Shards_[ElementToShard_(value)];
  172. }
  173. ////////////////////////////////////////////////////////////////////////////////
  174. } // namespace NYT