forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.hpp
179 lines (151 loc) · 5 KB
/
slice.hpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#ifndef ITER_SLICE_HPP_
#define ITER_SLICE_HPP_
#include "internal/iterator_wrapper.hpp"
#include "internal/iterbase.hpp"
#include <iterator>
#include <type_traits>
namespace iter {
namespace impl {
template <typename Container, typename DifferenceType>
class Sliced;
struct SliceFn;
}
}
template <typename Container, typename DifferenceType>
class iter::impl::Sliced {
private:
Container container_;
DifferenceType start_;
DifferenceType stop_;
DifferenceType step_;
friend SliceFn;
Sliced(Container&& container, DifferenceType start, DifferenceType stop,
DifferenceType step)
: container_(std::forward<Container>(container)),
start_{start < stop && step > 0 ? start : stop},
stop_{stop},
step_{step} {}
public:
Sliced(Sliced&&) = default;
template <typename ContainerT>
class Iterator {
private:
template <typename>
friend class Iterator;
IteratorWrapper<ContainerT> sub_iter_;
IteratorWrapper<ContainerT> sub_end_;
DifferenceType current_;
DifferenceType stop_;
DifferenceType step_;
public:
using iterator_category = std::input_iterator_tag;
using value_type = iterator_traits_deref<ContainerT>;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
Iterator(IteratorWrapper<ContainerT>&& sub_iter,
IteratorWrapper<ContainerT>&& sub_end, DifferenceType start,
DifferenceType stop, DifferenceType step)
: sub_iter_{std::move(sub_iter)},
sub_end_{std::move(sub_end)},
current_{start},
stop_{stop},
step_{step} {}
iterator_deref<ContainerT> operator*() {
return *sub_iter_;
}
iterator_arrow<ContainerT> operator->() {
return apply_arrow(sub_iter_);
}
Iterator& operator++() {
dumb_advance(sub_iter_, sub_end_, step_);
current_ += step_;
if (stop_ < current_) {
current_ = stop_;
}
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
template <typename T>
bool operator!=(const Iterator<T>& other) const {
return sub_iter_ != other.sub_iter_ && current_ != other.current_;
}
template <typename T>
bool operator==(const Iterator<T>& other) const {
return !(*this != other);
}
};
Iterator<Container> begin() {
auto it = get_begin(container_);
dumb_advance(it, get_end(container_), start_);
return {std::move(it), get_end(container_), start_, stop_, step_};
}
Iterator<Container> end() {
return {get_end(container_), get_end(container_), stop_, stop_, step_};
}
Iterator<AsConst<Container>> begin() const {
auto it = get_begin(std::as_const(container_));
dumb_advance(it, get_end(std::as_const(container_)), start_);
return {std::move(it), get_end(std::as_const(container_)), start_, stop_,
step_};
}
Iterator<AsConst<Container>> end() const {
return {get_end(std::as_const(container_)),
get_end(std::as_const(container_)), stop_, stop_, step_};
}
};
struct iter::impl::SliceFn {
private:
template <typename DifferenceType>
class FnPartial : public Pipeable<FnPartial<DifferenceType>> {
public:
template <typename Container>
Sliced<Container, DifferenceType> operator()(Container&& container) const {
return {std::forward<Container>(container), start_, stop_, step_};
}
private:
friend SliceFn;
constexpr FnPartial(DifferenceType start, DifferenceType stop,
DifferenceType step) noexcept : start_{start},
stop_{stop},
step_{step} {}
DifferenceType start_;
DifferenceType stop_;
DifferenceType step_;
};
public:
template <typename Container, typename DifferenceType,
typename = std::enable_if_t<is_iterable<Container>>>
Sliced<Container, DifferenceType> operator()(Container&& container,
DifferenceType start, DifferenceType stop,
DifferenceType step = 1) const {
return {std::forward<Container>(container), start, stop, step};
}
// only given the end, assume step_ is 1 and begin is 0
template <typename Container, typename DifferenceType,
typename = std::enable_if_t<is_iterable<Container>>>
iter::impl::Sliced<Container, DifferenceType> operator()(
Container&& container, DifferenceType stop) const {
return {std::forward<Container>(container), 0, stop, 1};
}
template <typename DifferenceType,
typename = std::enable_if_t<!is_iterable<DifferenceType>>>
constexpr FnPartial<DifferenceType> operator()(DifferenceType stop) const
noexcept {
return {0, stop, 1};
}
template <typename DifferenceType,
typename = std::enable_if_t<!is_iterable<DifferenceType>>>
constexpr FnPartial<DifferenceType> operator()(DifferenceType start,
DifferenceType stop, DifferenceType step = 1) const noexcept {
return {start, stop, step};
}
};
namespace iter {
constexpr impl::SliceFn slice{};
}
#endif