-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjoin-iterator.h
138 lines (113 loc) · 5.38 KB
/
join-iterator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (C) 2023 Gleb Bezborodov - All Rights Reserved
*/
#pragma once
#include <type_traits>
#include <utility>
#include <stdexcept>
#include <functional>
#include <array>
namespace detail {
struct join_iterator_tag final {};
template<typename T, size_t I>
struct indexed_ref_t {
explicit indexed_ref_t(const T& in_value)
: value(in_value) {}
const T& value;
};
template<typename TValue, typename, typename... TContainer>
class transform_join_container_view_t;
template<typename TValue, std::size_t... Is, typename... TContainer>
class transform_join_container_view_t<TValue, std::index_sequence<Is...>, TContainer...> : public indexed_ref_t<TContainer, Is>... {
public:
template<typename T, size_t I>
struct iterator_pair_t {
explicit iterator_pair_t(const std::pair<T, T>& in_pair)
: begin(in_pair.first)
, end(in_pair.second) { }
T begin;
T end;
};
template<typename... TIterator>
class transform_join_iterator_t;
template<typename... TIterator>
class transform_join_iterator_t<std::index_sequence<Is...>, TIterator...> : public iterator_pair_t<TIterator, Is>... {
public:
explicit transform_join_iterator_t(const std::pair<TIterator, TIterator>&... in_pair)
: iterator_pair_t<TIterator, Is>(in_pair)...
{
increment = {[this] (bool first_iteration) {
auto&& cur_pair = static_cast<iterator_pair_t<TIterator, Is>&>(*this);
if (cur_pair.begin == cur_pair.end) return true;
if (first_iteration)
++cur_pair.begin;
return cur_pair.begin == cur_pair.end;
}...};
extract = { [this] {
auto&& cur_pair = static_cast<iterator_pair_t<TIterator, Is>&>(*this);
if (cur_pair.begin == cur_pair.end) return (TValue*)nullptr;
return (TValue*)extract_value(cur_pair.begin, detail::join_iterator_tag{});
}... };
}
transform_join_iterator_t& operator++() {
const auto cashed_index = active_container_index;
while (active_container_index < increment.size() && increment[active_container_index](cashed_index == active_container_index))
++active_container_index;
return *this;
}
bool operator!=(const transform_join_iterator_t& Rhs) const {
return active_container_index != Rhs.active_container_index ||
(... || !is_same(static_cast<const iterator_pair_t<TIterator, Is> &>(*this),
static_cast<const iterator_pair_t<TIterator, Is> &>(Rhs))
);
}
decltype(auto) operator*() const {
if (auto* value = extract[active_container_index]())
return *value;
throw std::runtime_error{ "" };
}
uint32_t active_container_index{ 0 };
private:
template<typename TThis>
static bool is_same(const TThis& lhs, const TThis& rhs) {
return lhs.begin == rhs.begin && lhs.end == rhs.end;
}
std::array<std::function<bool(bool)>, sizeof...(TIterator)> increment;
std::array<std::function<TValue*()>, sizeof...(TIterator)> extract;
};
template<typename... Ts>
transform_join_iterator_t(std::pair<Ts, Ts>...) ->
transform_join_iterator_t<std::make_index_sequence<sizeof...(Ts)>, std::decay_t<Ts>...>;
explicit transform_join_container_view_t(const TContainer&... Containers)
: indexed_ref_t<TContainer, Is>(Containers)...{}
auto begin() const {
return transform_join_iterator_t(std::pair{
static_cast<const indexed_ref_t<TContainer, Is>&>(*this).value.begin(),
static_cast<const indexed_ref_t<TContainer, Is>&>(*this).value.end()
}...);
}
auto end() const {
auto end_it = transform_join_iterator_t(std::pair{
static_cast<const indexed_ref_t<TContainer, Is>&>(*this).value.end(),
static_cast<const indexed_ref_t<TContainer, Is>&>(*this).value.end()
}...);
end_it.active_container_index = sizeof...(TContainer);
return end_it;
}
};
template<typename TIterator>
decltype(auto) extract_value(TIterator&& iterator, detail::join_iterator_tag) {
return &(*iterator);
}
}
template<typename T>
struct type_holder_t{};
template<typename TValue, typename... Ts>
class transform_join_container_t final :
public detail::transform_join_container_view_t<TValue, std::make_index_sequence<sizeof...(Ts)>, Ts...> {
public:
explicit transform_join_container_t(type_holder_t<TValue>, const Ts&...args)
: detail::transform_join_container_view_t<TValue, std::make_index_sequence<sizeof...(Ts)>, Ts...> (args...){}
};
template<typename TValue, typename... Ts>
transform_join_container_t(type_holder_t<TValue>, Ts...)->transform_join_container_t<TValue, Ts...>;