forked from FWGS/MiniUTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utlvector.h
1066 lines (903 loc) · 30.8 KB
/
utlvector.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
// A growable array class that maintains a free list and keeps elements
// in the same location
//=============================================================================//
#ifndef UTLVECTOR_H
#define UTLVECTOR_H
#ifdef _WIN32
#pragma once
#endif
#include <string.h>
#include "utlmemory.h"
#define FOR_EACH_VEC( vecName, iteratorName ) \
for ( int iteratorName = 0; iteratorName < (vecName).Count(); iteratorName++ )
#define FOR_EACH_VEC_BACK( vecName, iteratorName ) \
for ( int iteratorName = (vecName).Count()-1; iteratorName >= 0; iteratorName-- )
//-----------------------------------------------------------------------------
// The CUtlVector class:
// A growable array class which doubles in size by default.
// It will always keep all elements consecutive in memory, and may move the
// elements around in memory (via a PvRealloc) when elements are inserted or
// removed. Clients should therefore refer to the elements of the vector
// by index (they should *never* maintain pointers to elements in the vector).
//-----------------------------------------------------------------------------
template< class T, class A = CUtlMemory<T> >
class CUtlVector
{
typedef A CAllocator;
public:
typedef T ElemType_t;
typedef T* iterator;
typedef const T* const_iterator;
// constructor, destructor
CUtlVector( int growSize = 0, int initSize = 0 );
CUtlVector( T* pMemory, int allocationCount, int numElements = 0 );
~CUtlVector();
// Copy the array.
CUtlVector<T, A>& operator=( const CUtlVector<T, A> &other );
// element access
T& operator[]( int i );
const T& operator[]( int i ) const;
T& Element( int i );
const T& Element( int i ) const;
T& Head();
const T& Head() const;
T& Tail();
const T& Tail() const;
// Gets the base address (can change when adding elements!)
T* Base() { return m_Memory.Base(); }
const T* Base() const { return m_Memory.Base(); }
// Returns the number of elements in the vector
int Count() const;
bool IsEmpty() const { return (Count() == 0); }
// Is element index valid?
bool IsValidIndex( int i ) const;
static int InvalidIndex();
// Adds an element, uses default constructor
int AddToHead();
int AddToTail();
T *AddToTailGetPtr();
int InsertBefore( int elem );
int InsertAfter( int elem );
// Adds an element, uses copy constructor
int AddToHead( const T& src );
int AddToTail( const T& src );
int InsertBefore( int elem, const T& src );
int InsertAfter( int elem, const T& src );
// Adds multiple elements, uses default constructor
int AddMultipleToHead( int num );
int AddMultipleToTail( int num, const T *pToCopy=NULL );
int InsertMultipleBefore( int elem, int num, const T *pToCopy=NULL ); // If pToCopy is set, then it's an array of length 'num' and
int InsertMultipleAfter( int elem, int num, const T *pToCopy=NULL );
// Matches desired element count by removing or adding at tail
void SetCount( int count );
// Calls SetSize and copies each element.
void CopyArray( const T *pArray, int size );
// Fast swap
void Swap( CUtlVector< T, A > &vec );
// Add the specified array to the tail.
int AddVectorToTail( CUtlVector<T, A> const &src );
// Finds an element (element needs operator== defined)
int Find( const T& src ) const;
bool HasElement( const T& src ) const;
// Makes sure we have enough memory allocated to store a requested # of elements
void EnsureCapacity( int num );
// Makes sure we have at least this many elements
void EnsureCount( int num );
// Element removal
void FastRemove( int elem ); // doesn't preserve order
void Remove( int elem ); // preserves order, shifts elements
bool FindAndRemove( const T& src ); // removes first occurrence of src, preserves order, shifts elements
bool FindAndFastRemove( const T& src ); // removes first occurrence of src, doesn't preserve order
void RemoveMultiple( int elem, int num ); // preserves order, shifts elements
void RemoveMultipleFromTail( int num );
void RemoveAll(); // doesn't deallocate memory
// Memory deallocation
void Purge();
// Purges the list and calls delete on each element in it.
void PurgeAndDeleteElements();
void PurgeAndDeleteElementsArray();
// Compacts the vector to the number of elements actually in use
void Compact();
// Set the size by which it grows when it needs to allocate more memory.
void SetGrowSize( int size ) { m_Memory.SetGrowSize( size ); }
int NumAllocated() const { return m_Memory.NumAllocated(); } // Only use this if you really know what you're doing!
// Reverses the order of elements via swaps
void Reverse();
// Finds an element within the list using a binary search
// You must sort the list before using these or your results will be wrong
int SortedFind( const T& search, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2, void *pCtx ), void *pLessContext ) const;
int SortedFind( const T& search, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2 ) ) const;
int SortedFindFirst( const T& search, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2, void *pCtx ), void *pLessContext ) const;
int SortedFindLessOrEqual( const T& search, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2, void *pCtx ), void *pLessContext, int start, int end ) const;
int SortedFindLessOrEqual( const T& search, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2, void *pCtx ), void *pLessContext ) const;
int SortedFindLessOrEqual( const T& search, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2 ), int start, int end ) const;
int SortedFindLessOrEqual( const T& search, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2 ) ) const;
int SortedInsert( const T& src, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2, void *pCtx ), void *pLessContext );
int SortedInsert( const T& src, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2 ) );
// WARNING: The less func for these Sort functions expects -1, 0 for equal, or 1. If you pass only true/false back, you won't get correct sorting.
void Sort( int (__cdecl *pfnCompare)(const T *, const T *) );
#ifdef DBGFLAG_VALIDATE
void Validate( CValidator &validator, const char *pchName ); // Validate our internal structures
void ValidateSelfAndElements( CValidator &validator, const char *pchName ); // Validate our internal structures
#endif // DBGFLAG_VALIDATE
protected:
// Can't copy this unless we explicitly do it!
CUtlVector( CUtlVector const& );
// Grows the vector
void GrowVector( int num = 1 );
// Shifts elements....
void ShiftElementsRight( int elem, int num = 1 );
void ShiftElementsLeft( int elem, int num = 1 );
CAllocator m_Memory;
int m_Size;
};
//-----------------------------------------------------------------------------
// The CUtlVectorFixed class:
// A array class with a fixed allocation scheme
//-----------------------------------------------------------------------------
template< class T, size_t MAX_SIZE >
class CUtlVectorFixed : public CUtlVector< T, CUtlMemoryFixed<T, MAX_SIZE > >
{
typedef CUtlVector< T, CUtlMemoryFixed<T, MAX_SIZE > > BaseClass;
public:
// constructor, destructor
CUtlVectorFixed( int growSize = 0, int initSize = 0 ) : BaseClass( growSize, initSize ) {}
CUtlVectorFixed( T* pMemory, int numElements ) : BaseClass( pMemory, numElements ) {}
};
//-----------------------------------------------------------------------------
// The CCopyableUtlVectorFixed class:
// A array class that allows copy construction (so you can nest a CUtlVector inside of another one of our containers)
// WARNING - this class lets you copy construct which can be an expensive operation if you don't carefully control when it happens
// Only use this when nesting a CUtlVector() inside of another one of our container classes (i.e a CUtlMap)
//-----------------------------------------------------------------------------
template< class T, size_t MAX_SIZE >
class CCopyableUtlVectorFixed : public CUtlVectorFixed< T, MAX_SIZE >
{
typedef CUtlVectorFixed< T, MAX_SIZE > BaseClass;
public:
CCopyableUtlVectorFixed( int growSize = 0, int initSize = 0 ) : BaseClass( growSize, initSize ) {}
CCopyableUtlVectorFixed( T* pMemory, int numElements ) : BaseClass( pMemory, numElements ) {}
CCopyableUtlVectorFixed( CCopyableUtlVectorFixed const& vec ) : BaseClass(0,0) { this->CopyArray( vec.Base(), vec.Count() ); }
};
//-----------------------------------------------------------------------------
// The CCopyableUtlVector class:
// A array class that allows copy construction (so you can nest a CUtlVector inside of another one of our containers)
// WARNING - this class lets you copy construct which can be an expensive operation if you don't carefully control when it happens
// Only use this when nesting a CUtlVector() inside of another one of our container classes (i.e a CUtlMap)
//-----------------------------------------------------------------------------
template< class T, class A = CUtlMemory<T> >
class CCopyableUtlVector : public CUtlVector< T, A >
{
typedef CUtlVector< T, A > BaseClass;
public:
CCopyableUtlVector( int growSize = 0, int initSize = 0 ) : BaseClass( growSize, initSize ) {}
CCopyableUtlVector( T* pMemory, int numElements ) : BaseClass( pMemory, numElements ) {}
CCopyableUtlVector( CCopyableUtlVector const& vec ) : BaseClass(0,0) { this->CopyArray( vec.Base(), vec.Count() ); }
CCopyableUtlVector( CUtlVector<T,A> const& vec ) : BaseClass( 0, 0 ) { this->CopyArray( vec.Base(), vec.Count() ); }
};
//-----------------------------------------------------------------------------
// constructor, destructor
//-----------------------------------------------------------------------------
template< typename T, class A >
inline CUtlVector<T, A>::CUtlVector( int growSize, int initSize ) :
m_Memory(growSize, initSize), m_Size(0)
{
}
template< typename T, class A >
inline CUtlVector<T, A>::CUtlVector( T* pMemory, int allocationCount, int numElements ) :
m_Memory(pMemory, allocationCount), m_Size(numElements)
{
}
template< typename T, class A >
inline CUtlVector<T, A>::~CUtlVector()
{
Purge();
}
template< typename T, class A >
inline CUtlVector<T, A>& CUtlVector<T, A>::operator=( const CUtlVector<T, A> &other )
{
this->CopyArray( other.Base(), other.Count() );
return *this;
}
//-----------------------------------------------------------------------------
// element access
//-----------------------------------------------------------------------------
template< typename T, class A >
inline T& CUtlVector<T, A>::operator[]( int i )
{
DbgAssert( IsValidIndex(i) );
return Base()[i];
}
template< typename T, class A >
inline const T& CUtlVector<T, A>::operator[]( int i ) const
{
DbgAssert( IsValidIndex(i) );
return Base()[i];
}
template< typename T, class A >
inline T& CUtlVector<T, A>::Element( int i )
{
DbgAssert( IsValidIndex(i) );
return Base()[i];
}
template< typename T, class A >
inline const T& CUtlVector<T, A>::Element( int i ) const
{
DbgAssert( IsValidIndex(i) );
return Base()[i];
}
template< typename T, class A >
inline T& CUtlVector<T, A>::Head()
{
DbgAssert( m_Size > 0 );
return m_Memory[0];
}
template< typename T, class A >
inline const T& CUtlVector<T, A>::Head() const
{
DbgAssert( m_Size > 0 );
return m_Memory[0];
}
template< typename T, class A >
inline T& CUtlVector<T, A>::Tail()
{
DbgAssert( m_Size > 0 );
return m_Memory[m_Size - 1];
}
template< typename T, class A >
inline const T& CUtlVector<T, A>::Tail() const
{
DbgAssert( m_Size > 0 );
return m_Memory[m_Size - 1];
}
//-----------------------------------------------------------------------------
// Count
//-----------------------------------------------------------------------------
template< typename T, class A >
inline int CUtlVector<T, A>::Count() const
{
return m_Size;
}
//-----------------------------------------------------------------------------
// Is element index valid?
//-----------------------------------------------------------------------------
template< typename T, class A >
inline bool CUtlVector<T, A>::IsValidIndex( int i ) const
{
return (i >= 0) && (i < m_Size);
}
//-----------------------------------------------------------------------------
// Returns in invalid index
//-----------------------------------------------------------------------------
template< typename T, class A >
inline int CUtlVector<T, A>::InvalidIndex()
{
return -1;
}
//-----------------------------------------------------------------------------
// Grows the vector
//-----------------------------------------------------------------------------
template< typename T, class A >
inline void CUtlVector<T, A>::GrowVector( int num )
{
if (m_Size + num > m_Memory.NumAllocated())
{
m_Memory.Grow( m_Size + num - m_Memory.NumAllocated() );
}
m_Size += num;
}
//-----------------------------------------------------------------------------
// Reverses the order of elements
//-----------------------------------------------------------------------------
template< typename T, class A >
inline void CUtlVector<T, A>::Reverse()
{
T* pBase = Base();
int iRight = m_Size - 1;
for( int iLeft = 0; iLeft < m_Size / 2; iLeft++ )
{
SWAP( pBase[iLeft], pBase[iRight] );
iRight--;
}
}
//-----------------------------------------------------------------------------
// finds a particular element
// You must sort the list before using or your results will be wrong
//-----------------------------------------------------------------------------
template< typename T, class A >
inline int CUtlVector<T, A>::SortedFind( const T& search, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2, void *pCtx ), void *pLessContext ) const
{
int start = 0, stop = Count() - 1;
while (start <= stop)
{
int mid = (start + stop) >> 1;
if ( pfnLessFunc( Base()[mid], search, pLessContext ) )
{
start = mid + 1;
}
else if ( pfnLessFunc( search, Base()[mid], pLessContext ) )
{
stop = mid - 1;
}
else
{
return mid;
}
}
return InvalidIndex();
}
//-----------------------------------------------------------------------------
// finds a particular element
// You must sort the list before using or your results will be wrong
//-----------------------------------------------------------------------------
template< typename T, class A >
inline int CUtlVector<T, A>::SortedFind( const T& search, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2 ) ) const
{
int start = 0, stop = Count() - 1;
while (start <= stop)
{
int mid = (start + stop) >> 1;
if ( pfnLessFunc( Base()[mid], search ) )
{
start = mid + 1;
}
else if ( pfnLessFunc( search, Base()[mid] ) )
{
stop = mid - 1;
}
else
{
return mid;
}
}
return InvalidIndex();
}
//-----------------------------------------------------------------------------
// finds the FIRST matching element ( assumes dupes )
// You must sort the list before using or your results will be wrong
//-----------------------------------------------------------------------------
template< typename T, class A >
inline int CUtlVector<T, A>::SortedFindFirst( const T& search, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2, void *pCtx ), void *pLessContext ) const
{
int start = 0, stop = Count() - 1;
while (start <= stop)
{
int mid = (start + stop) >> 1;
if ( pfnLessFunc( Base()[mid], search, pLessContext ) )
{
start = mid + 1;
}
else if ( pfnLessFunc( search, Base()[mid], pLessContext ) )
{
stop = mid - 1;
}
else
{
// found a match - but we want the first one - keep looking
if ( start == mid )
return mid;
stop = mid;
}
}
return InvalidIndex();
}
//-----------------------------------------------------------------------------
// Implementation of upper_bound(). Finds the element with the highest index
// that is less than or equal to what you're looking for.
// You must sort the list before using or your results will be wrong
// This takes a range in the vector to search, end is inclusive (Count() - 1)
//-----------------------------------------------------------------------------
template< typename T, class A >
inline int CUtlVector<T, A>::SortedFindLessOrEqual( const T& search, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2, void *pCtx ), void *pLessContext, int start, int stop ) const
{
while (start <= stop)
{
int mid = (start + stop) >> 1;
if ( pfnLessFunc( Base()[mid], search, pLessContext ) )
{
start = mid + 1;
}
else if ( pfnLessFunc( search, Base()[mid], pLessContext ) )
{
stop = mid - 1;
}
else
{
// found a match - but we want the last one - keep looking
if( stop == mid )
return mid;
if( mid == start )
{
// This means we have just start and stop elements left to check
if( stop > mid && pfnLessFunc( search, Base()[mid + 1], pLessContext ) )
return mid;
else
return mid + 1;
}
else
{
start = mid;
}
}
}
return stop;
}
//-----------------------------------------------------------------------------
// Implementation of upper_bound(). Finds the element with the highest index
// that is less than or equal to what you're looking for.
// You must sort the list before using or your results will be wrong
// Searches the entire vector
//-----------------------------------------------------------------------------
template< typename T, class A >
inline int CUtlVector<T, A>::SortedFindLessOrEqual( const T& search, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2, void *pCtx ), void *pLessContext ) const
{
return SortedFindLessOrEqual( search, pfnLessFunc, pLessContext, 0, Count() - 1 );
}
template< typename T, class A >
inline int CUtlVector<T, A>::SortedInsert( const T& src, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2, void *pCtx ), void *pLessContext )
{
int pos = SortedFindLessOrEqual( src, pfnLessFunc, pLessContext ) + 1;
GrowVector();
ShiftElementsRight(pos);
CopyConstruct<T>( &Element(pos), src );
return pos;
}
//-----------------------------------------------------------------------------
// sorted find, with no context pointer
//-----------------------------------------------------------------------------
template< typename T, class A >
inline int CUtlVector<T, A>::SortedFindLessOrEqual( const T& search, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2 ), int start, int stop ) const
{
while (start <= stop)
{
int mid = (start + stop) >> 1;
if ( pfnLessFunc( Base()[mid], search ) )
{
start = mid + 1;
}
else if ( pfnLessFunc( search, Base()[mid] ) )
{
stop = mid - 1;
}
else
{
// found a match - but we want the last one - keep looking
if( stop == mid )
return mid;
if( mid == start )
{
// This means we have just start and stop elements left to check
if( stop > mid && pfnLessFunc( search, Base()[mid + 1] ) )
return mid;
else
return mid + 1;
}
else
{
start = mid;
}
}
}
return stop;
}
//-----------------------------------------------------------------------------
// sorted find, with no context pointer
//-----------------------------------------------------------------------------
template< typename T, class A >
inline int CUtlVector<T, A>::SortedFindLessOrEqual( const T& search, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2 ) ) const
{
return SortedFindLessOrEqual( search, pfnLessFunc, 0, Count() - 1 );
}
template< typename T, class A >
inline int CUtlVector<T, A>::SortedInsert( const T& src, bool (__cdecl *pfnLessFunc)( const T& src1, const T& src2 ) )
{
int pos = SortedFindLessOrEqual( src, pfnLessFunc ) + 1;
GrowVector();
ShiftElementsRight(pos);
CopyConstruct<T>( &Element(pos), src );
return pos;
}
//-----------------------------------------------------------------------------
// Sorts the vector
//-----------------------------------------------------------------------------
template< typename T, class A >
inline void CUtlVector<T, A>::Sort( int (__cdecl *pfnCompare)(const T *, const T *) )
{
qsort( &Head(), Count(), sizeof( T ), (void*)pfnCompare );
}
//-----------------------------------------------------------------------------
// Makes sure we have enough memory allocated to store a requested # of elements
//-----------------------------------------------------------------------------
template< typename T, class A >
inline void CUtlVector<T, A>::EnsureCapacity( int num )
{
m_Memory.EnsureCapacity(num);
}
//-----------------------------------------------------------------------------
// Makes sure we have at least this many elements
//-----------------------------------------------------------------------------
template< typename T, class A >
inline void CUtlVector<T, A>::EnsureCount( int num )
{
if (Count() < num)
AddMultipleToTail( num - Count() );
}
//-----------------------------------------------------------------------------
// Shifts elements
//-----------------------------------------------------------------------------
template< typename T, class A >
inline void CUtlVector<T, A>::ShiftElementsRight( int elem, int num )
{
DbgAssert( IsValidIndex(elem) || ( m_Size == 0 ) || ( num == 0 ));
int numToMove = m_Size - elem - num;
if ((numToMove > 0) && (num > 0))
memmove( (void*)&Element(elem+num), (void *)&Element(elem), numToMove * sizeof(T) );
}
template< typename T, class A >
inline void CUtlVector<T, A>::ShiftElementsLeft( int elem, int num )
{
DbgAssert( IsValidIndex(elem) || ( m_Size == 0 ) || ( num == 0 ));
int numToMove = m_Size - elem - num;
if ((numToMove > 0) && (num > 0))
{
memmove( (void*)&Element(elem), (void*)&Element(elem+num), numToMove * sizeof(T) );
#ifdef _DEBUG
memset( (void*)&Element(m_Size-num), 0xDD, num * sizeof(T) );
#endif
}
}
//-----------------------------------------------------------------------------
// Adds an element, uses default constructor
//-----------------------------------------------------------------------------
template< typename T, class A >
inline int CUtlVector<T, A>::AddToHead()
{
return InsertBefore(0);
}
template< typename T, class A >
inline int CUtlVector<T, A>::AddToTail()
{
return InsertBefore( m_Size );
}
template< typename T, class A >
inline T *CUtlVector<T, A>::AddToTailGetPtr()
{
return &Element( AddToTail() );
}
template< typename T, class A >
inline int CUtlVector<T, A>::InsertAfter( int elem )
{
return InsertBefore( elem + 1 );
}
template< typename T, class A >
inline int CUtlVector<T, A>::InsertBefore( int elem )
{
// Can insert at the end
Assert( (elem == Count()) || IsValidIndex(elem) );
GrowVector();
ShiftElementsRight(elem);
Construct( &Element(elem) );
return elem;
}
//-----------------------------------------------------------------------------
// Adds an element, uses copy constructor
//-----------------------------------------------------------------------------
template< typename T, class A >
inline int CUtlVector<T, A>::AddToHead( const T& src )
{
return InsertBefore( 0, src );
}
template< typename T, class A >
inline int CUtlVector<T, A>::AddToTail( const T& src )
{
return InsertBefore( m_Size, src );
}
template< typename T, class A >
inline int CUtlVector<T, A>::InsertAfter( int elem, const T& src )
{
return InsertBefore( elem + 1, src );
}
template< typename T, class A >
inline int CUtlVector<T, A>::InsertBefore( int elem, const T& src )
{
// Can't insert something that's in the list... reallocation may hose us
Assert( (&src < Base()) || (&src >= (Base() + Count()) ) );
// Can insert at the end
Assert( (elem == Count()) || IsValidIndex(elem) );
GrowVector();
ShiftElementsRight(elem);
CopyConstruct( &Element(elem), src );
return elem;
}
//-----------------------------------------------------------------------------
// Adds multiple elements, uses default constructor
//-----------------------------------------------------------------------------
template< typename T, class A >
inline int CUtlVector<T, A>::AddMultipleToHead( int num )
{
return InsertMultipleBefore( 0, num );
}
template< typename T, class A >
inline int CUtlVector<T, A>::AddMultipleToTail( int num, const T *pToCopy )
{
// Can't insert something that's in the list... reallocation may hose us
Assert( !pToCopy || (pToCopy + num <= Base()) || (pToCopy >= (Base() + Count()) ) );
return InsertMultipleBefore( m_Size, num, pToCopy );
}
template< typename T, class A >
inline int CUtlVector<T, A>::InsertMultipleAfter( int elem, int num, const T *pToCopy )
{
return InsertMultipleBefore( elem + 1, num, pToCopy );
}
template< typename T, class A >
inline void CUtlVector<T, A>::SetCount( int count )
{
if ( count > m_Size )
{
int i = m_Size;
GrowVector( count - m_Size );
for ( ; i < m_Size; ++i )
{
Construct( m_Memory.Base() + i );
}
}
else if ( count >= 0 )
{
int nToRemove = m_Size - count;
m_Size = count;
while ( nToRemove-- )
{
Destruct( m_Memory.Base() + m_Size + nToRemove );
}
}
else
{
Assert( count >= 0 );
}
}
template< typename T, class A >
inline void CUtlVector<T, A>::CopyArray( const T *pArray, int size )
{
// Can't insert something that's in the list... reallocation may hose us
Assert( !pArray || (Base() >= (pArray + size)) || (pArray >= (Base() + Count()) ) );
SetCount( size );
for( int i=0; i < size; i++ )
{
m_Memory.Base()[i] = pArray[i];
}
}
template< typename T, class A >
inline void CUtlVector<T, A>::Swap( CUtlVector< T, A > &vec )
{
m_Memory.Swap( vec.m_Memory );
SWAP( m_Size, vec.m_Size );
}
template< typename T, class A >
inline int CUtlVector<T, A>::AddVectorToTail( CUtlVector const &src )
{
return AddMultipleToTail( src.Count(), src.Base() );
}
template< typename T, class A >
inline int CUtlVector<T, A>::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
for (int i = 0; i < num; ++i)
Construct( &Element(elem+i) );
// Copy stuff in?
if ( pToInsert )
{
for ( int i=0; i < num; i++ )
{
Element( elem+i ) = pToInsert[i];
}
}
return elem;
}
//-----------------------------------------------------------------------------
// Finds an element (element needs operator== defined)
//-----------------------------------------------------------------------------
template< typename T, class A >
inline int CUtlVector<T, A>::Find( const T& src ) const
{
for ( int i = 0; i < Count(); ++i )
{
if (Base()[i] == src)
return i;
}
return InvalidIndex();
}
template< typename T, class A >
inline bool CUtlVector<T, A>::HasElement( const T& src ) const
{
return ( Find(src) != InvalidIndex() );
}
//-----------------------------------------------------------------------------
// Element removal
//-----------------------------------------------------------------------------
template< typename T, class A >
inline void CUtlVector<T, A>::FastRemove( int elem )
{
Assert( IsValidIndex(elem) );
Destruct( &Base()[elem] );
if (m_Size > 0)
{
if ( elem != m_Size - 1 )
{
// explicitly cast the first + second param here to void * to stop the compiler
// noticing that we are memsetting the this pointer for non-trivial classes
memcpy( (void *)&Base()[elem], (void *)&Base()[m_Size-1], sizeof(T) );
}
--m_Size;
}
}
template< typename T, class A >
inline void CUtlVector<T, A>::Remove( int elem )
{
Destruct( &Element(elem) );
ShiftElementsLeft(elem);
--m_Size;
}
template< typename T, class A >
inline bool CUtlVector<T, A>::FindAndRemove( const T& src )
{
int elem = Find( src );
if ( elem != InvalidIndex() )
{
Remove( elem );
return true;
}
return false;
}
template< typename T, class A >
inline bool CUtlVector<T, A>::FindAndFastRemove( const T& src )
{
int elem = Find( src );
if ( elem != InvalidIndex() )
{
FastRemove( elem );
return true;
}
return false;
}
template< typename T, class A >
void CUtlVector<T, A>::RemoveMultiple( int elem, int num )
{
Assert( elem >= 0 && num >= 0 && INT_MAX - elem >= num );
#if defined(COMPILER_GCC) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5))
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-overflow"
#endif
Assert( elem + num <= Count() );
#if defined(COMPILER_GCC) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5))
#pragma GCC diagnostic pop
#endif
for (int i = elem + num; --i >= elem; )
Destruct(&Base()[i]);
ShiftElementsLeft(elem, num);
m_Size -= num;
}
template< typename T, class A >
inline void CUtlVector<T, A>::RemoveMultipleFromTail( int num )
{
int nToRemove = Min( m_Size, num );
if ( nToRemove > 0 )
{
m_Size -= nToRemove;
while ( nToRemove-- )
{
Destruct( m_Memory.Base() + m_Size + nToRemove );
}
}
else
{
Assert( num >= 0 );
}
}
template< typename T, class A >
inline void CUtlVector<T, A>::RemoveAll()
{
for (int i = m_Size; --i >= 0; )
{
Destruct( m_Memory.Base() + i );
}
m_Size = 0;
}
//-----------------------------------------------------------------------------
// Memory deallocation
//-----------------------------------------------------------------------------
template< typename T, class A >
inline void CUtlVector<T, A>::Purge()
{
RemoveAll();
m_Memory.Purge();
}
template< typename T, class A >
inline void CUtlVector<T, A>::PurgeAndDeleteElements()
{
for( int i=0; i < m_Size; i++ )
{
delete Element(i);
}
Purge();
}
template< typename T, class A >
inline void CUtlVector<T, A>::PurgeAndDeleteElementsArray()
{
for( int i=0; i < m_Size; i++ )
{
delete[] Element(i);
}
Purge();
}
template< typename T, class A >
inline void CUtlVector<T, A>::Compact()
{
m_Memory.Purge(m_Size);
}
// easy string list class with dynamically allocated strings. For use with V_SplitString, etc.