cxxomfort  rel.20210622
Simple backports for C++ - http://ryan.gulix.cl/fossil.cgi/cxxomfort/
Namespaces | Functions
library/algorithm.hpp File Reference

cxxomfort Supplementals for <algorithm> More...

Namespaces

 cxxomfort
 Namespace of the cxxomfort library.
 
 cxxomfort::library
 Supplements to backports and other utilities. Namespace that holds the library's own implementations, emulations and supplements.
 

Functions

template<typename Container >
void erase (Container &C, typename Container::value_type const &u)
 Invokes erase-remove idiom on a container to remove element u as per remove() . More...
 
template<typename Container , typename Pred >
void erase_if (Container &C, Pred if_)
 Invokes erase-remove idiom on a container to remove elements conditionally given predicate if_ as per remove_if() . More...
 
template<typename T , typename Compare >
CXXO_CONSTEXPR bool is_clamped (T const &value, T const &lo, T const &hi, Compare less) CXXO_NOEXCEPTNOTHROW
 Checks if a value is clamped. More...
 
template<typename T >
CXXO_CONSTEXPR bool is_clamped (T const &value, T const &lo, T const &hi) CXXO_NOEXCEPTNOTHROW
 
template<class T , class Compare >
CXXO_CONSTEXPR T const & clamp_halfopen (T const &val, T const &lo, T const &hi, Compare less) CXXO_NOEXCEPT
 Clamps a value within a half-open range. More...
 
template<class T >
CXXO_CONSTEXPR T const & clamp_halfopen (T const &val, T const &lo, T const &hi) CXXO_NOEXCEPT
 
template<typename T , typename Compare >
CXXO_CONSTEXPR bool is_clamped_halfopen (T const &value, T const &lo, T const &hi, Compare less) CXXO_NOEXCEPTNOTHROW
 Checks if a value is clamped in a half-open interval as per clamp_halfopen() More...
 
template<typename T >
CXXO_CONSTEXPR bool is_clamped_halfopen (T const &value, T const &lo, T const &hi) CXXO_NOEXCEPTNOTHROW
 
template<typename OutI , typename InpI , typename Pred >
OutI copy_if_not (InpI ini, InpI fin, OutI dest, Pred p)
 Copy like copy_if with inverted predicate (for symmetry with find_if_not ). More...
 
template<typename Iter1 , typename Integer , typename Iter2 >
Iter2 copy_leftmost_n (Iter1 ini1, Iter1, Integer n, Iter2 ini2)
 Like copy_n in that it copies leftmost ("starting") n elements from the sequence. More...
 
template<typename It1 , typename Integer , typename It2 >
It2 copy_rightmost_n (It1 ini1, It1 fin1, Integer n, It2 ini2)
 Like copy_n except it copies the rightmost n elements from the sequence. More...
 
template<typename It , typename Pred , typename T >
It find (It ini, It fin, Pred if_, T const &v)
 Finds an element x in a sequence such that if_(x,v) holds. More...
 
template<typename It , typename T >
It find_not (It ini, It fin, T const &t)
 Finds the first element in the sequence not equal to t . More...
 
template<typename It , typename Pred >
It find_if_not (It ini, It fin, Pred if_)
 Finds the first element in the sequence not fitting predicate if_ . More...
 
template<typename Iter , typename Less >
Iter find_inversion (Iter ini, Iter fin, Less lt)
 Finds a pair of elements that are out of place (ie.: the second one is "less" than the first). More...
 
template<typename Iter >
Iter find_inversion (Iter ini, Iter fin)
 
template<typename Iter , typename Less >
Iter fix_inversion (Iter ini, Iter fin, Less m)
 Fixes a pair of misplaced elements where the second one is "less" than the first. More...
 
template<typename Iter , typename Pred >
Iter find_last_if (Iter ini, Iter fin, Pred if_)
 Finds the latest item in a sequence that fits predicate if_ . More...
 
template<class InputIterator , class UnaryPredicate >
std::iterator_traits< InputIterator >::difference_type count_while (InputIterator first, InputIterator last, UnaryPredicate pred)
 
template<typename FIter , typename TF >
FIter transform_inplace (FIter ini, FIter fin, TF tf)
 Transform elements in the range in-place via the transformation tf . More...
 
template<typename FIter , typename TF , typename P >
FIter transform_inplace_if (FIter ini, FIter fin, TF tf, P p)
 Transform elements in the range conditionally. More...
 
template<typename IIterator , typename OIterator , typename TF , typename Integer >
OIterator transform_n (IIterator ini, Integer n, OIterator dest, TF tf)
 Transform elements up to n times. More...
 
template<typename Iterator , typename ValueMapIterator , typename Reductor >
Iterator count_frequencies_map (Iterator ini, Iterator fin, ValueMapIterator vi, Reductor const rint)
 Constructs a map of frequencies each element appears in the sequence. More...
 

Detailed Description

cxxomfort Supplementals for <algorithm>

Function Documentation

◆ is_clamped() [1/2]

CXXO_CONSTEXPR bool cxxomfort::library::algorithm::is_clamped ( T const &  value,
T const &  lo,
T const &  hi,
Compare  less 
)

