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,
const Iterator last, UnaryPredicate pred)
227 for (; first != last; ++first)
237template <
class Iterator,
class UnaryPredicate>
238inline bool noneOf(Iterator first,
const Iterator last, UnaryPredicate pred)
240 for (; first != last; ++first)
250template <
class Iterator,
class UnaryPredicate>
251inline bool anyOf(Iterator first,
const Iterator last, UnaryPredicate pred)
253 for (; first != last; ++first)
263template <
class Iterator,
class Function>
264inline Function forEach(Iterator first,
const Iterator last, Function fn)
266 for (; first != last; ++first)
273template <
class Iterator,
class T>
274inline Iterator find(Iterator first,
const Iterator last,
const T &value)
276 for (; first != last; ++first)
286template <
class Iterator,
class UnaryPredicate>
287inline Iterator findIf(Iterator first,
const Iterator last, UnaryPredicate pred)
289 for (; first != last; ++first)
299template <
class Iterator,
class UnaryPredicate>
300inline Iterator findIfNot(Iterator first,
const Iterator last, UnaryPredicate pred)
302 for (; first != last; ++first)
312template <
class Iterator,
class T>
313inline int count(Iterator first,
const Iterator last,
const T &value)
317 for (; first != last; ++first)
327template <
class Iterator,
class UnaryPredicate>
328inline int countIf(Iterator first,
const Iterator last, UnaryPredicate pred)
332 for (; first != last; ++first)
342template <
class Iterator1,
class Iterator2>
343inline bool equal(Iterator1 first1,
const Iterator1 last1, Iterator2 first2)
345 while (first1 != last1)
347 if (*first1 != *first2)
357template <
class Iterator>
358inline Iterator minElement(Iterator first,
const Iterator last)
363 Iterator smallest = first;
366 for (; first != last; ++first)
368 if (*first < *smallest)
376template <
class Iterator>
377inline Iterator maxElement(Iterator first,
const 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,
const 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,
const 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,
const IteratorIn last, IteratorOut result, UnaryPredicate pred)
445 while (first != last)
459template <
class IteratorIn,
class IteratorOut,
class UnaryOperation>
460inline IteratorOut transform(IteratorIn first1,
const 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,
const 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,
const 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,
const Iterator last, UnaryPredicate pred,
const T &newValue)
503 while (first != last)
512template <
class IteratorIn,
class IteratorOut,
class T>
513inline IteratorOut replaceCopy(IteratorIn first,
const 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,
const 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,
const 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,
const 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,
const 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,
const 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,
const Iterator last)
648 Iterator next = first;
649 while (++next != last)
660template <
class Iterator,
class Compare>
661inline bool isSorted(Iterator first,
const Iterator last, Compare comp)
666 Iterator next = first;
667 while (++next != last)
669 if (comp(*next, *first))
678template <
class Iterator>
679inline const Iterator isSortedUntil(Iterator first,
const Iterator last)
684 Iterator next = first;
685 while (++next != last)
696template <
class Iterator,
class Compare>
697inline const Iterator isSortedUntil(Iterator first,
const Iterator last, Compare comp)
702 Iterator next = first;
703 while (++next != last)
705 if (comp(*next, *first))
716 template <
class Iterator,
class Compare>
717 inline Iterator partition(Iterator first, Iterator last, Compare comp)
719 const Iterator pivot = last;
721 while (first != last)
723 while (comp(*first, *pivot))
735 }
while (!comp(*last, *pivot));
745 template <
class Iterator,
class Compare>
746 inline void quicksort(Iterator first, Iterator last, RandomAccessIteratorTag, Compare comp)
748 const int size = distance(first, last);
751 Iterator p = prev(last);
752 swap(*next(first, size / 2), *p);
753 Iterator q = partition(first, p, comp);
755 quicksort(first, q, RandomAccessIteratorTag(), comp);
756 quicksort(next(q), last, RandomAccessIteratorTag(), comp);
761 template <
class Iterator,
class Compare>
762 inline void quicksort(Iterator first, Iterator last, BidirectionalIteratorTag, Compare comp)
766 Iterator p = prev(last);
768 Iterator q = partition(first, p, comp);
770 quicksort(first, q, BidirectionalIteratorTag(), comp);
771 quicksort(next(q), last, BidirectionalIteratorTag(), comp);
778template <
class Iterator,
class Compare>
779inline void quicksort(Iterator first, Iterator last, Compare comp)
781 quicksort(first, last, IteratorTraits<Iterator>::IteratorCategory(), comp);
785template <
class Iterator>
786inline void quicksort(Iterator first, Iterator last)
788 quicksort(first, last, IteratorTraits<Iterator>::IteratorCategory(), IsLess<
typename IteratorTraits<Iterator>::ValueType>);
792template <
class Iterator>
793inline void quicksortDesc(Iterator first, Iterator last)
795 quicksort(first, last, IteratorTraits<Iterator>::IteratorCategory(), IsNotLess<
typename IteratorTraits<Iterator>::ValueType>);
801 inline int iLeftChild(
int i)
809template <
class Iterator,
class Compare>
810inline void heapsort(Iterator first, Iterator last, Compare comp)
812 const int size = distance(first, last);
813 int start = floor(size / 2);
823 swap(*(first + end), *first);
827 while (iLeftChild(root) < end)
829 int child = iLeftChild(root);
830 if (child + 1 < end && comp(*(first + child), *(first + child + 1)))
833 if (comp(*(first + root), *(first + child)))
835 swap(*(first + root), *(first + child));
845template <
class Iterator>
846inline void heapsort(Iterator first, Iterator last)
848 heapsort(first, last, IsLess<
typename IteratorTraits<Iterator>::ValueType>);
852template <
class Iterator>
853inline void heapsortDesc(Iterator first, Iterator last)
855 heapsort(first, last, IsNotLess<
typename IteratorTraits<Iterator>::ValueType>);
859template <
class Iterator,
class Compare>
860inline void insertionsort(Iterator first, Iterator last, Compare comp)
862 const int size = distance(first, last);
867 auto x = *(first + i);
869 while (j > 0 && comp(x, *(first + j - 1)))
871 *(first + j) = *(first + j - 1);
880template <
class Iterator>
881inline void insertionsort(Iterator first, Iterator last)
883 insertionsort(first, last, IsLess<
typename IteratorTraits<Iterator>::ValueType>);
887template <
class Iterator>
888inline void insertionsortDesc(Iterator first, Iterator last)
890 insertionsort(first, last, IsNotLess<
typename IteratorTraits<Iterator>::ValueType>);
896 template <
class Iterator,
class Compare>
897 inline void introsort(Iterator first, Iterator last, Compare comp,
unsigned int maxDepth)
899 const int size = distance(first, last);
901 insertionsort(first, last, comp);
902 else if (maxDepth == 0)
903 heapsort(first, last, comp);
906 Iterator p = prev(last);
907 swap(*next(first, size / 2), *p);
908 Iterator q = partition(first, p, comp);
910 introsort(first, q, comp, maxDepth - 1);
911 introsort(next(q), last, comp, maxDepth - 1);
918template <
class Iterator,
class Compare>
919inline void sort(Iterator first, Iterator last, Compare comp)
921 const unsigned int maxDepth = log(distance(first, last)) * 2;
922 introsort(first, last, comp, maxDepth);
926template <
class Iterator>
927inline void sort(Iterator first, Iterator last)
929 const unsigned int maxDepth = log(distance(first, last)) * 2;
930 introsort(first, last, IsLess<
typename IteratorTraits<Iterator>::ValueType>, maxDepth);
934template <
class Iterator>
935inline void sortDesc(Iterator first, Iterator last)
937 const unsigned int maxDepth = log(distance(first, last)) * 2;
938 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