-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathndarray.h
175 lines (146 loc) · 4.47 KB
/
ndarray.h
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
#pragma once
#include <array>
#include <vector>
#include <stdexcept>
#include <type_traits>
#include "alignalloc.h"
template <std::size_t N, class T, std::size_t LoBound = 0, std::size_t HiBound = LoBound, class AllocatorT = AlignedAllocator<T>>
class ndarray {
static_assert(N > 0, "N cannot be 0");
static_assert(std::is_same_v<std::remove_reference_t<std::remove_cv_t<T>>, T>, "T cannot be cvref");
using Dim = std::array<std::intptr_t, N>;
using Shape = std::array<std::size_t, N>;
std::vector<T, AllocatorT> m_arr;
Shape m_shape{};
constexpr static std::size_t _calc_size(Shape const &shape) noexcept
{
std::size_t size = shape[0] + (LoBound + HiBound);
for (std::size_t i = 1; i < N; i++) {
size *= shape[i] + (LoBound + HiBound);
}
return size;
}
public:
ndarray() = default;
ndarray(ndarray const &) = default;
ndarray(ndarray &&) = default;
ndarray &operator=(ndarray const &) = default;
ndarray &operator=(ndarray &&) = default;
~ndarray() = default;
explicit ndarray(Shape const &shape)
: m_arr(_calc_size(shape))
, m_shape(shape)
{
}
explicit ndarray(Shape const &shape, T const &value)
: m_arr(_calc_size(shape), value)
, m_shape(shape)
{
}
template <class ...Ts, std::enable_if_t<sizeof...(Ts) == N && (std::is_integral_v<Ts> && ...), int> = 0>
explicit ndarray(Ts const &...ts)
: ndarray(Shape{ts...})
{
}
void reshape(Shape const &shape)
{
std::size_t size = _calc_size(shape);
m_shape = shape;
m_arr.clear();
m_arr.resize(size);
}
void reshape(Shape const &shape, T const &value)
{
std::size_t size = _calc_size(shape);
m_shape = shape;
m_arr.clear();
m_arr.resize(size, value);
}
void shrink_to_fit()
{
m_arr.shrink_to_fit();
}
template <class ...Ts, std::enable_if_t<sizeof...(Ts) == N && (std::is_integral_v<Ts> && ...), int> = 0>
void reshape(Ts const &...ts)
{
this->reshape(Shape{ts...});
}
constexpr Shape shape() const noexcept
{
return m_shape;
}
constexpr std::size_t shape(std::size_t i) const noexcept
{
return m_shape[i];
}
constexpr std::size_t linearize(Dim const &dim) const noexcept
{
std::size_t offset{dim[0] + LoBound};
std::size_t term = 1;
for (std::size_t i = 1; i < N; i++) {
term *= m_shape[i - 1] + (LoBound + HiBound);
offset += term * std::size_t{dim[i] + LoBound};
}
return offset;
}
std::size_t safe_linearize(Dim const &dim) const
{
for (std::size_t i = 0; i < N; i++) {
if (dim[i] < -std::intptr_t{LoBound} || dim[i] >= m_shape[i] + HiBound)
throw std::out_of_range("ndarray::at");
}
return linearize(dim);
}
constexpr T *data() noexcept
{
return m_arr.data();
}
constexpr T const *data() const noexcept
{
return m_arr.data();
}
constexpr T &operator()(Dim const &dim) noexcept
{
return data()[linearize(dim)];
}
constexpr T const &operator()(Dim const &dim) const noexcept
{
return data()[linearize(dim)];
}
template <class ...Ts, std::enable_if_t<sizeof...(Ts) == N && (std::is_integral_v<Ts> && ...), int> = 0>
constexpr T &operator()(Ts const &...ts) noexcept
{
return operator()(Dim{ts...});
}
template <class ...Ts, std::enable_if_t<sizeof...(Ts) == N && (std::is_integral_v<Ts> && ...), int> = 0>
constexpr T const &operator()(Ts const &...ts) const noexcept
{
return operator()(Dim{ts...});
}
constexpr T &operator[](Dim const &dim) noexcept
{
return operator()(linearize(dim));
}
constexpr T const &operator[](Dim const &dim) const noexcept
{
return operator()(linearize(dim));
}
T &at(Dim const &dim)
{
return data()[safe_linearize(dim)];
}
T const &at(Dim const &dim) const
{
return data()[safe_linearize(dim)];
}
template <class ...Ts, std::enable_if_t<sizeof...(Ts) == N && (std::is_integral_v<Ts> && ...), int> = 0>
T &at(Ts const &...ts)
{
return at(Dim{ts...});
}
template <class ...Ts, std::enable_if_t<sizeof...(Ts) == N && (std::is_integral_v<Ts> && ...), int> = 0>
T const &at(Ts const &...ts) const
{
return at(Dim{ts...});
}
};