Checks if a value is clamped.

Parameters
lessA relational predicate with semantics similar to those of std::less or operator<()
Returns
true if value is within the constraints.
""
See also
{p1440} algorithm/clamp @ cppreference

This is an implementation of p1440 "is_clamped": a complement to clamp() in the same way is_sorted() is a complement to sort().

Given general invocations of clamp(), the following assertion should hold:

(is_clamped(x, lo, hi)) == (x == clamp(x,lo,hi))
Warning
The behaviour is undefined if hi < lo.
Note
Unlike the p1440 proposal, is_clamped() here is marked noexcept ; I consider there is no good scenario where relational comparison operators, or anything that mimics them, would be throwing, and clamp() is to abort rather than throw upon precondition violation.

Referenced by cxxomfort::library::algorithm::is_clamped().

◆ is_clamped() [2/2]

CXXO_CONSTEXPR bool cxxomfort::library::algorithm::is_clamped ( T const &  value,
T const &  lo,
T const &  hi 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ clamp_halfopen() [1/2]

CXXO_CONSTEXPR T const& cxxomfort::library::algorithm::clamp_halfopen ( T const &  val,
T const &  lo,
T const &  hi,
Compare  less 
)

Clamps a value within a half-open range.

Parameters
lessA relational predicate with semantics similar to those of std::less or operator<()
Returns
An altered value from val to fit into the half-open interval [lo , hi ).
See also
algorithm/clamp @ cppreference

clamp_halfopen(V,a,b) is similar to clamp(V,a,b) except that b is excluded as a potential resulting value.

Warning
The behaviour is undefined if hi < lo.

Referenced by cxxomfort::library::algorithm::clamp_halfopen(), and cxxomfort::library::algorithm::is_clamped_halfopen().

◆ clamp_halfopen() [2/2]

CXXO_CONSTEXPR T const& cxxomfort::library::algorithm::clamp_halfopen ( T const &  val,
T const &  lo,
T const &  hi 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ is_clamped_halfopen() [1/2]

CXXO_CONSTEXPR bool cxxomfort::library::algorithm::is_clamped_halfopen ( T const &  value,
T const &  lo,
T const &  hi,
Compare  less 
)

Checks if a value is clamped in a half-open interval as per clamp_halfopen()

Returns
a bool.

This is a supplement to p1440 "is_clamped" in that it checks if the value is clamped in a half-open interval.

Given the general concept of calls to clamp(), the following assertion should hold:

(is_clamped_halfopen(x, lo, hi)) if and only if (x == clamp_halfopen(x,lo,hi))
Warning
The behaviour is undefined if hi < lo.
Note
is_clamped_halfopen() here is marked noexcept ; I consider there is no good scenario where relational comparison operators, or anything that mimics them, would be throwing.

Referenced by cxxomfort::library::algorithm::is_clamped_halfopen().

◆ is_clamped_halfopen() [2/2]

CXXO_CONSTEXPR bool cxxomfort::library::algorithm::is_clamped_halfopen ( T const &  value,
T const &  lo,
T const &  hi 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ copy_if_not()

OutI cxxomfort::library::algorithm::copy_if_not ( InpI  ini,
InpI  fin,
OutI  dest,
Pred  p 
)
inline

Copy like copy_if with inverted predicate (for symmetry with find_if_not ).

See also
algorithm/copy_if @ cppreference

This is provided for symmetry with: std::find_if_not(), std::copy_if().

Referenced by cxxomfort::library::algorithm::copy_if_not().

◆ copy_leftmost_n()

Iter2 cxxomfort::library::algorithm::copy_leftmost_n ( Iter1  ini1,
Iter1  ,
Integer  n,
Iter2  ini2 
)

Like copy_n in that it copies leftmost ("starting") n elements from the sequence.

This function copies the first n elements from the sequence starting at ini1. While it is basically equivalent to copy_n() , it is provided for symmetry and completeness with copy_rightmost_n() .

See also
algorithm/copy_n @ cppreference
copy_rightmost_n() .

Referenced by cxxomfort::library::algorithm::copy_leftmost_n().

◆ copy_rightmost_n()

It2 cxxomfort::library::algorithm::copy_rightmost_n ( It1  ini1,
It1  fin1,
Integer  n,
It2  ini2 
)

Like copy_n except it copies the rightmost n elements from the sequence.

Returns
Iterator pointing to the end of the copied sequence at the destination.
See also
copy_leftmost_n()

◆ find()

It cxxomfort::library::algorithm::find ( It  ini,
It  fin,
Pred  if_,
T const &  v 
)

Finds an element x in a sequence such that if_(x,v) holds.

Returns
Iterator pointing to the found element, or fin.

This is taken from proposal wlg217.

See also: std::find_if.

◆ find_not()

It cxxomfort::library::algorithm::find_not ( It  ini,
It  fin,
T const &  t 
)

Finds the first element in the sequence not equal to t .

Returns
An iterator, or fin .
See also
find()

This is provided for homology with: std::find, std::find_if_not.

Referenced by cxxomfort::library::algorithm::find_not().

◆ find_if_not()

It cxxomfort::library::algorithm::find_if_not ( It  ini,
It  fin,
Pred  if_ 
)

Finds the first element in the sequence not fitting predicate if_ .

Returns
An iterator, or fin .
See also
find_if()

◆ find_inversion() [1/2]

Iter cxxomfort::library::algorithm::find_inversion ( Iter  ini,
Iter  fin,
Less  lt 
)
inline

Finds a pair of elements that are out of place (ie.: the second one is "less" than the first).

Returns
Iterator pointing to the first such element, or fin if none.

For example, for the following example find_inversion will return an iterator pointing to the element 6:

vector<int> elems = { -17, 3, 6, -2, -3, -4 };
// ^ sorting order is broken in this pair
vector<int>::iterator br = find_inversion( begin(elems), end(elems) );
// at this point, br points to index 2 (element 6).

See also: fix_inversion().

Referenced by cxxomfort::library::algorithm::find_inversion(), and cxxomfort::library::algorithm::fix_inversion().

◆ find_inversion() [2/2]

Iter cxxomfort::library::algorithm::find_inversion ( Iter  ini,
Iter  fin 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ fix_inversion()

Iter cxxomfort::library::algorithm::fix_inversion ( Iter  ini,
Iter  fin,
Less  m 
)
inline

Fixes a pair of misplaced elements where the second one is "less" than the first.

Returns
Iterator pointing to the first such element, or fin if none.

See also: find_inversion().

Referenced by cxxomfort::library::algorithm::fix_inversion().

◆ find_last_if()

Iter cxxomfort::library::algorithm::find_last_if ( Iter  ini,
Iter  fin,
Pred  if_ 
)

Finds the latest item in a sequence that fits predicate if_ .

Parameters
ini,finDelimiters to the sequence to be examined.
if_A predicate (a function from ini...fin's alue type to bool ).
Returns
An iterator pointing to the latest item in [ini,fin) that fulfills if_ . This is provided for homology with: std::find_if.

◆ count_while()

std::iterator_traits<InputIterator>::difference_type cxxomfort::library::algorithm::count_while ( InputIterator  first,
InputIterator  last,
UnaryPredicate  pred 
)

◆ transform_inplace()

FIter cxxomfort::library::algorithm::transform_inplace ( FIter  ini,
FIter  fin,
TF  tf 
)

Transform elements in the range in-place via the transformation tf .

Parameters
ini,finIterators marking a writable sequence.
tfA transformation of the form TF(value_type) → value_type.
Returns
The end iterator of the sequence.
""

Referenced by cxxomfort::library::algorithm::transform_inplace().

◆ transform_inplace_if()

FIter cxxomfort::library::algorithm::transform_inplace_if ( FIter  ini,
FIter  fin,
TF  tf,
p 
)

Transform elements in the range conditionally.

Parameters
ini,finIterators marking a writable sequence.
tfA transformation of the form TF(*FIter) -> *FIter.
pa predicate to decide if the elements need transformation.
Returns
The end iterator of the sequence.

This is provided for homology with: std::copy_if

Referenced by cxxomfort::library::algorithm::transform_inplace_if().

◆ transform_n()

OIterator cxxomfort::library::algorithm::transform_n ( IIterator  ini,
Integer  n,
OIterator  dest,
TF  tf 
)
inline

Transform elements up to n times.

Parameters
tfA transformation of the form TF(*FIter) -> *FIter.

This is provided for homology with: std::copy_n.

Referenced by cxxomfort::library::algorithm::transform_n().

◆ count_frequencies_map()

Iterator cxxomfort::library::algorithm::count_frequencies_map ( Iterator  ini,
Iterator  fin,
ValueMapIterator  vi,
Reductor const  rint 
)

Constructs a map of frequencies each element appears in the sequence.

Given a sequence S=[ini,fin), and a map vi of elements of type X with an associated mapping function rint such that:

f "rint" : (X) → [0, 1, ..., k]

Then it iterates over S and increments the value at vi+f(x) .

Referenced by cxxomfort::library::algorithm::count_frequencies_map().

◆ erase()

void cxxomfort::library::algorithm::lfv2::erase ( Container &  C,
typename Container::value_type const &  u 
)

Invokes erase-remove idiom on a container to remove element u as per remove() .

Parameters
Ca container object (lvalue).
uthe value to pass to C.remove() .
Exceptions
exceptionWhatever the expression C.erase(...) throws.
See also
experimental/lib_extensions_2 @ cppreference

Referenced by cxxomfort::library::algorithm::lfv2::erase().

◆ erase_if()

void cxxomfort::library::algorithm::lfv2::erase_if ( Container &  C,
Pred  if_ 
)

Invokes erase-remove idiom on a container to remove elements conditionally given predicate if_ as per remove_if() .

Parameters
Ca container object (lvalue).
if_A predicate to pass to C.remove_if() .
Exceptions
exceptionWhatever the expression C.erase(...) throws.
See also
experimental/lib_extensions_2 @ cppreference

Referenced by cxxomfort::library::algorithm::lfv2::erase_if().