-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrange.hpp
133 lines (122 loc) · 3.91 KB
/
range.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
/* range.hpp
*
* Copyright (c) 2013 Alexander Duchene <[email protected]>
*
* This piece of software was created as part of the Drosophila Population
* Genomics Project opensource agreement.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef RANGE_ITERATOR_HPP
#define RANGE_ITERATOR_HPP
#include <iterator>
namespace rangepp {
template<typename value_t>
class range_impl{
private:
value_t rbegin;
value_t rend;
value_t step;
int step_end;
public:
range_impl(value_t begin, value_t end, value_t step=1):
rbegin(begin),rend(end),step(step){
step_end=(rend-rbegin)/step;
if(rbegin+step_end*step != rend){
step_end++;
}
}
class iterator:
public std::iterator<std::random_access_iterator_tag,value_t>
{
private:
value_t current_value;
int current_step;
range_impl& parent;
public:
iterator(int start,range_impl& parent): current_step(start), parent(parent){current_value=parent.rbegin+current_step*parent.step;}
value_t operator*() {return current_value;}
const iterator* operator++(){
current_value+=parent.step;
current_step++;
return this;
}
const iterator* operator++(int){
current_value+=parent.step;
current_step++;
return this;
}
bool operator==(const iterator& other) {
return current_step==other.current_step;
}
bool operator!=(const iterator& other) {
return current_step!=other.current_step;
}
iterator operator+(int s) {
iterator ret=*this;
ret.current_step+=s;
ret.current_value+=s*parent.step;
return ret;
}
iterator operator-(int s){
iterator ret=*this;
ret.current_step-=s;
ret.current_value-=s*parent.step;
return ret;
}
const iterator* operator--(){
current_value-=parent.step;
current_step--;
return this;}
iterator operator--(int){
iterator old=*this;
current_value-=parent.step;
current_step--;
return old;
}
};
iterator begin(){
return iterator(0,*this);
}
iterator end(){
return iterator(step_end,*this);
}
value_t operator[](int s){
return rbegin+s*step;
}
int size(){
return step_end;
}
};
}
template<typename vt,typename other>
auto range(other begin, other end, vt stepsize)->rangepp::range_impl<decltype(begin+end+stepsize)>
{
return rangepp::range_impl<decltype(begin+end+stepsize)>(begin,end,stepsize);
}
template<typename b,typename e>
auto range(b begin, e end) -> rangepp::range_impl<decltype(begin+end)>
{
return rangepp::range_impl<decltype(begin+end)>(begin,end,1);
}
template<typename e>
rangepp::range_impl<e> range(e end){
return rangepp::range_impl<e>(0,end,1);
}
#endif