 Search on this site  Template Algorithms

template <class T>

void Swap(T& a, T& b)

Swaps values. Specific types might specialize Swap.

template <class I>

void IterSwap(I a, I b)

Swaps values pointed to by iterators. Specific types might specialize IterSwap.

template <class T

int sgn(T a)

Returns 1 if  a. is greater than zero, -1 if a is smaller than zero and zero if a is zero.

template <class T

T tabs(T a)

Returns the absolute value of a.

template <class T

int cmp(const T& a, const T& b)

Returns 1 if a is greater than b, -1 if a is smaller than b and zero if a is equal than b.

template <class Range

void Reverse(Range&& r)

Reverses the order of values in a range.

template <class Range

ValueTypeOf<RangeSum(const Range& r, const ValueTypeOf<Range>& zero)

Returns the sum of all elements in range r, with zero representing initial zero value. T must have defined operator+=.

template <class Range

typename ValueTypeOf<RangeSum(const T& c)

Same as Sum(c, 0).

template <class Range, class V

int Count(const Range& r, const V& val)

Counts the number of elements in the Range r that are equal to val .

template <class Range, class Predicate

int CountIf(const Range& r, const Predicate& p)

Counts the number of elements in the Range r that satisfy condition p.

template <class Range, class Pred

int FindBest(const Range& r, const Pred& pred)

Finds the most suitable element in a range r as specified by pred. E.g. if pred is std::less, finds minimum. If r is empty, returns -1.

template <class Range

int FindMin(const Range& r)

Returns the index of minimal element of r, using std::less to compare elements. If r is empty, returns -1.

template <class Range

const ValueTypeOf<Range>& Min(const Range& r)

Returns the value of minimal element of r, using std::less to compare elements. If r is empty, behavior is undefined (ASSERT fails in debug).

template <class Range

const ValueTypeOf<Range>& Min(const Range& r, const ValueTypeOf<Range>& def)

Returns the value of minimal element of r, using std::less to compare elements. If r is empty, returns def.

template <class Range

int FindMax(const Range& r)

Returns the index of maximal element of r, using std::greater to compare elements. If r is empty, returns -1.

template <class Range

const ValueTypeOf<Range>& Max(const Range& r)

Returns the value of maximal element of r, using std::less to compare elements. If r is empty, behavior is undefined (ASSERT fails in debug)..

template <class Range

const ValueTypeOf<Range>& Max(const Range& r, const ValueTypeOf<Range>& def)

Returns the value of maximal element of r, using std::less to compare elements. If r is empty, returns def.

template <class Range1, class Range2

bool IsEqualRange(const Range1& a, const Range2& b)

Returns true if a and b are equal. operator== is used to compare elements. Ranges are considered equal if they have the same number of elements and for every element at index i: a[i] == b[i].

template <class Range1, class Range2

int CompareRanges(const Range1& a, const Range2& b)

Lexicographically compares ranges a b, using SgnCompare to compare elements. SgnCompare is supposed to return value < 0 if first element is less than second, 0 if they are equal, >0 otherwise. Returns value <0, 0, >0 if a < b, a == b, a > b.

template <class Range, class C

int FindMatch(const Range& r, const C& match, int from = 0)

Returns the index of first element for which predicate match is true. If not found, returns -1. Search starts at index from.

template <class Range, class V

int FindIndex(const Range& r, const V& value, int from = 0)

Returns the index of first element which is equal to value. If not found, returns -1. Search starts at index from.

template <class Range, class Predicate

Vector<intFindAll(const Range& r, Predicate match, int from = 0)

Returns the Vector of indices of  ALL elements for which match is true. Returned Vector is sorted in ascending order. Search starts at index from. The match parameter is an element.

template <class Range, class PredicateVector<intFindAlli(const Range& r, Predicate match, int from = 0)

Returns the Vector of indices of  ALL elements for which match is true. Returned Vector is sorted in ascending order. Search starts at index from. Unlike FindAll, the parameter to match is index of element.

template <class Range, class T, class Less

int FindLowerBound(const Range& r, const T& val, const Less& less)

template <class Range, class T

int FindLowerBound(const Range& r, const T& val)

Finds the first index in sorted range r, which must be sorted by less predicate (or std::less in second overload), where val can be inserted without breaking the ordering.

template <class Range, class T, class L

int FindUpperBound(const Range& r, const T& val, const L& less)

template <class Range, class T

int FindUpperBound(const Range& r, const T& val)

Finds the last index in sorted range r, which must be sorted by less predicate (or std::less in second overload), where val can be inserted without breaking the ordering.

template <class Range, class T, class L

int FindBinary(const Range& r, const T& val, const L& less)

template <class Range, class T

int FindBinary(const Range& r, const T& val)

Finds the index of val in sorted range r, which must be sorted by less predicate (or std::less in second overload). If val is not present in r, return -1.

template <class Container, class T

void LruAdd(Container& lru, T value, int limit = 10)

This specialized algorithm is intended to manage Least-Recently-Used lists. lru has to be U++ array type (Vector, Array, InVector, InArray) and represents the list. value is the value to be eventually added to the list, limit is the maximum number of elements in the list. On call, if value is present in the lru, it is moved to the front. If it is not present, it is inserted to the front and the size of list is eventually reduced to limit, possibly removing the element at the end of list.

template <class C = Vector<int>, class V

C MakeIota(V end, V start = 0, V step = 1)

Returns a cointainer of type C filled with sequence of numbers starting at start and less than end increasing by step increments.

 Last edit by cxl on 06/07/2019. Do you want to contribute?. T++