ndarray
NumPy-friendly multidimensional arrays in C++
Loading...
Searching...
No Matches
initialization.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_initialization_h_INCLUDED
12#define NDARRAY_initialization_h_INCLUDED
13
18#include "ndarray/Array.h"
19#include "ndarray/ArrayRef.h"
20#include "ndarray/Manager.h"
21
22namespace ndarray {
23namespace detail {
24
25struct NullOwner {};
26
27template <int N, typename Derived>
29public:
30
31 template <typename T, int C>
32 operator Array<T,N,C> () const {
33 return static_cast<Derived const *>(this)->template apply< Array<T,N,C> >();
34 }
35
36 template <typename T, int C>
37 operator ArrayRef<T,N,C> () const {
38 return static_cast<Derived const *>(this)->template apply< ArrayRef<T,N,C> >();
39 }
40
41};
42
43template <int N>
44class SimpleInitializer : public Initializer< N, SimpleInitializer<N> > {
45public:
46
47 template <typename Target>
48 Target apply() const {
49 typedef detail::ArrayAccess< Target > Access;
50 typedef typename Access::Core Core;
51 typedef typename Access::Element Element;
52 DataOrderEnum order = (ExpressionTraits< Target >::RMC::value < 0) ? COLUMN_MAJOR : ROW_MAJOR;
53 Size total = _shape.product();
54 std::pair<Manager::Ptr,Element*> p = SimpleManager<Element>::allocate(total);
55 return Access::construct(p.second, Core::create(_shape, order, p.first));
56 }
57
58 explicit SimpleInitializer(Vector<Size,N> const & shape) : _shape(shape) {}
59
60private:
61 Vector<Size,N> _shape;
62};
63
64template <typename T, int N, typename Owner>
65class ExternalInitializer : public Initializer< N, ExternalInitializer<T,N,Owner> > {
66public:
67
68 template <typename Target>
69 Target apply() const {
70 typedef detail::ArrayAccess< Target > Access;
71 typedef typename Access::Core Core;
72 Manager::Ptr manager;
73 if (!boost::is_same<Owner,NullOwner>::value) {
74 manager = makeManager(_owner);
75 }
76 return Access::construct(_data, Core::create(_shape, _strides, manager));
77 }
78
80 T * data,
81 Vector<Size,N> const & shape,
82 Vector<Offset,N> const & strides,
83 Owner const & owner
84 ) : _data(data), _owner(owner), _shape(shape), _strides(strides) {}
85
86private:
87 T * _data;
88 Owner _owner;
89 Vector<Size,N> _shape;
90 Vector<Offset,N> _strides;
91};
92
93} // namespace detail
94
97
103template <int N, typename U>
106}
107
114 return detail::SimpleInitializer<1>(ndarray::makeVector(n));
115}
116
122inline detail::SimpleInitializer<2> allocate(Size n1, Size n2) {
123 return detail::SimpleInitializer<2>(ndarray::makeVector(n1, n2));
124}
125
131inline detail::SimpleInitializer<3> allocate(Size n1, Size n2, Size n3) {
132 return detail::SimpleInitializer<3>(ndarray::makeVector(n1, n2, n3));
133}
134
138template <typename Derived>
139inline ArrayRef<typename boost::remove_const<typename Derived::Element>::type,
140 Derived::ND::value, Derived::ND::value>
143 Derived::ND::value,Derived::ND::value> r(
144 allocate(expr.getShape())
145 );
146 r = expr;
147 return r;
148}
149
151template <int N>
153 Vector<Offset,N> r(1);
154 if (order == ROW_MAJOR) {
155 for (int n=N-1; n > 0; --n) r[n-1] = r[n] * shape[n];
156 } else {
157 for (int n=1; n < N; ++n) r[n] = r[n-1] * shape[n-1];
158 }
159 return r;
160}
161
175template <typename T, int N, typename U, typename V, typename Owner>
177 T * data,
178 Vector<U,N> const & shape,
179 Vector<V,N> const & strides,
180 Owner const & owner
181) {
183 data,
184 Vector<Size,N>(shape.template cast<Size>()),
185 Vector<Offset,N>(strides.template cast<Offset>()),
186 owner
187 );
188}
189
202template <typename T, int N, typename U, typename V>
204 T * data,
205 Vector<U,N> const & shape,
206 Vector<V,N> const & strides
207) {
209 data,
210 Vector<Size,N>(shape),
211 Vector<Offset,N>(strides),
213 );
214}
215
229template <typename T, int N, typename U, typename Owner>
231 T * data,
232 Vector<U,N> const & shape,
233 DataOrderEnum order,
234 Owner const & owner
235) {
237 data,
238 Vector<Size,N>(shape.template cast<Size>()),
239 computeStrides(shape.template cast<Size>(), order),
240 owner
241 );
242}
243
256template <typename T, int N, typename U>
258 T * data,
259 Vector<U,N> const & shape,
260 DataOrderEnum order = ROW_MAJOR
261) {
263 data,
264 Vector<Size,N>(shape.template cast<Size>()),
265 computeStrides(shape.template cast<Size>(), order),
267 );
268}
269
271
272template <typename T, int N, int C>
273Array<T,N,C>::Array(Size n1, Size n2, Size n3, Size n4, Size n5, Size n6, Size n7, Size n8)
274 : Super(0, CorePtr())
275{
276 typename Super::Index shape;
277 if (N > 0) shape[0] = n1;
278 if (N > 1) shape[1] = n2;
279 if (N > 2) shape[2] = n3;
280 if (N > 3) shape[3] = n4;
281 if (N > 4) shape[4] = n5;
282 if (N > 5) shape[5] = n6;
283 if (N > 6) shape[6] = n7;
284 if (N > 7) shape[7] = n8;
285 this->operator=(ndarray::allocate(shape));
286}
287
288template <typename T, int N, int C>
289template <typename U>
291 : Super(0, CorePtr())
292{
293 this->operator=(ndarray::allocate(shape.template cast<U>()));
294}
295
296template <typename T, int N, int C>
297ArrayRef<T,N,C>::ArrayRef(Size n1, Size n2, Size n3, Size n4, Size n5, Size n6, Size n7, Size n8)
298 : Super(Array<T,N,C>(n1, n2, n3, n4, n5, n6, n7, n8))
299{}
300
301template <typename T, int N, int C>
302template <typename U>
304 : Super(Array<T,N,C>(shape))
305{}
306
307} // namespace ndarray
308
309#endif // !NDARRAY_initialization_h_INCLUDED
Definitions for ArrayRef.
Definitions for Array.
Definition of Manager, which manages the ownership of array data.
An intermediate CRTP base class for Array and ArrayRef.
Definition ArrayBaseN.h:29
A proxy class for Array with deep assignment operators.
Definition ArrayRef.h:34
A multidimensional strided array.
Definition Array.h:35
Array & operator=(Array const &other)
Non-converting shallow assignment.
Definition Array.h:102
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
Definition Manager.h:55
Definition ArrayAccess.h:26
Definition Core.h:47
static Ptr create(Vector< Size, M > const &shape, Vector< Offset, M > const &strides, Manager::Ptr const &manager=Manager::Ptr())
Create a Core::Ptr with the given shape, strides, and manager.
Definition Core.h:56
Definition initialization.h:65
Definition initialization.h:28
Definition initialization.h:44
ArrayRef< typename boost::remove_const< typename Derived::Element >::type, Derived::ND::value, Derived::ND::value > copy(ExpressionBase< Derived > const &expr)
Create a new Array by copying an Expression.
Definition initialization.h:141
detail::SimpleInitializer< N > allocate(Vector< U, N > const &shape)
Create an expression that allocates uninitialized memory for an array.
Definition initialization.h:104
detail::ExternalInitializer< T, N, Owner > external(T *data, Vector< U, N > const &shape, Vector< V, N > const &strides, Owner const &owner)
Create an expression that initializes an Array with externally allocated memory.
Definition initialization.h:176
Vector< Offset, N > computeStrides(Vector< Size, N > const &shape, DataOrderEnum order=ROW_MAJOR)
Compute row- or column-major strides for the given shape.
Definition initialization.h:152
DataOrderEnum
An enumeration for stride computation.
Definition ndarray_fwd.h:51
Traits for expressions.
Definition ExpressionTraits.h:30
A fixed-size 1D array class.
Definition Vector.h:82
Vector< T, M > first() const
Create a new Vector from the first M elements of this.
Definition Vector.h:150
T product() const
Return the product of all elements.
Definition Vector.h:214
Definition initialization.h:25