forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxaccessible.hpp
349 lines (295 loc) · 11.5 KB
/
xaccessible.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef XTENSOR_ACCESSIBLE_HPP
#define XTENSOR_ACCESSIBLE_HPP
#include "xexception.hpp"
#include "xstrides.hpp"
#include "xtensor_forward.hpp"
namespace xt
{
/**
* @class xconst_accessible
* @brief Base class for implementation of common expression constant access methods.
*
* The xaccessible class implements constant access methods common to all expressions.
*
* @tparam D The derived type, i.e. the inheriting class for which xconst_accessible
* provides the interface.
*/
template <class D>
class xconst_accessible
{
public:
using derived_type = D;
using inner_types = xcontainer_inner_types<D>;
using reference = typename inner_types::reference;
using const_reference = typename inner_types::const_reference;
using size_type = typename inner_types::size_type;
size_type size() const noexcept;
size_type dimension() const noexcept;
size_type shape(size_type index) const;
template <class... Args>
const_reference at(Args... args) const;
template <class S>
disable_integral_t<S, const_reference> operator[](const S& index) const;
template <class I>
const_reference operator[](std::initializer_list<I> index) const;
const_reference operator[](size_type i) const;
template <class... Args>
const_reference periodic(Args... args) const;
template <class... Args>
bool in_bounds(Args... args) const;
const_reference front() const;
const_reference back() const;
protected:
xconst_accessible() = default;
~xconst_accessible() = default;
xconst_accessible(const xconst_accessible&) = default;
xconst_accessible& operator=(const xconst_accessible&) = default;
xconst_accessible(xconst_accessible&&) = default;
xconst_accessible& operator=(xconst_accessible&&) = default;
private:
const derived_type& derived_cast() const noexcept;
};
/**
* @class xaccessible
* @brief Base class for implementation of common expression access methods.
*
* The xaccessible class implements access methods common to all expressions.
*
* @tparam D The derived type, i.e. the inheriting class for which xaccessible
* provides the interface.
*/
template <class D>
class xaccessible : public xconst_accessible<D>
{
public:
using base_type = xconst_accessible<D>;
using derived_type = typename base_type::derived_type;
using reference = typename base_type::reference;
using size_type = typename base_type::size_type;
template <class... Args>
reference at(Args... args);
template <class S>
disable_integral_t<S, reference> operator[](const S& index);
template <class I>
reference operator[](std::initializer_list<I> index);
reference operator[](size_type i);
template <class... Args>
reference periodic(Args... args);
reference front();
reference back();
using base_type::at;
using base_type::operator[];
using base_type::periodic;
using base_type::front;
using base_type::back;
protected:
xaccessible() = default;
~xaccessible() = default;
xaccessible(const xaccessible&) = default;
xaccessible& operator=(const xaccessible&) = default;
xaccessible(xaccessible&&) = default;
xaccessible& operator=(xaccessible&&) = default;
private:
derived_type& derived_cast() noexcept;
};
/************************************
* xconst_accessible implementation *
************************************/
/**
* Returns the size of the expression.
*/
template <class D>
inline auto xconst_accessible<D>::size() const noexcept -> size_type
{
return compute_size(derived_cast().shape());
}
/**
* Returns the number of dimensions of the expression.
*/
template <class D>
inline auto xconst_accessible<D>::dimension() const noexcept -> size_type
{
return derived_cast().shape().size();
}
/**
* Returns the i-th dimension of the expression.
*/
template <class D>
inline auto xconst_accessible<D>::shape(size_type index) const -> size_type
{
return derived_cast().shape()[index];
}
/**
* Returns a constant reference to the element at the specified position in the expression,
* after dimension and bounds checking.
* @param args a list of indices specifying the position in the expression. Indices
* must be unsigned integers, the number of indices should be equal to the number of dimensions
* of the expression.
* @exception std::out_of_range if the number of argument is greater than the number of dimensions
* or if indices are out of bounds.
*/
template <class D>
template <class... Args>
inline auto xconst_accessible<D>::at(Args... args) const -> const_reference
{
check_access(derived_cast().shape(), static_cast<size_type>(args)...);
return derived_cast().operator()(args...);
}
/**
* Returns a constant reference to the element at the specified position in the expression.
* @param index a sequence of indices specifying the position in the expression. Indices
* must be unsigned integers, the number of indices in the list should be equal or greater
* than the number of dimensions of the expression.
*/
template <class D>
template <class S>
inline auto xconst_accessible<D>::operator[](const S& index) const
-> disable_integral_t<S, const_reference>
{
return derived_cast().element(index.cbegin(), index.cend());
}
template <class D>
template <class I>
inline auto xconst_accessible<D>::operator[](std::initializer_list<I> index) const -> const_reference
{
return derived_cast().element(index.begin(), index.end());
}
template <class D>
inline auto xconst_accessible<D>::operator[](size_type i) const -> const_reference
{
return derived_cast().operator()(i);
}
/**
* Returns a constant reference to the element at the specified position in the expression,
* after applying periodicity to the indices (negative and 'overflowing' indices are changed).
* @param args a list of indices specifying the position in the expression. Indices
* must be integers, the number of indices should be equal to the number of dimensions
* of the expression.
*/
template <class D>
template <class... Args>
inline auto xconst_accessible<D>::periodic(Args... args) const -> const_reference
{
normalize_periodic(derived_cast().shape(), args...);
return derived_cast()(static_cast<size_type>(args)...);
}
/**
* Returns a constant reference to first the element of the expression
*/
template <class D>
inline auto xconst_accessible<D>::front() const -> const_reference
{
return *derived_cast().begin();
}
/**
* Returns a constant reference to last the element of the expression
*/
template <class D>
inline auto xconst_accessible<D>::back() const -> const_reference
{
return *std::prev(derived_cast().end());
}
/**
* Returns ``true`` only if the the specified position is a valid entry in the expression.
* @param args a list of indices specifying the position in the expression.
* @return bool
*/
template <class D>
template <class... Args>
inline bool xconst_accessible<D>::in_bounds(Args... args) const
{
return check_in_bounds(derived_cast().shape(), args...);
}
template <class D>
inline auto xconst_accessible<D>::derived_cast() const noexcept -> const derived_type&
{
return *static_cast<const derived_type*>(this);
}
/******************************
* xaccessible implementation *
******************************/
/**
* Returns a reference to the element at the specified position in the expression,
* after dimension and bounds checking.
* @param args a list of indices specifying the position in the expression. Indices
* must be unsigned integers, the number of indices should be equal to the number of dimensions
* of the expression.
* @exception std::out_of_range if the number of argument is greater than the number of dimensions
* or if indices are out of bounds.
*/
template <class D>
template <class... Args>
inline auto xaccessible<D>::at(Args... args) -> reference
{
check_access(derived_cast().shape(), static_cast<size_type>(args)...);
return derived_cast().operator()(args...);
}
/**
* Returns a reference to the element at the specified position in the expression.
* @param index a sequence of indices specifying the position in the expression. Indices
* must be unsigned integers, the number of indices in the list should be equal or greater
* than the number of dimensions of the expression.
*/
template <class D>
template <class S>
inline auto xaccessible<D>::operator[](const S& index)
-> disable_integral_t<S, reference>
{
return derived_cast().element(index.cbegin(), index.cend());
}
template <class D>
template <class I>
inline auto xaccessible<D>::operator[](std::initializer_list<I> index) -> reference
{
return derived_cast().element(index.begin(), index.end());
}
template <class D>
inline auto xaccessible<D>::operator[](size_type i) -> reference
{
return derived_cast().operator()(i);
}
/**
* Returns a reference to the element at the specified position in the expression,
* after applying periodicity to the indices (negative and 'overflowing' indices are changed).
* @param args a list of indices specifying the position in the expression. Indices
* must be integers, the number of indices should be equal to the number of dimensions
* of the expression.
*/
template <class D>
template <class... Args>
inline auto xaccessible<D>::periodic(Args... args) -> reference
{
normalize_periodic(derived_cast().shape(), args...);
return derived_cast()(static_cast<size_type>(args)...);
}
/**
* Returns a reference to the first element of the expression.
*/
template <class D>
inline auto xaccessible<D>::front() -> reference
{
return *derived_cast().begin();
}
/**
* Returns a reference to the last element of the expression.
*/
template <class D>
inline auto xaccessible<D>::back() -> reference
{
return *std::prev(derived_cast().end());
}
template <class D>
inline auto xaccessible<D>::derived_cast() noexcept -> derived_type&
{
return *static_cast<derived_type*>(this);
}
}
#endif