1#ifndef CPARK_MERGED_VIEW_H
2#define CPARK_MERGED_VIEW_H
13 std::ranges::forward_range<R> && std::ranges::view<std::ranges::range_value_t<R>>;
21template <concepts::RangeOfView R>
22class MergedSameView :
public std::ranges::view_interface<MergedSameView<R>> {
24 using ViewType = std::ranges::range_value_t<R>;
30 class Iterator :
public std::forward_iterator_tag {
32 using difference_type = std::ptrdiff_t;
33 using value_type = std::ranges::range_value_t<ViewType>;
46 while (index_of_view_ < std::ranges::size(*views_) &&
47 inner_iterator_ == std::ranges::end((*views_)[index_of_view_])) {
49 if (index_of_view_ < std::ranges::size(*views_)) {
50 inner_iterator_ = std::ranges::begin((*views_)[index_of_view_]);
57 value_type operator*()
const {
return *inner_iterator_; }
59 Iterator& operator++() {
62 while (index_of_view_ < std::ranges::size(*views_) &&
63 inner_iterator_ == std::ranges::end((*views_)[index_of_view_])) {
65 if (index_of_view_ < std::ranges::size(*views_)) {
66 inner_iterator_ = std::ranges::begin((*views_)[index_of_view_]);
84 if (isEnd() &&
other.isEnd()) {
87 return views_ ==
other.views_ && inner_iterator_ ==
other.inner_iterator_ &&
88 index_of_view_ ==
other.index_of_view_;
96 bool isEnd()
const {
return !(index_of_view_ < std::ranges::size(*views_)); }
100 std::ranges::iterator_t<const ViewType> inner_iterator_{};
102 std::vector<ViewType>* views_{
nullptr};
104 std::vector<ViewType>::size_type index_of_view_{0};
116 auto start_iterator = std::ranges::empty(views_) ? std::ranges::iterator_t<ViewType>{}
117 : std::ranges::begin(views_.front());
119 static_cast<std::vector<ViewType>::size_type
>(0)};
123 constexpr auto end()
const {
124 return Iterator{std::ranges::iterator_t<ViewType>{},
125 const_cast<std::vector<ViewType>*
>(&views_), std::ranges::size(views_)};
129 std::vector<ViewType> views_;
136template <std::ranges::view V1, std::ranges::view V2>
137requires std::same_as<std::ranges::range_value_t<V1>,
139 :
public std::ranges::view_interface<MergedTwoDiffView<V1, V2>> {
144 class Iterator :
public std::forward_iterator_tag {
146 using difference_type = std::ptrdiff_t;
147 using value_type = std::ranges::range_value_t<V1>;
151 using OriginalIterator1 = std::ranges::iterator_t<const V1>;
152 using OriginalIterator2 = std::ranges::iterator_t<const V2>;
164 template <
typename Iter>
169 if constexpr (std::same_as<OriginalIterator1, OriginalIterator2>) {
176 if constexpr (std::convertible_to<Iter, OriginalIterator1>) {
183 if (hold1() && get1() == std::ranges::end(*view1_)) {
184 get2() = std::ranges::begin(*view2_);
191 value_type operator*()
const {
return hold1() ? *get1() : *get2(); }
193 Iterator& operator++() {
198 if (hold1() && get1() == std::ranges::end(*view1_)) {
199 get2() = std::ranges::begin(*view2_);
211 bool operator==(
const Iterator& other)
const {
212 return (hold1() && other.hold1() && get1() == other.get1()) ||
213 (hold2() && other.hold2() && get2() == other.get2());
216 bool operator!=(
const Iterator& other)
const {
return !(*
this == other); }
219 bool hold1()
const {
return hold1_; }
220 decltype(
auto) get1() {
return (inner_iterator1_); }
221 decltype(
auto) get1()
const {
return (inner_iterator1_); }
223 bool hold2()
const {
return !hold1_; }
224 decltype(
auto) get2() {
return (inner_iterator2_); }
225 decltype(
auto) get2()
const {
return (inner_iterator2_); }
229 OriginalIterator1 inner_iterator1_;
230 OriginalIterator2 inner_iterator2_;
246 return Iterator{std::ranges::begin(view1_),
const_cast<V1*
>(&view1_),
const_cast<V2*
>(&view2_),
251 constexpr auto end()
const {
252 return Iterator{std::ranges::end(view2_),
const_cast<V1*
>(&view1_),
const_cast<V2*
>(&view2_),
261template <std::ranges::view FirstView>
262auto MergedDiffView(
const FirstView& view) {
267template <std::ranges::view FirstView, std::ranges::view... OtherViews>
268auto MergedDiffView(
const FirstView& first_view,
const OtherViews&... other_views) {
269 return MergedTwoDiffView{first_view, MergedDiffView(other_views...)};
Definition merged_view.h:30
Iterator(std::ranges::iterator_t< const ViewType > start_iterator, std::vector< ViewType > *views, std::vector< ViewType >::size_type start_index)
Definition merged_view.h:42
bool operator==(const Iterator &other) const
Definition merged_view.h:83
Definition merged_view.h:22
constexpr auto end() const
Definition merged_view.h:123
constexpr auto begin() const
Definition merged_view.h:115
MergedSameView(const R &views)
Definition merged_view.h:111
Definition merged_view.h:144
Iterator(Iter iterator, V1 *view1, V2 *view2, bool if_hold1)
Definition merged_view.h:165
Definition merged_view.h:139
constexpr auto begin() const
Definition merged_view.h:245
constexpr auto end() const
Definition merged_view.h:251
MergedTwoDiffView(V1 view1, V2 view2)
Definition merged_view.h:240
Definition merged_view.h:12