-
Notifications
You must be signed in to change notification settings - Fork 7
/
segmented_vector.h
523 lines (447 loc) · 13.7 KB
/
segmented_vector.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_SEGMENTED_VECTOR_H
#define EASTL_SEGMENTED_VECTOR_H
#if defined(EASTL_PRAGMA_ONCE_SUPPORTED)
#pragma once
#endif
#include <eastl/internal/config.h>
namespace eastl
{
template<typename T, size_t Count, typename Allocator = EASTLAllocatorType>
class segment
{
public:
typedef eastl_size_t size_type;
typedef segment<T, Count, Allocator> this_type;
typedef T* iterator;
typedef const T* const_iterator;
const this_type* next_segment() const;
this_type* next_segment();
const_iterator begin() const;
iterator begin();
const_iterator end() const;
iterator end();
private:
static const uintptr_t kIsLastSegment = 1 << 0;
uintptr_t mPrev;
union
{
this_type* mNext;
size_type mSize;
};
T mData[Count];
template<typename, size_t, typename> friend class segmented_vector;
template<typename, size_t, typename> friend struct segmented_vector_iterator;
};
template <typename T, size_t Count, typename Allocator = EASTLAllocatorType>
struct segmented_vector_iterator
{
public:
typedef segmented_vector_iterator<T, Count, Allocator> this_type;
typedef segment<T, Count, Allocator> segment_type;
T* operator->() const;
T& operator*() const;
this_type& operator++();
this_type operator++(int);
public:
T* mCurrent;
T* mEnd;
segment_type* mSegment;
};
template <typename T, size_t Count, typename Allocator = EASTLAllocatorType>
class segmented_vector
{
public:
typedef eastl_size_t size_type;
typedef segmented_vector<T, Count, Allocator> this_type;
typedef segment<T, Count, Allocator> segment_type;
typedef Allocator allocator_type;
typedef segmented_vector_iterator<const T, Count, Allocator> const_iterator;
typedef segmented_vector_iterator<T, Count, Allocator> iterator;
segmented_vector(const Allocator& allocator = Allocator());
~segmented_vector();
allocator_type& getAllocator();
const segment_type* first_segment() const;
segment_type* first_segment();
const_iterator begin() const;
iterator begin();
const_iterator end() const;
iterator end();
size_type size() const;
size_type segment_count() const;
T& front();
T& back();
bool empty() const;
void clear();
T& pushBack();
T& pushBack(const T& value);
void* pushBackUninitialized();
void popBack();
void erase_unsorted(segment_type& segment, typename segment_type::iterator it);
iterator erase_unsorted(const iterator& i);
void swap(this_type& other);
protected:
segment_type* DoAllocSegment(segment_type* prevSegment);
void* DoPushBack();
allocator_type mAllocator;
segment_type* mFirstSegment;
segment_type* mLastSegment;
size_type mSegmentCount;
};
template<typename T, size_t Count, typename Allocator>
inline const segment<T, Count, Allocator>*
segment<T, Count, Allocator>::next_segment() const
{
if (mPrev & kIsLastSegment)
return 0;
else
return mNext;
}
template<typename T, size_t Count, typename Allocator>
inline segment<T, Count, Allocator>*
segment<T, Count, Allocator>::next_segment()
{
if (mPrev & kIsLastSegment)
return 0;
else
return mNext;
}
template<typename T, size_t Count, typename Allocator>
inline typename segment<T, Count, Allocator>::const_iterator
segment<T, Count, Allocator>::begin() const
{
return mData;
}
template<typename T, size_t Count, typename Allocator>
inline typename segment<T, Count, Allocator>::iterator
segment<T, Count, Allocator>::begin()
{
return mData;
}
template<typename T, size_t Count, typename Allocator>
inline typename segment<T, Count, Allocator>::const_iterator
segment<T, Count, Allocator>::end() const
{
if (mPrev & kIsLastSegment)
return mData + mSize;
else
return mData + Count;
}
template<typename T, size_t Count, typename Allocator>
inline typename segment<T, Count, Allocator>::iterator
segment<T, Count, Allocator>::end()
{
if (mPrev & kIsLastSegment)
return mData + mSize;
else
return mData + Count;
}
template<typename T, size_t Count, typename Allocator>
T*
segmented_vector_iterator<T, Count, Allocator>::operator->() const
{
return mCurrent;
}
template<typename T, size_t Count, typename Allocator>
T&
segmented_vector_iterator<T, Count, Allocator>::operator*() const
{
return *mCurrent;
}
template<typename T, size_t Count, typename Allocator>
segmented_vector_iterator<T, Count, Allocator>&
segmented_vector_iterator<T, Count, Allocator>::operator++()
{
++mCurrent;
if(EASTL_UNLIKELY(mCurrent == mEnd))
{
if (!(mSegment->mPrev & segment_type::kIsLastSegment))
{
mSegment = mSegment->mNext;
mCurrent = mSegment->begin();
mEnd = mSegment->end();
}
else
mCurrent = 0;
}
return *this;
}
template<typename T, size_t Count, typename Allocator>
segmented_vector_iterator<T, Count, Allocator>
segmented_vector_iterator<T, Count, Allocator>::operator++(int)
{
this_type i(*this);
operator++();
return i;
}
template <typename T, size_t Count, typename Allocator>
inline segmented_vector<T, Count, Allocator>::segmented_vector(const Allocator& allocator)
: mAllocator(allocator)
, mFirstSegment(0)
, mLastSegment(0)
, mSegmentCount(0)
{
}
template <typename T, size_t Count, typename Allocator>
inline segmented_vector<T, Count, Allocator>::~segmented_vector()
{
clear();
}
template <typename T, size_t Count, typename Allocator>
inline typename segmented_vector<T, Count, Allocator>::allocator_type&
segmented_vector<T, Count, Allocator>::getAllocator()
{
return mAllocator;
}
template <typename T, size_t Count, typename Allocator>
inline const typename segmented_vector<T, Count, Allocator>::segment_type*
segmented_vector<T, Count, Allocator>::first_segment() const
{
return mFirstSegment;
}
template <typename T, size_t Count, typename Allocator>
inline typename segmented_vector<T, Count, Allocator>::segment_type*
segmented_vector<T, Count, Allocator>::first_segment()
{
return mFirstSegment;
}
template <typename T, size_t Count, typename Allocator>
inline typename segmented_vector<T, Count, Allocator>::const_iterator
segmented_vector<T, Count, Allocator>::begin() const
{
iterator i;
i.mSegment = mFirstSegment;
if (mFirstSegment)
{
i.mCurrent = mFirstSegment->begin();
i.mEnd = mFirstSegment->end();
}
else
i.mCurrent = 0;
return (const_iterator&)i;
}
template <typename T, size_t Count, typename Allocator>
inline typename segmented_vector<T, Count, Allocator>::iterator
segmented_vector<T, Count, Allocator>::begin()
{
iterator i;
i.mSegment = mFirstSegment;
if (mFirstSegment)
{
i.mCurrent = mFirstSegment->begin();
i.mEnd = mFirstSegment->end();
}
else
i.mCurrent = 0;
return i;
}
template <typename T, size_t Count, typename Allocator>
inline typename segmented_vector<T, Count, Allocator>::const_iterator
segmented_vector<T, Count, Allocator>::end() const
{
iterator i;
i.mCurrent = 0;
return (const_iterator&)i;
}
template <typename T, size_t Count, typename Allocator>
inline typename segmented_vector<T, Count, Allocator>::iterator
segmented_vector<T, Count, Allocator>::end()
{
iterator i;
i.mCurrent = 0;
return i;
}
template <typename T, size_t Count, typename Allocator>
inline typename segmented_vector<T, Count, Allocator>::size_type
segmented_vector<T, Count, Allocator>::size() const
{
if (segment_type* segment = mLastSegment)
return (mSegmentCount-1)*Count + segment->mSize;
return 0;
}
template <typename T, size_t Count, typename Allocator>
inline typename segmented_vector<T, Count, Allocator>::size_type
segmented_vector<T, Count, Allocator>::segment_count() const
{
return mSegmentCount;
}
template <typename T, size_t Count, typename Allocator>
inline T&
segmented_vector<T, Count, Allocator>::front()
{
return mFirstSegment->mData[0];
}
template <typename T, size_t Count, typename Allocator>
inline T&
segmented_vector<T, Count, Allocator>::back()
{
segment_type* lastSegment = mLastSegment;
return lastSegment->mData[lastSegment->mSize-1];
}
template <typename T, size_t Count, typename Allocator>
inline bool
segmented_vector<T, Count, Allocator>::empty() const
{
return mFirstSegment == 0;
}
template <typename T, size_t Count, typename Allocator>
inline void
segmented_vector<T, Count, Allocator>::clear()
{
if (segment_type* segment = mFirstSegment)
{
while (segment != mLastSegment)
{
segment_type* nextSegment = segment->mNext;
segment->~segment_type();
EASTLFree(mAllocator, segment, sizeof(segment_type));
segment = nextSegment;
}
for (T* i = segment->mData, *e = segment->mData + segment->mSize; i!=e; ++i)
i->~T();
EASTLFree(mAllocator, segment, sizeof(segment_type));
mFirstSegment = 0;
mLastSegment = 0;
mSegmentCount = 0;
}
}
template <typename T, size_t Count, typename Allocator>
inline T&
segmented_vector<T, Count, Allocator>::pushBack()
{
return *(new (DoPushBack()) T());
}
template <typename T, size_t Count, typename Allocator>
inline T&
segmented_vector<T, Count, Allocator>::pushBack(const T& value)
{
return *(new (DoPushBack()) T(value));
}
template <typename T, size_t Count, typename Allocator>
inline void*
segmented_vector<T, Count, Allocator>::pushBackUninitialized()
{
return DoPushBack();
}
template <typename T, size_t Count, typename Allocator>
inline void
segmented_vector<T, Count, Allocator>::popBack()
{
segment_type* lastSegment = mLastSegment;
#if EASTL_ASSERT_ENABLED
if(EASTL_UNLIKELY(!lastSegment))
EASTL_FAIL_MSG("segmented_vector::popBack -- segmented vector is empty");
#endif
--lastSegment->mSize;
(lastSegment->mData + lastSegment->mSize)->T::~T();
if (!lastSegment->mSize)
{
--mSegmentCount;
mLastSegment = (segment_type*)(lastSegment->mPrev & (~segment_type::kIsLastSegment));
EASTLFree(mAllocator, lastSegment, sizeof(segment_type));
if (mLastSegment)
{
mLastSegment->mPrev |= segment_type::kIsLastSegment;
mLastSegment->mSize = Count;
}
else
mFirstSegment = 0;
}
}
template <typename T, size_t Count, typename Allocator>
inline void
segmented_vector<T, Count, Allocator>::erase_unsorted(segment_type& segment, typename segment_type::iterator it)
{
EA_UNUSED(segment);
*it = back();
popBack();
}
template <typename T, size_t Count, typename Allocator>
inline typename segmented_vector<T, Count, Allocator>::iterator
segmented_vector<T, Count, Allocator>::erase_unsorted(const iterator& i)
{
iterator ret(i);
*i = back();
if (i.mSegment == mLastSegment && mLastSegment->mSize == 1)
ret.mCurrent = 0;
popBack();
return ret;
}
template <typename T, size_t Count, typename Allocator>
void
segmented_vector<T, Count, Allocator>::swap(this_type& other)
{
allocator_type tempAllocator(mAllocator);
segment_type* tempFirstSegment = mFirstSegment;
segment_type* tempLastSegment = mLastSegment;
size_type tempSegmentCount = mSegmentCount;
mAllocator = other.mAllocator;
mFirstSegment = other.mFirstSegment;
mLastSegment = other.mLastSegment;
mSegmentCount = other.mSegmentCount;
other.mAllocator = tempAllocator;
other.mFirstSegment = tempFirstSegment;
other.mLastSegment = tempLastSegment;
other.mSegmentCount = tempSegmentCount;
}
template <typename T, size_t Count, typename Allocator>
segment<T, Count, Allocator>*
segmented_vector<T, Count, Allocator>::DoAllocSegment(segment_type* prevSegment)
{
++mSegmentCount;
segment_type* segment = (segment_type*)allocate_memory(mAllocator, sizeof(segment_type), EASTL_ALIGN_OF(segment_type), 0);
segment->mPrev = uintptr_t(prevSegment) | segment_type::kIsLastSegment;
segment->mSize = 1;
return segment;
}
template <typename T, size_t Count, typename Allocator>
inline void*
segmented_vector<T, Count, Allocator>::DoPushBack()
{
if (segment_type* segment = mLastSegment)
{
size_type size = segment->mSize;
if (size < Count)
{
++segment->mSize;
return segment->mData + size;
}
else
{
segment_type* lastSegment = mLastSegment;
segment_type* newSegment = mLastSegment = DoAllocSegment(mLastSegment);
lastSegment->mPrev &= ~segment_type::kIsLastSegment;
lastSegment->mNext = newSegment;
return newSegment->mData;
}
}
else
{
segment = mFirstSegment = mLastSegment = DoAllocSegment(0);
return segment->mData;
}
}
template<typename T, size_t Count, typename Allocator>
inline bool operator==(const segmented_vector_iterator<const T, Count, Allocator>& a, const segmented_vector_iterator<const T, Count, Allocator>& b)
{
return a.mCurrent == b.mCurrent;
}
template<typename T, size_t Count, typename Allocator>
inline bool operator!=(const segmented_vector_iterator<const T, Count, Allocator>& a, const segmented_vector_iterator<const T, Count, Allocator>& b)
{
return a.mCurrent != b.mCurrent;
}
template<typename T, size_t Count, typename Allocator>
inline bool operator==(const segmented_vector_iterator<T, Count, Allocator>& a, const segmented_vector_iterator<T, Count, Allocator>& b)
{
return a.mCurrent == b.mCurrent;
}
template<typename T, size_t Count, typename Allocator>
inline bool operator!=(const segmented_vector_iterator<T, Count, Allocator>& a, const segmented_vector_iterator<T, Count, Allocator>& b)
{
return a.mCurrent != b.mCurrent;
}
}
#endif