LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
ArrayBase.h
Go to the documentation of this file.
1 // -*- 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_ArrayBase_h_INCLUDED
24 #define NDARRAY_ArrayBase_h_INCLUDED
25 
33 #include <boost/iterator/counting_iterator.hpp>
34 
35 #include "ndarray/ExpressionBase.h"
36 #include "ndarray/Vector.h"
37 #include "ndarray/detail/Core.h"
42 #include "ndarray/ArrayTraits.h"
43 #include "ndarray/eigen_fwd.h"
44 
45 namespace ndarray {
46 
55 template <typename Derived>
56 class ArrayBase : public ExpressionBase<Derived> {
57 protected:
59  typedef typename Traits::Core Core;
60  typedef typename Traits::CorePtr CorePtr;
61 public:
63  typedef typename Traits::Element Element;
65  typedef typename Traits::Iterator Iterator;
67  typedef typename Traits::Reference Reference;
69  typedef typename Traits::Value Value;
71  typedef typename Traits::ND ND;
73  typedef typename Traits::RMC RMC;
77  typedef ArrayRef<Element,ND::value,-RMC::value> FullTranspose;
84 
86  Reference operator[](int n) const {
87  return Traits::makeReference(
88  this->_data + n * this->template getStride<0>(),
89  this->_core
90  );
91  }
92 
94  Element & operator[](Index const & i) const {
95  return *(this->_data + this->_core->template computeOffset(i));
96  }
97 
99  Iterator begin() const {
100  return Traits::makeIterator(
101  this->_data,
102  this->_core,
103  this->template getStride<0>()
104  );
105  }
106 
108  Iterator end() const {
109  return Traits::makeIterator(
110  this->_data + this->template getSize<0>() * this->template getStride<0>(),
111  this->_core,
112  this->template getStride<0>()
113  );
114  }
115 
117  Element * getData() const { return _data; }
118 
120  bool isEmpty() const { return _data == 0; }
121 
123  Manager::Ptr getManager() const { return this->_core->getManager(); }
124 
126  template <int P> int getSize() const {
127  return detail::getDimension<P>(*this->_core).getSize();
128  }
129 
131  template <int P> int getStride() const {
132  return detail::getDimension<P>(*this->_core).getStride();
133  }
134 
136  Index getShape() const { Index r; this->_core->fillShape(r); return r; }
137 
139  Index getStrides() const { Index r; this->_core->fillStrides(r); return r; }
140 
142  int getNumElements() const { return this->_core->getNumElements(); }
143 
146  Index shape = getShape();
147  Index strides = getStrides();
148  for (int n=0; n < ND::value / 2; ++n) {
149  std::swap(shape[n], shape[ND::value-n-1]);
150  std::swap(strides[n], strides[ND::value-n-1]);
151  }
152  return FullTranspose(
153  getData(),
154  Core::create(shape, strides, getManager())
155  );
156  }
157 
159  Transpose transpose(Index const & order) const {
160  Index newShape;
161  Index newStrides;
162  Index oldShape = getShape();
163  Index oldStrides = getStrides();
164  for (int n=0; n < ND::value; ++n) {
165  newShape[n] = oldShape[order[n]];
166  newStrides[n] = oldStrides[order[n]];
167  }
168  return Transpose(
169  getData(),
170  Core::create(newShape, newStrides, getManager())
171  );
172  }
173 
175  Shallow const shallow() const { return Shallow(this->getSelf()); }
176 
178  Deep const deep() const { return Deep(this->getSelf()); }
179 
181 
190  template <typename XprKind, int Rows, int Cols>
192 
193  template <typename XprKind>
195 
196  template <int Rows, int Cols>
198 
201 
203  template <typename View_>
204  struct ResultOf {
205  typedef Element Element_;
210  };
211 
213  template <typename Seq>
214  typename ResultOf< View<Seq> >::Type
215  operator[](View<Seq> const & def) const {
216  return detail::buildView(this->getSelf(), def._seq);
217  }
218 
219 protected:
220  template <typename T_, int N_, int C_> friend class Array;
221  template <typename T_, int N_, int C_> friend class ArrayRef;
222  template <typename T_, int N_, int C_> friend struct ArrayTraits;
223  template <typename T_, int N_, int C_> friend class detail::NestedIterator;
224  template <typename Derived_> friend class ArrayBase;
225  template <typename Array_> friend class detail::ArrayAccess;
226 
229 
230  void operator=(ArrayBase const & other) {
231  _data = other._data;
232  _core = other._core;
233  }
234 
235  ArrayBase(Element * data, CorePtr const & core) : _data(data), _core(core) {}
236 };
237 
238 } // namespace ndarray
239 
240 #endif // !NDARRAY_ArrayBase_h_INCLUDED
A proxy class for Array with deep assignment operators.
Definition: ArrayRef.h:46
Array< Element_, ND_::value, RMC_::value > Value
Definition: ArrayBase.h:209
Dimension-specialized traits shared by Array and ArrayRef.
Definition: ArrayTraits.h:56
Definition for Vector.
ArrayRef< Element_, ND_::value, RMC_::value > Type
Definition: ArrayBase.h:208
ExpressionTraits< Derived >::Reference Reference
Nested expression or element reference.
Traits::Value Value
Nested array or element value type.
Definition: ArrayBase.h:69
Index getStrides() const
Return a Vector of the strides of all dimensions.
Definition: ArrayBase.h:139
void swap(ImageBase< PixelT > &a, ImageBase< PixelT > &b)
Definition: Image.cc:291
Eigen3 view into an ndarray::Array.
Definition: eigen.h:232
Vector< int, ND::value > Index
Vector type for N-dimensional indices.
Definition: ArrayBase.h:75
Traits::Reference Reference
Nested array or element reference.
Definition: ArrayBase.h:67
Traits::ND ND
Number of dimensions (boost::mpl::int_).
Definition: ArrayBase.h:71
Traits::Iterator Iterator
Nested array or element iterator.
Definition: ArrayBase.h:65
int getStride() const
Return the stride in a specific dimension.
Definition: ArrayBase.h:131
detail::ViewTraits< ND::value, RMC::value, typename View_::Sequence >::ND ND_
Definition: ArrayBase.h:206
Traits for Array.
boost::mpl::int_< C > RMC
Definition: ArrayTraits.h:59
Array< Element, ND::value, RMC::value > Shallow
The corresponding Array type.
Definition: ArrayBase.h:81
void operator=(ArrayBase const &other)
Definition: ArrayBase.h:230
ArrayRef< Element, ND::value, RMC::value > Deep
The corresponding ArrayRef type.
Definition: ArrayBase.h:83
FullTranspose transpose() const
Return a view of the array with the order of the dimensions reversed.
Definition: ArrayBase.h:145
EigenView< Element, ND::value, RMC::value, XprKind, Rows, Cols > asEigen() const
Traits::Core Core
Definition: ArrayBase.h:59
ExpressionTraits< Derived > Traits
Definition: ArrayBase.h:58
Element * _data
Definition: ArrayBase.h:227
A template metafunction class to determine the result of a view indexing operation.
Definition: ArrayBase.h:204
int getSize() const
Return the size of a specific dimension.
Definition: ArrayBase.h:126
ExpressionTraits< Derived >::Element Element
Data type of expression elements.
A fixed-size 1D array class.
Definition: Vector.h:93
Index getShape() const
Return a Vector of the sizes of all dimensions.
Definition: ArrayBase.h:136
boost::intrusive_ptr< Manager > Ptr
Definition: Manager.h:42
detail::ViewTraits< ND::value, RMC::value, typename View_::Sequence >::RMC RMC_
Definition: ArrayBase.h:207
ResultOf< View< Seq > >::Type operator[](View< Seq > const &def) const
Return a general view into this array (see Tutorial).
Definition: ArrayBase.h:215
Definitions for ArrayAccess.
Definitions for ExpressionBase.
int getNumElements() const
Return the total number of elements in the array.
Definition: ArrayBase.h:142
Traits::RMC RMC
Number of guaranteed row-major contiguous dimensions, counted from the end (boost::mpl::int_).
Definition: ArrayBase.h:73
boost::mpl::int_< N > ND
Definition: ArrayTraits.h:58
Implementation of arbitrary views into arrays.
Traits for expressions.
Forward declarations for ndarray/eigen interface.
CRTP implementation for Array and ArrayRef.
Definition: ArrayBase.h:56
Element * getData() const
Return a raw pointer to the first element of the array.
Definition: ArrayBase.h:117
Traits::CorePtr CorePtr
Definition: ArrayBase.h:60
ArrayRef< Element, ND::value,-RMC::value > FullTranspose
ArrayRef to a reverse-ordered contiguous array; the result of a call to transpose().
Definition: ArrayBase.h:77
bool isEmpty() const
Return true if the array has a null data point.
Definition: ArrayBase.h:120
A multidimensional strided array.
Definition: Array.h:47
Deep const deep() const
Return an ArrayRef view to this.
Definition: ArrayBase.h:178
Traits::Element Element
Data type of array elements.
Definition: ArrayBase.h:63
Iterator end() const
Return an Iterator to one past the end of the array.
Definition: ArrayBase.h:108
A template meta-sequence that defines an arbitrary view into an unspecified array.
Definition: views.h:84
Element & operator[](Index const &i) const
Return a single element from the array.
Definition: ArrayBase.h:94
ArrayBase(Element *data, CorePtr const &core)
Definition: ArrayBase.h:235
Sequence _seq
A boost::fusion sequence of index objects.
Definition: views.h:86
Definition of NestedIterator.
Shallow const shallow() const
Return a Array view to this.
Definition: ArrayBase.h:175
ExpressionTraits< Derived >::Iterator Iterator
Nested expression or element iterator.
ArrayRef< Element, ND::value, 0 > Transpose
ArrayRef to a noncontiguous array; the result of a call to transpose(...).
Definition: ArrayBase.h:79
Definitions for Core.
Definition of StridedIterator.
Transpose transpose(Index const &order) const
Return a view of the array with the dimensions permuted.
Definition: ArrayBase.h:159
ViewBuilder< Array_, Seq_ >::OutputArray buildView(Array_ const &array, Seq_ const &seq)
Definition: ViewBuilder.h:320
Manager::Ptr getManager() const
Return the opaque object responsible for memory management.
Definition: ArrayBase.h:123
Core::ConstPtr CorePtr
Definition: ArrayTraits.h:64
CRTP base class for all multidimensional expressions.
Reference operator[](int n) const
Return a single subarray.
Definition: ArrayBase.h:86
Iterator begin() const
Return an Iterator to the beginning of the array.
Definition: ArrayBase.h:99