Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
230 lines (199 loc) · 7.62 KB

File metadata and controls

230 lines (199 loc) · 7.62 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// -*- c++ -*-
/*
* Copyright (c) 2010-2012, Jim Bosch
* All rights reserved.
*
* ndarray is distributed under a simple BSD-like license;
* see the LICENSE file that should be present in the root
* of the source distribution, or alternately available at:
* https://github.com/ndarray/ndarray
*/
#ifndef NDARRAY_ArrayBase_h_INCLUDED
#define NDARRAY_ArrayBase_h_INCLUDED
/**
* @file ndarray/ArrayBase.h
*
* @brief Definitions for ArrayBase.
*/
#include <boost/iterator/counting_iterator.hpp>
#include "ndarray/ExpressionBase.h"
#include "ndarray/Vector.h"
#include "ndarray/detail/Core.h"
#include "ndarray/detail/NestedIterator.h"
#include "ndarray/detail/StridedIterator.h"
#include "ndarray/detail/ArrayAccess.h"
#include "ndarray/detail/ViewBuilder.h"
#include "ndarray/ArrayTraits.h"
namespace ndarray {
/**
* @class ArrayBase
* @brief CRTP implementation for Array and ArrayRef.
*
* @ingroup MainGroup
*
* Implements member functions that need specialization for 1D arrays.
*/
template <typename Derived>
class ArrayBase : public ExpressionBase<Derived> {
protected:
typedef ExpressionTraits<Derived> Traits;
typedef typename Traits::Core Core;
typedef typename Traits::CorePtr CorePtr;
public:
/// @brief Data type of array elements.
typedef typename Traits::Element Element;
/// @brief Nested array or element iterator.
typedef typename Traits::Iterator Iterator;
/// @brief Nested array or element reference.
typedef typename Traits::Reference Reference;
/// @brief Nested array or element value type.
typedef typename Traits::Value Value;
/// @brief Number of dimensions (boost::mpl::int_).
typedef typename Traits::ND ND;
/// @brief Number of guaranteed row-major contiguous dimensions, counted from the end (boost::mpl::int_).
typedef typename Traits::RMC RMC;
/// @brief Vector type for N-dimensional indices and shapes.
typedef Vector<Size,ND::value> Index;
/// @brief Vector type for N-dimensional offsets and strides.
typedef Vector<Offset,ND::value> Strides;
/// @brief ArrayRef to a reverse-ordered contiguous array; the result of a call to transpose().
typedef ArrayRef<Element,ND::value,-RMC::value> FullTranspose;
/// @brief ArrayRef to a noncontiguous array; the result of a call to transpose(...).
typedef ArrayRef<Element,ND::value,0> Transpose;
/// @brief The corresponding Array type.
typedef Array<Element,ND::value,RMC::value> Shallow;
/// @brief The corresponding ArrayRef type.
typedef ArrayRef<Element,ND::value,RMC::value> Deep;
/// @brief Return a single subarray.
Reference operator[](Size n) const {
return Traits::makeReference(
this->_data + n * this->
#ifndef _MSC_VER
template
#endif
getStride<0>(),
this->_core
);
}
/// @brief Return a single element from the array.
Element & operator[](Index const & i) const {
return *(this->_data + this->_core->computeOffset(i));
}
/// @brief Return an Iterator to the beginning of the array.
Iterator begin() const {
return Traits::makeIterator(
this->_data,
this->_core,
this->
#ifndef _MSC_VER
template
#endif
getStride<0>()
);
}
/// @brief Return an Iterator to one past the end of the array.
Iterator end() const {
return Traits::makeIterator(
this->_data + this->
#ifndef _MSC_VER
template
#endif
getSize<0>() * this->
#ifndef _MSC_VER
template
#endif
getStride<0>(),
this->_core,
this->
#ifndef _MSC_VER
template
#endif
getStride<0>()
);
}
/// @brief Return a raw pointer to the first element of the array.
Element * getData() const { return _data; }
/// @brief Return true if the array has a null data point.
bool isEmpty() const { return _data == 0; }
/// @brief Return the opaque object responsible for memory management.
Manager::Ptr getManager() const { return this->_core->getManager(); }
/// @brief Return the size of a specific dimension.
template <int P> Size getSize() const {
return detail::getDimension<P>(*this->_core).getSize();
}
/// @brief Return the stride in a specific dimension.
template <int P> Offset getStride() const {
return detail::getDimension<P>(*this->_core).getStride();
}
/// @brief Return a Vector of the sizes of all dimensions.
Index getShape() const { Index r; this->_core->fillShape(r); return r; }
/// @brief Return a Vector of the strides of all dimensions.
Strides getStrides() const { Strides r; this->_core->fillStrides(r); return r; }
/// @brief Return the total number of elements in the array.
Size getNumElements() const { return this->_core->getNumElements(); }
/// @brief Return a view of the array with the order of the dimensions reversed.
FullTranspose transpose() const {
Index shape = getShape();
Strides strides = getStrides();
for (int n=0; n < ND::value / 2; ++n) {
std::swap(shape[n], shape[ND::value-n-1]);
std::swap(strides[n], strides[ND::value-n-1]);
}
return FullTranspose(
getData(),
Core::create(shape, strides, getManager())
);
}
/// @brief Return a view of the array with the dimensions permuted.
Transpose transpose(Index const & order) const {
Index newShape;
Strides newStrides;
Index oldShape = getShape();
Strides oldStrides = getStrides();
for (int n=0; n < ND::value; ++n) {
newShape[n] = oldShape[order[n]];
newStrides[n] = oldStrides[order[n]];
}
return Transpose(
getData(),
Core::create(newShape, newStrides, getManager())
);
}
/// @brief Return a Array view to this.
Shallow const shallow() const { return Shallow(this->getSelf()); }
/// @brief Return an ArrayRef view to this.
Deep const deep() const { return Deep(this->getSelf()); }
/// @brief A template metafunction class to determine the result of a view indexing operation.
template <typename View_>
struct ResultOf {
typedef Element Element_;
typedef typename detail::ViewTraits<ND::value, RMC::value, typename View_::Sequence>::ND ND_;
typedef typename detail::ViewTraits<ND::value, RMC::value, typename View_::Sequence>::RMC RMC_;
typedef ArrayRef<Element_,ND_::value,RMC_::value> Type;
typedef Array<Element_,ND_::value,RMC_::value> Value;
};
/// @brief Return a general view into this array (see @ref ndarrayTutorial).
template <typename Seq>
typename ResultOf< View<Seq> >::Type
operator[](View<Seq> const & def) const {
return detail::buildView(this->getSelf(), def._seq);
}
protected:
template <typename T_, int N_, int C_> friend class Array;
template <typename T_, int N_, int C_> friend class ArrayRef;
template <typename T_, int N_, int C_> friend struct ArrayTraits;
template <typename T_, int N_, int C_> friend class detail::NestedIterator;
template <typename Derived_> friend class ArrayBase;
template <typename Array_> friend class detail::ArrayAccess;
Element * _data;
CorePtr _core;
void operator=(ArrayBase const & other) {
_data = other._data;
_core = other._core;
}
template <typename Other>
ArrayBase(ArrayBase<Other> const & other) : _data(other._data), _core(other._core) {}
ArrayBase(Element * data, CorePtr const & core) : _data(data), _core(core) {}
};
} // namespace ndarray
#endif // !NDARRAY_ArrayBase_h_INCLUDED
Morty Proxy This is a proxified and sanitized view of the page, visit original site.