LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
ArrayRef.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008, 2009, 2010, 2011 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 #ifndef NDARRAY_ArrayRef_h_INCLUDED
24 #define NDARRAY_ArrayRef_h_INCLUDED
25 
32 #include "ndarray_fwd.h"
33 #include "ndarray/ArrayTraits.h"
34 #include "ndarray/ArrayBaseN.h"
36 #include "ndarray/Vector.h"
37 #include "ndarray/detail/Core.h"
38 #include "ndarray/views.h"
39 
40 namespace ndarray {
41 
45 template <typename T, int N, int C>
46 class ArrayRef : public ArrayBaseN< ArrayRef<T,N,C> > {
48  typedef typename Super::Core Core;
49  typedef typename Super::CorePtr CorePtr;
50 public:
51  typedef typename Super::Iterator Iterator;
52 
56  ArrayRef(ArrayRef const & other) : Super(other._data, other._core) {}
57 
64  template <typename T_, int C_>
65  explicit ArrayRef(
66  Array<T_,N,C_> const & other
67 #ifndef DOXYGEN
68  , typename boost::enable_if<detail::Convertible<N,T_,C_,T,C>,void*>::type=0
69 #endif
70  ) : Super(other._data, other._core) {}
71 
78  template <typename T_, int C_>
80  ArrayRef<T_,N,C_> const & other
81 #ifndef DOXYGEN
82  , typename boost::enable_if<detail::Convertible<N,T_,C_,T,C>,void*>::type=0
83 #endif
84  ) : Super(other._data, other._core) {}
85 
94  ArrayRef const & operator=(Array<T,N,C> const & other) const {
96  NDARRAY_ASSERT(other.getShape() == this->getShape());
97  std::copy(other.begin(), other.end(), this->begin());
98  return *this;
99  }
100 
101  ArrayRef const & operator=(ArrayRef 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 
109  template <typename Other>
110  ArrayRef const &
111  operator =(ExpressionBase<Other> const & expr) const {
112  NDARRAY_ASSERT(expr.getShape()
113  == this->getShape().template first<ExpressionBase<Other>::ND::value>());
114  std::copy(expr.begin(),expr.end(),this->begin());
115  return *this;
116  }
117 
119  template <typename Scalar>
120 #ifndef DOXYGEN
121  typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
122 #else
123  ArrayRef const &
124 #endif
125  operator =(Scalar const & scalar) const {
126  Super::Traits::fill(this->begin(),this->end(),scalar);
127  return *this;
128  }
129 
131  template <typename Other>
132  ArrayRef const &
133  operator +=(ExpressionBase<Other> const & expr) const {
134  NDARRAY_ASSERT(expr.getShape()
135  == this->getShape().template first<ExpressionBase<Other>::ND::value>());
136  Iterator const i_end = this->end();
137  typename Other::Iterator j = expr.begin();
138  for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) += (*j);
139  return *this;
140  }
141 
143  template <typename Scalar>
144 #ifndef DOXYGEN
145  typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
146 #else
147  ArrayRef const &
148 #endif
149  operator +=(Scalar const & scalar) const {
150  Iterator const i_end = this->end();
151  for (Iterator i = this->begin(); i != i_end; ++i) (*i) += scalar;
152  return *this;
153  }
154 
156  template <typename Other>
157  ArrayRef const &
158  operator -=(ExpressionBase<Other> const & expr) const {
159  NDARRAY_ASSERT(expr.getShape()
160  == this->getShape().template first<ExpressionBase<Other>::ND::value>());
161  Iterator const i_end = this->end();
162  typename Other::Iterator j = expr.begin();
163  for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) -= (*j);
164  return *this;
165  }
166 
168  template <typename Scalar>
169 #ifndef DOXYGEN
170  typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
171 #else
172  ArrayRef const &
173 #endif
174  operator -=(Scalar const & scalar) const {
175  Iterator const i_end = this->end();
176  for (Iterator i = this->begin(); i != i_end; ++i) (*i) -= scalar;
177  return *this;
178  }
179 
181  template <typename Other>
182  ArrayRef const &
183  operator *=(ExpressionBase<Other> const & expr) const {
184  NDARRAY_ASSERT(expr.getShape()
185  == this->getShape().template first<ExpressionBase<Other>::ND::value>());
186  Iterator const i_end = this->end();
187  typename Other::Iterator j = expr.begin();
188  for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) *= (*j);
189  return *this;
190  }
191 
193  template <typename Scalar>
194 #ifndef DOXYGEN
195  typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
196 #else
197  ArrayRef const &
198 #endif
199  operator *=(Scalar const & scalar) const {
200  Iterator const i_end = this->end();
201  for (Iterator i = this->begin(); i != i_end; ++i) (*i) *= scalar;
202  return *this;
203  }
204 
206  template <typename Other>
207  ArrayRef const &
208  operator /=(ExpressionBase<Other> const & expr) const {
209  NDARRAY_ASSERT(expr.getShape()
210  == this->getShape().template first<ExpressionBase<Other>::ND::value>());
211  Iterator const i_end = this->end();
212  typename Other::Iterator j = expr.begin();
213  for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) /= (*j);
214  return *this;
215  }
216 
218  template <typename Scalar>
219 #ifndef DOXYGEN
220  typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
221 #else
222  ArrayRef const &
223 #endif
224  operator /=(Scalar const & scalar) const {
225  Iterator const i_end = this->end();
226  for (Iterator i = this->begin(); i != i_end; ++i) (*i) /= scalar;
227  return *this;
228  }
229 
231  template <typename Other>
232  ArrayRef const &
233  operator %=(ExpressionBase<Other> const & expr) const {
234  NDARRAY_ASSERT(expr.getShape()
235  == this->getShape().template first<ExpressionBase<Other>::ND::value>());
236  Iterator const i_end = this->end();
237  typename Other::Iterator j = expr.begin();
238  for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) %= (*j);
239  return *this;
240  }
241 
243  template <typename Scalar>
244 #ifndef DOXYGEN
245  typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
246 #else
247  ArrayRef const &
248 #endif
249  operator %=(Scalar const & scalar) const {
250  Iterator const i_end = this->end();
251  for (Iterator i = this->begin(); i != i_end; ++i) (*i) %= scalar;
252  return *this;
253  }
254 
256  template <typename Other>
257  ArrayRef const &
258  operator ^=(ExpressionBase<Other> const & expr) const {
259  NDARRAY_ASSERT(expr.getShape()
260  == this->getShape().template first<ExpressionBase<Other>::ND::value>());
261  Iterator const i_end = this->end();
262  typename Other::Iterator j = expr.begin();
263  for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) ^= (*j);
264  return *this;
265  }
266 
268  template <typename Scalar>
269 #ifndef DOXYGEN
270  typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
271 #else
272  ArrayRef const &
273 #endif
274  operator ^=(Scalar const & scalar) const {
275  Iterator const i_end = this->end();
276  for (Iterator i = this->begin(); i != i_end; ++i) (*i) ^= scalar;
277  return *this;
278  }
279 
281  template <typename Other>
282  ArrayRef const &
283  operator &=(ExpressionBase<Other> const & expr) const {
284  NDARRAY_ASSERT(expr.getShape()
285  == this->getShape().template first<ExpressionBase<Other>::ND::value>());
286  Iterator const i_end = this->end();
287  typename Other::Iterator j = expr.begin();
288  for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) &= (*j);
289  return *this;
290  }
291 
293  template <typename Scalar>
294 #ifndef DOXYGEN
295  typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
296 #else
297  ArrayRef const &
298 #endif
299  operator &=(Scalar const & scalar) const {
300  Iterator const i_end = this->end();
301  for (Iterator i = this->begin(); i != i_end; ++i) (*i) &= scalar;
302  return *this;
303  }
304 
306  template <typename Other>
307  ArrayRef const &
308  operator |=(ExpressionBase<Other> const & expr) const {
309  NDARRAY_ASSERT(expr.getShape()
310  == this->getShape().template first<ExpressionBase<Other>::ND::value>());
311  Iterator const i_end = this->end();
312  typename Other::Iterator j = expr.begin();
313  for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) |= (*j);
314  return *this;
315  }
316 
318  template <typename Scalar>
319 #ifndef DOXYGEN
320  typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
321 #else
322  ArrayRef const &
323 #endif
324  operator |=(Scalar const & scalar) const {
325  Iterator const i_end = this->end();
326  for (Iterator i = this->begin(); i != i_end; ++i) (*i) |= scalar;
327  return *this;
328  }
329 
331  template <typename Other>
332  ArrayRef const &
333  operator <<=(ExpressionBase<Other> const & expr) const {
334  NDARRAY_ASSERT(expr.getShape()
335  == this->getShape().template first<ExpressionBase<Other>::ND::value>());
336  Iterator const i_end = this->end();
337  typename Other::Iterator j = expr.begin();
338  for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) <<= (*j);
339  return *this;
340  }
341 
343  template <typename Scalar>
344 #ifndef DOXYGEN
345  typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
346 #else
347  ArrayRef const &
348 #endif
349  operator <<=(Scalar const & scalar) const {
350  Iterator const i_end = this->end();
351  for (Iterator i = this->begin(); i != i_end; ++i) (*i) <<= scalar;
352  return *this;
353  }
354 
356  template <typename Other>
357  ArrayRef const &
358  operator >>=(ExpressionBase<Other> const & expr) const {
359  NDARRAY_ASSERT(expr.getShape()
360  == this->getShape().template first<ExpressionBase<Other>::ND::value>());
361  Iterator const i_end = this->end();
362  typename Other::Iterator j = expr.begin();
363  for (Iterator i = this->begin(); i != i_end; ++i, ++j) (*i) >>= (*j);
364  return *this;
365  }
366 
368  template <typename Scalar>
369 #ifndef DOXYGEN
370  typename boost::enable_if<boost::is_convertible<Scalar,T>, ArrayRef const &>::type
371 #else
372  ArrayRef const &
373 #endif
374  operator >>=(Scalar const & scalar) const {
375  Iterator const i_end = this->end();
376  for (Iterator i = this->begin(); i != i_end; ++i) (*i) >>= scalar;
377  return *this;
378  }
380 
381 private:
382  template <typename T_, int N_, int C_> friend class Array;
383  template <typename T_, int N_, int C_> friend class ArrayRef;
384  template <typename T_, int N_, int C_> friend struct ArrayTraits;
385  template <typename Derived> friend class ArrayBase;
386  template <typename Array_> friend class detail::ArrayAccess;
387 
389  ArrayRef(T * data, CorePtr const & core) : Super(data, core) {}
390 
391 };
392 
393 } // namespace ndarray
394 
395 #endif // !NDARRAY_ArrayRef_h_INCLUDED
A proxy class for Array with deep assignment operators.
Definition: ArrayRef.h:46
ArrayRef(ArrayRef const &other)
Non-converting copy constructor.
Definition: ArrayRef.h:56
ArrayRef const & operator=(Array< T, N, C > const &other) const
= assignment of arrays and array expressions.
Definition: ArrayRef.h:95
Dimension-specialized traits shared by Array and ArrayRef.
Definition: ArrayTraits.h:56
ArrayRef const & operator%=(ExpressionBase< Other > const &expr) const
%= assignment of arrays and array expressions.
Definition: ArrayRef.h:233
Definition for Vector.
Index getShape() const
Return a Vector of the sizes of all dimensions.
ArrayRef const & operator|=(ExpressionBase< Other > const &expr) const
|= assignment of arrays and array expressions.
Definition: ArrayRef.h:308
Traits::Iterator Iterator
Nested array or element iterator.
Definition: ArrayBase.h:65
Traits for Array.
ArrayRef const & operator/=(ExpressionBase< Other > const &expr) const
/= assignment of arrays and array expressions.
Definition: ArrayRef.h:208
SelectEigenView< T >::Type copy(Eigen::EigenBase< T > const &other)
Copy an arbitrary Eigen expression into a new EigenView.
Definition: eigen.h:390
ArrayRef const & operator&=(ExpressionBase< Other > const &expr) const
&amp;= assignment of arrays and array expressions.
Definition: ArrayRef.h:283
ArrayBaseN< ArrayRef > Super
Definition: ArrayRef.h:47
Forward declarations and default template parameters for ndarray.
ArrayRef const & operator=(ArrayRef const &other) const
= assignment of arrays and array expressions.
Definition: ArrayRef.h:101
#define NDARRAY_ASSERT(ARG)
Definition: ndarray_fwd.h:51
Super::Core Core
Definition: ArrayRef.h:48
Index getShape() const
Return a Vector of the sizes of all dimensions.
Definition: ArrayBase.h:136
Definitions for ArrayAccess.
ArrayRef const & operator-=(ExpressionBase< Other > const &expr) const
-= assignment of arrays and array expressions.
Definition: ArrayRef.h:158
Super::CorePtr CorePtr
Definition: ArrayBaseN.h:44
An intermediate CRTP base class for Array and ArrayRef.
Definition: ArrayBaseN.h:40
ArrayRef const & operator^=(ExpressionBase< Other > const &expr) const
^= assignment of arrays and array expressions.
Definition: ArrayRef.h:258
ArrayRef(ArrayRef< T_, N, C_ > const &other)
Converting copy constructor.
Definition: ArrayRef.h:79
ArrayRef(Array< T_, N, C_ > const &other)
Converting copy constructor.
Definition: ArrayRef.h:65
Super::Iterator Iterator
Definition: ArrayRef.h:51
CRTP implementation for Array and ArrayRef.
Definition: ArrayBase.h:56
ArrayRef const & operator+=(ExpressionBase< Other > const &expr) const
+= assignment of arrays and array expressions.
Definition: ArrayRef.h:133
Iterator begin() const
Return an Iterator to the beginning of the expression.
A multidimensional strided array.
Definition: Array.h:47
Iterator end() const
Return an Iterator to one past the end of the array.
Definition: ArrayBase.h:108
Iterator end() const
Return an Iterator to one past the end of the expression.
ArrayRef const & operator<<=(ExpressionBase< Other > const &expr) const
&lt;&lt;= assignment of arrays and array expressions.
Definition: ArrayRef.h:333
ArrayRef const & operator>>=(ExpressionBase< Other > const &expr) const
&gt;&gt;= assignment of arrays and array expressions.
Definition: ArrayRef.h:358
Definitions for Core.
Super::CorePtr CorePtr
Definition: ArrayRef.h:49
ArrayRef(T *data, CorePtr const &core)
Definition: ArrayRef.h:389
Public interface for arbitrary views into arrays.
Definition of ArrayBaseN, a dimension-specialized CRTP base class for Array and ArrayRef.
ArrayRef const & operator*=(ExpressionBase< Other > const &expr) const
*= assignment of arrays and array expressions.
Definition: ArrayRef.h:183
CRTP base class for all multidimensional expressions.
Iterator begin() const
Return an Iterator to the beginning of the array.
Definition: ArrayBase.h:99
Super::Core Core
Definition: ArrayBaseN.h:43