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
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  Manager::Ptr manager;
84  if (!boost::is_same<Owner,NullOwner>::value) {
85  manager = makeManager(_owner);
86  }
87  return Access::construct(_data, Core::create(_shape, _strides, manager));
88  }
89 
91  T * data,
92  Vector<int,N> const & shape,
93  Vector<int,N> const & strides,
94  Owner const & owner
95  ) : _data(data), _owner(owner), _shape(shape), _strides(strides) {}
96 
97 private:
98  T * _data;
99  Owner _owner;
102 };
103 
104 } // namespace detail
105 
108 
114 template <int N>
116  return detail::SimpleInitializer<N>(shape);
117 }
118 
126 }
127 
133 inline detail::SimpleInitializer<2> allocate(int n1, int n2) {
135 }
136 
142 inline detail::SimpleInitializer<3> allocate(int n1, int n2, int n3) {
144 }
145 
149 template <typename Derived>
150 inline ArrayRef<typename boost::remove_const<typename Derived::Element>::type,
151  Derived::ND::value, Derived::ND::value>
154  Derived::ND::value,Derived::ND::value> r(
155  allocate(expr.getShape())
156  );
157  r = expr;
158  return r;
159 }
160 
162 template <int N>
164  Vector<int,N> r(1);
165  if (order == ROW_MAJOR) {
166  for (int n=N-1; n > 0; --n) r[n-1] = r[n] * shape[n];
167  } else {
168  for (int n=1; n < N; ++n) r[n] = r[n-1] * shape[n-1];
169  }
170  return r;
171 }
172 
186 template <typename T, int N, typename Owner>
188  T * data,
189  Vector<int,N> const & shape,
190  Vector<int,N> const & strides,
191  Owner const & owner
192 ) {
193  return detail::ExternalInitializer<T,N,Owner>(data, shape, strides, owner);
194 }
195 
208 template <typename T, int N>
210  T * data,
211  Vector<int,N> const & shape,
212  Vector<int,N> const & strides
213 ) {
215 }
216 
230 template <typename T, int N, typename Owner>
232  T * data,
233  Vector<int,N> const & shape,
234  DataOrderEnum order,
235  Owner const & owner
236 ) {
237  return detail::ExternalInitializer<T,N,Owner>(data, shape, computeStrides(shape, order), owner);
238 }
239 
252 template <typename T, int N>
254  T * data,
255  Vector<int,N> const & shape,
256  DataOrderEnum order = ROW_MAJOR
257 ) {
259  data, shape, computeStrides(shape, order), detail::NullOwner()
260  );
261 }
262 
264 
265 } // namespace ndarray
266 
267 #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)