LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
initialization.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_initialization_h_INCLUDED
24 #define NDARRAY_initialization_h_INCLUDED
25 
30 #include "ndarray/Array.h"
31 #include "ndarray/Manager.h"
32 
33 namespace ndarray {
34 namespace detail {
35 
36 struct NullOwner {};
37 
38 template <int N, typename Derived>
39 class Initializer {
40 public:
41 
42  template <typename T, int C>
43  operator Array<T,N,C> () const {
44  return static_cast<Derived const *>(this)->template apply< Array<T,N,C> >();
45  }
46 
47  template <typename T, int C>
48  operator ArrayRef<T,N,C> () const {
49  return static_cast<Derived const *>(this)->template apply< ArrayRef<T,N,C> >();
50  }
51 
52 };
53 
54 template <int N>
55 class SimpleInitializer : public Initializer< N, SimpleInitializer<N> > {
56 public:
57 
58  template <typename Target>
59  Target apply() const {
60  typedef detail::ArrayAccess< Target > Access;
61  typedef typename Access::Core Core;
62  typedef typename Access::Element Element;
64  int total = _shape.product();
65  std::pair<Manager::Ptr,Element*> p = SimpleManager<Element>::allocate(total);
66  return Access::construct(p.second, Core::create(_shape, order, p.first));
67  }
68 
69  explicit SimpleInitializer(Vector<int,N> const & shape) : _shape(shape) {}
70 
71 private:
73 };
74 
75 template <typename T, int N, typename Owner>
76 class ExternalInitializer : public Initializer< N, ExternalInitializer<T,N,Owner> > {
77 public:
78 
79  template <typename Target>
80  Target apply() const {
81  typedef detail::ArrayAccess< Target > Access;
82  typedef typename Access::Core Core;
83  typedef typename Access::Element Element;
84  Manager::Ptr manager;
85  if (!boost::is_same<Owner,NullOwner>::value) {
86  manager = makeManager(_owner);
87  }
88  return Access::construct(_data, Core::create(_shape, _strides, manager));
89  }
90 
92  T * data,
93  Vector<int,N> const & shape,
94  Vector<int,N> const & strides,
95  Owner const & owner
96  ) : _data(data), _owner(owner), _shape(shape), _strides(strides) {}
97 
98 private:
99  T * _data;
100  Owner _owner;
103 };
104 
105 } // namespace detail
106 
109 
115 template <int N>
117  return detail::SimpleInitializer<N>(shape);
118 }
119 
127 }
128 
134 inline detail::SimpleInitializer<2> allocate(int n1, int n2) {
136 }
137 
143 inline detail::SimpleInitializer<3> allocate(int n1, int n2, int n3) {
145 }
146 
150 template <typename Derived>
151 inline ArrayRef<typename boost::remove_const<typename Derived::Element>::type,
152  Derived::ND::value, Derived::ND::value>
155  Derived::ND::value,Derived::ND::value> r(
156  allocate(expr.getShape())
157  );
158  r = expr;
159  return r;
160 }
161 
163 template <int N>
165  Vector<int,N> r(1);
166  if (order == ROW_MAJOR) {
167  for (int n=N-1; n > 0; --n) r[n-1] = r[n] * shape[n];
168  } else {
169  for (int n=1; n < N; ++n) r[n] = r[n-1] * shape[n-1];
170  }
171  return r;
172 }
173 
187 template <typename T, int N, typename Owner>
189  T * data,
190  Vector<int,N> const & shape,
191  Vector<int,N> const & strides,
192  Owner const & owner
193 ) {
194  return detail::ExternalInitializer<T,N,Owner>(data, shape, strides, owner);
195 }
196 
209 template <typename T, int N>
211  T * data,
212  Vector<int,N> const & shape,
213  Vector<int,N> const & strides
214 ) {
216 }
217 
231 template <typename T, int N, typename Owner>
233  T * data,
234  Vector<int,N> const & shape,
235  DataOrderEnum order,
236  Owner const & owner
237 ) {
238  return detail::ExternalInitializer<T,N,Owner>(data, shape, computeStrides(shape, order), owner);
239 }
240 
253 template <typename T, int N>
255  T * data,
256  Vector<int,N> const & shape,
257  DataOrderEnum order = ROW_MAJOR
258 ) {
260  data, shape, computeStrides(shape, order), detail::NullOwner()
261  );
262 }
263 
265 
266 } // namespace ndarray
267 
268 #endif // !NDARRAY_initialization_h_INCLUDED
ExternalInitializer(T *data, Vector< int, N > const &shape, Vector< int, N > const &strides, Owner const &owner)
A proxy class for Array with deep assignment operators.
Definition: ArrayRef.h:46
static Ptr create(Vector< int, M > const &shape, Vector< int, M > const &strides, Manager::Ptr const &manager=Manager::Ptr())
Create a Core::Ptr with the given shape, strides, and manager.
Definition: Core.h:68
Vector< int, N > computeStrides(Vector< int, N > const &shape, DataOrderEnum order=ROW_MAJOR)
Compute row- or column-major strides for the given shape.
Index getShape() const
Return a Vector of the sizes of all dimensions.
detail::ExternalInitializer< T, N, Owner > external(T *data, Vector< int, N > const &shape, Vector< int, N > const &strides, Owner const &owner)
Create an expression that initializes an Array with externally allocated memory.
SelectEigenView< T >::Type copy(Eigen::EigenBase< T > const &other)
Copy an arbitrary Eigen expression into a new EigenView.
Definition: eigen.h:390
Definition of Manager, which manages the ownership of array data.
Definitions for Array.
T product() const
Return the product of all elements.
Definition: Vector.h:216
boost::intrusive_ptr< Manager > Ptr
Definition: Manager.h:42
Vector< T, N > makeVector(T v1, T v2,..., T vN)
Variadic constructor for Vector.
Traits for expressions.
Vector< T, M > first() const
Create a new Vector from the first M elements of this.
Definition: Vector.h:159
detail::SimpleInitializer< N > allocate(Vector< int, N > const &shape)
Create an expression that allocates uninitialized memory for an array.
DataOrderEnum
An enumeration for stride computation.
Definition: ndarray_fwd.h:60
A multidimensional strided array.
Definition: Array.h:47
static std::pair< Manager::Ptr, T * > allocate(int size)
Definition: Manager.h:71
Manager::Ptr makeManager(T const &owner)
Definition: Manager.h:101
CRTP base class for all multidimensional expressions.
SimpleInitializer(Vector< int, N > const &shape)