This repository was archived by the owner on Nov 15, 2022. It is now read-only.
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
141 lines (117 loc) · 4.98 KB
/
Copy pathslice.hpp
File metadata and controls
141 lines (117 loc) · 4.98 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
#ifndef ITER_SLICE_HPP_
#define ITER_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 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 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}
{ }
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& 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};
}
};
// 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 {std::move(il), start, stop, step};
}
template <typename T, typename DifferenceType>
Slice<std::initializer_list<T>, DifferenceType> slice(
std::initializer_list<T> il, DifferenceType stop) {
return {std::move(il), 0, stop, 1};
}
}
#endif