enumerate.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #include <util/generic/store_policy.h>
  3. #include <limits>
  4. #include <tuple>
  5. namespace NPrivate {
  6. template <typename TContainer>
  7. struct TEnumerator {
  8. private:
  9. using TStorage = TAutoEmbedOrPtrPolicy<TContainer>;
  10. using TValue = std::tuple<const std::size_t, decltype(*std::begin(std::declval<TContainer&>()))>;
  11. using TIteratorState = decltype(std::begin(std::declval<TContainer&>()));
  12. using TSentinelState = decltype(std::end(std::declval<TContainer&>()));
  13. static constexpr bool TrivialSentinel = std::is_same_v<TIteratorState, TSentinelState>;
  14. struct TIterator;
  15. struct TSentinelCandidate {
  16. TSentinelState Iterator_;
  17. };
  18. using TSentinel = std::conditional_t<TrivialSentinel, TIterator, TSentinelCandidate>;
  19. struct TIterator {
  20. using difference_type = std::ptrdiff_t;
  21. using value_type = TValue;
  22. using pointer = void;
  23. using reference = value_type;
  24. using iterator_category = std::input_iterator_tag;
  25. reference operator*() const {
  26. return {Index_, *Iterator_};
  27. }
  28. TIterator& operator++() {
  29. ++Index_;
  30. ++Iterator_;
  31. return *this;
  32. }
  33. TIterator operator++(int) {
  34. TIterator result = *this;
  35. ++(*this);
  36. return result;
  37. }
  38. bool operator!=(const TSentinel& other) const {
  39. return Iterator_ != other.Iterator_;
  40. }
  41. bool operator==(const TSentinel& other) const {
  42. return Iterator_ == other.Iterator_;
  43. }
  44. std::size_t Index_;
  45. TIteratorState Iterator_;
  46. };
  47. public:
  48. using iterator = TIterator;
  49. using const_iterator = TIterator;
  50. using value_type = typename TIterator::value_type;
  51. using reference = typename TIterator::reference;
  52. using const_reference = typename TIterator::reference;
  53. TIterator begin() const {
  54. return {0, std::begin(*Storage_.Ptr())};
  55. }
  56. TSentinel end() const {
  57. if constexpr (TrivialSentinel) {
  58. return TIterator{std::numeric_limits<std::size_t>::max(), std::end(*Storage_.Ptr())};
  59. } else {
  60. return TSentinel{std::end(*Storage_.Ptr())};
  61. }
  62. }
  63. mutable TStorage Storage_;
  64. };
  65. }
  66. //! Usage: for (auto [i, x] : Enumerate(container)) {...}
  67. template <typename TContainerOrRef>
  68. auto Enumerate(TContainerOrRef&& container) {
  69. return NPrivate::TEnumerator<TContainerOrRef>{std::forward<TContainerOrRef>(container)};
  70. }