recursive_iterator.h 6.7 KB

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