|
| 1 | +#ifndef MELON_DETAIL_CONCAT_VIEW_HPP |
| 2 | +#define MELON_DETAIL_CONCAT_VIEW_HPP |
| 3 | + |
| 4 | +#include <concepts> |
| 5 | +#include <iterator> |
| 6 | +#include <ranges> |
| 7 | +#include <type_traits> |
| 8 | +#include <utility> |
| 9 | + |
| 10 | +namespace fhamonic { |
| 11 | +namespace melon { |
| 12 | +namespace detail { |
| 13 | +namespace views { |
| 14 | + |
| 15 | +#if defined(__cpp_lib_ranges_concat) |
| 16 | + |
| 17 | +inline constexpr auto concat = std::views::concat; |
| 18 | + |
| 19 | +#else |
| 20 | + |
| 21 | +template <typename V1, typename V2> |
| 22 | +concept concat_view_compatible = |
| 23 | + std::ranges::view<V1> && std::ranges::view<V2> && |
| 24 | + std::ranges::input_range<V1> && std::ranges::input_range<V2> && |
| 25 | + std::common_reference_with<std::ranges::range_reference_t<V1>, |
| 26 | + std::ranges::range_reference_t<V2> > && |
| 27 | + std::common_reference_with<std::ranges::range_reference_t<V1>, |
| 28 | + std::ranges::range_value_t<V2> &> && |
| 29 | + std::common_reference_with<std::ranges::range_reference_t<V2>, |
| 30 | + std::ranges::range_value_t<V1> &>; |
| 31 | + |
| 32 | +template <std::ranges::view V1, std::ranges::view V2> |
| 33 | + requires concat_view_compatible<V1, V2> |
| 34 | +class concat_view : public std::ranges::view_interface<concat_view<V1, V2> > { |
| 35 | +private: |
| 36 | + V1 _first; |
| 37 | + V2 _second; |
| 38 | + |
| 39 | + template <bool Const> |
| 40 | + using base_t = std::conditional_t<Const, const concat_view<V1, V2>, |
| 41 | + concat_view<V1, V2> >; |
| 42 | + |
| 43 | + template <bool Const> |
| 44 | + class iterator { |
| 45 | + using Parent = base_t<Const>; |
| 46 | + using FirstBase = std::conditional_t<Const, const V1, V1>; |
| 47 | + using SecondBase = std::conditional_t<Const, const V2, V2>; |
| 48 | + |
| 49 | + std::ranges::iterator_t<FirstBase> _first_it{}; |
| 50 | + std::ranges::sentinel_t<FirstBase> _first_end{}; |
| 51 | + std::ranges::iterator_t<SecondBase> _second_it{}; |
| 52 | + std::ranges::sentinel_t<SecondBase> _second_end{}; |
| 53 | + bool _in_first = true; |
| 54 | + |
| 55 | + void satisfy() { |
| 56 | + if(_in_first && _first_it == _first_end) _in_first = false; |
| 57 | + } |
| 58 | + |
| 59 | + public: |
| 60 | + using iterator_concept = std::input_iterator_tag; |
| 61 | + using iterator_category = std::input_iterator_tag; |
| 62 | + using difference_type = std::ptrdiff_t; |
| 63 | + using value_type = |
| 64 | + std::common_type_t<std::ranges::range_value_t<FirstBase>, |
| 65 | + std::ranges::range_value_t<SecondBase> >; |
| 66 | + using reference = std::common_reference_t< |
| 67 | + std::ranges::range_reference_t<FirstBase>, |
| 68 | + std::ranges::range_reference_t<SecondBase> >; |
| 69 | + |
| 70 | + iterator() = default; |
| 71 | + |
| 72 | + iterator(Parent & parent) |
| 73 | + : _first_it(std::ranges::begin(parent._first)) |
| 74 | + , _first_end(std::ranges::end(parent._first)) |
| 75 | + , _second_it(std::ranges::begin(parent._second)) |
| 76 | + , _second_end(std::ranges::end(parent._second)) { |
| 77 | + satisfy(); |
| 78 | + } |
| 79 | + |
| 80 | + reference operator*() const { |
| 81 | + return _in_first ? *_first_it : *_second_it; |
| 82 | + } |
| 83 | + |
| 84 | + iterator & operator++() { |
| 85 | + if(_in_first) { |
| 86 | + ++_first_it; |
| 87 | + satisfy(); |
| 88 | + } else { |
| 89 | + ++_second_it; |
| 90 | + } |
| 91 | + return *this; |
| 92 | + } |
| 93 | + |
| 94 | + void operator++(int) { ++(*this); } |
| 95 | + |
| 96 | + friend bool operator==(const iterator & it, |
| 97 | + std::default_sentinel_t) noexcept { |
| 98 | + if(it._in_first) return false; |
| 99 | + return it._second_it == it._second_end; |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | +public: |
| 104 | + concat_view() |
| 105 | + requires std::default_initializable<V1> && |
| 106 | + std::default_initializable<V2> |
| 107 | + = default; |
| 108 | + |
| 109 | + constexpr concat_view(V1 first, V2 second) |
| 110 | + : _first(std::move(first)), _second(std::move(second)) {} |
| 111 | + |
| 112 | + constexpr auto begin() { return iterator<false>(*this); } |
| 113 | + |
| 114 | + constexpr auto begin() const |
| 115 | + requires std::ranges::range<const V1> && std::ranges::range<const V2> |
| 116 | + { |
| 117 | + return iterator<true>(*this); |
| 118 | + } |
| 119 | + |
| 120 | + constexpr auto end() const noexcept { return std::default_sentinel; } |
| 121 | + constexpr auto end() noexcept { return std::default_sentinel; } |
| 122 | +}; |
| 123 | + |
| 124 | +template <std::ranges::viewable_range R1, std::ranges::viewable_range R2> |
| 125 | +concat_view(std::views::all_t<R1>, std::views::all_t<R2>) |
| 126 | + -> concat_view<std::views::all_t<R1>, std::views::all_t<R2> >; |
| 127 | + |
| 128 | +struct concat_fn { |
| 129 | + template <std::ranges::viewable_range R1, std::ranges::viewable_range R2> |
| 130 | + requires concat_view_compatible<std::views::all_t<R1>, |
| 131 | + std::views::all_t<R2> > |
| 132 | + constexpr auto operator()(R1 && r1, R2 && r2) const { |
| 133 | + return concat_view(std::views::all(std::forward<R1>(r1)), |
| 134 | + std::views::all(std::forward<R2>(r2))); |
| 135 | + } |
| 136 | +}; |
| 137 | + |
| 138 | +inline constexpr concat_fn concat{}; |
| 139 | + |
| 140 | +#endif |
| 141 | + |
| 142 | +} // namespace views |
| 143 | +} // namespace detail |
| 144 | +} // namespace melon |
| 145 | +} // namespace fhamonic |
| 146 | + |
| 147 | +#endif // MELON_DETAIL_CONCAT_VIEW_HPP |
0 commit comments