-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil-containers.h
180 lines (164 loc) · 4.77 KB
/
util-containers.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
176
177
178
179
180
#pragma once
#include "util-basics.h"
#include "util-err.h"
#include <initializer_list>
#include <type_traits>
namespace util
{
// Base array type: not really a container, just a pointer and size,
// with the actual data stored elsewhere.
template <typename T>
struct array
{
T * data;
size_t size;
// Subscript accessor
T & operator[] (size_t i)
{
ASSERT_ERR(i < size);
return data[i];
}
// Constructors
array(): data(nullptr), size(0) {}
array(T * data_, size_t size_): data(data_), size(size_) {}
template <typename U> array(std::initializer_list<U> initList): data(&(*initList.begin())), size(initList.size()) {}
template <typename U> array(array<U> a): data(a.data), size(a.size) {}
template <typename U, size_t N> array(U(& a)[N]): data(a), size(N) {}
// Create a "view" of a sub-range of the array
array<T> slice(size_t start, size_t sliceSize)
{
ASSERT_ERR(start < size);
ASSERT_ERR(start + sliceSize < size);
return { data + start, sliceSize };
}
};
// What to call:
// * array with a fixed memory block (can't be realloced) but size can vary within fixed limit?
// * fixed_array, placed_array, limited_array, prealloced_array, buffer, sized_array, static_array
// * array that can realloc to grow?
// * dynamic_array, resizable_array, growable_array, stretchy_buffer
// "Fixed" array: has a fixed storage block and maximum capacity (no realloc), but size is variable
template <typename T>
struct fixedarray : public array<T>
{
static const bool trivial = std::is_trivial<T>::value;
size_t capacity;
// Constructors
fixedarray(): capacity(0) {}
fixedarray(T * data_, size_t size_, size_t capacity_): array<T>{data_, size_}, capacity(capacity_) {}
// Methods for adding and removing data
bool isFull() const { return (size >= capacity); }
template <typename U>
void append(U u)
{
ASSERT_ERR(size < capacity);
data[size] = T(u);
++size;
}
T * appendNew()
{
ASSERT_ERR(size < capacity);
T * result = &data[size];
++size;
return result;
}
template <typename U>
void appendSeveral(array<U> a)
{
ASSERT_ERR(size + a.size < capacity);
for (size_t i = 0; i < a.size; ++i)
data[size+i] = T(a.data[i]);
size += a.size;
}
template <typename U>
void appendSeveral(U * data_, size_t size_)
{
ASSERT_ERR(size + size_ < capacity);
for (size_t i = 0; i < size_; ++i)
data[size+i] = T(data_[i]);
size += size_;
}
void removeSwap(size_t i)
{
ASSERT_ERR(i < size);
if (i < size - 1)
swap(data[i], data[size - 1]);
--size;
}
void clear() { size = 0; }
};
// "Dynamic" array: owns storage and reallocs it to grow
template <typename T>
struct dynarray : public fixedarray<T>
{
// Constructors
dynarray() {}
explicit dynarray(size_t capacityInitial) { ensureCapacity(capacityInitial); }
dynarray(T * data_, size_t size_) { appendSeveral(data_, size_); }
template <typename U> dynarray(std::initializer_list<U> initList) { appendSeveral(&(*initList.begin()), initList.size()); }
template <typename U> explicit dynarray(array<U> a) { appendSeveral(a); }
template <typename U, size_t N> explicit dynarray(U(& a)[N]) { appendSeveral(a, N); }
// Copy, move, destruct
template <typename U> dynarray(dynarray<U> const & a) { appendSeveral(a); }
template <typename U> dynarray(dynarray<U> && a): fixedarray<T>(a) { a.data = nullptr; a.size = 0; a.capacity = 0; }
~dynarray() { reset(); }
// Methods for managing memory allocation
void ensureCapacity(size_t capacityNeeded)
{
if (capacityNeeded < capacity)
return;
static const size_t capacityStarter = 8;
size_t capacityNew = max(capacityNeeded, max(2 * capacity, capacityStarter));
T * dataOld = data;
T * dataNew = new T[capacityNew];
memcpy(dataNew, dataOld, size * sizeof(T));
data = dataNew;
capacity = capacityNew;
delete [] dataOld;
}
void reset()
{
delete [] data;
data = nullptr;
size = 0;
capacity = 0;
}
// Methods for adding and removing data
template <typename U>
void append(U u)
{
ensureCapacity(size + 1);
data[size] = T(u);
++size;
}
T * appendNew()
{
ensureCapacity(size + 1);
T * result = &data[size];
++size;
return result;
}
template <typename U>
void appendSeveral(array<U> a)
{
ensureCapacity(size + a.size);
for (size_t i = 0; i < a.size; ++i)
data[size+i] = T(a.data[i]);
size += a.size;
}
template <typename U>
void appendSeveral(U * data_, size_t size_)
{
ensureCapacity(size + size_);
for (size_t i = 0; i < size_; ++i)
data[size+i] = T(data_[i]);
size += size_;
}
};
// NYI:
// * sorting and searching
// * map, reduce, filter
// * multidim arrays
// * configurable owned storage (with custom allocator support?)
// * constructor-safety
}