-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCUtlVector.h
461 lines (377 loc) · 8.31 KB
/
CUtlVector.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#pragma once
#include "utlvector.h"
#if 0
#include "CUtlMemory.h"
template< class T >
class CUtlFixedMemory
{
protected:
struct BlockHeader_t;
public:
T & operator[](int i)
{
return *(T*)i;
}
const T& operator[](int i) const
{
return *(T*)i;
}
class Iterator_t
{
public:
Iterator_t(BlockHeader_t *p, int i) : m_pBlockHeader(p), m_nIndex(i) {}
BlockHeader_t *m_pBlockHeader;
int m_nIndex;
bool operator==(const Iterator_t it) const { return m_pBlockHeader == it.m_pBlockHeader && m_nIndex == it.m_nIndex; }
bool operator!=(const Iterator_t it) const { return m_pBlockHeader != it.m_pBlockHeader || m_nIndex != it.m_nIndex; }
};
static const int INVALID_INDEX = 0; // For use with COMPILE_TIME_ASSERT
static int InvalidIndex() { return INVALID_INDEX; }
bool IsIdxValid(int i) const
{
return i != InvalidIndex();
}
bool IsInBlock(int i, BlockHeader_t *pBlockHeader) const
{
T *p = (T*)i;
const T *p0 = HeaderToBlock(pBlockHeader);
return p >= p0 && p < p0 + pBlockHeader->m_nBlockSize;
}
int GetIndex(const Iterator_t &it) const
{
if (!IsValidIterator(it))
return InvalidIndex();
return (int)(HeaderToBlock(it.m_pBlockHeader) + it.m_nIndex);
}
bool IsIdxAfter(int i, const Iterator_t &it) const
{
if (!IsValidIterator(it))
return false;
if (IsInBlock(i, it.m_pBlockHeader))
return i > GetIndex(it);
for (BlockHeader_t * __restrict pbh = it.m_pBlockHeader->m_pNext; pbh; pbh = pbh->m_pNext)
{
if (IsInBlock(i, pbh))
return true;
}
return false;
}
protected:
bool IsValidIterator(const Iterator_t &it) const { return it.m_pBlockHeader && it.m_nIndex >= 0 && it.m_nIndex < it.m_pBlockHeader->m_nBlockSize; }
const T *HeaderToBlock(const BlockHeader_t *pHeader) const { return (T*)(pHeader + 1); }
struct BlockHeader_t
{
BlockHeader_t *m_pNext;
int m_nBlockSize;
};
BlockHeader_t* m_pBlocks;
int m_nAllocationCount;
int m_nGrowSize;
};
template< class T, class A = CUtlMemory<T> >
class CUtlVector_full
{
typedef A CAllocator;
public:
typedef T ElemType_t;
inline CUtlVector_full<T, A>& operator=(const CUtlVector_full<T, A> &other)
{
int nCount = other.Count();
SetSize(nCount);
for (int i = 0; i < nCount; i++)
{
(*this)[i] = other[i];
}
return *this;
}
// element access
inline T& operator[](int i)
{
return m_Memory[i];
}
inline const T& operator[](int i) const
{
return m_Memory[i];
}
inline T& Element(int i)
{
return m_Memory[i];
}
inline const T& Element(int i) const
{
return m_Memory[i];
}
inline T& Head()
{
return m_Memory[0];
}
inline const T& Head() const
{
return m_Memory[0];
}
inline T& Tail()
{
return m_Memory[m_Size - 1];
}
inline const T& Tail() const
{
return m_Memory[m_Size - 1];
}
// Gets the base address (can change when adding elements!)
T* Base()
{
return m_Memory.Base();
}
const T* Base() const
{
return m_Memory.Base();
}
inline int InsertMultipleBefore(int elem, int num)
{
if (num == 0)
return elem;
// Can insert at the end
if (!((elem == Count()) || IsValidIndex(elem)))
return 0;
GrowVector(num);
ShiftElementsRight(elem, num);
// Invoke default constructors
for (int i = 0; i < num; ++i) {
Construct(&Element(elem + i));
}
return elem;
}
inline int InsertMultipleBefore(int elem, int num, const T *pToInsert)
{
if (num == 0)
return elem;
// Can insert at the end
assert((elem == Count()) || IsValidIndex(elem));
GrowVector(num);
ShiftElementsRight(elem, num);
// Invoke default constructors
if (!pToInsert) {
for (int i = 0; i < num; ++i) {
Construct(&Element(elem + i));
}
}
else {
for (int i = 0; i < num; i++) {
CopyConstruct(&Element(elem + i), pToInsert[i]);
}
}
return elem;
}
inline int AddMultipleToHead(int num)
{
return InsertMultipleBefore(0, num);
}
inline int AddMultipleToTail(int num)
{
return InsertMultipleBefore(m_Size, num);
}
inline int AddMultipleToTail(int num, const T *pToCopy)
{
// Can't insert something that's in the list... reallocation may hose us
assert((Base() == NULL) || !pToCopy || (pToCopy + num <= Base()) || (pToCopy >= (Base() + Count())));
return InsertMultipleBefore(m_Size, num, pToCopy);
}
// Returns the number of elements in the vector
// SIZE IS DEPRECATED!
inline int Size() const
{
return m_Size;
}
// don't use me!
inline int Count() const
{
return m_Size;
}
// Is element index valid?
inline bool IsValidIndex(int i) const
{
return (i >= 0) && (i < m_Size);
}
// Returns in invalid index
inline int InvalidIndex()
{
return -1;
}
// Adds an element, uses default constructor
inline int AddToHead()
{
return InsertBefore(0);
}
inline int AddToTail()
{
return InsertBefore(m_Size);
}
inline int AddToTail(const T& src)
{
return InsertBefore(m_Size, src);
}
inline int InsertAfter(int elem)
{
return InsertBefore(elem + 1);
}
//-----------------------------------------------------------------------------
// Makes sure we have enough memory allocated to store a requested # of elements
//-----------------------------------------------------------------------------
void EnsureCapacity(int num)
{
m_Memory.EnsureCapacity(num);
ResetDbgInfo();
}
int InsertBefore(int elem)
{
// Can insert at the end
GrowVector();
ShiftElementsRight(elem);
Construct(&Element(elem));
return elem;
}
int InsertBefore(int elem, const T& src)
{
GrowVector();
ShiftElementsRight(elem);
CopyConstruct(&Element(elem), src);
return elem;
}
// Calls RemoveAll() then AddMultipleToTail.
void SetCount(int count)
{
RemoveAll();
AddMultipleToTail(count);
}
inline void SetSize(int size)
{
SetCount(size);
}
inline void SetSize2(int size)
{
m_Size = size;
}
// Calls SetSize and copies each element.
void CopyArray(const T *pArray, int size)
{
SetSize(size);
for (int i = 0; i < size; i++)
{
(*this)[i] = pArray[i];
}
}
// Finds an element (element needs operator== defined)
int Find(const T& src) const
{
for (int i = 0; i < Count(); ++i)
{
if (Element(i) == src)
return i;
}
return -1;
}
bool HasElement(const T& src) const
{
return (Find(src) >= 0);
}
// Element removal
void FastRemove(int elem)
{
Destruct(&Element(elem));
if (m_Size > 0)
{
memcpy(&Element(elem), &Element(m_Size - 1), sizeof(T));
--m_Size;
}
}
void Remove(int elem)
{
Destruct(&Element(elem));
ShiftElementsLeft(elem);
--m_Size;
}
bool FindAndRemove(const T& src)
{
int elem = Find(src);
if (elem != -1)
{
Remove(elem);
return true;
}
return false;
}
void RemoveMultiple(int elem, int num)
{
for (int i = elem + num; --i >= elem;)
Destruct(&Element(i));
ShiftElementsLeft(elem, num);
m_Size -= num;
}
void RemoveAll()
{
for (int i = m_Size; --i >= 0;)
Destruct(&Element(i));
m_Size = 0;
}
// Memory deallocation
inline void Purge()
{
RemoveAll();
m_Memory.Purge();
ResetDbgInfo();
}
// Purges the list and calls delete on each element in it.
inline void PurgeAndDeleteElements()
{
for (int i = 0; i < m_Size; i++)
delete Element(i);
Purge();
}
// Compacts the vector to the number of elements actually in use
inline void Compact()
{
m_Memory.Purge(m_Size);
}
// Set the size by which it grows when it needs to allocate more memory.
void SetGrowSize(int size)
{
m_Memory.SetGrowSize(size);
}
// Only use this if you really know what you're doing!
inline int NumAllocated() const
{
return m_Memory.NumAllocated();
}
protected:
// Grows the vector
void GrowVector(int num = 1)
{
if (m_Size + num > m_Memory.NumAllocated())
m_Memory.Grow(m_Size + num - m_Memory.NumAllocated());
m_Size += num;
ResetDbgInfo();
}
// Shifts elements....
void ShiftElementsRight(int elem, int num = 1)
{
int numToMove = m_Size - elem - num;
if ((numToMove > 0) && (num > 0))
memmove(&Element(elem + num), &Element(elem), numToMove * sizeof(T));
}
void ShiftElementsLeft(int elem, int num = 1)
{
int numToMove = m_Size - elem - num;
if ((numToMove > 0) && (num > 0))
memmove(&Element(elem), &Element(elem + num), numToMove * sizeof(T));
}
CAllocator m_Memory;
int m_Size;
// For easier access to the elements through the debugger
// it's in release builds so this can be used in libraries correctly
T *m_pElements;
inline void ResetDbgInfo()
{
m_pElements = Base();
}
};
#endif