forked from facebookarchive/RakNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS_Multilist.h
1650 lines (1455 loc) · 48.6 KB
/
DS_Multilist.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) 2014, Oculus VR, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
/// \file DS_Multilist.h
/// \internal
/// \brief ADT that can represent an unordered list, ordered list, stack, or queue with a common interface
///
#ifndef __MULTILIST_H
#define __MULTILIST_H
#include "RakAssert.h"
#include <string.h> // memmove
#include "Export.h"
#include "RakMemoryOverride.h"
#include "NativeTypes.h"
#ifdef _MSC_VER
#pragma warning( push )
#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant
#pragma warning( disable : 4512 ) // warning C4512: assignment operator could not be generated
#endif
/// What algorithm to use to store the data for the Multilist
enum MultilistType
{
/// Removing from the middle of the list will swap the end of the list rather than shift the elements. Push and Pop operate on the tail.
ML_UNORDERED_LIST,
/// A normal list, with the list order preserved. Push and Pop operate on the tail.
ML_STACK,
/// A queue. Push and Pop operate on the head
ML_QUEUE,
/// A list that is always kept in order. Elements must be unique, and compare against each other consistently using <, ==, and >
ML_ORDERED_LIST,
/// A list whose type can change at runtime
ML_VARIABLE_DURING_RUNTIME
};
/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures
/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish.
namespace DataStructures
{
/// Can be used with Multilist::ForEach
/// Assuming the Multilist holds pointers, will delete those pointers
template <class templateType>
void DeletePtr_RakNet(templateType &ptr, const char *file, unsigned int line ) {RakNet::OP_DELETE(ptr, file, line);}
/// Can be used with Multilist::ForEach
/// Assuming the Multilist holds pointers, will delete those pointers
template <class templateType>
void DeletePtr(templateType &ptr) {delete ptr;}
/// The following is invalid.
/// bool operator<( const MyClass *myClass, const int &inputKey ) {return myClass->value < inputKey;}
/// At least one type has to be a reference to a class
/// MLKeyRef is a helper class to turn a native type into a class, so you can compare that native type against a pointer to a different class
/// Used for he Multilist, when _DataType != _KeyType
template < class templateType >
class MLKeyRef
{
public:
MLKeyRef(const templateType& input) : val(input) {}
const templateType &Get(void) const {return val;}
bool operator<( const templateType &right ) {return val < right;}
bool operator>( const templateType &right ) {return val > right;}
bool operator==( const templateType &right ) {return val == right;}
protected:
const templateType &val;
};
/// For the Multilist, when _DataType != _KeyType, you must define the comparison operators between the key and the data
/// This is non-trivial due to the need to use MLKeyRef in case the type held is a pointer to a structure or class and the key type is not a class
/// For convenience, this macro will implement the comparison operators under the following conditions
/// 1. _DataType is a pointer to a class or structure
/// 2. The key is a member variable of _DataType
#define DEFINE_MULTILIST_PTR_TO_MEMBER_COMPARISONS( _CLASS_NAME_, _KEY_TYPE_, _MEMBER_VARIABLE_NAME_ ) \
bool operator<( const DataStructures::MLKeyRef<_KEY_TYPE_> &inputKey, const _CLASS_NAME_ *cls ) {return inputKey.Get() < cls->_MEMBER_VARIABLE_NAME_;} \
bool operator>( const DataStructures::MLKeyRef<_KEY_TYPE_> &inputKey, const _CLASS_NAME_ *cls ) {return inputKey.Get() > cls->_MEMBER_VARIABLE_NAME_;} \
bool operator==( const DataStructures::MLKeyRef<_KEY_TYPE_> &inputKey, const _CLASS_NAME_ *cls ) {return inputKey.Get() == cls->_MEMBER_VARIABLE_NAME_;}
typedef uint32_t DefaultIndexType;
/// \brief The multilist, representing an abstract data type that generally holds lists.
/// \param[in] _MultilistType What type of list this is, \sa MultilistType
/// \param[in] _DataType What type of data this list holds.
/// \param[in] _KeyType If a function takes a key to sort on, what type of key this is. The comparison operator between _DataType and _KeyType must be defined
/// \param[in] _IndexType What variable type to use for indices
template <const MultilistType _MultilistType, class _DataType, class _KeyType=_DataType, class _IndexType=DefaultIndexType>
class RAK_DLL_EXPORT Multilist
{
public:
Multilist();
~Multilist();
Multilist( const Multilist& source );
Multilist& operator= ( const Multilist& source );
_DataType& operator[] ( const _IndexType position ) const;
/// Unordered list, stack is LIFO
/// QUEUE is FIFO
/// Ordered list is inserted in order
void Push(const _DataType &d, const char *file=__FILE__, unsigned int line=__LINE__ );
void Push(const _DataType &d, const _KeyType &key, const char *file=__FILE__, unsigned int line=__LINE__ );
/// \brief Gets or removes and gets an element from the list, according to the same rules as Push().
/// Ordered list is LIFO for the purposes of Pop and Peek.
_DataType &Pop(const char *file=__FILE__, unsigned int line=__LINE__);
_DataType &Peek(void) const;
/// \brief Same as Push(), except FIFO and LIFO are reversed.
/// Ordered list still inserts in order.
void PushOpposite(const _DataType &d, const char *file=__FILE__, unsigned int line=__LINE__ );
void PushOpposite(const _DataType &d, const _KeyType &key, const char *file=__FILE__, unsigned int line=__LINE__ );
/// \brief Same as Pop() and Peek(), except FIFO and LIFO are reversed.
_DataType &PopOpposite(const char *file=__FILE__, unsigned int line=__LINE__);
_DataType &PeekOpposite(void) const;
/// \brief Stack,Queue: Inserts at index indicated, elements are shifted.
/// Ordered list: Inserts, position is ignored
void InsertAtIndex(const _DataType &d, _IndexType index, const char *file=__FILE__, unsigned int line=__LINE__);
/// \brief Unordered list, removes at index indicated, swaps last element with that element.
/// Otherwise, array is shifted left to overwrite removed element
/// \details Index[0] returns the same as Pop() for a queue.
/// Same as PopOpposite() for the list and ordered list
void RemoveAtIndex(_IndexType position, const char *file=__FILE__, unsigned int line=__LINE__);
/// \brief Find the index of \a key, and remove at that index.
bool RemoveAtKey(_KeyType key, bool assertIfDoesNotExist, const char *file=__FILE__, unsigned int line=__LINE__);
/// \brief Finds the index of \a key. Return -1 if the key is not found.
_IndexType GetIndexOf(_KeyType key) const;
/// \brief Returns where in the list we should insert the item, to preserve list order.
/// Returns -1 if the item is already in the list
_IndexType GetInsertionIndex(_KeyType key) const;
/// \brief Finds the index of \a key. Return 0 if the key is not found. Useful if _DataType is always non-zero pointers.
_DataType GetPtr(_KeyType key) const;
/// \brief Iterate over the list, calling the function pointer on each element.
void ForEach(void (*func)(_DataType &item, const char *file, unsigned int line), const char *file, unsigned int line);
void ForEach(void (*func)(_DataType &item));
/// \brief Returns if the list is empty.
bool IsEmpty(void) const;
/// \brief Returns the number of elements used in the list.
_IndexType GetSize(void) const;
/// \brief Empties the list. The list is not deallocated if it is small,
/// unless \a deallocateSmallBlocks is true
void Clear( bool deallocateSmallBlocks=true, const char *file=__FILE__, unsigned int line=__LINE__ );
/// \brief Empties the list, first calling RakNet::OP_Delete on all items.
/// \details The list is not deallocated if it is small, unless \a deallocateSmallBlocks is true
void ClearPointers( bool deallocateSmallBlocks=true, const char *file=__FILE__, unsigned int line=__LINE__ );
/// \brief Empty one item from the list, first calling RakNet::OP_Delete on that item.
void ClearPointer( _KeyType key, const char *file=__FILE__, unsigned int line=__LINE__ );
/// \brief Reverses the elements in the list, and flips the sort order
/// returned by GetSortOrder() if IsSorted() returns true at the time the function is called
void ReverseList(void);
/// \brief Reallocates the list to a larger size.
/// If \a size is smaller than the value returned by GetSize(), the call does nothing.
void Reallocate(_IndexType size, const char *file=__FILE__, unsigned int line=__LINE__);
/// \brief Sorts the list unless it is an ordered list, in which it does nothing as the list is assumed to already be sorted.
/// \details However, if \a force is true, it will also resort the ordered list, useful if the comparison operator between _KeyType and _DataType would now return different results
/// Once the list is sorted, further operations to lookup by key will be log2(n) until the list is modified
void Sort(bool force);
/// \brief Sets the list to be remembered as sorted.
/// \details Optimization if the source is sorted already
void TagSorted(void);
/// \brief Defaults to ascending.
/// \details Used by Sort(), and by ML_ORDERED_LIST
void SetSortOrder(bool ascending);
/// \brief Returns true if ascending.
bool GetSortOrder(void) const;
/// \brief Returns true if the list is currently believed to be in a sorted state.
/// \details Doesn't actually check for sortedness, just if Sort()
/// was recently called, or MultilistType is ML_ORDERED_LIST
bool IsSorted(void) const;
/// Returns what type of list this is
MultilistType GetMultilistType(void) const;
/// \brief Changes what type of list this is.
/// \pre Template must be defined with ML_VARIABLE_DURING_RUNTIME for this to do anything
/// \param[in] mlType Any value of the enum MultilistType, except ML_VARIABLE_DURING_RUNTIME
void SetMultilistType(MultilistType newType);
/// \brief Returns the intersection of two lists.
/// Intersection is items common to both lists.
static void FindIntersection(
Multilist& source1,
Multilist& source2,
Multilist& intersection,
Multilist& uniqueToSource1,
Multilist& uniqueToSource2);
protected:
void ReallocateIfNeeded(const char *file, unsigned int line);
void DeallocateIfNeeded(const char *file, unsigned int line);
void ReallocToSize(_IndexType newAllocationSize, const char *file, unsigned int line);
void ReverseListInternal(void);
void InsertInOrderedList(const _DataType &d, const _KeyType &key);
_IndexType GetIndexFromKeyInSortedList(const _KeyType &key, bool *objectExists) const;
void InsertShiftArrayRight(const _DataType &d, _IndexType index);
void DeleteShiftArrayLeft(_IndexType index);
void QSortAscending(_IndexType left, _IndexType right);
void QSortDescending(_IndexType left, _IndexType right);
void CopySource( const Multilist& source );
/// An array of user values
_DataType* data;
/// Number of elements in the list
_IndexType dataSize;
/// Size of \a array
_IndexType allocationSize;
/// Array index for the head of the queue
_IndexType queueHead;
/// Array index for the tail of the queue
_IndexType queueTail;
/// How many bytes the user chose to preallocate
/// Won't automatically deallocate below this
_IndexType preallocationSize;
enum
{
ML_UNSORTED,
ML_SORTED_ASCENDING,
ML_SORTED_DESCENDING
} sortState;
bool ascendingSort;
// In case we are using the variable type multilist
MultilistType variableMultilistType;
};
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Multilist()
{
data=0;
dataSize=0;
allocationSize=0;
ascendingSort=true;
sortState=ML_UNSORTED;
queueHead=0;
queueTail=0;
preallocationSize=0;
if (_MultilistType==ML_ORDERED_LIST)
sortState=ML_SORTED_ASCENDING;
else
sortState=ML_UNSORTED;
if (_MultilistType==ML_VARIABLE_DURING_RUNTIME)
variableMultilistType=ML_UNORDERED_LIST;
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::~Multilist()
{
if (data!=0)
RakNet::OP_DELETE_ARRAY(data, _FILE_AND_LINE_);
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Multilist( const Multilist& source )
{
CopySource(source);
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
Multilist<_MultilistType, _DataType, _KeyType, _IndexType>& Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::operator= ( const Multilist& source )
{
Clear(true);
CopySource(source);
return *this;
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::CopySource( const Multilist& source )
{
dataSize=source.GetSize();
ascendingSort=source.ascendingSort;
sortState=source.sortState;
queueHead=0;
queueTail=dataSize;
preallocationSize=source.preallocationSize;
variableMultilistType=source.variableMultilistType;
if (source.data==0)
{
data=0;
allocationSize=0;
}
else
{
allocationSize=dataSize;
data = RakNet::OP_NEW_ARRAY<_DataType>(dataSize,_FILE_AND_LINE_);
_IndexType i;
for (i=0; i < dataSize; i++)
data[i]=source[i];
}
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
_DataType& Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::operator[] ( const _IndexType position ) const
{
RakAssert(position<GetSize());
if (GetMultilistType()==ML_QUEUE)
{
if ( queueHead + position >= allocationSize )
return data[ queueHead + position - allocationSize ];
else
return data[ queueHead + position ];
}
return data[position];
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Push(const _DataType &d, const char *file, unsigned int line )
{
Push(d,d,file,line);
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Push(const _DataType &d, const _KeyType &key, const char *file, unsigned int line )
{
ReallocateIfNeeded(file,line);
if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK)
{
data[dataSize]=d;
dataSize++;
}
else if (GetMultilistType()==ML_QUEUE)
{
data[queueTail++] = d;
if ( queueTail == allocationSize )
queueTail = 0;
dataSize++;
}
else
{
RakAssert(GetMultilistType()==ML_ORDERED_LIST);
InsertInOrderedList(d,key);
}
if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_QUEUE)
{
// Break sort if no longer sorted
if (sortState!=ML_UNSORTED && dataSize>1)
{
if (ascendingSort)
{
if ( MLKeyRef<_KeyType>(key) < operator[](dataSize-2) )
sortState=ML_UNSORTED;
}
else
{
if ( MLKeyRef<_KeyType>(key) > operator[](dataSize-2) )
sortState=ML_UNSORTED;
}
sortState=ML_UNSORTED;
}
}
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
_DataType &Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Pop(const char *file, unsigned int line)
{
RakAssert(IsEmpty()==false);
DeallocateIfNeeded(file,line);
if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST)
{
dataSize--;
return data[dataSize];
}
else
{
RakAssert(GetMultilistType()==ML_QUEUE);
if ( ++queueHead == allocationSize )
queueHead = 0;
if ( queueHead == 0 )
return data[ allocationSize -1 ];
dataSize--;
return data[ queueHead -1 ];
}
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
_DataType &Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Peek(void) const
{
RakAssert(IsEmpty()==false);
if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST)
{
return data[dataSize-1];
}
else
{
RakAssert(GetMultilistType()==ML_QUEUE);
return data[ queueHead ];
}
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::PushOpposite(const _DataType &d, const char *file, unsigned int line )
{
PushOpposite(d,d,file,line);
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::PushOpposite(const _DataType &d, const _KeyType &key, const char *file, unsigned int line )
{
ReallocateIfNeeded(file,line);
// Unordered list Push at back
if (GetMultilistType()==ML_UNORDERED_LIST)
{
data[dataSize]=d;
dataSize++;
}
else if (GetMultilistType()==ML_STACK)
{
// Stack push at front of the list, instead of back as normal
InsertAtIndex(d,0,file,line);
}
else if (GetMultilistType()==ML_QUEUE)
{
// Queue push at front of the list, instead of back as normal
InsertAtIndex(d,0,file,line);
}
else
{
RakAssert(GetMultilistType()==ML_ORDERED_LIST);
InsertInOrderedList(d,key);
}
if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_QUEUE)
{
// Break sort if no longer sorted
if (sortState!=ML_UNSORTED && dataSize>1)
{
if (ascendingSort)
{
if ( MLKeyRef<_KeyType>(key) > operator[](1) )
sortState=ML_UNSORTED;
}
else
{
if ( MLKeyRef<_KeyType>(key) < operator[](1) )
sortState=ML_UNSORTED;
}
}
}
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
_DataType &Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::PopOpposite(const char *file, unsigned int line)
{
RakAssert(IsEmpty()==false);
if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST)
{
// Copy leftmost to end
ReallocateIfNeeded(file,line);
data[dataSize]=data[0];
DeleteShiftArrayLeft(0);
--dataSize;
// Assuming still leaves at least one element past the end of the list allocated
DeallocateIfNeeded(file,line);
// Return end
return data[dataSize+1];
}
else
{
RakAssert(GetMultilistType()==ML_QUEUE);
// Deallocate first, since we are returning off the existing list
DeallocateIfNeeded(file,line);
dataSize--;
if (queueTail==0)
queueTail=allocationSize-1;
else
--queueTail;
return data[queueTail];
}
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
_DataType &Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::PeekOpposite(void) const
{
RakAssert(IsEmpty()==false);
if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST)
{
return data[0];
}
else
{
RakAssert(GetMultilistType()==ML_QUEUE);
_IndexType priorIndex;
if (queueTail==0)
priorIndex=allocationSize-1;
else
priorIndex=queueTail-1;
return data[priorIndex];
}
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::InsertAtIndex(const _DataType &d, _IndexType index, const char *file, unsigned int line)
{
ReallocateIfNeeded(file,line);
if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST)
{
if (index>=dataSize)
{
// insert at end
data[dataSize]=d;
dataSize++;
}
else
{
// insert at index
InsertShiftArrayRight(d,index);
}
}
else
{
data[queueTail++] = d;
if ( queueTail == allocationSize )
queueTail = 0;
++dataSize;
if (dataSize==1)
return;
_IndexType writeIndex, readIndex, trueWriteIndex, trueReadIndex;
writeIndex=dataSize-1;
readIndex=writeIndex-1;
while (readIndex >= index)
{
if ( queueHead + writeIndex >= allocationSize )
trueWriteIndex = queueHead + writeIndex - allocationSize;
else
trueWriteIndex = queueHead + writeIndex;
if ( queueHead + readIndex >= allocationSize )
trueReadIndex = queueHead + readIndex - allocationSize;
else
trueReadIndex = queueHead + readIndex;
data[trueWriteIndex]=data[trueReadIndex];
if (readIndex==0)
break;
writeIndex--;
readIndex--;
}
if ( queueHead + index >= allocationSize )
trueWriteIndex = queueHead + index - allocationSize;
else
trueWriteIndex = queueHead + index;
data[trueWriteIndex]=d;
}
if (_MultilistType!=ML_ORDERED_LIST)
sortState=ML_UNSORTED;
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::RemoveAtIndex(_IndexType position, const char *file, unsigned int line)
{
RakAssert(position < dataSize);
RakAssert(IsEmpty()==false);
if (GetMultilistType()==ML_UNORDERED_LIST)
{
// Copy tail to current
data[position]=data[dataSize-1];
}
else if (GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST)
{
DeleteShiftArrayLeft(position);
}
else
{
RakAssert(GetMultilistType()==ML_QUEUE);
_IndexType index, next;
if ( queueHead + position >= allocationSize )
index = queueHead + position - allocationSize;
else
index = queueHead + position;
next = index + 1;
if ( next == allocationSize )
next = 0;
while ( next != queueTail )
{
// Overwrite the previous element
data[ index ] = data[ next ];
index = next;
//next = (next + 1) % allocationSize;
if ( ++next == allocationSize )
next = 0;
}
// Move the queueTail back
if ( queueTail == 0 )
queueTail = allocationSize - 1;
else
--queueTail;
}
dataSize--;
DeallocateIfNeeded(file,line);
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
bool Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::RemoveAtKey(_KeyType key, bool assertIfDoesNotExist, const char *file, unsigned int line)
{
_IndexType index = GetIndexOf(key);
if (index==(_IndexType)-1)
{
RakAssert(assertIfDoesNotExist==false && "RemoveAtKey element not found");
return false;
}
RemoveAtIndex(index,file,line);
return true;
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
_IndexType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetIndexOf(_KeyType key) const
{
_IndexType i;
if (IsSorted())
{
bool objectExists;
i=GetIndexFromKeyInSortedList(key, &objectExists);
if (objectExists)
return i;
return (_IndexType)-1;
}
else if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK)
{
for (i=0; i < dataSize; i++)
{
if (MLKeyRef<_KeyType>(key)==data[i])
return i;
}
return (_IndexType)-1;
}
else
{
RakAssert( GetMultilistType()==ML_QUEUE );
for (i=0; i < dataSize; i++)
{
if (MLKeyRef<_KeyType>(key)==operator[](i))
return i;
}
return (_IndexType)-1;
}
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
_IndexType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetInsertionIndex(_KeyType key) const
{
_IndexType i;
if (IsSorted())
{
bool objectExists;
i=GetIndexFromKeyInSortedList(key, &objectExists);
if (objectExists)
return (_IndexType)-1;
return i;
}
else if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK)
{
for (i=0; i < dataSize; i++)
{
if (MLKeyRef<_KeyType>(key)==data[i])
return (_IndexType)-1;
}
return dataSize;
}
else
{
RakAssert( GetMultilistType()==ML_QUEUE );
for (i=0; i < dataSize; i++)
{
if (MLKeyRef<_KeyType>(key)==operator[](i))
return (_IndexType)-1;
}
return dataSize;
}
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
_DataType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetPtr(_KeyType key) const
{
_IndexType i = GetIndexOf(key);
if (i==(_IndexType)-1)
return 0;
return data[i];
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ForEach(void (*func)(_DataType &item, const char *file, unsigned int line), const char *file, unsigned int line)
{
_IndexType i;
for (i=0; i < dataSize; i++)
func(operator[](i), file, line);
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ForEach(void (*func)(_DataType &item))
{
_IndexType i;
for (i=0; i < dataSize; i++)
func(operator[](i));
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
bool Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::IsEmpty(void) const
{
return dataSize==0;
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
_IndexType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetSize(void) const
{
return dataSize;
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Clear( bool deallocateSmallBlocks, const char *file, unsigned int line )
{
dataSize=0;
if (GetMultilistType()==ML_ORDERED_LIST)
if (ascendingSort)
sortState=ML_SORTED_ASCENDING;
else
sortState=ML_SORTED_DESCENDING;
else
sortState=ML_UNSORTED;
queueHead=0;
queueTail=0;
if (deallocateSmallBlocks && allocationSize < 128 && data)
{
RakNet::OP_DELETE_ARRAY(data,file,line);
data=0;
allocationSize=0;
}
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ClearPointers( bool deallocateSmallBlocks, const char *file, unsigned int line )
{
_IndexType i;
for (i=0; i < dataSize; i++)
RakNet::OP_DELETE(operator[](i), file, line);
Clear(deallocateSmallBlocks, file, line);
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ClearPointer( _KeyType key, const char *file, unsigned int line )
{
_IndexType i;
i = GetIndexOf(key);
if (i!=-1)
{
RakNet::OP_DELETE(operator[](i), file, line);
RemoveAtIndex(i);
}
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ReverseList(void)
{
if (IsSorted())
ascendingSort=!ascendingSort;
ReverseListInternal();
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Reallocate(_IndexType size, const char *file, unsigned int line)
{
_IndexType newAllocationSize;
if (size < dataSize)
newAllocationSize=dataSize;
else
newAllocationSize=size;
preallocationSize=size;
ReallocToSize(newAllocationSize,file,line);
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Sort(bool force)
{
if (IsSorted() && force==false)
return;
if (dataSize>1)
{
if (ascendingSort)
QSortAscending(0,dataSize-1);
else
QSortDescending(0,dataSize-1);
}
TagSorted();
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::TagSorted(void)
{
if (ascendingSort)
sortState=ML_SORTED_ASCENDING;
else
sortState=ML_SORTED_DESCENDING;
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::QSortAscending(_IndexType leftEdge, _IndexType rightEdge)
{
_DataType temp;
_IndexType left=leftEdge;
_IndexType right=rightEdge;
_IndexType pivotIndex=left++;
while (left<right)
{
if (data[left] <= data[pivotIndex])
{
++left;
}
else
{
temp=data[left];
data[left]=data[right];
data[right]=temp;
--right;
}
}
temp=data[pivotIndex];
// Move pivot to center
if (data[left] > data[pivotIndex])
{
--left;
data[pivotIndex]=data[left];
data[left]=temp;
}
else
{
data[pivotIndex]=data[left];
data[left]=temp;
--left;
}
if (left!=leftEdge)
QSortAscending(leftEdge, left);
if (right!=rightEdge)
QSortAscending(right, rightEdge);
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::QSortDescending(_IndexType leftEdge, _IndexType rightEdge)
{
_DataType temp;
_IndexType left=leftEdge;
_IndexType right=rightEdge;
_IndexType pivotIndex=left++;
while (left<right)
{
if (data[left] >= data[pivotIndex])
{
++left;
}
else
{
temp=data[left];
data[left]=data[right];
data[right]=temp;
--right;
}
}
temp=data[pivotIndex];
// Move pivot to center
if (data[left] < data[pivotIndex])
{
--left;
data[pivotIndex]=data[left];
data[left]=temp;
}
else
{
data[pivotIndex]=data[left];
data[left]=temp;
--left;
}
if (left!=leftEdge)
QSortDescending(leftEdge, left);
if (right!=rightEdge)
QSortDescending(right, rightEdge);
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::SetSortOrder(bool ascending)
{
if (ascendingSort!=ascending && IsSorted())
{
ascendingSort=ascending;
// List is sorted, and the sort order has changed. So reverse the list
ReverseListInternal();
}
else
ascendingSort=ascending;
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
bool Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetSortOrder(void) const
{
return ascendingSort;
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
bool Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::IsSorted(void) const
{
return GetMultilistType()==ML_ORDERED_LIST || sortState!=ML_UNSORTED;
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
MultilistType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetMultilistType(void) const
{
if (_MultilistType==ML_VARIABLE_DURING_RUNTIME)
return variableMultilistType;
return _MultilistType;
}
template <const MultilistType _MultilistType, class _DataType, class _KeyType, class _IndexType>
void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::SetMultilistType(MultilistType newType)
{
RakAssert(_MultilistType==ML_VARIABLE_DURING_RUNTIME);