-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslice_iter.h
129 lines (103 loc) · 3.53 KB
/
slice_iter.h
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
#pragma once
#include<iostream>
#include<valarray>
#include<algorithm>
#include<numeric> // for inner_product
using namespace std;
// forward declarations to allow friend declarations:
template<class T> class slice_iter;
template<class T> bool operator==(const slice_iter<T>&, const slice_iter<T>&);
template<class T> bool operator!=(const slice_iter<T>&, const slice_iter<T>&);
template<class T> bool operator< (const slice_iter<T>&, const slice_iter<T>&);
template<class T>
class slice_iter
{
valarray<T>* v;
slice s;
size_t curr; // index of current element
T& ref(size_t i) const { return (*v)[s.start()+i*s.stride()]; }
// valarray<double> & const { return (*v)[]};
public:
slice_iter(valarray<T>* vv, slice ss) :v(vv), s(ss), curr(0) { }
slice_iter end() const
{
slice_iter t = *this;
t.curr = s.size(); // index of last-plus-one element
return t;
}
slice_iter& operator++() { curr++; return *this; }
slice_iter operator++(int) { slice_iter t = *this; curr++; return t; }
T& operator[](size_t i) { return ref(i); } // C style subscript
T& operator()(size_t i) { return ref(i); } // Fortran-style subscript
T& operator*() { return ref(curr); } // current element
friend bool operator==<>(const slice_iter& p, const slice_iter& q);
friend bool operator!=<>(const slice_iter& p, const slice_iter& q);
friend bool operator< <>(const slice_iter& p, const slice_iter& q);
};
template<class T>
bool operator==(const slice_iter<T>& p, const slice_iter<T>& q)
{
return p.curr==q.curr
&& p.s.stride()==q.s.stride()
&& p.s.start()==q.s.start();
}
template<class T>
bool operator!=(const slice_iter<T>& p, const slice_iter<T>& q)
{
return !(p==q);
}
template<class T>
bool operator<(const slice_iter<T>& p, const slice_iter<T>& q)
{
return p.curr<q.curr
&& p.s.stride()==q.s.stride()
&& p.s.start()==q.s.start();
}
//-------------------------------------------------------------
// forward declarations to allow friend declarations:
template<class T> class Cslice_iter;
template<class T> bool operator==(const Cslice_iter<T>&, const Cslice_iter<T>&);
template<class T> bool operator!=(const Cslice_iter<T>&, const Cslice_iter<T>&);
template<class T> bool operator< (const Cslice_iter<T>&, const Cslice_iter<T>&);
template<class T> class Cslice_iter
{
valarray<T>* v;
slice s;
size_t curr; // index of current element
const T& ref(size_t i) const { return (*v)[s.start()+i*s.stride()]; }
public:
Cslice_iter(valarray<T>* vv, slice ss): v(vv), s(ss), curr(0){}
Cslice_iter end() const
{
Cslice_iter t = *this;
t.curr = s.size(); // index of one plus last element
return t;
}
Cslice_iter& operator++() { curr++; return *this; }
Cslice_iter operator++(int) { Cslice_iter t = *this; curr++; return t; }
const T& operator[](size_t i) const { return ref(i); }
const T& operator()(size_t i) const { return ref(i); }
const T& operator*() const { return ref(curr); }
friend bool operator==<>(const Cslice_iter& p, const Cslice_iter& q);
friend bool operator!=<>(const Cslice_iter& p, const Cslice_iter& q);
friend bool operator< <>(const Cslice_iter& p, const Cslice_iter& q);
};
template<class T>
bool operator==(const Cslice_iter<T>& p, const Cslice_iter<T>& q)
{
return p.curr==q.curr
&& p.s.stride()==q.s.stride()
&& p.s.start()==q.s.start();
}
template<class T>
bool operator!=(const Cslice_iter<T>& p, const Cslice_iter<T>& q)
{
return !(p==q);
}
template<class T>
bool operator<(const Cslice_iter<T>& p, const Cslice_iter<T>& q)
{
return p.curr<q.curr
&& p.s.stride()==q.s.stride()
&& p.s.start()==q.s.start();
}