xtensor
Loading...
Searching...
No Matches
xaccessible.hpp
1/***************************************************************************
2 * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
3 * Copyright (c) QuantStack *
4 * *
5 * Distributed under the terms of the BSD 3-Clause License. *
6 * *
7 * The full license is in the file LICENSE, distributed with this software. *
8 ****************************************************************************/
9
10#ifndef XTENSOR_ACCESSIBLE_HPP
11#define XTENSOR_ACCESSIBLE_HPP
12
13#include "xexception.hpp"
14#include "xstrides.hpp"
15#include "xtensor_forward.hpp"
16
17namespace xt
18{
28 template <class D>
30 {
31 public:
32
33 using derived_type = D;
35 using reference = typename inner_types::reference;
36 using const_reference = typename inner_types::const_reference;
37 using size_type = typename inner_types::size_type;
38
39 size_type size() const noexcept;
40 size_type dimension() const noexcept;
41 size_type shape(size_type index) const;
42
43 template <class... Args>
44 const_reference at(Args... args) const;
45
46 template <class S>
47 disable_integral_t<S, const_reference> operator[](const S& index) const;
48 template <class I>
49 const_reference operator[](std::initializer_list<I> index) const;
50 const_reference operator[](size_type i) const;
51
52 template <class... Args>
53 const_reference periodic(Args... args) const;
54
55 template <class... Args>
56 bool in_bounds(Args... args) const;
57
58 const_reference front() const;
59 const_reference back() const;
60
61 protected:
62
63 xconst_accessible() = default;
64 ~xconst_accessible() = default;
65
66 xconst_accessible(const xconst_accessible&) = default;
67 xconst_accessible& operator=(const xconst_accessible&) = default;
68
70 xconst_accessible& operator=(xconst_accessible&&) = default;
71
72 private:
73
74 const derived_type& derived_cast() const noexcept;
75 };
76
86 template <class D>
88 {
89 public:
90
92 using derived_type = typename base_type::derived_type;
93 using reference = typename base_type::reference;
94 using size_type = typename base_type::size_type;
95
96 template <class... Args>
97 reference at(Args... args);
98
99 template <class S>
100 disable_integral_t<S, reference> operator[](const S& index);
101 template <class I>
102 reference operator[](std::initializer_list<I> index);
103 reference operator[](size_type i);
104
105 template <class... Args>
106 reference periodic(Args... args);
107
108 reference front();
109 reference back();
110
111 using base_type::at;
112 using base_type::operator[];
113 using base_type::back;
114 using base_type::front;
115 using base_type::periodic;
116
117 protected:
118
119 xaccessible() = default;
120 ~xaccessible() = default;
121
122 xaccessible(const xaccessible&) = default;
123 xaccessible& operator=(const xaccessible&) = default;
124
125 xaccessible(xaccessible&&) = default;
126 xaccessible& operator=(xaccessible&&) = default;
127
128 private:
129
130 derived_type& derived_cast() noexcept;
131 };
132
133 /************************************
134 * xconst_accessible implementation *
135 ************************************/
136
140 template <class D>
141 inline auto xconst_accessible<D>::size() const noexcept -> size_type
142 {
143 return compute_size(derived_cast().shape());
145
149 template <class D>
150 inline auto xconst_accessible<D>::dimension() const noexcept -> size_type
151 {
152 return derived_cast().shape().size();
153 }
154
158 template <class D>
159 inline auto xconst_accessible<D>::shape(size_type index) const -> size_type
160 {
161 return derived_cast().shape()[index];
162 }
163
173 template <class D>
174 template <class... Args>
175 inline auto xconst_accessible<D>::at(Args... args) const -> const_reference
176 {
177 check_access(derived_cast().shape(), args...);
178 return derived_cast().operator()(args...);
179 }
180
187 template <class D>
188 template <class S>
189 inline auto xconst_accessible<D>::operator[](const S& index) const
190 -> disable_integral_t<S, const_reference>
191 {
192 return derived_cast().element(index.cbegin(), index.cend());
193 }
194
195 template <class D>
196 template <class I>
197 inline auto xconst_accessible<D>::operator[](std::initializer_list<I> index) const -> const_reference
198 {
199 return derived_cast().element(index.begin(), index.end());
201
202 template <class D>
203 inline auto xconst_accessible<D>::operator[](size_type i) const -> const_reference
205 return derived_cast().operator()(i);
206 }
207
215 template <class D>
216 template <class... Args>
217 inline auto xconst_accessible<D>::periodic(Args... args) const -> const_reference
218 {
219 normalize_periodic(derived_cast().shape(), args...);
220 return derived_cast()(static_cast<size_type>(args)...);
221 }
222
226 template <class D>
227 inline auto xconst_accessible<D>::front() const -> const_reference
228 {
229 return *derived_cast().begin();
230 }
231
235 template <class D>
236 inline auto xconst_accessible<D>::back() const -> const_reference
237 {
238 return *std::prev(derived_cast().end());
239 }
240
246 template <class D>
247 template <class... Args>
248 inline bool xconst_accessible<D>::in_bounds(Args... args) const
249 {
250 return check_in_bounds(derived_cast().shape(), args...);
251 }
252
253 template <class D>
254 inline auto xconst_accessible<D>::derived_cast() const noexcept -> const derived_type&
255 {
256 return *static_cast<const derived_type*>(this);
257 }
258
259 /******************************
260 * xaccessible implementation *
261 ******************************/
262
272 template <class D>
273 template <class... Args>
274 inline auto xaccessible<D>::at(Args... args) -> reference
275 {
276 check_access(derived_cast().shape(), args...);
277 return derived_cast().operator()(args...);
278 }
279
286 template <class D>
287 template <class S>
288 inline auto xaccessible<D>::operator[](const S& index) -> disable_integral_t<S, reference>
289 {
290 return derived_cast().element(index.cbegin(), index.cend());
291 }
292
293 template <class D>
294 template <class I>
295 inline auto xaccessible<D>::operator[](std::initializer_list<I> index) -> reference
296 {
297 return derived_cast().element(index.begin(), index.end());
298 }
299
300 template <class D>
301 inline auto xaccessible<D>::operator[](size_type i) -> reference
302 {
303 return derived_cast().operator()(i);
304 }
305
313 template <class D>
314 template <class... Args>
315 inline auto xaccessible<D>::periodic(Args... args) -> reference
316 {
317 normalize_periodic(derived_cast().shape(), args...);
318 return derived_cast()(args...);
319 }
320
324 template <class D>
325 inline auto xaccessible<D>::front() -> reference
326 {
327 return *derived_cast().begin();
328 }
329
333 template <class D>
334 inline auto xaccessible<D>::back() -> reference
335 {
336 return *std::prev(derived_cast().end());
337 }
338
339 template <class D>
340 inline auto xaccessible<D>::derived_cast() noexcept -> derived_type&
341 {
342 return *static_cast<derived_type*>(this);
343 }
344
345}
346
347#endif
Base class for implementation of common expression access methods.
reference back()
Returns a reference to the last element of the expression.
reference front()
Returns a reference to the first element of the expression.
Base class for implementation of common expression constant access methods.
const_reference front() const
Returns a constant reference to first the element of the expression.
size_type size() const noexcept
Returns the size of the expression.
size_type dimension() const noexcept
Returns the number of dimensions of the expression.
bool in_bounds(Args... args) const
Returns true only if the the specified position is a valid entry in the expression.
const_reference back() const
Returns a constant reference to last the element of the expression.
size_type shape(size_type index) const
Returns the i-th dimension of the expression.
void normalize_periodic(const S &shape, Args &... args)
Normalise an index of a periodic array.
Definition xstrides.hpp:909
standard mathematical functions for xexpressions