ndarray
NumPy-friendly multidimensional arrays in C++
Loading...
Searching...
No Matches
Core.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_DETAIL_Core_h_INCLUDED
12#define NDARRAY_DETAIL_Core_h_INCLUDED
13
20#include <boost/intrusive_ptr.hpp>
21#include <boost/mpl/int.hpp>
22#include "ndarray/Vector.h"
23#include "ndarray/Manager.h"
24
25namespace ndarray {
26namespace detail {
27
46template <int N>
47class Core : public Core<N-1> {
48public:
49 typedef boost::mpl::int_<N> ND;
50 typedef Core<N-1> Super;
51 typedef boost::intrusive_ptr<Core> Ptr;
52 typedef boost::intrusive_ptr<Core const> ConstPtr;
53
55 template <int M>
56 static Ptr create(
57 Vector<Size,M> const & shape,
58 Vector<Offset,M> const & strides,
59 Manager::Ptr const & manager = Manager::Ptr()
60 ) {
61 return Ptr(new Core(shape, strides, manager), false);
62 }
63
65 template <int M>
66 static Ptr create(
67 Vector<Size,M> const & shape,
68 DataOrderEnum order,
69 Manager::Ptr const & manager = Manager::Ptr()
70 ) {
71 if (order == ROW_MAJOR) {
72 return Ptr(new Core(shape, manager), false);
73 } else {
74 return Ptr(new Core(shape, 1, manager), false);
75 }
76 }
77
79 static Ptr create(
80 Manager::Ptr const & manager = Manager::Ptr()
81 ) {
82 return Ptr(new Core(manager), false);
83 }
84
85 Ptr copy() const { return Ptr(new Core(*this)); }
86
88 Size getSize() const { return _size; }
89
91 Offset getStride() const { return _stride; }
92
94 void setSize(Size size) { _size = size; }
95
97 void setStride(Offset stride) { _stride = stride; }
98
100 template <int M>
101 Offset computeOffset(Vector<Size,M> const & index) const {
102 return index[M-N] * this->getStride() + Super::computeOffset(index);
103 }
104
106 template <int M>
107 void fillShape(Vector<Size,M> & shape) const {
108 shape[M-N] = this->getSize();
109 Super::fillShape(shape);
110 }
111
113 template <int M>
114 void fillStrides(Vector<Offset,M> & strides) const {
115 strides[M-N] = this->getStride();
116 Super::fillStrides(strides);
117 }
118
120 Size getNumElements() const {
121 return getSize() * Super::getNumElements();
122 }
123
124protected:
125
126 // Explicit strides
127 template <int M>
128 Core (
129 Vector<Size,M> const & shape,
130 Vector<Offset,M> const & strides,
131 Manager::Ptr const & manager
132 ) : Super(shape, strides, manager), _size(shape[M-N]), _stride(strides[M-N]) {}
133
134 // Row-major strides
135 template <int M>
136 Core (
137 Vector<Size,M> const & shape,
138 Manager::Ptr const & manager
139 ) : Super(shape, manager), _size(shape[M-N]), _stride(Super::getStride() * Super::getSize()) {}
140
141 // Column-major strides
142 template <int M>
143 Core (
144 Vector<Size,M> const & shape,
145 Offset stride,
146 Manager::Ptr const & manager
147 ) : Super(shape, stride * shape[M-N], manager), _size(shape[M-N]), _stride(stride) {}
148
149 // Zero shape and strides
150 Core (
151 Manager::Ptr const & manager
152 ) : Super(manager), _size(0), _stride(0) {}
153
154 Core(Core const & other) : Super(other), _size(other._size), _stride(other._stride) {}
155
156private:
157 Size _size;
158 Offset _stride;
159};
160
170template <>
171class Core<0> {
172public:
173 typedef boost::mpl::int_<0> ND;
174 typedef boost::intrusive_ptr<Core> Ptr;
175 typedef boost::intrusive_ptr<Core const> ConstPtr;
176
177 friend inline void intrusive_ptr_add_ref(Core const * core) {
178 ++core->_rc;
179 }
180
181 friend inline void intrusive_ptr_release(Core const * core) {
182 if ((--core->_rc)==0) delete core;
183 }
184
185 Ptr copy() const { return Ptr(new Core(*this)); }
186
187 Size getSize() const { return 1; }
188 Offset getStride() const { return 1; }
189
191 template <int M>
192 Offset computeOffset(Vector<Size,M> const & index) const { return 0; }
193
195 Manager::Ptr getManager() const { return _manager; }
196
198 void setManager(Manager::Ptr const & manager) { _manager = manager; }
199
201 template <int M>
202 void fillShape(Vector<Size,M> const & shape) const {}
203
205 template <int M>
206 void fillStrides(Vector<Offset,M> const & strides) const {}
207
209 Size getNumElements() const { return 1; }
210
212 int getRC() const { return _rc; }
213
215 bool isUnique() const { return (_rc == 1) && (_manager->getRC() == 1) && _manager->isUnique(); }
216
217protected:
218
219 virtual ~Core() {}
220
221 template <int M>
222 Core(
223 Vector<Size,M> const & shape,
224 Vector<Offset,M> const & strides,
225 Manager::Ptr const & manager
226 ) : _manager(manager), _rc(1) {}
227
228 template <int M>
229 Core(
230 Vector<Size,M> const & shape,
231 Manager::Ptr const & manager
232 ) : _manager(manager), _rc(1) {}
233
234 template <int M>
235 Core(
236 Vector<Size,M> const & shape,
237 Offset stride,
238 Manager::Ptr const & manager
239 ) : _manager(manager), _rc(1) {}
240
241 Core(
242 Manager::Ptr const & manager
243 ) : _manager(manager), _rc(1) {}
244
245 Core(Core const & other) : _manager(other._manager), _rc(1) {}
246
247private:
248 Manager::Ptr _manager;
249 mutable int _rc;
250};
251
252
258template <int P, int N>
259inline Core<N-P> const &
260getDimension(Core<N> const & core) { return core; }
261
267template <int P, int N>
268inline typename Core<N-P>::Ptr
269getDimension(typename Core<N>::Ptr const & core) { return core; }
270
271} // namespace detail
272} // namespace ndarray
273
274#endif // !NDARRAY_DETAIL_Core_h_INCLUDED
Definition of Manager, which manages the ownership of array data.
Definition for Vector.
bool isUnique() const
Return true if the Core and Manager reference counts are 1 and the manager is unique.
Definition Core.h:215
void setManager(Manager::Ptr const &manager)
Set the Manager that determines the lifetime of the array data.
Definition Core.h:198
Manager::Ptr getManager() const
Return the Manager that determines the lifetime of the array data.
Definition Core.h:195
Offset computeOffset(Vector< Size, M > const &index) const
Recursively compute the offset to an element.
Definition Core.h:192
Size getNumElements() const
Recursively determine the total number of elements.
Definition Core.h:209
void fillShape(Vector< Size, M > const &shape) const
Recursively fill a shape vector.
Definition Core.h:202
int getRC() const
Return the reference count (for debugging purposes).
Definition Core.h:212
void fillStrides(Vector< Offset, M > const &strides) const
Recursively fill a strides vector.
Definition Core.h:206
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
Size getSize() const
Return the size of the Nth dimension.
Definition Core.h:88
void fillStrides(Vector< Offset, M > &strides) const
Recursively fill a strides vector.
Definition Core.h:114
void fillShape(Vector< Size, M > &shape) const
Recursively fill a shape vector.
Definition Core.h:107
static Ptr create(Vector< Size, M > const &shape, DataOrderEnum order, Manager::Ptr const &manager=Manager::Ptr())
Create a Core::Ptr with the given shape and manager with contiguous strides.
Definition Core.h:66
Offset computeOffset(Vector< Size, M > const &index) const
Recursively compute the offset to an element.
Definition Core.h:101
Size getNumElements() const
Recursively determine the total number of elements.
Definition Core.h:120
void setSize(Size size)
Set the size of the Nth dimension.
Definition Core.h:94
boost::intrusive_ptr< Core > Ptr
intrusive_ptr to Core
Definition Core.h:51
boost::intrusive_ptr< Core const > ConstPtr
const intrusive_ptr to Core
Definition Core.h:52
static Ptr create(Manager::Ptr const &manager=Manager::Ptr())
Create a Core::Ptr with the given manager and zero shape and strides.
Definition Core.h:79
boost::mpl::int_< N > ND
number of dimensions
Definition Core.h:49
Core< N-1 > Super
base class
Definition Core.h:50
void setStride(Offset stride)
Set the stride of the Nth dimension.
Definition Core.h:97
Offset getStride() const
Return the stride of the Nth dimension.
Definition Core.h:91
DataOrderEnum
An enumeration for stride computation.
Definition ndarray_fwd.h:51
A fixed-size 1D array class.
Definition Vector.h:82