ndarray
NumPy-friendly multidimensional arrays in C++
Loading...
Searching...
No Matches
ArrayBase.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_ArrayBase_h_INCLUDED
12#define NDARRAY_ArrayBase_h_INCLUDED
13
21#include <boost/iterator/counting_iterator.hpp>
22
24#include "ndarray/Vector.h"
25#include "ndarray/detail/Core.h"
30#include "ndarray/ArrayTraits.h"
31
32namespace ndarray {
33
42template <typename Derived>
43class ArrayBase : public ExpressionBase<Derived> {
44protected:
46 typedef typename Traits::Core Core;
47 typedef typename Traits::CorePtr CorePtr;
48public:
50 typedef typename Traits::Element Element;
52 typedef typename Traits::Iterator Iterator;
54 typedef typename Traits::Reference Reference;
56 typedef typename Traits::Value Value;
58 typedef typename Traits::ND ND;
60 typedef typename Traits::RMC RMC;
66 typedef ArrayRef<Element,ND::value,-RMC::value> FullTranspose;
73
75 Reference operator[](Size n) const {
76 return Traits::makeReference(
77 this->_data + n * this->
78 #ifndef _MSC_VER
79 template
80 #endif
81 getStride<0>(),
82 this->_core
83 );
84 }
85
87 Element & operator[](Index const & i) const {
88 return *(this->_data + this->_core->
89 #ifndef _MSC_VER
90 template
91 #endif
92 computeOffset(i));
93 }
94
96 Iterator begin() const {
97 return Traits::makeIterator(
98 this->_data,
99 this->_core,
100 this->
101 #ifndef _MSC_VER
102 template
103 #endif
104 getStride<0>()
105 );
106 }
107
109 Iterator end() const {
110 return Traits::makeIterator(
111 this->_data + this->
112 #ifndef _MSC_VER
113 template
114 #endif
115 getSize<0>() * this->
116 #ifndef _MSC_VER
117 template
118 #endif
119 getStride<0>(),
120 this->_core,
121 this->
122 #ifndef _MSC_VER
123 template
124 #endif
125 getStride<0>()
126 );
127 }
128
130 Element * getData() const { return _data; }
131
133 bool isEmpty() const { return _data == 0; }
134
136 Manager::Ptr getManager() const { return this->_core->getManager(); }
137
139 template <int P> Size getSize() const {
140 return detail::getDimension<P>(*this->_core).getSize();
141 }
142
144 template <int P> Offset getStride() const {
145 return detail::getDimension<P>(*this->_core).getStride();
146 }
147
149 Index getShape() const { Index r; this->_core->fillShape(r); return r; }
150
152 Strides getStrides() const { Strides r; this->_core->fillStrides(r); return r; }
153
155 Size getNumElements() const { return this->_core->getNumElements(); }
156
159 Index shape = getShape();
160 Strides strides = getStrides();
161 for (int n=0; n < ND::value / 2; ++n) {
162 std::swap(shape[n], shape[ND::value-n-1]);
163 std::swap(strides[n], strides[ND::value-n-1]);
164 }
165 return FullTranspose(
166 getData(),
167 Core::create(shape, strides, getManager())
168 );
169 }
170
172 Transpose transpose(Index const & order) const {
173 Index newShape;
174 Strides newStrides;
175 Index oldShape = getShape();
176 Strides oldStrides = getStrides();
177 for (int n=0; n < ND::value; ++n) {
178 newShape[n] = oldShape[order[n]];
179 newStrides[n] = oldStrides[order[n]];
180 }
181 return Transpose(
182 getData(),
183 Core::create(newShape, newStrides, getManager())
184 );
185 }
186
188 Shallow const shallow() const { return Shallow(this->getSelf()); }
189
191 Deep const deep() const { return Deep(this->getSelf()); }
192
194 template <typename View_>
195 struct ResultOf {
196 typedef Element Element_;
201 };
202
204 template <typename Seq>
205 typename ResultOf< View<Seq> >::Type
206 operator[](View<Seq> const & def) const {
207 return detail::buildView(this->getSelf(), def._seq);
208 }
209
210protected:
211 template <typename T_, int N_, int C_> friend class Array;
212 template <typename T_, int N_, int C_> friend class ArrayRef;
213 template <typename T_, int N_, int C_> friend struct ArrayTraits;
214 template <typename T_, int N_, int C_> friend class detail::NestedIterator;
215 template <typename Derived_> friend class ArrayBase;
216 template <typename Array_> friend class detail::ArrayAccess;
217
218 Element * _data;
219 CorePtr _core;
220
221 void operator=(ArrayBase const & other) {
222 _data = other._data;
223 _core = other._core;
224 }
225
226 template <typename Other>
227 ArrayBase(ArrayBase<Other> const & other) : _data(other._data), _core(other._core) {}
228
229 ArrayBase(Element * data, CorePtr const & core) : _data(data), _core(core) {}
230};
231
232} // namespace ndarray
233
234#endif // !NDARRAY_ArrayBase_h_INCLUDED
Definitions for ArrayAccess.
Traits for Array.
Definitions for Core.
Definitions for ExpressionBase.
Definition of NestedIterator.
Definition of StridedIterator.
Definition for Vector.
Implementation of arbitrary views into arrays.
CRTP implementation for Array and ArrayRef.
Definition ArrayBase.h:43
Offset getStride() const
Return the stride in a specific dimension.
Definition ArrayBase.h:144
Manager::Ptr getManager() const
Return the opaque object responsible for memory management.
Definition ArrayBase.h:136
Shallow const shallow() const
Return a Array view to this.
Definition ArrayBase.h:188
Reference operator[](Size n) const
Return a single subarray.
Definition ArrayBase.h:75
FullTranspose transpose() const
Return a view of the array with the order of the dimensions reversed.
Definition ArrayBase.h:158
ArrayRef< Element, ND::value,-RMC::value > FullTranspose
ArrayRef to a reverse-ordered contiguous array; the result of a call to transpose().
Definition ArrayBase.h:66
Array< Element, ND::value, RMC::value > Shallow
The corresponding Array type.
Definition ArrayBase.h:70
ArrayRef< Element, ND::value, 0 > Transpose
ArrayRef to a noncontiguous array; the result of a call to transpose(...).
Definition ArrayBase.h:68
bool isEmpty() const
Return true if the array has a null data point.
Definition ArrayBase.h:133
ResultOf< View< Seq > >::Type operator[](View< Seq > const &def) const
Return a general view into this array (see Tutorial).
Definition ArrayBase.h:206
Transpose transpose(Index const &order) const
Return a view of the array with the dimensions permuted.
Definition ArrayBase.h:172
Traits::ND ND
Number of dimensions (boost::mpl::int_).
Definition ArrayBase.h:58
Size getNumElements() const
Return the total number of elements in the array.
Definition ArrayBase.h:155
Size getSize() const
Return the size of a specific dimension.
Definition ArrayBase.h:139
Traits::Element Element
Data type of array elements.
Definition ArrayBase.h:50
Traits::Iterator Iterator
Nested array or element iterator.
Definition ArrayBase.h:52
Traits::Value Value
Nested array or element value type.
Definition ArrayBase.h:56
Deep const deep() const
Return an ArrayRef view to this.
Definition ArrayBase.h:191
Iterator end() const
Return an Iterator to one past the end of the array.
Definition ArrayBase.h:109
Vector< Offset, ND::value > Strides
Vector type for N-dimensional offsets and strides.
Definition ArrayBase.h:64
Traits::Reference Reference
Nested array or element reference.
Definition ArrayBase.h:54
ArrayRef< Element, ND::value, RMC::value > Deep
The corresponding ArrayRef type.
Definition ArrayBase.h:72
Element * getData() const
Return a raw pointer to the first element of the array.
Definition ArrayBase.h:130
Strides getStrides() const
Return a Vector of the strides of all dimensions.
Definition ArrayBase.h:152
Iterator begin() const
Return an Iterator to the beginning of the array.
Definition ArrayBase.h:96
Element & operator[](Index const &i) const
Return a single element from the array.
Definition ArrayBase.h:87
Index getShape() const
Return a Vector of the sizes of all dimensions.
Definition ArrayBase.h:149
Vector< Size, ND::value > Index
Vector type for N-dimensional indices and shapes.
Definition ArrayBase.h:62
Traits::RMC RMC
Number of guaranteed row-major contiguous dimensions, counted from the end (boost::mpl::int_).
Definition ArrayBase.h:60
A proxy class for Array with deep assignment operators.
Definition ArrayRef.h:34
A multidimensional strided array.
Definition Array.h:35
CRTP base class for all multidimensional expressions.
Definition ExpressionBase.h:40
Definition ArrayAccess.h:26
Definition NestedIterator.h:46
A template metafunction class to determine the result of a view indexing operation.
Definition ArrayBase.h:195
Dimension-specialized traits shared by Array and ArrayRef.
Definition ArrayTraits.h:44
Traits for expressions.
Definition ExpressionTraits.h:30
A fixed-size 1D array class.
Definition Vector.h:82
A template meta-sequence that defines an arbitrary view into an unspecified array.
Definition views.h:74
Sequence _seq
A boost::fusion sequence of index objects.
Definition views.h:76
Definition ViewBuilder.h:206