recursive_iterator.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // recursive_iterator.h
  3. // iterator
  4. //
  5. // Created by Sam Jaffe on 2/17/17.
  6. //
  7. #pragma once
  8. #include <ranges>
  9. #include <tuple>
  10. #include <utility>
  11. #include <iterator/concepts.h>
  12. #include <iterator/detail/projection_tuple.h>
  13. #include <iterator/detail/recursive_expander.h>
  14. #include <iterator/end_aware_iterator.h>
  15. #include <iterator/facade.h>
  16. #include <iterator/forwards.h>
  17. #include <iterator/detail/macro.h>
  18. namespace iterator {
  19. template <typename Tuple, typename Indices> class recursive_iterator_base;
  20. template <typename... It, size_t... Is>
  21. class recursive_iterator_base<std::tuple<It...>, std::index_sequence<Is...>>
  22. : public std::tuple<It...> {
  23. public:
  24. static constexpr size_t LastIndex = sizeof...(It) - 1;
  25. template <size_t I>
  26. using iterator_type = std::tuple_element_t<I, std::tuple<It...>>;
  27. template <size_t I> using value_type = std::iter_value_t<iterator_type<I>>;
  28. public:
  29. template <typename T> operator end_aware_iterator<T>() const {
  30. return std::get<end_aware_iterator<T>>(*this);
  31. }
  32. decltype(auto) dereference() const {
  33. auto rval = std::tuple_cat(get<Is>()...);
  34. // Special Case Handling for circumstances where at least everything up to
  35. // the deepest nested container is non-associative. In this case, we don't
  36. // want to transmute our single element/association into a tuple, since
  37. // there's no benefit from that.
  38. if constexpr (std::tuple_size_v<decltype(rval)> == 1) {
  39. return std::get<0>(rval); // May be a reference
  40. } else {
  41. return rval; // Tuple-of-references
  42. }
  43. }
  44. void increment() { increment<>(); }
  45. bool at_end() const { return std::get<0>(*this).at_end(); }
  46. bool equal_to(recursive_iterator_base const & other) const {
  47. return *this == other;
  48. }
  49. protected:
  50. recursive_iterator_base() = default;
  51. recursive_iterator_base(iterator_type<0> iter) { assign<0>(iter); }
  52. private:
  53. template <size_t I = LastIndex> bool increment() {
  54. auto & iter = std::get<I>(*this);
  55. if (iter.at_end()) { return false; } // Make sure we don't go OOB
  56. ++iter;
  57. if constexpr (I > 0) {
  58. while (iter.at_end() && increment<I - 1>()) {
  59. assign<I>(*std::get<I - 1>(*this));
  60. }
  61. }
  62. return !iter.at_end();
  63. }
  64. template <size_t I> decltype(auto) get() const {
  65. auto iter = std::get<I>(*this);
  66. if constexpr (I + 1 == sizeof...(It)) {
  67. if constexpr (Assoc<value_type<I>>) {
  68. return std::tie(iter->first, iter->second);
  69. } else {
  70. return std::tie(*iter);
  71. }
  72. } else if constexpr (Assoc<value_type<I>>) {
  73. return std::tie(iter->first);
  74. } else {
  75. return std::make_tuple();
  76. }
  77. }
  78. template <size_t I, typename T> void assign(end_aware_iterator<T> it) {
  79. std::get<I>(*this) = it;
  80. if constexpr (I < LastIndex) {
  81. if (!it.at_end()) { assign<I + 1>(*it); }
  82. }
  83. }
  84. template <size_t I, typename T> void assign(T && value) {
  85. if constexpr (Range<T>) {
  86. assign<I>(end_aware_iterator(std::forward<T>(value)));
  87. } else {
  88. assign<I>(value.second);
  89. }
  90. }
  91. };
  92. template <typename It, typename MaxDepth,
  93. typename Projs = detail::Projections<>>
  94. struct recursive_iterator_helper {
  95. static typename detail::ProjectionExpander<It, Projs>::type const & _projs;
  96. using Projections = decltype(detail::Projections(_projs));
  97. using iterator_tuple =
  98. typename detail::RecursiveExpander<It, Projections, MaxDepth>::type;
  99. static constexpr auto extent = std::tuple_size_v<iterator_tuple>;
  100. using indices = decltype(std::make_index_sequence<extent>());
  101. using type = recursive_iterator_base<iterator_tuple, indices>;
  102. };
  103. /**
  104. * @brief An iterator type for nested collections, allowing you to treat it as
  105. * a single-layer collection.
  106. *
  107. * In order to provide a simple interface, if an associative container is used
  108. * in the chain, the type returned by operator*() is a tuple. If multiple
  109. * associative containers are nested, then the tuple will be of the form
  110. * std::tuple<key1, key2, ..., keyN, value>. To avoid copies, and allow
  111. * editting of underlying values, the tuple contains references.
  112. *
  113. * @tparam It The iterator type of the top-level collection.
  114. * @tparam MaxDepth The bounding type, representing how many layers this
  115. * iterator is willing to delve in the parent object.
  116. */
  117. template <typename It, typename MaxDepth>
  118. class recursive_iterator : public recursive_iterator_helper<It, MaxDepth>::type,
  119. public facade<recursive_iterator<It, MaxDepth>> {
  120. public:
  121. using sentinel_type = sentinel_t;
  122. public:
  123. recursive_iterator() = default;
  124. explicit recursive_iterator(Range auto & range, MaxDepth = {})
  125. : recursive_iterator(end_aware_iterator(range)) {}
  126. explicit recursive_iterator(end_aware_iterator<It> iter, MaxDepth = {})
  127. : recursive_iterator::recursive_iterator_base(iter) {}
  128. template <typename Ot>
  129. explicit recursive_iterator(end_aware_iterator<Ot> other, MaxDepth = {})
  130. : recursive_iterator(end_aware_iterator<It>(other)) {}
  131. template <typename Ot>
  132. explicit recursive_iterator(recursive_iterator<Ot, MaxDepth> other)
  133. : recursive_iterator(end_aware_iterator<Ot>(other)) {}
  134. };
  135. template <typename Range, typename MaxDepth>
  136. recursive_iterator(Range &, MaxDepth)
  137. -> recursive_iterator<iterator_t<Range>, MaxDepth>;
  138. template <typename Range>
  139. recursive_iterator(Range &) -> recursive_iterator<iterator_t<Range>, unbounded>;
  140. }
  141. namespace std {
  142. template <size_t I, typename It, typename MaxDepth>
  143. auto get(::iterator::recursive_iterator<It, MaxDepth> const & iter) {
  144. using return_type = std::decay_t<decltype(iter)>::template iterator_type<I>;
  145. return static_cast<return_type>(iter);
  146. }
  147. }
  148. namespace iterator::views {
  149. template <size_t N>
  150. struct recursive_n_fn : std::ranges::range_adaptor_closure<recursive_n_fn<N>> {
  151. template <std::ranges::range Rng> auto operator()(Rng && rng) const {
  152. auto begin = recursive_iterator(std::forward<Rng>(rng), bounded<N>{});
  153. return std::ranges::subrange(begin, decltype(begin)());
  154. }
  155. };
  156. struct recursive_fn : std::ranges::range_adaptor_closure<recursive_fn> {
  157. template <std::ranges::range Rng> auto operator()(Rng && rng) const {
  158. auto begin = recursive_iterator(std::forward<Rng>(rng));
  159. return std::ranges::subrange(begin, decltype(begin)());
  160. }
  161. };
  162. template <size_t N> constexpr recursive_n_fn<N> recursive_n{};
  163. constexpr recursive_fn recursive;
  164. }
  165. #include <iterator/detail/undef.h>