forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.hpp
169 lines (137 loc) · 5.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
#ifndef SLICE_HPP
#define SLICE_HPP
#include "iterbase.hpp"
#include <iterator>
#include <type_traits>
#include <initializer_list>
namespace iter {
//Forward declarations of Slice and slice
//template <typename Container, typename DifferenceType>
//class Slice;
//template <typename T>
//Slice<std::initializer_list<T>> slice( std::initializer_list<T>);
//template <typename Container, typename DifferenceType>
//Slice<Container> slice(Container &&);
template <typename T>
class has_size
{
typedef char one;
typedef long two;
template <typename C> static one test( decltype(&C::size) ) ;
template <typename C> static two test(...);
public:
enum { value = sizeof(test<T>(0)) == sizeof(char) };
};
/* template <typename Container>
typename std::enable_if<has_size<Container>::value, std::size_t>::type
size(const Container& container) {
return container.size();
}
template <typename Container>
typename std::enable_if<!has_size<Container>::value, std::size_t>::type
size(const Container& container) {
return std::distance(std::begin(container), std::end(container));
}
template <typename T, std::size_t N>
std::size_t size(const T (&)[N]) {
return N;
}*/
template <typename Container, typename DifferenceType>
class Slice {
private:
Container container;
DifferenceType start;
DifferenceType stop;
DifferenceType step;
// The only thing allowed to directly instantiate an Slice is
// the slice function
//friend Slice slice<Container, DifferenceType>(Container &&);
//template <typename T>
//friend Slice<std::initializer_list<T>> slice(std::initializer_list<T>);
public:
Slice(Container in_container, DifferenceType start,
DifferenceType stop, DifferenceType step)
: container(std::forward<Container>(in_container)),
start{start},
stop{stop},
step{step}
{
// sets stop = start if the range is empty
if ((start < stop && step <=0) ||
(start > stop && step >=0)){
this->stop = start;
}
if (this->stop > static_cast<DifferenceType>(
size(this->container))) {
this->stop = static_cast<DifferenceType>(size(
this->container));
}
if (this->start < 0) {
this->start = 0;
}
}
Slice() = delete;
Slice& operator=(const Slice&) = delete;
Slice(const Slice &) = default;
class Iterator {
private:
iterator_type<Container> sub_iter;
DifferenceType current;
const DifferenceType stop;
const DifferenceType step;
public:
Iterator (iterator_type<Container> si, DifferenceType start,
DifferenceType stop, DifferenceType step)
: sub_iter{si},
current{start},
stop{stop},
step{step}
{ }
iterator_deref<Container> operator*() const {
return *this->sub_iter;
}
Iterator& operator++() {
std::advance(this->sub_iter, this->step);
this->current += this->step;
return *this;
}
bool operator!=(const Iterator &) const {
return (this->step > 0 && this->current < this->stop)||
(this->step < 0 && this->current > this->stop);
}
};
Iterator begin() {
return {std::next(std::begin(this->container), this->start),
this->start, this->stop, this->step};
}
Iterator end() {
return {std::next(std::begin(this->container), this->stop),
this->stop, this->stop, this->step};
}
};
// Helper function to instantiate a Slice
template <typename Container, typename DifferenceType>
Slice<Container, DifferenceType> slice(
Container&& container,
DifferenceType start, DifferenceType stop, DifferenceType step=1) {
return {std::forward<Container>(container), start, stop, step};
}
//only give the end as an arg and assume step is 1 and begin is 0
template <typename Container, typename DifferenceType>
Slice<Container, DifferenceType> slice(
Container&& container, DifferenceType stop) {
return {std::forward<Container>(container), 0, stop, 1};
}
template <typename T, typename DifferenceType>
Slice<std::initializer_list<T>, DifferenceType> slice(
std::initializer_list<T> il, DifferenceType start,
DifferenceType stop, DifferenceType step=1) {
return {il, start, stop, step};
}
template <typename T, typename DifferenceType>
Slice<std::initializer_list<T>, DifferenceType> slice(
std::initializer_list<T> il, DifferenceType stop) {
return {il, 0, stop, 1};
}
}
#endif //SLICE_HPP