recursive_expander.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //
  2. // RecursiveExpander.h
  3. // iterator
  4. //
  5. // Created by Sam Jaffe on 9/23/25.
  6. // Copyright © 2025 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. #include <iterator>
  10. #include <type_traits>
  11. #include <iterator/concepts.h>
  12. #include <iterator/detail/projection_tuple.h>
  13. #include <iterator/end_aware_iterator.h>
  14. namespace iterator::detail {
  15. template <typename Proj, typename It, size_t I>
  16. using projection_t = std::invoke_result_t<Proj, std::iter_reference_t<It>,
  17. std::integral_constant<size_t, I>>;
  18. template <typename It, typename Projs, typename MaxDepth, size_t N = 0,
  19. typename V = std::decay_t<projection_t<Projs, It, N>>>
  20. struct RecursiveExpander {
  21. using type = std::tuple<EndAwareIterator<It>>;
  22. };
  23. template <typename It, typename Projs, size_t N>
  24. struct RecursiveExpander<It, Projs, bounded<N + 1>, N> {
  25. using type = std::tuple<EndAwareIterator<It>>;
  26. };
  27. template <typename It, typename Projs, typename MaxDepth, size_t N, Range V>
  28. struct RecursiveExpander<It, Projs, MaxDepth, N, V> {
  29. using projected_value_type = projection_t<Projs, It, N>;
  30. using expand_next = RecursiveExpander<iterator_t<projected_value_type>, Projs,
  31. MaxDepth, N + 1>;
  32. using type =
  33. tuple_cat_t<std::tuple<EndAwareIterator<It>>, typename expand_next::type>;
  34. };
  35. template <typename It, typename Projs, typename MaxDepth, size_t N,
  36. AssocRange V>
  37. struct RecursiveExpander<It, Projs, MaxDepth, N, V> {
  38. using projected_value_type =
  39. std::tuple_element_t<1,
  40. std::remove_reference_t<projection_t<Projs, It, N>>>;
  41. using expand_next = RecursiveExpander<iterator_t<projected_value_type>, Projs,
  42. MaxDepth, N + 1>;
  43. using type =
  44. tuple_cat_t<std::tuple<EndAwareIterator<It>>, typename expand_next::type>;
  45. };
  46. }