ndarray
NumPy-friendly multidimensional arrays in C++
Loading...
Searching...
No Matches
ArrayRef.h
Go to the documentation of this file.
1// -*- c++ -*-
2/*
3 * Copyright (c) 2010-2012, Jim Bosch
4 * All rights reserved.
5 *
6 * ndarray is distributed under a simple BSD-like license;
7 * see the LICENSE file that should be present in the root
8 * of the source distribution, or alternately available at:
9 * https://github.com/ndarray/ndarray
10 */
11#ifndef NDARRAY_ArrayRef_h_INCLUDED
12#define NDARRAY_ArrayRef_h_INCLUDED
13
20#include "ndarray_fwd.h"
21#include "ndarray/ArrayTraits.h"
22#include "ndarray/ArrayBaseN.h"
24#include "ndarray/Vector.h"
25#include "ndarray/detail/Core.h"
26#include "ndarray/views.h"
27
28namespace ndarray {
29
33template <typename T, int N, int C>
34class ArrayRef : public ArrayBaseN< ArrayRef<T,N,C> > {
36 typedef typename Super::Core Core;
37 typedef typename Super::CorePtr CorePtr;
38public:
39 typedef typename Super::Iterator Iterator;
40
49 explicit ArrayRef(Size n1, Size n2=1, Size n3=1, Size n4=1, Size n5=1, Size n6=1, Size n7=1, Size n8=1);
50
56 template <typename U>
57 explicit ArrayRef(Vector<U,N> const & shape);
58
62 ArrayRef(ArrayRef const & other) : Super(other._data, other._core) {}
63
70 template <typename T_, int C_>
71 explicit ArrayRef(
72 Array<T_,N,C_> const & other
73#ifndef DOXYGEN
74 , typename boost::enable_if<detail::Convertible<N,T_,C_,T,C>,void*>::type=0
75#endif
76 ) : Super(other._data, other._core) {}
77
84 template <typename T_, int C_>
86 ArrayRef<T_,N,C_> const & other
87#ifndef DOXYGEN
88 , typename boost::enable_if<detail::Convertible<N,T_,C_,T,C>,void*>::type=0
89#endif
90 ) : Super(other._data, other._core) {}
91
101 ArrayRef const & operator=(Array<T,N,C> const & other) const {
102 NDARRAY_ASSERT(other.getShape() == this->getShape());
103 std::copy(other.begin(), other.end(), this->begin());
104 return *this;
105 }
106
107 ArrayRef const & operator=(ArrayRef const & other) const {
108 NDARRAY_ASSERT(other.getShape() == this->getShape());
109 std::copy(other.begin(), other.end(), this->begin());
110 return *this;
111 }
112
113
115 template <typename Other>
116 ArrayRef const &
117 operator =(ExpressionBase<Other> const & expr) const {
118 NDARRAY_ASSERT(expr.getShape()
119 == this->getShape().template first<ExpressionBase<Other>::ND::value>());
120 std::copy(expr.begin(),expr.end(),this->begin());
121 return *this;
122 }
123
125 template <typename Scalar>
126#ifndef DOXYGEN
127 typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
128#else
129 ArrayRef const &
130#endif
131 operator =(Scalar const & scalar) const {
132 Super::Traits::fill(this->begin(),this->end(),scalar);
133 return *this;
134 }
135
137 template <typename Other>
138 ArrayRef const &
139 operator +=(ExpressionBase<Other> const & expr) const {
140 NDARRAY_ASSERT(expr.getShape()
141 == this->getShape().template first<ExpressionBase<Other>::ND::value>());
142 Iterator const i_end = this->end();
143 typename Other::Iterator j = expr.begin();
144 for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) += (*j);
145 return *this;
146 }
147
149 template <typename Scalar>
150#ifndef DOXYGEN
151 typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
152#else
153 ArrayRef const &
154#endif
155 operator +=(Scalar const & scalar) const {
156 Iterator const i_end = this->end();
157 for (Iterator i = this->begin(); i != i_end; ++i) (*i) += scalar;
158 return *this;
159 }
160
162 template <typename Other>
163 ArrayRef const &
164 operator -=(ExpressionBase<Other> const & expr) const {
165 NDARRAY_ASSERT(expr.getShape()
166 == this->getShape().template first<ExpressionBase<Other>::ND::value>());
167 Iterator const i_end = this->end();
168 typename Other::Iterator j = expr.begin();
169 for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) -= (*j);
170 return *this;
171 }
172
174 template <typename Scalar>
175#ifndef DOXYGEN
176 typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
177#else
178 ArrayRef const &
179#endif
180 operator -=(Scalar const & scalar) const {
181 Iterator const i_end = this->end();
182 for (Iterator i = this->begin(); i != i_end; ++i) (*i) -= scalar;
183 return *this;
184 }
185
187 template <typename Other>
188 ArrayRef const &
189 operator *=(ExpressionBase<Other> const & expr) const {
190 NDARRAY_ASSERT(expr.getShape()
191 == this->getShape().template first<ExpressionBase<Other>::ND::value>());
192 Iterator const i_end = this->end();
193 typename Other::Iterator j = expr.begin();
194 for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) *= (*j);
195 return *this;
196 }
197
199 template <typename Scalar>
200#ifndef DOXYGEN
201 typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
202#else
203 ArrayRef const &
204#endif
205 operator *=(Scalar const & scalar) const {
206 Iterator const i_end = this->end();
207 for (Iterator i = this->begin(); i != i_end; ++i) (*i) *= scalar;
208 return *this;
209 }
210
212 template <typename Other>
213 ArrayRef const &
214 operator /=(ExpressionBase<Other> const & expr) const {
215 NDARRAY_ASSERT(expr.getShape()
216 == this->getShape().template first<ExpressionBase<Other>::ND::value>());
217 Iterator const i_end = this->end();
218 typename Other::Iterator j = expr.begin();
219 for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) /= (*j);
220 return *this;
221 }
222
224 template <typename Scalar>
225#ifndef DOXYGEN
226 typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
227#else
228 ArrayRef const &
229#endif
230 operator /=(Scalar const & scalar) const {
231 Iterator const i_end = this->end();
232 for (Iterator i = this->begin(); i != i_end; ++i) (*i) /= scalar;
233 return *this;
234 }
235
237 template <typename Other>
238 ArrayRef const &
239 operator %=(ExpressionBase<Other> const & expr) const {
240 NDARRAY_ASSERT(expr.getShape()
241 == this->getShape().template first<ExpressionBase<Other>::ND::value>());
242 Iterator const i_end = this->end();
243 typename Other::Iterator j = expr.begin();
244 for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) %= (*j);
245 return *this;
246 }
247
249 template <typename Scalar>
250#ifndef DOXYGEN
251 typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
252#else
253 ArrayRef const &
254#endif
255 operator %=(Scalar const & scalar) const {
256 Iterator const i_end = this->end();
257 for (Iterator i = this->begin(); i != i_end; ++i) (*i) %= scalar;
258 return *this;
259 }
260
262 template <typename Other>
263 ArrayRef const &
264 operator ^=(ExpressionBase<Other> const & expr) const {
265 NDARRAY_ASSERT(expr.getShape()
266 == this->getShape().template first<ExpressionBase<Other>::ND::value>());
267 Iterator const i_end = this->end();
268 typename Other::Iterator j = expr.begin();
269 for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) ^= (*j);
270 return *this;
271 }
272
274 template <typename Scalar>
275#ifndef DOXYGEN
276 typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
277#else
278 ArrayRef const &
279#endif
280 operator ^=(Scalar const & scalar) const {
281 Iterator const i_end = this->end();
282 for (Iterator i = this->begin(); i != i_end; ++i) (*i) ^= scalar;
283 return *this;
284 }
285
287 template <typename Other>
288 ArrayRef const &
289 operator &=(ExpressionBase<Other> const & expr) const {
290 NDARRAY_ASSERT(expr.getShape()
291 == this->getShape().template first<ExpressionBase<Other>::ND::value>());
292 Iterator const i_end = this->end();
293 typename Other::Iterator j = expr.begin();
294 for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) &= (*j);
295 return *this;
296 }
297
299 template <typename Scalar>
300#ifndef DOXYGEN
301 typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
302#else
303 ArrayRef const &
304#endif
305 operator &=(Scalar const & scalar) const {
306 Iterator const i_end = this->end();
307 for (Iterator i = this->begin(); i != i_end; ++i) (*i) &= scalar;
308 return *this;
309 }
310
312 template <typename Other>
313 ArrayRef const &
314 operator |=(ExpressionBase<Other> const & expr) const {
315 NDARRAY_ASSERT(expr.getShape()
316 == this->getShape().template first<ExpressionBase<Other>::ND::value>());
317 Iterator const i_end = this->end();
318 typename Other::Iterator j = expr.begin();
319 for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) |= (*j);
320 return *this;
321 }
322
324 template <typename Scalar>
325#ifndef DOXYGEN
326 typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
327#else
328 ArrayRef const &
329#endif
330 operator |=(Scalar const & scalar) const {
331 Iterator const i_end = this->end();
332 for (Iterator i = this->begin(); i != i_end; ++i) (*i) |= scalar;
333 return *this;
334 }
335
337 template <typename Other>
338 ArrayRef const &
339 operator <<=(ExpressionBase<Other> const & expr) const {
340 NDARRAY_ASSERT(expr.getShape()
341 == this->getShape().template first<ExpressionBase<Other>::ND::value>());
342 Iterator const i_end = this->end();
343 typename Other::Iterator j = expr.begin();
344 for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) <<= (*j);
345 return *this;
346 }
347
349 template <typename Scalar>
350#ifndef DOXYGEN
351 typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
352#else
353 ArrayRef const &
354#endif
355 operator <<=(Scalar const & scalar) const {
356 Iterator const i_end = this->end();
357 for (Iterator i = this->begin(); i != i_end; ++i) (*i) <<= scalar;
358 return *this;
359 }
360
362 template <typename Other>
363 ArrayRef const &
365 NDARRAY_ASSERT(expr.getShape()
366 == this->getShape().template first<ExpressionBase<Other>::ND::value>());
367 Iterator const i_end = this->end();
368 typename Other::Iterator j = expr.begin();
369 for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) >>= (*j);
370 return *this;
371 }
372
374 template <typename Scalar>
375#ifndef DOXYGEN
376 typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
377#else
378 ArrayRef const &
379#endif
380 operator >>=(Scalar const & scalar) const {
381 Iterator const i_end = this->end();
382 for (Iterator i = this->begin(); i != i_end; ++i) (*i) >>= scalar;
383 return *this;
384 }
386
387private:
388 template <typename T_, int N_, int C_> friend class Array;
389 template <typename T_, int N_, int C_> friend class ArrayRef;
390 template <typename T_, int N_, int C_> friend struct ArrayTraits;
391 template <typename Derived> friend class ArrayBase;
392 template <typename Array_> friend class detail::ArrayAccess;
393
395 ArrayRef(T * data, CorePtr const & core) : Super(data, core) {}
396
397};
398
399} // namespace ndarray
400
401#endif // !NDARRAY_ArrayRef_h_INCLUDED
Definitions for ArrayAccess.
Definition of ArrayBaseN, a dimension-specialized CRTP base class for Array and ArrayRef.
Traits for Array.
Definitions for Core.
Definition for Vector.
An intermediate CRTP base class for Array and ArrayRef.
Definition ArrayBaseN.h:29
CRTP implementation for Array and ArrayRef.
Definition ArrayBase.h:43
Traits::Iterator Iterator
Nested array or element iterator.
Definition ArrayBase.h:52
Iterator end() const
Return an Iterator to one past the end of the array.
Definition ArrayBase.h:109
Iterator begin() const
Return an Iterator to the beginning of the array.
Definition ArrayBase.h:96
Index getShape() const
Return a Vector of the sizes of all dimensions.
Definition ArrayBase.h:149
A proxy class for Array with deep assignment operators.
Definition ArrayRef.h:34
ArrayRef const & operator|=(ExpressionBase< Other > const &expr) const
|= assignment of arrays and array expressions.
Definition ArrayRef.h:314
ArrayRef const & operator^=(ExpressionBase< Other > const &expr) const
^= assignment of arrays and array expressions.
Definition ArrayRef.h:264
ArrayRef const & operator*=(ExpressionBase< Other > const &expr) const
*= assignment of arrays and array expressions.
Definition ArrayRef.h:189
ArrayRef const & operator&=(ExpressionBase< Other > const &expr) const
&= assignment of arrays and array expressions.
Definition ArrayRef.h:289
ArrayRef const & operator%=(ExpressionBase< Other > const &expr) const
%= assignment of arrays and array expressions.
Definition ArrayRef.h:239
ArrayRef const & operator/=(ExpressionBase< Other > const &expr) const
/= assignment of arrays and array expressions.
Definition ArrayRef.h:214
ArrayRef const & operator-=(ExpressionBase< Other > const &expr) const
-= assignment of arrays and array expressions.
Definition ArrayRef.h:164
ArrayRef const & operator+=(ExpressionBase< Other > const &expr) const
+= assignment of arrays and array expressions.
Definition ArrayRef.h:139
ArrayRef(Array< T_, N, C_ > const &other, typename boost::enable_if< detail::Convertible< N, T_, C_, T, C >, void * >::type=0)
Converting copy constructor.
Definition ArrayRef.h:71
ArrayRef const & operator>>=(ExpressionBase< Other > const &expr) const
>>= assignment of arrays and array expressions.
Definition ArrayRef.h:364
ArrayRef const & operator<<=(ExpressionBase< Other > const &expr) const
<<= assignment of arrays and array expressions.
Definition ArrayRef.h:339
ArrayRef(ArrayRef< T_, N, C_ > const &other, typename boost::enable_if< detail::Convertible< N, T_, C_, T, C >, void * >::type=0)
Converting copy constructor.
Definition ArrayRef.h:85
ArrayRef(ArrayRef const &other)
Non-converting copy constructor.
Definition ArrayRef.h:62
A multidimensional strided array.
Definition Array.h:35
CRTP base class for all multidimensional expressions.
Definition ExpressionBase.h:40
Index getShape() const
Return a Vector of the sizes of all dimensions.
Definition ExpressionBase.h:76
Iterator end() const
Return an Iterator to one past the end of the expression.
Definition ExpressionBase.h:70
Iterator begin() const
Return an Iterator to the beginning of the expression.
Definition ExpressionBase.h:67
Definition ArrayAccess.h:26
Forward declarations and default template parameters for ndarray.
Dimension-specialized traits shared by Array and ArrayRef.
Definition ArrayTraits.h:44
A fixed-size 1D array class.
Definition Vector.h:82
Definition ArrayTraits.h:33
Public interface for arbitrary views into arrays.