forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice.hpp
More file actions
157 lines (132 loc) · 4.41 KB
/
slice.hpp
File metadata and controls
157 lines (132 loc) · 4.41 KB
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
#ifndef ITER_SLICE_HPP_
#define ITER_SLICE_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&& in_container, DifferenceType in_start,
DifferenceType in_stop, DifferenceType in_step)
: container(std::forward<Container>(in_container)),
start{in_start < in_stop && in_step > 0 ? in_start : in_stop},
stop{in_stop},
step{in_step} {}
public:
Sliced(Sliced&&) = default;
class Iterator : public std::iterator<std::input_iterator_tag,
iterator_traits_deref<Container>> {
private:
iterator_type<Container> sub_iter;
iterator_type<Container> sub_end;
DifferenceType current;
DifferenceType stop;
DifferenceType step;
public:
Iterator(iterator_type<Container>&& si, iterator_type<Container>&& se,
DifferenceType in_start, DifferenceType in_stop, DifferenceType in_step)
: sub_iter{std::move(si)},
sub_end{std::move(se)},
current{in_start},
stop{in_stop},
step{in_step} {}
iterator_deref<Container> operator*() {
return *this->sub_iter;
}
iterator_arrow<Container> operator->() {
return apply_arrow(this->sub_iter);
}
Iterator& operator++() {
dumb_advance(this->sub_iter, this->sub_end, this->step);
this->current += this->step;
if (this->stop < this->current) {
this->current = this->stop;
}
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
bool operator!=(const Iterator& other) const {
return this->sub_iter != other.sub_iter && this->current != other.current;
}
bool operator==(const Iterator& other) const {
return !(*this != other);
}
};
Iterator begin() {
auto it = std::begin(this->container);
dumb_advance(it, std::end(this->container), this->start);
return {std::move(it), std::end(this->container), this->start, this->stop,
this->step};
}
Iterator end() {
return {std::end(this->container), std::end(this->container), this->stop,
this->stop, this->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 in_start, DifferenceType in_stop,
DifferenceType in_step) noexcept : start{in_start},
stop{in_stop},
step{in_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