sharded_set.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #pragma once
  2. #include <util/generic/hash_set.h>
  3. #include <array>
  4. #include <cstddef>
  5. #include <utility>
  6. namespace NYT {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. //! A set that stores elements divided into fixed amount of shards.
  9. //! Provides access to whole set and particular shards.
  10. //! The interface is pretty minimalistic, feel free to extend it when needed.
  11. template <class T, int N, class F, class S = THashSet<T>>
  12. class TShardedSet
  13. {
  14. public:
  15. using size_type = size_t;
  16. using difference_type = ptrdiff_t;
  17. using value_type = T;
  18. class const_iterator;
  19. explicit TShardedSet(F elementToShard = F());
  20. [[nodiscard]] bool empty() const;
  21. size_type size() const;
  22. const T& front() const;
  23. size_type count(const T& value) const;
  24. bool contains(const T& value) const;
  25. std::pair<const_iterator, bool> insert(const T& value);
  26. bool erase(const T& value);
  27. void clear();
  28. const_iterator begin() const;
  29. const_iterator cbegin() const;
  30. const_iterator end() const;
  31. const_iterator cend() const;
  32. const S& Shard(int shardIndex) const;
  33. S& MutableShard(int shardIndex);
  34. private:
  35. std::array<S, N> Shards_;
  36. const F ElementToShard_;
  37. S& GetShard(const T& value);
  38. const S& GetShard(const T& value) const;
  39. };
  40. ////////////////////////////////////////////////////////////////////////////////
  41. } // namespace NYT
  42. #define SHARDED_SET_INL_H_
  43. #include "sharded_set-inl.h"
  44. #undef SHARDED_SET_INL_H_