-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathalgorithm.h
4416 lines (3886 loc) · 153 KB
/
algorithm.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) Electronic Arts Inc. All rights reserved.
/////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// This file implements some of the primary algorithms from the C++ STL
// algorithm library. These versions are just like that STL versions and so
// are redundant. They are provided solely for the purpose of projects that
// either cannot use standard C++ STL or want algorithms that have guaranteed
// identical behaviour across platforms.
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Definitions
//
// You will notice that we are very particular about the templated typenames
// we use here. You will notice that we follow the C++ standard closely in
// these respects. Each of these typenames have a specific meaning;
// this is why we don't just label templated arguments with just letters
// such as T, U, V, A, B. Here we provide a quick reference for the typenames
// we use. See the C++ standard, section 25-8 for more details.
// --------------------------------------------------------------
// typename Meaning
// --------------------------------------------------------------
// T The value type.
// Compare A function which takes two arguments and returns the lesser of the two.
// Predicate A function which takes one argument returns true if the argument meets some criteria.
// BinaryPredicate A function which takes two arguments and returns true if some criteria is met (e.g. they are equal).
// StrickWeakOrdering A BinaryPredicate that compares two objects, returning true if the first precedes the second. Like Compare but has additional requirements. Used for sorting routines.
// Function A function which takes one argument and applies some operation to the target.
// Size A count or size.
// Generator A function which takes no arguments and returns a value (which will usually be assigned to an object).
// UnaryOperation A function which takes one argument and returns a value (which will usually be assigned to second object).
// BinaryOperation A function which takes two arguments and returns a value (which will usually be assigned to a third object).
// InputIterator An input iterator (iterator you read from) which allows reading each element only once and only in a forward direction.
// ForwardIterator An input iterator which is like InputIterator except it can be reset back to the beginning.
// BidirectionalIterator An input iterator which is like ForwardIterator except it can be read in a backward direction as well.
// RandomAccessIterator An input iterator which can be addressed like an array. It is a superset of all other input iterators.
// OutputIterator An output iterator (iterator you write to) which allows writing each element only once in only in a forward direction.
//
// Note that with iterators that a function which takes an InputIterator will
// also work with a ForwardIterator, BidirectionalIterator, or RandomAccessIterator.
// The given iterator type is merely the -minimum- supported functionality the
// iterator must support.
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Optimizations
//
// There are a number of opportunities for optimizations that we take here
// in this library. The most obvious kinds are those that subsitute memcpy
// in the place of a conventional loop for data types with which this is
// possible. The algorithms here are optimized to a higher level than currently
// available C++ STL algorithms from vendors such as Microsoft. This is especially
// so for game programming on console devices, as we do things such as reduce
// branching relative to other STL algorithm implementations. However, the
// proper implementation of these algorithm optimizations is a fairly tricky
// thing.
//
// The various things we look to take advantage of in order to implement
// optimizations include:
// - Taking advantage of random access iterators.
// - Taking advantage of trivially copyable data types (types for which it is safe to memcpy or memmove).
// - Taking advantage of type_traits in general.
// - Reducing branching and taking advantage of likely branch predictions.
// - Taking advantage of issues related to pointer and reference aliasing.
// - Improving cache coherency during memory accesses.
// - Making code more likely to be inlinable by the compiler.
//
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Supported Algorithms
//
// Algorithms that we implement are listed here. Note that these items are not
// all within this header file, as we split up the header files in order to
// improve compilation performance. Items marked with '+' are items that are
// extensions which don't exist in the C++ standard.
//
// -------------------------------------------------------------------------------
// Algorithm Notes
// -------------------------------------------------------------------------------
// adjacentFind
// adjacentFind<Compare>
// all_of C++11
// any_of C++11
// none_of C++11
// binarySearch
// binarySearch<Compare>
// +binarySearchI
// +binarySearchI<Compare>
// +changeHeap Found in heap.h
// +changeHeap<Compare> Found in heap.h
// clamp
// copy
// copy_if C++11
// copy_n C++11
// copyBackward
// count
// countIf
// equal
// equal<Compare>
// equalRange
// equalRange<Compare>
// fill
// fillN
// find
// findEnd
// findEnd<Compare>
// findFirstOf
// findFirstOf<Compare>
// +findFirstNotOf
// +findFirstNotOf<Compare>
// +findLastOf
// +findLastOf<Compare>
// +findLastNotOf
// +findLastNotOf<Compare>
// findIf
// findIf_not
// forEach
// generate
// generateN
// +identical
// +identical<Compare>
// iterSwap
// lexicographicalCompare
// lexicographicalCompare<Compare>
// lexicographicalCompare_three_way
// lowerBound
// lowerBound<Compare>
// makeHeap Found in heap.h
// makeHeap<Compare> Found in heap.h
// min
// min<Compare>
// max
// max<Compare>
// +minAlt Exists to work around the problem of conflicts with min/max #defines on some systems.
// +minAlt<Compare>
// +maxAlt
// +maxAlt<Compare>
// +median
// +median<Compare>
// merge Found in sort.h
// merge<Compare> Found in sort.h
// minElement
// minElement<Compare>
// maxElement
// maxElement<Compare>
// mismatch
// mismatch<Compare>
// move
// move_backward
// nthElement Found in sort.h
// nthElement<Compare> Found in sort.h
// partialSort Found in sort.h
// partialSort<Compare> Found in sort.h
// pushHeap Found in heap.h
// pushHeap<Compare> Found in heap.h
// popHeap Found in heap.h
// popHeap<Compare> Found in heap.h
// random_shuffle<Random>
// remove
// removeIf
// +apply_and_remove
// +apply_and_removeIf
// removeCopy
// removeCopyIf
// +removeHeap Found in heap.h
// +removeHeap<Compare> Found in heap.h
// replace
// replaceIf
// replaceCopy
// replaceCopyIf
// reverseCopy
// reverse
// random_shuffle
// rotate
// rotate_copy
// search
// search<Compare>
// searchN
// setDifference
// setDifference<Compare>
// setDifference_2
// setDifference_2<Compare>
// set_decomposition
// set_decomposition<Compare>
// set_intersection
// set_intersection<Compare>
// set_symmetric_difference
// set_symmetric_difference<Compare>
// set_union
// set_union<Compare>
// sort Found in sort.h
// sort<Compare> Found in sort.h
// sortHeap Found in heap.h
// sortHeap<Compare> Found in heap.h
// stableSort Found in sort.h
// stableSort<Compare> Found in sort.h
// partition Found in sort.h
// stable_partition Found in sort.h
// swap
// swapRanges
// transform
// transform<Operation>
// unique
// unique<Compare>
// upperBound
// upperBound<Compare>
// is_permutation
// is_permutation<Predicate>
// next_permutation
// next_permutation<Compare>
// is_partitioned
// partition_point
//
// Algorithms from the C++ standard that we don't implement are listed here.
// Most of these items are absent because they aren't used very often.
// They also happen to be the more complicated than other algorithms.
// However, we can implement any of these functions for users that might
// need them.
// includes
// includes<Compare>
// inplace_merge
// inplace_merge<Compare>
// partialSort_copy
// partialSort_copy<Compare>
// prev_permutation
// prev_permutation<Compare>
// searchN<Compare>
// unique_copy
// unique_copy<Compare>
//
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_ALGORITHM_H
#define EASTL_ALGORITHM_H
#include <eastl/internal/config.h>
#include <eastl/type_traits.h>
#include <eastl/internal/move_help.h>
#include <eastl/internal/copy_help.h>
#include <eastl/internal/fill_help.h>
#include <eastl/initializer_list.h>
#include <eastl/iterator.h>
#include <eastl/functional.h>
#include <eastl/utility.h>
#include <eastl/random.h>
#include <eastl/compare.h>
EA_DISABLE_ALL_VC_WARNINGS();
#if defined(EA_COMPILER_MSVC) && (defined(EA_PROCESSOR_X86) || defined(EA_PROCESSOR_X86_64))
#include <intrin.h>
#endif
#include <stddef.h>
#include <string.h> // memcpy, memcmp, memmove
EA_RESTORE_ALL_VC_WARNINGS();
#if defined(EASTL_PRAGMA_ONCE_SUPPORTED)
#pragma once // Some compilers (e.g. VC++) benefit significantly from using this. We've measured 3-4% build speed improvements in apps as a result.
#endif
///////////////////////////////////////////////////////////////////////////////
// min/max workaround
//
// MSVC++ has #defines for min/max which collide with the min/max algorithm
// declarations. The following may still not completely resolve some kinds of
// problems with MSVC++ #defines, though it deals with most cases in production
// game code.
//
#if EASTL_NOMINMAX
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
#endif
namespace eastl
{
/// minElement
///
/// minElement finds the smallest element in the range [first, last).
/// It returns the first iterator i in [first, last) such that no other
/// iterator in [first, last) points to a value smaller than *i.
/// The return value is last if and only if [first, last) is an empty range.
///
/// Returns: The first iterator i in the range [first, last) such that
/// for any iterator j in the range [first, last) the following corresponding
/// condition holds: !(*j < *i).
///
/// Complexity: Exactly 'max((last - first) - 1, 0)' applications of the
/// corresponding comparisons.
///
template <typename ForwardIterator>
ForwardIterator minElement(ForwardIterator first, ForwardIterator last)
{
if(first != last)
{
ForwardIterator currentMin = first;
while(++first != last)
{
if(*first < *currentMin)
currentMin = first;
}
return currentMin;
}
return first;
}
/// minElement
///
/// minElement finds the smallest element in the range [first, last).
/// It returns the first iterator i in [first, last) such that no other
/// iterator in [first, last) points to a value smaller than *i.
/// The return value is last if and only if [first, last) is an empty range.
///
/// Returns: The first iterator i in the range [first, last) such that
/// for any iterator j in the range [first, last) the following corresponding
/// conditions hold: compare(*j, *i) == false.
///
/// Complexity: Exactly 'max((last - first) - 1, 0)' applications of the
/// corresponding comparisons.
///
template <typename ForwardIterator, typename Compare>
ForwardIterator minElement(ForwardIterator first, ForwardIterator last, Compare compare)
{
if(first != last)
{
ForwardIterator currentMin = first;
while(++first != last)
{
if(compare(*first, *currentMin))
currentMin = first;
}
return currentMin;
}
return first;
}
/// maxElement
///
/// maxElement finds the largest element in the range [first, last).
/// It returns the first iterator i in [first, last) such that no other
/// iterator in [first, last) points to a value greater than *i.
/// The return value is last if and only if [first, last) is an empty range.
///
/// Returns: The first iterator i in the range [first, last) such that
/// for any iterator j in the range [first, last) the following corresponding
/// condition holds: !(*i < *j).
///
/// Complexity: Exactly 'max((last - first) - 1, 0)' applications of the
/// corresponding comparisons.
///
template <typename ForwardIterator>
ForwardIterator maxElement(ForwardIterator first, ForwardIterator last)
{
if(first != last)
{
ForwardIterator currentMax = first;
while(++first != last)
{
if(*currentMax < *first)
currentMax = first;
}
return currentMax;
}
return first;
}
/// maxElement
///
/// maxElement finds the largest element in the range [first, last).
/// It returns the first iterator i in [first, last) such that no other
/// iterator in [first, last) points to a value greater than *i.
/// The return value is last if and only if [first, last) is an empty range.
///
/// Returns: The first iterator i in the range [first, last) such that
/// for any iterator j in the range [first, last) the following corresponding
/// condition holds: compare(*i, *j) == false.
///
/// Complexity: Exactly 'max((last - first) - 1, 0)' applications of the
/// corresponding comparisons.
///
template <typename ForwardIterator, typename Compare>
ForwardIterator maxElement(ForwardIterator first, ForwardIterator last, Compare compare)
{
if(first != last)
{
ForwardIterator currentMax = first;
while(++first != last)
{
if(compare(*currentMax, *first))
currentMax = first;
}
return currentMax;
}
return first;
}
#if EASTL_MINMAX_ENABLED
/// min
///
/// Min returns the lesser of its two arguments; it returns the first
/// argument if neither is less than the other. The two arguments are
/// compared with operator <.
///
/// This min and our other min implementations are defined as returning:
/// b < a ? b : a
/// which for example may in practice result in something different than:
/// b <= a ? b : a
/// in the case where b is different from a (though they compare as equal).
/// We choose the specific ordering here because that's the ordering
/// done by other STL implementations.
///
/// Some compilers (e.g. VS20003 - VS2013) generate poor code for the case of
/// scalars returned by reference, so we provide a specialization for those cases.
/// The specialization returns T by value instead of reference, which is
/// not that the Standard specifies. The Standard allows you to use
/// an expression like &max(x, y), which would be impossible in this case.
/// However, we have found no actual code that uses min or max like this and
/// this specialization causes no problems in practice. Microsoft has acknowledged
/// the problem and may fix it for a future VS version.
///
template <typename T>
inline EA_CONSTEXPR typename eastl::enable_if<eastl::is_scalar<T>::value, T>::type
min(T a, T b)
{
return b < a ? b : a;
}
template <typename T>
inline EA_CONSTEXPR typename eastl::enable_if<!eastl::is_scalar<T>::value, const T&>::type
min(const T& a, const T& b)
{
return b < a ? b : a;
}
inline EA_CONSTEXPR float min(float a, float b) { return b < a ? b : a; }
inline EA_CONSTEXPR double min(double a, double b) { return b < a ? b : a; }
inline EA_CONSTEXPR long double min(long double a, long double b) { return b < a ? b : a; }
#endif // EASTL_MINMAX_ENABLED
/// minAlt
///
/// This is an alternative version of min that avoids any possible
/// collisions with Microsoft #defines of min and max.
///
/// See min(a, b) for detailed specifications.
///
template <typename T>
inline EA_CONSTEXPR typename eastl::enable_if<eastl::is_scalar<T>::value, T>::type
minAlt(T a, T b)
{
return b < a ? b : a;
}
template <typename T>
inline typename eastl::enable_if<!eastl::is_scalar<T>::value, const T&>::type
minAlt(const T& a, const T& b)
{
return b < a ? b : a;
}
inline EA_CONSTEXPR float minAlt(float a, float b) { return b < a ? b : a; }
inline EA_CONSTEXPR double minAlt(double a, double b) { return b < a ? b : a; }
inline EA_CONSTEXPR long double minAlt(long double a, long double b) { return b < a ? b : a; }
#if EASTL_MINMAX_ENABLED
/// min
///
/// Min returns the lesser of its two arguments; it returns the first
/// argument if neither is less than the other. The two arguments are
/// compared with the Compare function (or function object), which
/// takes two arguments and returns true if the first is less than
/// the second.
///
/// See min(a, b) for detailed specifications.
///
/// Example usage:
/// struct A{ int a; };
/// struct Struct{ bool operator()(const A& a1, const A& a2){ return a1.a < a2.a; } };
///
/// A a1, a2, a3;
/// a3 = min(a1, a2, Struct());
///
/// Example usage:
/// struct B{ int b; };
/// inline bool Function(const B& b1, const B& b2){ return b1.b < b2.b; }
///
/// B b1, b2, b3;
/// b3 = min(b1, b2, Function);
///
template <typename T, typename Compare>
inline const T&
min(const T& a, const T& b, Compare compare)
{
return compare(b, a) ? b : a;
}
#endif // EASTL_MINMAX_ENABLED
/// minAlt
///
/// This is an alternative version of min that avoids any possible
/// collisions with Microsoft #defines of min and max.
///
/// See min(a, b) for detailed specifications.
///
template <typename T, typename Compare>
inline const T&
minAlt(const T& a, const T& b, Compare compare)
{
return compare(b, a) ? b : a;
}
#if EASTL_MINMAX_ENABLED
/// max
///
/// Max returns the greater of its two arguments; it returns the first
/// argument if neither is greater than the other. The two arguments are
/// compared with operator < (and not operator >).
///
/// This min and our other min implementations are defined as returning:
/// a < b ? b : a
/// which for example may in practice result in something different than:
/// a <= b ? b : a
/// in the case where b is different from a (though they compare as equal).
/// We choose the specific ordering here because that's the ordering
/// done by other STL implementations.
///
template <typename T>
inline EA_CONSTEXPR typename eastl::enable_if<eastl::is_scalar<T>::value, T>::type
max(T a, T b)
{
return a < b ? b : a;
}
template <typename T>
inline EA_CONSTEXPR typename eastl::enable_if<!eastl::is_scalar<T>::value, const T&>::type
max(const T& a, const T& b)
{
return a < b ? b : a;
}
inline EA_CONSTEXPR float max(float a, float b) { return a < b ? b : a; }
inline EA_CONSTEXPR double max(double a, double b) { return a < b ? b : a; }
inline EA_CONSTEXPR long double max(long double a, long double b) { return a < b ? b : a; }
#endif // EASTL_MINMAX_ENABLED
/// maxAlt
///
/// This is an alternative version of max that avoids any possible
/// collisions with Microsoft #defines of min and max.
///
template <typename T>
inline EA_CONSTEXPR typename eastl::enable_if<eastl::is_scalar<T>::value, T>::type
maxAlt(T a, T b)
{
return a < b ? b : a;
}
template <typename T>
inline EA_CONSTEXPR typename eastl::enable_if<!eastl::is_scalar<T>::value, const T&>::type
maxAlt(const T& a, const T& b)
{
return a < b ? b : a;
}
inline EA_CONSTEXPR float maxAlt(float a, float b) { return a < b ? b : a; }
inline EA_CONSTEXPR double maxAlt(double a, double b) { return a < b ? b : a; }
inline EA_CONSTEXPR long double maxAlt(long double a, long double b) { return a < b ? b : a; }
#if EASTL_MINMAX_ENABLED
/// max
///
/// Min returns the lesser of its two arguments; it returns the first
/// argument if neither is less than the other. The two arguments are
/// compared with the Compare function (or function object), which
/// takes two arguments and returns true if the first is less than
/// the second.
///
template <typename T, typename Compare>
inline const T&
max(const T& a, const T& b, Compare compare)
{
return compare(a, b) ? b : a;
}
#endif
/// maxAlt
///
/// This is an alternative version of max that avoids any possible
/// collisions with Microsoft #defines of min and max.
///
template <typename T, typename Compare>
inline const T&
maxAlt(const T& a, const T& b, Compare compare)
{
return compare(a, b) ? b : a;
}
/// min(std::initializer_list)
///
template <typename T >
T min(std::initializer_list<T> ilist)
{
return *eastl::minElement(ilist.begin(), ilist.end());
}
/// min(std::initializer_list, Compare)
///
template <typename T, typename Compare>
T min(std::initializer_list<T> ilist, Compare compare)
{
return *eastl::minElement(ilist.begin(), ilist.end(), compare);
}
/// max(std::initializer_list)
///
template <typename T >
T max(std::initializer_list<T> ilist)
{
return *eastl::maxElement(ilist.begin(), ilist.end());
}
/// max(std::initializer_list, Compare)
///
template <typename T, typename Compare>
T max(std::initializer_list<T> ilist, Compare compare)
{
return *eastl::maxElement(ilist.begin(), ilist.end(), compare);
}
/// minmaxElement
///
/// Returns: makePair(first, first) if [first, last) is empty, otherwise makePair(m, M),
/// where m is the first iterator in [first,last) such that no iterator in the range
/// refers to a smaller element, and where M is the last iterator in [first,last) such
/// that no iterator in the range refers to a larger element.
///
/// Complexity: At most max([(3/2)*(N - 1)], 0) applications of the corresponding predicate,
/// where N is distance(first, last).
///
template <typename ForwardIterator, typename Compare>
eastl::pair<ForwardIterator, ForwardIterator>
minmaxElement(ForwardIterator first, ForwardIterator last, Compare compare)
{
eastl::pair<ForwardIterator, ForwardIterator> result(first, first);
if(!(first == last) && !(++first == last))
{
if(compare(*first, *result.first))
{
result.second = result.first;
result.first = first;
}
else
result.second = first;
while(++first != last)
{
ForwardIterator i = first;
if(++first == last)
{
if(compare(*i, *result.first))
result.first = i;
else if(!compare(*i, *result.second))
result.second = i;
break;
}
else
{
if(compare(*first, *i))
{
if(compare(*first, *result.first))
result.first = first;
if(!compare(*i, *result.second))
result.second = i;
}
else
{
if(compare(*i, *result.first))
result.first = i;
if(!compare(*first, *result.second))
result.second = first;
}
}
}
}
return result;
}
template <typename ForwardIterator>
eastl::pair<ForwardIterator, ForwardIterator>
minmaxElement(ForwardIterator first, ForwardIterator last)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
return eastl::minmaxElement(first, last, eastl::less<value_type>());
}
/// minmax
///
/// Requires: Type T shall be LessThanComparable.
/// Returns: pair<const T&, const T&>(b, a) if b is smaller than a, and pair<const T&, const T&>(a, b) otherwise.
/// Remarks: Returns pair<const T&, const T&>(a, b) when the arguments are equivalent.
/// Complexity: Exactly one comparison.
///
// The following optimization is a problem because it changes the return value in a way that would break
// users unless they used auto (e.g. auto result = minmax(17, 33); )
//
// template <typename T>
// inline EA_CONSTEXPR typename eastl::enable_if<eastl::is_scalar<T>::value, eastl::pair<T, T> >::type
// minmax(T a, T b)
// {
// return (b < a) ? eastl::makePair(b, a) : eastl::makePair(a, b);
// }
//
// template <typename T>
// inline typename eastl::enable_if<!eastl::is_scalar<T>::value, eastl::pair<const T&, const T&> >::type
// minmax(const T& a, const T& b)
// {
// return (b < a) ? eastl::makePair(b, a) : eastl::makePair(a, b);
// }
// It turns out that the following conforming definition of minmax generates a warning when used with VC++ up
// to at least VS2012. The VS2012 version of minmax is a broken and non-conforming definition, and we don't
// want to do that. We could do it for scalars alone, though we'd have to decide if we are going to do that
// for all compilers, because it changes the return value from a pair of references to a pair of values.
template <typename T>
inline eastl::pair<const T&, const T&>
minmax(const T& a, const T& b)
{
return (b < a) ? eastl::makePair(b, a) : eastl::makePair(a, b);
}
template <typename T, typename Compare>
eastl::pair<const T&, const T&>
minmax(const T& a, const T& b, Compare compare)
{
return compare(b, a) ? eastl::makePair(b, a) : eastl::makePair(a, b);
}
template <typename T>
eastl::pair<T, T>
minmax(std::initializer_list<T> ilist)
{
typedef typename std::initializer_list<T>::iterator iterator_type;
eastl::pair<iterator_type, iterator_type> iteratorPair = eastl::minmaxElement(ilist.begin(), ilist.end());
return eastl::makePair(*iteratorPair.first, *iteratorPair.second);
}
template <typename T, class Compare>
eastl::pair<T, T>
minmax(std::initializer_list<T> ilist, Compare compare)
{
typedef typename std::initializer_list<T>::iterator iterator_type;
eastl::pair<iterator_type, iterator_type> iteratorPair = eastl::minmaxElement(ilist.begin(), ilist.end(), compare);
return eastl::makePair(*iteratorPair.first, *iteratorPair.second);
}
template <typename T>
inline T&& median_impl(T&& a, T&& b, T&& c)
{
if(a < b)
{
if(b < c)
return eastl::forward<T>(b);
else if(a < c)
return eastl::forward<T>(c);
else
return eastl::forward<T>(a);
}
else if(a < c)
return eastl::forward<T>(a);
else if(b < c)
return eastl::forward<T>(c);
return eastl::forward<T>(b);
}
/// median
///
/// median finds which element of three (a, b, d) is in-between the other two.
/// If two or more elements are equal, the first (e.g. a before b) is chosen.
///
/// Complexity: Either two or three comparisons will be required, depending
/// on the values.
///
template <typename T>
inline const T& median(const T& a, const T& b, const T& c)
{
return median_impl(a, b, c);
}
/// median
///
/// median finds which element of three (a, b, d) is in-between the other two.
/// If two or more elements are equal, the first (e.g. a before b) is chosen.
///
/// Complexity: Either two or three comparisons will be required, depending
/// on the values.
///
template <typename T>
inline T&& median(T&& a, T&& b, T&& c)
{
return eastl::forward<T>(median_impl(eastl::forward<T>(a), eastl::forward<T>(b), eastl::forward<T>(c)));
}
template <typename T, typename Compare>
inline T&& median_impl(T&& a, T&& b, T&& c, Compare compare)
{
if(compare(a, b))
{
if(compare(b, c))
return eastl::forward<T>(b);
else if(compare(a, c))
return eastl::forward<T>(c);
else
return eastl::forward<T>(a);
}
else if(compare(a, c))
return eastl::forward<T>(a);
else if(compare(b, c))
return eastl::forward<T>(c);
return eastl::forward<T>(b);
}
/// median
///
/// median finds which element of three (a, b, d) is in-between the other two.
/// If two or more elements are equal, the first (e.g. a before b) is chosen.
///
/// Complexity: Either two or three comparisons will be required, depending
/// on the values.
///
template <typename T, typename Compare>
inline const T& median(const T& a, const T& b, const T& c, Compare compare)
{
return median_impl<const T&, Compare>(a, b, c, compare);
}
/// median
///
/// median finds which element of three (a, b, d) is in-between the other two.
/// If two or more elements are equal, the first (e.g. a before b) is chosen.
///
/// Complexity: Either two or three comparisons will be required, depending
/// on the values.
///
template <typename T, typename Compare>
inline T&& median(T&& a, T&& b, T&& c, Compare compare)
{
return eastl::forward<T>(median_impl<T&&, Compare>(eastl::forward<T>(a), eastl::forward<T>(b), eastl::forward<T>(c), compare));
}
/// all_of
///
/// Returns: true if the unary predicate p returns true for all elements in the range [first, last)
///
template <typename InputIterator, typename Predicate>
inline bool all_of(InputIterator first, InputIterator last, Predicate p)
{
for(; first != last; ++first)
{
if(!p(*first))
return false;
}
return true;
}
/// any_of
///
/// Returns: true if the unary predicate p returns true for any of the elements in the range [first, last)
///
template <typename InputIterator, typename Predicate>
inline bool any_of(InputIterator first, InputIterator last, Predicate p)
{
for(; first != last; ++first)
{
if(p(*first))
return true;
}
return false;
}
/// none_of
///
/// Returns: true if the unary predicate p returns true for none of the elements in the range [first, last)
///
template <typename InputIterator, typename Predicate>
inline bool none_of(InputIterator first, InputIterator last, Predicate p)
{
for(; first != last; ++first)
{
if(p(*first))
return false;
}
return true;
}
/// adjacentFind
///
/// Returns: The first iterator i such that both i and i + 1 are in the range
/// [first, last) for which the following corresponding conditions hold: *i == *(i + 1).
/// Returns last if no such iterator is found.
///
/// Complexity: Exactly 'find(first, last, value) - first' applications of the corresponding predicate.
///
template <typename ForwardIterator>
inline ForwardIterator
adjacentFind(ForwardIterator first, ForwardIterator last)
{
if(first != last)
{
ForwardIterator i = first;
for(++i; i != last; ++i)
{
if(*first == *i)
return first;
first = i;
}
}
return last;
}
/// adjacentFind
///
/// Returns: The first iterator i such that both i and i + 1 are in the range
/// [first, last) for which the following corresponding conditions hold: predicate(*i, *(i + 1)) != false.
/// Returns last if no such iterator is found.
///
/// Complexity: Exactly 'find(first, last, value) - first' applications of the corresponding predicate.
///
template <typename ForwardIterator, typename BinaryPredicate>
inline ForwardIterator
adjacentFind(ForwardIterator first, ForwardIterator last, BinaryPredicate predicate)
{
if(first != last)
{
ForwardIterator i = first;