-
Notifications
You must be signed in to change notification settings - Fork 67
/
VariableSizeVector.h
250 lines (219 loc) · 6.03 KB
/
VariableSizeVector.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
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
#pragma once
#include "Util.h"
//
// A variable-size vector that does not perform any memory allocation except to grow.
//
template<typename V, int grow = 150, bool big = false>
class VariableSizeVector
{
private:
inline static void* allocate(size_t bytes)
{
#ifdef USE_DEVTEAM_OPTIONS
if (bytes > (1L << 23) && ! big) {
WriteErrorMessage("%s: allocate %lld - consider using BigAlloc\n", __FUNCTION__, bytes);
}
#endif
return big ? BigAlloc(bytes) : malloc(bytes);
}
inline static void deallocate(void* p)
{
if (big) { BigDealloc(p); } else { free(p); }
}
public:
VariableSizeVector(int i_capacity = 16)
: entries(NULL), count(0), capacity(i_capacity)
{}
VariableSizeVector(VariableSizeVector& other)
: entries(other.entries), count(other.count), capacity(other.capacity)
{
other.count = 0;
other.entries = NULL;
}
~VariableSizeVector()
{
if (entries != NULL) {
deallocate(entries);
entries = NULL;
count = 0;
}
}
private:
inline void increase()
{
if (entries == NULL) {
reserve(capacity);
} else if (count == capacity) {
reserve((int) (((_int64) count * grow) / 100));
}
}
public:
void operator=(VariableSizeVector<V>& other)
{
entries = other.entries;
capacity = other.capacity;
count = other.count;
other.entries = NULL;
other.count = 0;
}
void reserve(_int64 newCapacity)
{
_ASSERT(newCapacity >= 0);
if (newCapacity <= capacity && entries != NULL) {
return;
}
V* old = entries;
capacity = __max(newCapacity, capacity);
entries = (V*) allocate(capacity * sizeof(V));
if (old != NULL) {
memcpy(entries, old, count * sizeof(V));
deallocate(old);
}
}
inline void clear()
{
count = 0;
}
inline void clean()
{
if (entries != NULL) {
deallocate(entries);
entries = NULL;
count = 0;
}
}
inline _int64 size() const
{
return count;
}
void truncate(int newCount)
{
if (newCount < count) {
count = newCount;
}
}
inline void push_back(V& value)
{
if (entries == NULL) {
reserve(capacity);
} else if (count == capacity) {
reserve((int) (((_int64) count * grow) / 100));
}
_ASSERT(count < capacity);
entries[count++] = value;
}
inline void push_back(const V& value)
{
increase();
_ASSERT(count < capacity);
entries[count++] = value;
}
inline void append(VariableSizeVector<V>* other)
{
if (other->count == 0) {
return;
}
reserve(count + other->count);
// todo: allow for operator assign/copy constructor?
memcpy(&entries[count], other->entries, other->count * sizeof(V));
count += other->count;
}
typedef bool comparator(const V& a, const V& b);
inline int insertionIndex(const V& value, comparator compare, bool before = false)
{
V* p = before ? std::lower_bound(entries, entries + count, value, compare)
: std::upper_bound(entries, entries + count, value, compare);
int index = (int) (p - entries);
_ASSERT(index >= 0 && index <= count);
return index;
}
// insert into sorted list, AFTER existing elements with same value
inline int insert(const V& value, comparator compare, bool before = false)
{
int index = insertionIndex( value, compare, before);
increase(); // todo: could fold memmove into new array copy to save time...
_ASSERT(count < capacity);
if (index < count) {
memmove(entries + (index + 1), entries + index, (count - index) * sizeof(V));
}
entries[index] = value;
count++;
return index;
}
inline bool add(const V& value)
{
for (int i = 0; i < count; i++) {
if (entries[i] == value) {
return false;
}
}
push_back(value);
return true;
}
inline void erase(_int64 index)
{
_ASSERT(index >= 0 && index < count);
if (index < 0 || index >= count) {
return;
}
if (index < count - 1) {
memmove(entries + index, entries + index + 1, (count - index - 1) * sizeof(V));
}
count--;
}
inline void extend(int size)
{
if (count < size) {
reserve(size);
memset(((V*)entries) + count, 0, sizeof(V) * (size - count));
count = size;
}
}
inline V& operator[](_int64 index) const
{
_ASSERT(index >= 0 && index < count);
return entries[index];
}
typedef V* iterator;
inline iterator findRange(const V& low, const V& high, comparator compare, iterator* o_end)
{
*o_end = entries + insertionIndex(high, compare, true);
return entries + insertionIndex(low, compare, true);
}
// unsorted search
inline iterator search(const V& value)
{
for (iterator i = begin(); i != end(); i++) {
if (*i == value) {
return i;
}
}
return end();
}
iterator begin()
{
return entries;
}
iterator end()
{
return &entries[count];
}
inline void remove(iterator p)
{
_ASSERT(p >= entries && p < entries + count);
if (p < entries + count - 1) {
memmove(p, p + 1, (count - (p - entries) - 1) * sizeof(V));
}
count--;
}
private:
V *entries;
_int64 capacity;
_int64 count;
};
typedef VariableSizeVector<unsigned> IdVector;
typedef VariableSizeVector<int> IntVector;
using util::IdPair;
using util::IdIntPair;
typedef VariableSizeVector<IdPair> IdPairVector;
typedef VariableSizeVector<IdIntPair> IdIntPairVector;