16inline const T &min(
const T &a,
const T &b)
18 return !(b < a) ? a : b;
23inline const T &max(
const T &a,
const T &b)
25 return (a < b) ? b : a;
30inline const T &clamp(
const T &value,
const T &minValue,
const T &maxValue)
32 return min(max(value, minValue), maxValue);
46 inline bool operator()(T value)
const {
return value == refValue_; }
59 inline bool operator()(T value)
const {
return !(value == refValue_); }
72 inline bool operator()(T value)
const {
return value > refValue_; }
85 inline bool operator()(T value)
const {
return !(value > refValue_); }
98 inline bool operator()(T value)
const {
return value < refValue_; }
111 inline bool operator()(T value)
const {
return !(value < refValue_); }
122inline bool IsEqual(
const T &a,
const T &b)
128inline bool IsNotEqual(
const T &a,
const T &b)
134inline bool IsGreater(
const T &a,
const T &b)
140inline bool IsNotGreater(
const T &a,
const T &b)
146inline bool IsLess(
const T &a,
const T &b)
152inline bool IsNotLess(
const T &a,
const T &b)
162inline T Plus(
const T &a,
const T &b)
168inline T Minus(
const T &a,
const T &b)
174inline T Multiplies(
const T &a,
const T &b)
180inline T Divides(
const T &a,
const T &b)
186inline T Modulus(
const T &a,
const T &b)
192inline T Negate(
const T &a)
202inline T LogicalAnd(
const T &a,
const T &b)
208inline T LogicalOr(
const T &a,
const T &b)
214inline T LogicalNot(
const T &a)
224template <
class Iterator,
class UnaryPredicate>
225inline bool allOf(Iterator first, Iterator last, UnaryPredicate pred)
227 for (; first != last; ++first)
237template <
class Iterator,
class UnaryPredicate>
238inline bool noneOf(Iterator first, Iterator last, UnaryPredicate pred)
240 for (; first != last; ++first)
250template <
class Iterator,
class UnaryPredicate>
251inline bool anyOf(Iterator first, Iterator last, UnaryPredicate pred)
253 for (; first != last; ++first)
263template <
class Iterator,
class Function>
264inline Function forEach(Iterator first, Iterator last, Function fn)
266 for (; first != last; ++first)
273template <
class Iterator,
class T>
274inline Iterator find(Iterator first, Iterator last,
const T &value)
276 for (; first != last; ++first)
286template <
class Iterator,
class UnaryPredicate>
287inline Iterator findIf(Iterator first, Iterator last, UnaryPredicate pred)
289 for (; first != last; ++first)
299template <
class Iterator,
class UnaryPredicate>
300inline Iterator findIfNot(Iterator first, Iterator last, UnaryPredicate pred)
302 for (; first != last; ++first)
312template <
class Iterator,
class T>
313inline int count(Iterator first, Iterator last,
const T &value)
317 for (; first != last; ++first)
327template <
class Iterator,
class UnaryPredicate>
328inline int countIf(Iterator first, Iterator last, UnaryPredicate pred)
332 for (; first != last; ++first)
342template <
class Iterator1,
class Iterator2>
343inline bool equal(Iterator1 first1, Iterator1 last1, Iterator2 first2)
345 while (first1 != last1)
347 if (*first1 != *first2)
357template <
class Iterator>
358inline Iterator minElement(Iterator first, Iterator last)
363 Iterator smallest = first;
366 for (; first != last; ++first)
368 if (*first < *smallest)
376template <
class Iterator>
377inline Iterator maxElement(Iterator first, Iterator last)
382 Iterator largest = first;
385 for (; first != last; ++first)
387 if (*first > *largest)
395template <
class Iterator,
class T>
396inline void clampElements(Iterator first, Iterator last,
const T &minValue,
const T &maxValue)
398 for (; first != last; ++first)
399 *first = min(max(*first, minValue), maxValue);
406template <
class Iterator1,
class Iterator2>
407inline void iterSwap(Iterator1 a, Iterator2 b)
413template <
class IteratorIn,
class IteratorOut>
414inline IteratorOut copy(IteratorIn first, IteratorIn last, IteratorOut result)
416 while (first != last)
427template <
class IteratorIn,
class IteratorOut>
428inline IteratorOut copyN(IteratorIn first,
unsigned int n, IteratorOut result)
442template <
class IteratorIn,
class IteratorOut,
class UnaryPredicate>
443inline IteratorOut copyIf(IteratorIn first, IteratorIn last, IteratorOut result, UnaryPredicate pred)
445 while (first != last)
459template <
class IteratorIn,
class IteratorOut,
class UnaryOperation>
460inline IteratorOut transform(IteratorIn first1, IteratorIn last1, IteratorOut result, UnaryOperation op)
462 while (first1 != last1)
464 *result = op(*first1);
473template <
class IteratorIn1,
class IteratorIn2,
class IteratorOut,
class BinaryOperation>
474inline IteratorOut transform(IteratorIn1 first1, IteratorIn1 last1, IteratorIn2 first2,
475 IteratorOut result, BinaryOperation binaryOp)
477 while (first1 != last1)
479 *result = binaryOp(*first1, *first2++);
488template <
class Iterator,
class T>
489inline void replace(Iterator first, Iterator last,
const T &oldValue,
const T &newValue)
491 while (first != last)
493 if (*first == oldValue)
500template <
class Iterator,
class UnaryPredicate,
class T>
501inline void replaceIf(Iterator first, Iterator last, UnaryPredicate pred,
const T &newValue)
503 while (first != last)
512template <
class IteratorIn,
class IteratorOut,
class T>
513inline IteratorOut replaceCopy(IteratorIn first, IteratorIn last, IteratorOut result,
const T &oldValue,
const T &newValue)
515 while (first != last)
517 *result = (*first == oldValue) ? newValue : *first;
526template <
class IteratorIn,
class IteratorOut,
class UnaryPredicate,
class T>
527inline IteratorOut replaceCopyIf(IteratorIn first, IteratorIn last, IteratorOut result, UnaryPredicate pred,
const T &newValue)
529 while (first != last)
531 *result = (pred(*first)) ? newValue : *first;
540template <
class Iterator,
class T>
541inline void fill(Iterator first, Iterator last,
const T &value)
543 for (; first != last; ++first)
548template <
class Iterator,
class T>
549inline void fillN(Iterator first,
unsigned int n,
const T &value)
551 for (
unsigned int i = 0; i < n; i++, ++first)
556template <
class Iterator,
class Generator>
557inline void generate(Iterator first, Iterator last, Generator gen)
559 while (first != last)
567template <
class Iterator,
class Generator>
568inline void generateN(Iterator first,
unsigned int n, Generator gen)
579template <
class Iterator,
class T>
580inline Iterator remove(Iterator first, Iterator last,
const T &val)
582 Iterator result = first;
584 while (first != last)
586 if (!(*first == val))
598template <
class Iterator,
class UnaryPredicate>
599inline Iterator removeIf(Iterator first, Iterator last, UnaryPredicate pred)
601 Iterator result = first;
603 while (first != last)
617template <
class Iterator>
618inline void reverse(Iterator first, Iterator last)
620 while ((first != last) && (first != --last))
622 iterSwap(first, last);
628template <
class IteratorIn,
class IteratorOut>
629inline IteratorOut reverseCopy(IteratorIn first, IteratorIn last, IteratorOut result)
631 while (first != last)
642template <
class Iterator>
643inline bool isSorted(Iterator first, Iterator last)
648 Iterator next = first;
649 while (++next != last)
660template <
class Iterator,
class Compare>
661inline bool isSorted(Iterator first, Iterator last, Compare comp)
666 Iterator next = first;
667 while (++next != last)
669 if (comp(*next, *first))
678template <
class Iterator>
679inline Iterator isSortedUntil(Iterator first, Iterator last)
684 Iterator next = first;
685 while (++next != last)
696template <
class Iterator,
class Compare>
697inline Iterator isSortedUntil(Iterator first, Iterator last, Compare comp)
702 Iterator next = first;
703 while (++next != last)
705 if (comp(*next, *first))
716 template <
class Iterator,
class Compare>
717 Iterator medianOfThree(Iterator a, Iterator b, Iterator c, Compare comp)
721 if (comp(*b, *c))
return b;
722 else if (comp(*a, *c))
return c;
727 if (comp(*a, *c))
return a;
728 else if (comp(*b, *c))
return c;
734 template <
class Iterator,
class Compare>
735 inline Iterator partition(Iterator first, Iterator last, Compare comp)
737 Iterator pivot = last;
739 for (Iterator j = first; j != last; ++j)
741 if (comp(*j, *pivot))
753 template <
class Iterator,
class Compare>
754 inline void quicksort(Iterator first, Iterator last, RandomAccessIteratorTag, Compare comp)
756 const int size = distance(first, last);
759 Iterator lastElem = prev(last);
760 Iterator mid = next(first, size / 2);
761 Iterator pivot = medianOfThree(first, mid, lastElem, comp);
762 swap(*pivot, *lastElem);
763 Iterator q = partition(first, lastElem, comp);
765 quicksort(first, q, RandomAccessIteratorTag(), comp);
766 quicksort(next(q), last, RandomAccessIteratorTag(), comp);
771 template <
class Iterator,
class Compare>
772 inline void quicksort(Iterator first, Iterator last, BidirectionalIteratorTag, Compare comp)
776 Iterator p = prev(last);
778 Iterator q = partition(first, p, comp);
781 quicksort(first, q, BidirectionalIteratorTag(), comp);
782 quicksort(next(q), last, BidirectionalIteratorTag(), comp);
789template <
class Iterator,
class Compare>
790inline void quicksort(Iterator first, Iterator last, Compare comp)
792 quicksort(first, last,
typename IteratorTraits<Iterator>::IteratorCategory(), comp);
796template <
class Iterator>
797inline void quicksort(Iterator first, Iterator last)
799 quicksort(first, last,
typename IteratorTraits<Iterator>::IteratorCategory(), IsLess<
typename IteratorTraits<Iterator>::ValueType>);
803template <
class Iterator>
804inline void quicksortDesc(Iterator first, Iterator last)
806 quicksort(first, last,
typename IteratorTraits<Iterator>::IteratorCategory(), IsNotLess<
typename IteratorTraits<Iterator>::ValueType>);
812 inline int iLeftChild(
int i)
820template <
class Iterator,
class Compare>
821inline void heapsort(Iterator first, Iterator last, Compare comp)
823 const int size = distance(first, last);
824 int start = size / 2;
834 swap(*(first + end), *first);
838 while (iLeftChild(root) < end)
840 int child = iLeftChild(root);
841 if (child + 1 < end && comp(*(first + child), *(first + child + 1)))
844 if (comp(*(first + root), *(first + child)))
846 swap(*(first + root), *(first + child));
856template <
class Iterator>
857inline void heapsort(Iterator first, Iterator last)
859 heapsort(first, last, IsLess<
typename IteratorTraits<Iterator>::ValueType>);
863template <
class Iterator>
864inline void heapsortDesc(Iterator first, Iterator last)
866 heapsort(first, last, IsNotLess<
typename IteratorTraits<Iterator>::ValueType>);
870template <
class Iterator,
class Compare>
871inline void insertionsort(Iterator first, Iterator last, Compare comp)
873 const int size = distance(first, last);
875 for (
int i = 1; i < size; i++)
877 const auto x = *(first + i);
880 while (j > 0 && comp(x, *(first + j - 1)))
882 *(first + j) = *(first + j - 1);
890template <
class Iterator>
891inline void insertionsort(Iterator first, Iterator last)
893 insertionsort(first, last, IsLess<
typename IteratorTraits<Iterator>::ValueType>);
897template <
class Iterator>
898inline void insertionsortDesc(Iterator first, Iterator last)
900 insertionsort(first, last, IsNotLess<
typename IteratorTraits<Iterator>::ValueType>);
906 template <
class Iterator,
class Compare>
907 inline void introsort(Iterator first, Iterator last, Compare comp,
unsigned int maxDepth)
909 const int size = distance(first, last);
911 insertionsort(first, last, comp);
912 else if (maxDepth == 0)
913 heapsort(first, last, comp);
916 Iterator lastElem = prev(last);
917 Iterator mid = next(first, size / 2);
918 Iterator p = medianOfThree(first, mid, lastElem, comp);
920 Iterator q = partition(first, lastElem, comp);
922 introsort(first, q, comp, maxDepth - 1);
923 introsort(next(q), last, comp, maxDepth - 1);
930template <
class Iterator,
class Compare>
931inline void sort(Iterator first, Iterator last, Compare comp)
933 const unsigned int maxDepth = floor(log2(max(1, distance(first, last)))) * 2;
934 introsort(first, last, comp, maxDepth);
938template <
class Iterator>
939inline void sort(Iterator first, Iterator last)
941 const unsigned int maxDepth = floor(log2(max(1, distance(first, last)))) * 2;
942 introsort(first, last, IsLess<
typename IteratorTraits<Iterator>::ValueType>, maxDepth);
946template <
class Iterator>
947inline void sortDesc(Iterator first, Iterator last)
949 const unsigned int maxDepth = floor(log2(max(1, distance(first, last)))) * 2;
950 introsort(first, last, IsNotLess<
typename IteratorTraits<Iterator>::ValueType>, maxDepth);
A function object returning true if its argument is equal to a reference value sets upon object const...
Definition algorithms.h:42
A function object returning true if its argument is greater than a reference value sets upon object c...
Definition algorithms.h:68
A function object returning true if its argument is less than a reference value sets upon object cons...
Definition algorithms.h:94
A function object returning true if its argument is not equal to a reference value sets upon object c...
Definition algorithms.h:55
A function object returning true if its argument is not greater than a reference value sets upon obje...
Definition algorithms.h:81
A function object returning true if its argument is not less than a reference value sets upon object ...
Definition algorithms.h:107
A unique pointer implementation.
Definition UniquePtr.h:118