-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhypervector_container.h
More file actions
353 lines (282 loc) · 9.71 KB
/
hypervector_container.h
File metadata and controls
353 lines (282 loc) · 9.71 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#ifndef HYPERVECTOR_CONTAINER_H
#define HYPERVECTOR_CONTAINER_H
#include "hypervector_detail.h"
#include "hypervector_view.h"
#include <algorithm>
#include <cstddef>
#include <initializer_list>
#include <memory>
#include <stdexcept>
#include <type_traits>
/// hypervector container providing size/shape modifiers and ownership of storage
template<typename T, size_t Dims>
struct hypervector : public hypervector_view<T, Dims, false>
{
using view = hypervector_view<T, Dims, false>;
using size_type = typename view::size_type;
private:
// uses the view's members for access to shape and values
size_type capacity_; ///< pre-allocated memory managed via reserve()
public:
/// create empty container
hypervector()
: view(new hypervector_detail::dimension[Dims](/* zero-initialized */), nullptr)
, capacity_(0) {
}
// hypervector(size_type count..., const T& value)
/// create container with given dimensions;
/// values are initialized to given value
template<typename ...Sizes>
hypervector(
typename std::enable_if<sizeof...(Sizes) == Dims, size_type>::type size0,
Sizes&&... sizes)
: hypervector() {
(void)assign_(0, 1, size0, std::forward<Sizes>(sizes)...);
}
// hypervector(size_type count...)
/// create container with given dimensions;
/// values are default-initialized
template<typename ...Sizes>
hypervector(
typename std::enable_if<sizeof...(Sizes) == Dims - 1, size_type>::type size0,
Sizes&&... sizes)
: hypervector() {
(void)assign_(0, 1, size0, std::forward<Sizes>(sizes)..., T());
}
// hypervector(std::initializer_list<std::initializer_list<...>>)
/// creates container with given values and dimensions
template<typename U>
hypervector(std::initializer_list<U> init)
: hypervector() {
reserve_(0, list_check_<0>(init));
(void)list_init_<0>(0, std::move(init));
}
hypervector(const hypervector& other)
: hypervector(static_cast<const view&>(other)) {
}
template<bool IsConst>
hypervector(const hypervector_view<T, Dims, IsConst>& other)
: hypervector() {
reserve_(0, other.size());
std::uninitialized_copy_n(other.begin(), other.size(), view::begin()); // XXX unsafe if throws halfway in
std::copy_n(other.dims_, Dims, view::dims_);
}
hypervector(hypervector&& other)
: hypervector() {
swap(other, *this);
}
~hypervector() {
std::destroy_n(view::begin(), view::size());
deallocate_(view::begin(), capacity());
delete[] view::dims_;
}
hypervector& operator=(const hypervector& other) {
return operator=(static_cast<const view&>(other));
}
template<bool IsConst>
hypervector& operator=(const hypervector_view<T, Dims, IsConst>& other) {
clear();
reserve_(0, other.size());
std::uninitialized_copy_n(other.begin(), other.size(), view::begin()); // XXX unsafe if throws halfway in
std::copy_n(other.dims_, Dims, view::dims_);
return *this;
}
hypervector& operator=(hypervector&& other) {
swap(other, *this);
other.clear(); // moved-from keeps its reserved allocation
return *this;
}
// void resize(size_type count..., const T& value)
/// resize container to given dimensions;
/// newly created elements will be initialized to given value
template<typename ...Sizes>
typename std::enable_if<sizeof...(Sizes) == Dims, void>::type
resize(
size_type size0,
Sizes&&... sizes) {
(void)resize_(view::size(), 1, size0, std::forward<Sizes>(sizes)...);
}
// void resize(size_type count...)
/// resize container to given dimensions;
/// newly created elements will be default-initialized
template<typename ...Sizes>
typename std::enable_if<sizeof...(Sizes) == Dims - 1, void>::type
resize(
size_type size0,
Sizes&&... sizes) {
(void)resize_(view::size(), 1, size0, std::forward<Sizes>(sizes)..., T());
}
// void assign(size_type count..., const T& value)
/// assign given dimensions to container and
/// set all elements to given value
template<typename ...Sizes>
typename std::enable_if<sizeof...(Sizes) == Dims, void>::type
assign(
size_type size0,
Sizes&&... sizes) {
(void)assign_(view::size(), 1, size0, std::forward<Sizes>(sizes)...);
}
/// destroy contents and set dimension sizes to zero
void clear() {
clear_(view::size());
}
// void reserve(size_type count...)
/// pre-allocate container to given dimension sizes
template<typename ...Sizes>
typename std::enable_if<
sizeof...(Sizes) == Dims - 1 || sizeof...(Sizes) == 0,
void>::type
reserve(
size_type size0,
Sizes&&... sizes) {
reserve_(view::size(), size0, std::forward<Sizes>(sizes)...);
}
/// get current pre-allocated storage capacity
size_type capacity() const noexcept {
return capacity_;
}
private:
struct deallocator
{
size_type size;
void operator()(T* ptr) const {
deallocate_(ptr, size);
}
};
static std::unique_ptr<T[], deallocator> allocate_(size_type size) {
return {
std::allocator<T>().allocate(size),
deallocator{size}
};
}
static void deallocate_(T* ptr, size_type size) {
std::allocator<T>().deallocate(ptr, size);
}
template<typename ...Sizes>
typename std::enable_if<sizeof...(Sizes) <= Dims, size_type>::type
resize_(
size_type old_size,
size_type acc_size,
size_type size0,
Sizes&&... sizes) {
constexpr auto Dim = Dims - sizeof...(Sizes);
view::dims_[Dim].offset = resize_(old_size, acc_size * size0, std::forward<Sizes>(sizes)...);
view::dims_[Dim].size = size0;
return view::dims_[Dim].offset * size0;
}
size_type resize_(
size_type old_size,
size_type new_size,
const T& val) {
if (new_size > old_size) {
reserve_(old_size, new_size);
std::uninitialized_fill_n(view::begin() + old_size, new_size - old_size, val); // XXX unsafe if throws halfway in
} else if (old_size > new_size) {
std::destroy_n(view::begin() + new_size, old_size - new_size);
}
return 1;
}
template<typename ...Sizes>
typename std::enable_if<sizeof...(Sizes) <= Dims, size_type>::type
assign_(
size_type old_size,
size_type acc_size,
size_type size0,
Sizes&&... sizes) {
constexpr auto Dim = Dims - sizeof...(Sizes);
view::dims_[Dim].offset = assign_(old_size, acc_size * size0, std::forward<Sizes>(sizes)...);
view::dims_[Dim].size = size0;
return view::dims_[Dim].offset * size0;
}
size_type assign_(
size_type old_size,
size_type new_size,
const T& val) {
// no backup: destroy, possibly grow, overwrite
clear_(old_size);
reserve_(0, new_size);
std::uninitialized_fill_n(view::begin(), new_size, val); // XXX unsafe if throws halfway in
return 1;
}
void clear_(size_type old_size) {
std::destroy_n(view::begin(), old_size);
std::fill_n(view::dims_, Dims, hypervector_detail::dimension());
}
template<typename ...Sizes>
typename std::enable_if<sizeof...(Sizes) < Dims, void>::type
reserve_(
size_type old_size,
size_type acc_capacity,
size_type size0,
Sizes&&... sizes) {
reserve_(old_size, acc_capacity * size0, std::forward<Sizes>(sizes)...);
}
void reserve_(
size_type old_size,
size_type new_capacity) {
if (new_capacity <= capacity_)
return;
auto new_vals = allocate_(new_capacity);
std::uninitialized_move_n(view::vals_, old_size, new_vals.get()); // XXX unsafe if throws halfway in
std::destroy_n(view::vals_, old_size);
deallocate_(view::vals_, capacity_);
view::vals_ = new_vals.release();
capacity_ = new_capacity;
}
template<size_t Dim, typename U>
static size_type list_check_(
const std::initializer_list<std::initializer_list<U>>& curr) {
static_assert(Dim < Dims, "hypervector(std::initializer_list)");
size_type acc_size = 0;
for (auto&& next : curr) {
auto next_size = list_check_<Dim + 1>(next);
if (acc_size && (acc_size != next_size))
throw std::invalid_argument("hypervector(std::initializer_list): unequal list sizes");
acc_size = next_size;
}
return acc_size * curr.size();
}
template<size_t Dim>
static size_type list_check_(const std::initializer_list<T>& init) {
static_assert(Dim + 1 == Dims, "hypervector(std::initializer_list)");
return init.size();
}
template<size_t Dim, typename U>
size_type list_init_(
size_type acc_offset,
std::initializer_list<std::initializer_list<U>> curr) {
static_assert(Dim < Dims, "hypervector(std::initializer_list)");
size_type offset = 0;
for (auto&& next : curr) {
offset = list_init_<Dim + 1>(acc_offset, std::move(next));
acc_offset += offset;
}
// XXX offsets and sizes are applied before all values have been moved
// not ideal but the top-level offset and size relevant for size() are written last
view::dims_[Dim].offset = offset;
view::dims_[Dim].size = curr.size();
return view::dims_[Dim].offset * view::dims_[Dim].size;
}
template<size_t Dim>
size_type list_init_(
size_type offset,
std::initializer_list<T> init) {
static_assert(Dim + 1 == Dims, "hypervector(std::initializer_list)");
auto size = init.size();
std::uninitialized_move_n(init.begin(), size, view::begin() + offset); // XXX unsafe if throws
view::dims_[Dim].offset = 1;
view::dims_[Dim].size = size;
return size;
}
template<typename U, size_t Dim>
friend void swap(hypervector<U, Dim>&, hypervector<U, Dim>&) noexcept;
};
template<typename T, size_t Dims>
void swap(hypervector<T, Dims>& lhs, hypervector<T, Dims>& rhs) noexcept
{
using std::swap;
swap(lhs.dims_, rhs.dims_);
swap(lhs.vals_, rhs.vals_);
swap(lhs.capacity_, rhs.capacity_);
}
#endif // HYPERVECTOR_CONTAINER_H