-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.h
1231 lines (1016 loc) · 26.5 KB
/
List.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
#ifndef __LIST_H__
#define __LIST_H__
/*
===============================================================================
List template
Does not allocate memory until the first item is added.
===============================================================================
*/
/*
================
idListSortCompare<type>
================
*/
#ifdef __INTEL_COMPILER
// the intel compiler doesn't do the right thing here
template< class type >
ID_INLINE int idListSortCompare( const type *a, const type *b ) {
assert( 0 );
return 0;
}
#else
template< class type >
ID_INLINE int idListSortCompare( const type *a, const type *b ) {
return *a - *b;
}
#endif
/*
================
idListNewElement<type>
================
*/
template< class type >
ID_INLINE type *idListNewElement( void ) {
return new type;
}
/*
================
idSwap<type>
================
*/
template< class type >
ID_INLINE void idSwap( type &a, type &b ) {
type c = a;
a = b;
b = c;
}
template< class type >
class idList {
public:
typedef int cmp_t( const type *, const type * );
typedef int filter_t( const type * );
typedef type new_t( void );
idList( int newgranularity = 16 );
idList( const idList<type> &other );
~idList<type>( void );
void Clear( void ); // clear the list
int Num( void ) const; // returns number of elements in list
int NumAllocated( void ) const; // returns number of elements allocated for
void SetGranularity( int newgranularity ); // set new granularity
int GetGranularity( void ) const; // get the current granularity
// RAVEN BEGIN
// mwhitlock: Dynamic memory consolidation
#if defined(_RV_MEM_SYS_SUPPORT)
void SetAllocatorHeap ( rvHeap* heap ); // set the heap used for all allocations
#endif
// RAVEN END
size_t Allocated( void ) const; // returns total size of allocated memory
size_t Size( void ) const; // returns total size of allocated memory including size of list type
size_t MemoryUsed( void ) const; // returns size of the used elements in the list
idList<type> & operator=( const idList<type> &other );
const type & operator[]( int index ) const;
type & operator[]( int index );
void Condense( void ); // resizes list to exactly the number of elements it contains
void Resize( int newsize ); // resizes list to the given number of elements
void Resize( int newsize, int newgranularity ); // resizes list and sets new granularity
void SetNum( int newnum, bool resize = true ); // set number of elements in list and resize to exactly this number if necessary
void AssureSize( int newSize); // assure list has given number of elements, but leave them uninitialized
void AssureSize( int newSize, const type &initValue ); // assure list has given number of elements and initialize any new elements
void AssureSizeAlloc( int newSize, new_t *allocator ); // assure the pointer list has the given number of elements and allocate any new elements
type * Ptr( void ); // returns a pointer to the list
const type * Ptr( void ) const; // returns a pointer to the list
type & Alloc( void ); // returns reference to a new data element at the end of the list
int Append( const type & obj ); // append element
int Append( const idList<type> &other ); // append list
int AddUnique( const type & obj ); // add unique element
int Insert( const type & obj, int index = 0 ); // insert the element at the given index
int FindIndex( const type & obj ) const; // find the index for the given element
type * Find( type const & obj ) const; // find pointer to the given element
int FindNull( void ) const; // find the index for the first NULL pointer in the list
int IndexOf( const type *obj ) const; // returns the index for the pointer to an element in the list
bool RemoveIndex( int index ); // remove the element at the given index
bool Remove( const type & obj ); // remove the element
void Sort( cmp_t *compare = ( cmp_t * )&idListSortCompare<type> );
void SortSubSection( int startIndex, int endIndex, cmp_t *compare = ( cmp_t * )&idListSortCompare<type> );
void Swap( idList<type> &other ); // swap the contents of the lists
void DeleteContents( bool clear ); // delete the contents of the list
//RAVENBEGIN
// cdr : added Heap & Stack & Sort functionality
int FindBinary ( const type & key, cmp_t *compare = ( cmp_t * )&idListSortCompare<type> ) const;
void StackAdd( const type & obj ); // add to the stack
void StackPop( void ); // remove from the stack
type & StackTop( void ); // get the top element of the stack
void HeapAdd( const type & obj ); // add to the heap, and resort
void HeapPop( void ); // pop off the top of the heap & resort
// abahr:
int TypeSize() const { return sizeof(type); }
void RemoveNull();
// gcc 4.0: see ListGame.h
void RemoveContents( bool clear );
// ddynerman: range remove
bool RemoveRange( int low, int high );
void RemoveDuplicates( void );
//RAVEN END
private:
int num;
int size;
int granularity;
type * list;
// RAVEN BEGIN
// mwhitlock: Dynamic memory consolidation
#if defined(_RV_MEM_SYS_SUPPORT)
rvHeap* allocatorHeap;
#endif
// RAVEN END
};
/*
================
idList<type>::idList( int )
================
*/
template< class type >
ID_INLINE idList<type>::idList( int newgranularity ) {
assert( newgranularity > 0 );
list = NULL;
granularity = newgranularity;
Clear();
// RAVEN BEGIN
// mwhitlock: Dynamic memory consolidation
#if defined(_RV_MEM_SYS_SUPPORT)
allocatorHeap = 0;
#endif
// RAVEN END
}
/*
================
idList<type>::idList( const idList<type> &other )
================
*/
template< class type >
ID_INLINE idList<type>::idList( const idList<type> &other ) {
list = NULL;
*this = other;
}
/*
================
idList<type>::~idList<type>
================
*/
template< class type >
ID_INLINE idList<type>::~idList( void ) {
Clear();
}
/*
================
idList<type>::Clear
Frees up the memory allocated by the list. Assumes that type automatically handles freeing up memory.
================
*/
template< class type >
ID_INLINE void idList<type>::Clear( void ) {
if ( list ) {
delete[] list;
}
list = NULL;
num = 0;
size = 0;
}
/*
================
idList<type>::DeleteContents
Calls the destructor of all elements in the list. Conditionally frees up memory used by the list.
Note that this only works on lists containing pointers to objects and will cause a compiler error
if called with non-pointers. Since the list was not responsible for allocating the object, it has
no information on whether the object still exists or not, so care must be taken to ensure that
the pointers are still valid when this function is called. Function will set all pointers in the
list to NULL.
================
*/
template< class type >
ID_INLINE void idList<type>::DeleteContents( bool clear ) {
int i;
for( i = 0; i < num; i++ ) {
delete list[ i ];
list[ i ] = NULL;
}
if ( clear ) {
Clear();
} else {
memset( list, 0, size * sizeof( type ) );
}
}
/*
================
idList<type>::Allocated
return total memory allocated for the list in bytes, but doesn't take into account additional memory allocated by type
================
*/
template< class type >
ID_INLINE size_t idList<type>::Allocated( void ) const {
return size * sizeof( type );
}
/*
================
idList<type>::Size
return total size of list in bytes, but doesn't take into account additional memory allocated by type
================
*/
template< class type >
ID_INLINE size_t idList<type>::Size( void ) const {
return sizeof( idList<type> ) + Allocated();
}
/*
================
idList<type>::MemoryUsed
================
*/
template< class type >
ID_INLINE size_t idList<type>::MemoryUsed( void ) const {
return num * sizeof( *list );
}
/*
================
idList<type>::Num
Returns the number of elements currently contained in the list.
Note that this is NOT an indication of the memory allocated.
================
*/
template< class type >
ID_INLINE int idList<type>::Num( void ) const {
return num;
}
/*
================
idList<type>::NumAllocated
Returns the number of elements currently allocated for.
================
*/
template< class type >
ID_INLINE int idList<type>::NumAllocated( void ) const {
return size;
}
/*
================
idList<type>::SetNum
Resize to the exact size specified irregardless of granularity
================
*/
template< class type >
ID_INLINE void idList<type>::SetNum( int newnum, bool resize ) {
assert( newnum >= 0 );
if ( resize || newnum > size ) {
Resize( newnum );
}
num = newnum;
}
/*
================
idList<type>::SetGranularity
Sets the base size of the array and resizes the array to match.
================
*/
template< class type >
ID_INLINE void idList<type>::SetGranularity( int newgranularity ) {
int newsize;
assert( newgranularity > 0 );
granularity = newgranularity;
if ( list ) {
// resize it to the closest level of granularity
newsize = num + granularity - 1;
newsize -= newsize % granularity;
if ( newsize != size ) {
Resize( newsize );
}
}
}
/*
================
idList<type>::GetGranularity
Get the current granularity.
================
*/
template< class type >
ID_INLINE int idList<type>::GetGranularity( void ) const {
return granularity;
}
/*
================
idList<type>::Condense
Resizes the array to exactly the number of elements it contains or frees up memory if empty.
================
*/
template< class type >
ID_INLINE void idList<type>::Condense( void ) {
if ( list ) {
if ( num ) {
Resize( num );
} else {
Clear();
}
}
}
/*
================
idList<type>::Resize
Allocates memory for the amount of elements requested while keeping the contents intact.
Contents are copied using their = operator so that data is correnctly instantiated.
================
*/
template< class type >
ID_INLINE void idList<type>::Resize( int newsize ) {
type *temp;
int i;
assert( newsize >= 0 );
// free up the list if no data is being reserved
if ( newsize <= 0 ) {
Clear();
return;
}
if ( newsize == size ) {
// not changing the size, so just exit
return;
}
temp = list;
size = newsize;
if ( size < num ) {
num = size;
}
// RAVEN BEGIN
// mwhitlock: Dynamic memory consolidation
#if defined(_RV_MEM_SYS_SUPPORT_CONTAINERS)
bool ok=false;
if(allocatorHeap)
{
RV_PUSH_HEAP_PTR(allocatorHeap);
ok=true;
}
else
{
ok=rvPushHeapContainingMemory(this);
}
#endif
// RAVEN END
// copy the old list into our new one
list = new type[ size ];
for( i = 0; i < num; i++ ) {
list[ i ] = temp[ i ];
}
// RAVEN BEGIN
// mwhitlock: Dynamic memory consolidation
#if defined(_RV_MEM_SYS_SUPPORT_CONTAINERS)
if(ok)
{
RV_POP_HEAP();
}
#endif
// RAVEN END
// delete the old list if it exists
if ( temp ) {
delete[] temp;
}
}
/*
================
idList<type>::Resize
Allocates memory for the amount of elements requested while keeping the contents intact.
Contents are copied using their = operator so that data is correnctly instantiated.
================
*/
template< class type >
ID_INLINE void idList<type>::Resize( int newsize, int newgranularity ) {
type *temp;
int i;
assert( newsize >= 0 );
assert( newgranularity > 0 );
granularity = newgranularity;
// free up the list if no data is being reserved
if ( newsize <= 0 ) {
Clear();
return;
}
temp = list;
size = newsize;
if ( size < num ) {
num = size;
}
// copy the old list into our new one
// RAVEN BEGIN
// mwhitlock: Dynamic memory consolidation
#if defined(_RV_MEM_SYS_SUPPORT_CONTAINERS)
bool ok=rvPushHeapContainingMemory(this);
#endif
// RAVEN END
list = new type[ size ];
for( i = 0; i < num; i++ ) {
list[ i ] = temp[ i ];
}
// RAVEN BEGIN
// mwhitlock: Dynamic memory consolidation
#if defined(_RV_MEM_SYS_SUPPORT_CONTAINERS)
if(ok)
{
RV_POP_HEAP();
}
#endif
// RAVEN END
// delete the old list if it exists
if ( temp ) {
delete[] temp;
}
}
/*
================
idList<type>::AssureSize
Makes sure the list has at least the given number of elements.
================
*/
template< class type >
ID_INLINE void idList<type>::AssureSize( int newSize ) {
int newNum = newSize;
if ( newSize > size ) {
if ( granularity == 0 ) { // this is a hack to fix our memset classes
granularity = 16;
}
newSize += granularity - 1;
newSize -= newSize % granularity;
Resize( newSize );
}
num = newNum;
}
/*
================
idList<type>::AssureSize
Makes sure the list has at least the given number of elements and initialize any elements not yet initialized.
================
*/
template< class type >
ID_INLINE void idList<type>::AssureSize( int newSize, const type &initValue ) {
int newNum = newSize;
if ( newSize > size ) {
if ( granularity == 0 ) { // this is a hack to fix our memset classes
granularity = 16;
}
newSize += granularity - 1;
newSize -= newSize % granularity;
num = size;
Resize( newSize );
for ( int i = num; i < newSize; i++ ) {
list[i] = initValue;
}
}
num = newNum;
}
/*
================
idList<type>::AssureSizeAlloc
Makes sure the list has at least the given number of elements and allocates any elements using the allocator.
NOTE: This function can only be called on lists containing pointers. Calling it
on non-pointer lists will cause a compiler error.
================
*/
template< class type >
ID_INLINE void idList<type>::AssureSizeAlloc( int newSize, new_t *allocator ) {
int newNum = newSize;
if ( newSize > size ) {
if ( granularity == 0 ) { // this is a hack to fix our memset classes
granularity = 16;
}
newSize += granularity - 1;
newSize -= newSize % granularity;
num = size;
Resize( newSize );
for ( int i = num; i < newSize; i++ ) {
list[i] = (*allocator)();
}
}
num = newNum;
}
/*
================
idList<type>::operator=
Copies the contents and size attributes of another list.
================
*/
template< class type >
ID_INLINE idList<type> &idList<type>::operator=( const idList<type> &other ) {
int i;
Clear();
num = other.num;
size = other.size;
granularity = other.granularity;
if ( size ) {
// RAVEN BEGIN
// mwhitlock: Dynamic memory consolidation
#if defined(_RV_MEM_SYS_SUPPORT_CONTAINERS)
bool ok=rvPushHeapContainingMemory(this);
#endif
// RAVEN END
list = new type[ size ];
for( i = 0; i < num; i++ ) {
list[ i ] = other.list[ i ];
}
// RAVEN BEGIN
// mwhitlock: Dynamic memory consolidation
#if defined(_RV_MEM_SYS_SUPPORT_CONTAINERS)
if(ok)
{
RV_POP_HEAP();
}
#endif
// RAVEN END
}
return *this;
}
/*
================
idList<type>::operator[] const
Access operator. Index must be within range or an assert will be issued in debug builds.
Release builds do no range checking.
================
*/
template< class type >
ID_INLINE const type &idList<type>::operator[]( int index ) const {
assert( index >= 0 );
assert( index < num );
return list[ index ];
}
/*
================
idList<type>::operator[]
Access operator. Index must be within range or an assert will be issued in debug builds.
Release builds do no range checking.
================
*/
template< class type >
ID_INLINE type &idList<type>::operator[]( int index ) {
assert( index >= 0 );
assert( index < num );
return list[ index ];
}
/*
================
idList<type>::Ptr
Returns a pointer to the begining of the array. Useful for iterating through the list in loops.
Note: may return NULL if the list is empty.
FIXME: Create an iterator template for this kind of thing.
================
*/
template< class type >
ID_INLINE type *idList<type>::Ptr( void ) {
return list;
}
/*
================
idList<type>::Ptr
Returns a pointer to the begining of the array. Useful for iterating through the list in loops.
Note: may return NULL if the list is empty.
FIXME: Create an iterator template for this kind of thing.
================
*/
template< class type >
const ID_INLINE type *idList<type>::Ptr( void ) const {
return list;
}
/*
================
idList<type>::Alloc
Returns a reference to a new data element at the end of the list.
================
*/
template< class type >
ID_INLINE type &idList<type>::Alloc( void ) {
if ( !list ) {
Resize( granularity );
}
if ( num == size ) {
Resize( size + granularity );
}
return list[ num++ ];
}
/*
================
idList<type>::Append
Increases the size of the list by one element and copies the supplied data into it.
Returns the index of the new element.
================
*/
template< class type >
ID_INLINE int idList<type>::Append( type const & obj ) {
if ( !list ) {
Resize( granularity );
}
if ( num == size ) {
int newsize;
if ( granularity == 0 ) { // this is a hack to fix our memset classes
granularity = 16;
}
newsize = size + granularity;
Resize( newsize - newsize % granularity );
}
list[ num ] = obj;
num++;
return num - 1;
}
/*
================
idList<type>::Insert
Increases the size of the list by at leat one element if necessary
and inserts the supplied data into it.
Returns the index of the new element.
================
*/
template< class type >
ID_INLINE int idList<type>::Insert( type const & obj, int index ) {
if ( !list ) {
Resize( granularity );
}
if ( num == size ) {
int newsize;
if ( granularity == 0 ) { // this is a hack to fix our memset classes
granularity = 16;
}
newsize = size + granularity;
Resize( newsize - newsize % granularity );
}
if ( index < 0 ) {
index = 0;
}
else if ( index > num ) {
index = num;
}
for ( int i = num; i > index; --i ) {
list[i] = list[i-1];
}
num++;
list[index] = obj;
return index;
}
/*
================
idList<type>::Append
adds the other list to this one
Returns the size of the new combined list
================
*/
template< class type >
ID_INLINE int idList<type>::Append( const idList<type> &other ) {
if ( !list ) {
if ( granularity == 0 ) { // this is a hack to fix our memset classes
granularity = 16;
}
Resize( granularity );
}
int n = other.Num();
for (int i = 0; i < n; i++) {
Append(other[i]);
}
return Num();
}
/*
================
idList<type>::AddUnique
Adds the data to the list if it doesn't already exist. Returns the index of the data in the list.
================
*/
template< class type >
ID_INLINE int idList<type>::AddUnique( type const & obj ) {
int index;
index = FindIndex( obj );
if ( index < 0 ) {
index = Append( obj );
}
return index;
}
/*
================
idList<type>::FindIndex
Searches for the specified data in the list and returns it's index. Returns -1 if the data is not found.
================
*/
template< class type >
ID_INLINE int idList<type>::FindIndex( type const & obj ) const {
int i;
for( i = 0; i < num; i++ ) {
if ( list[ i ] == obj ) {
return i;
}
}
// Not found
return -1;
}
/*
================
idList<type>::Find
Searches for the specified data in the list and returns it's address. Returns NULL if the data is not found.
================
*/
template< class type >
ID_INLINE type *idList<type>::Find( type const & obj ) const {
int i;
i = FindIndex( obj );
if ( i >= 0 ) {
return &list[ i ];
}
return NULL;
}
/*
================
idList<type>::FindNull
Searches for a NULL pointer in the list. Returns -1 if NULL is not found.
NOTE: This function can only be called on lists containing pointers. Calling it
on non-pointer lists will cause a compiler error.
================
*/
template< class type >
ID_INLINE int idList<type>::FindNull( void ) const {
int i;
for( i = 0; i < num; i++ ) {
if ( list[ i ] == NULL ) {
return i;
}
}
// Not found
return -1;
}
/*
================
idList<type>::IndexOf
Takes a pointer to an element in the list and returns the index of the element.
This is NOT a guarantee that the object is really in the list.
Function will assert in debug builds if pointer is outside the bounds of the list,
but remains silent in release builds.
================
*/
template< class type >
ID_INLINE int idList<type>::IndexOf( type const *objptr ) const {
int index;
index = objptr - list;
assert( index >= 0 );
assert( index < num );
return index;
}
/*
================
idList<type>::RemoveIndex
Removes the element at the specified index and moves all data following the element down to fill in the gap.
The number of elements in the list is reduced by one. Returns false if the index is outside the bounds of the list.
Note that the element is not destroyed, so any memory used by it may not be freed until the destruction of the list.
================
*/
template< class type >
ID_INLINE bool idList<type>::RemoveIndex( int index ) {
int i;
assert( list != NULL );
assert( index >= 0 );
assert( index < num );
if ( ( index < 0 ) || ( index >= num ) ) {
return false;
}
num--;
for( i = index; i < num; i++ ) {
list[ i ] = list[ i + 1 ];
}
return true;
}
/*
================
idList<type>::Remove
Removes the element if it is found within the list and moves all data following the element down to fill in the gap.
The number of elements in the list is reduced by one. Returns false if the data is not found in the list. Note that
the element is not destroyed, so any memory used by it may not be freed until the destruction of the list.
================
*/
template< class type >
ID_INLINE bool idList<type>::Remove( type const & obj ) {
int index;
index = FindIndex( obj );
if ( index >= 0 ) {
return RemoveIndex( index );
}
return false;
}
/*
================
idList<type>::Sort
Performs a qsort on the list using the supplied comparison function. Note that the data is merely moved around the
list, so any pointers to data within the list may no longer be valid.
================
*/
template< class type >
ID_INLINE void idList<type>::Sort( cmp_t *compare ) {
if ( !list ) {
return;
}
typedef int cmp_c(const void *, const void *);
cmp_c *vCompare = (cmp_c *)compare;
qsort( ( void * )list, ( size_t )num, sizeof( type ), vCompare );
}
/*
================
idList<type>::SortSubSection
Sorts a subsection of the list.
================
*/
template< class type >
ID_INLINE void idList<type>::SortSubSection( int startIndex, int endIndex, cmp_t *compare ) {
if ( !list ) {
return;
}
if ( startIndex < 0 ) {
startIndex = 0;
}
if ( endIndex >= num ) {
endIndex = num - 1;
}
if ( startIndex >= endIndex ) {
return;
}
typedef int cmp_c(const void *, const void *);