-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcobarray.hpp
218 lines (190 loc) · 6.13 KB
/
cobarray.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* Compile time bound arrays. For documentation, see README.md.
* (C) Srdjan Veljkovic
* License: MIT (see LICENSE)
*/
#if !defined(INC_COBARRAY)
#define INC_COBARRAY
#include "cobi.hpp"
template <class T, int N>
struct cobarray {
static_assert(N > 0, "Array must have at least one element");
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T *;
using reference = T &;
using index = cobi<int, 0, N-1>;
constexpr bool empty() const noexcept { return false; }
constexpr unsigned size() const noexcept { return N; }
constexpr unsigned max_size() const noexcept { return N; }
constexpr T get(index i) const {
return d[i.get()];
}
constexpr T set(index i, T const& t) {
return d[i.get()] = t;
}
constexpr bool maybe_set(int i, T const& t) {
index idx;
if (idx.be(i)) {
d[idx.get()] = t;
return true;
}
return false;
}
template <class V, class U=T>
constexpr std::enable_if_t<std::is_class_v<U>,V> get(index i, V U::*m) const {
return d[i.get()].*m;
}
template <class V, class U=T>
constexpr std::enable_if_t<std::is_class_v<U>,V> set(index i, V U::*m, V const& v) {
return d[i.get()].*m = v;
}
void fill(T const& v) {
for (T* p = d; p < d + N; ++p) {
*p = v;
}
}
constexpr bool operator!=(cobarray const& x) {
auto [i, j] = std::mismatch(begin(), end(), x.begin());
return i != end();
}
class I {
cobarray* r;
T* p;
public:
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = T&;
using iterator_category = std::random_access_iterator_tag;
I(I const& x) : r(x.r), p(x.p) {}
I& operator=(I& x) {
r = x.r;
p = x.p;
return *this;
}
I& operator=(I&& x) {
r = x.r;
p = x.p;
return *this;
}
I& operator++() {
if (p - r->d < N) {
++p;
}
return *this;
}
I& operator--() {
if (p > r->d) {
--p;
}
return *this;
}
I operator+(difference_type x) const {
if (p + x < r->d + N) {
return { r, p + x };
}
return { r, r->d + N };
}
I operator+=(difference_type x) {
return *this = (*this) + x;
}
I operator-(difference_type x) const {
if (p - x >= r->d) {
return { r, p - x };
}
return { r, r->d + N };
}
I operator-=(difference_type x) {
return *this = (*this) - x;
}
difference_type operator-(I const& x) const {
return p - x.p;
}
T& operator*() const { return *p; }
T* operator->() const { return p; }
bool operator==(I const& x) const { return (r == x.r) && (p == x.p); }
bool operator!=(I const& x) const { return (r != x.r) || (p != x.p); }
bool operator< (I const& x) const { return (r == x.r) && (p < x.p); }
bool operator<=(I const& x) const { return (r == x.r) && (p <= x.p); }
bool operator> (I const& x) const { return (r == x.r) && (p > x.p); }
bool operator>=(I const& x) const { return (r == x.r) && (p >= x.p); }
friend struct cobarray;
protected:
I(cobarray const* r_, T const* p_) : r(const_cast<cobarray*>(r_)), p(const_cast<T*>(p_)) {}
};
class CI {
cobarray const* r;
T const* p;
public:
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = T&;
using iterator_category = std::random_access_iterator_tag;
CI(CI const& x) : r(x.r), p(x.p) {}
CI& operator=(CI& x) {
r = x.r;
p = x.p;
return *this;
}
CI& operator=(CI&& x) {
r = x.r;
p = x.p;
return *this;
}
CI& operator++() {
if (p - r->d < N) {
++p;
}
return *this;
}
CI& operator--() {
if (p > r->d) {
--p;
}
return *this;
}
CI operator+(difference_type x) const {
if (p + x < r->d + N) {
return { r, p + x };
}
return { r, r->d + N };
}
CI operator+=(difference_type x) {
return *this = (*this) + x;
}
CI operator-(difference_type x) const {
if (p - x >= r->d) {
return { r, p - x };
}
return { r, r->d + N };
}
CI operator-=(difference_type x) {
return *this = (*this) - x;
}
difference_type operator-(CI const& x) const {
return p - x.p;
}
T const& operator*() const { return *p; }
T const* operator->() const { return p; }
bool operator==(CI const& x) const { return (r == x.r) && (p == x.p); }
bool operator!=(CI const& x) const { return (r != x.r) || (p != x.p); }
bool operator< (CI const& x) const { return (r == x.r) && (p < x.p); }
bool operator<=(CI const& x) const { return (r == x.r) && (p <= x.p); }
bool operator> (CI const& x) const { return (r == x.r) && (p > x.p); }
bool operator>=(CI const& x) const { return (r == x.r) && (p >= x.p); }
friend struct cobarray;
protected:
CI(cobarray const* r_, T const* p_) : r(r_), p(p_) {}
};
constexpr I begin() { return I{this, &d[0]}; }
constexpr I end() { return I{this, d + N}; }
constexpr CI begin() const { return CI{this, &d[0]}; }
constexpr CI end() const { return CI{this, d + N}; }
constexpr CI cbegin() const { return CI{this, &d[0]}; }
constexpr CI cend() const { return CI{this, d + N}; }
constexpr cobirange<int,0,N-1> irange() { return cobirange<int,0,N-1>{}; }
private:
T d[N+1];
};
#endif // define INC_COBARRAY