4.6.0
Freundlich's C++ toolkit
|
General-purpose algorithms.
C++'s standard algorithms use iterators both for function inputs and outputs. This makes functional programming hard in two ways:
This module provides a collection of very common algorithms over ranges that fix both problems. Prominent examples are fcppt::algorithm::fold and fcppt::algorithm::map . To get the most out of them, these can be used in conjunction with a range library (like Boost.Range
).
Classes | |
struct | fcppt::algorithm::loop_break_impl< Range, Enable > |
Customization point for ranges. More... | |
Typedefs | |
template<typename Range > | |
using | fcppt::algorithm::range_element_type = decltype(*std::declval<Range>().begin()) |
The type of a range element. | |
Enumerations | |
enum class | fcppt::algorithm::update_action : std::uint8_t { fcppt::algorithm::update_action::keep , fcppt::algorithm::update_action::remove } |
Update action for container iteration. More... | |
Functions | |
template<typename Range , typename Pred > | |
bool | fcppt::algorithm::all_of (Range const &_range, Pred const &_pred) |
Checks if a given predicate is true for all elements of a range. | |
template<typename Range , typename T > | |
fcppt::optional::object< fcppt::container::to_iterator_type< std::remove_reference_t< Range > > > | fcppt::algorithm::binary_search (Range &&_range, T const &_value) |
Finds an element by binary search. | |
template<typename Range , typename T > | |
bool | fcppt::algorithm::contains (Range const &_range, T const &_value) |
Checks if a given value is inside a range. | |
template<typename Range , typename Pred > | |
bool | fcppt::algorithm::contains_if (Range const &_range, Pred const &_pred) |
Checks if a given value is inside a range, using a predicate. | |
template<typename Range1 , typename Range2 > | |
bool | fcppt::algorithm::equal (Range1 const &_range1, Range2 const &_range2) |
Compares two ranges for equality. | |
template<typename Range , typename T > | |
fcppt::iterator::range< fcppt::container::to_iterator_type< std::remove_reference_t< Range > > > | fcppt::algorithm::equal_range (Range &&_range, T const &_value) |
Returns the equal range of a given range and a value. | |
template<typename Result , typename Range , typename Function > | |
fcppt::optional::object< Result > | fcppt::algorithm::find_by_opt (Range &&_range, Function const &_function) |
Optionally finds an element and transforms it. | |
template<typename Range , typename Comp > | |
fcppt::optional::object< fcppt::container::to_iterator_type< std::remove_reference_t< Range > > > | fcppt::algorithm::find_if_opt (Range &&_range, Comp const &_comp) |
Like std::find_if but returns an empty optional on failure. | |
template<typename Range , typename T > | |
fcppt::optional::object< fcppt::container::to_iterator_type< std::remove_reference_t< Range > > > | fcppt::algorithm::find_opt (Range &&_range, T const &_value) |
Like std::find but returns an empty optional on failure. | |
template<typename Range , typename State , typename Function > | |
State | fcppt::algorithm::fold (Range &&_range, State _state, Function _function) |
Folds a range into a value. | |
template<typename Range , typename State , typename Function > | |
State | fcppt::algorithm::fold_break (Range &&_range, State _state, Function _function) |
Folds a range into a value, can break out early. | |
template<typename TargetContainer , typename Function > | |
TargetContainer | fcppt::algorithm::generate_n (std::size_t const _count, Function const &_function) |
Generates a container by calling a function a number of times. | |
template<typename Range , typename T > | |
fcppt::optional::object< typename Range::size_type > | fcppt::algorithm::index_of (Range const &_range, T const &_value) |
Returns the index of the first element found in a sequence. | |
template<typename Range > | |
fcppt::type_traits::value_type< Range > | fcppt::algorithm::join_strings (Range const &_range, fcppt::type_traits::value_type< Range > const &_delim) |
Joins a range of strings, using a delimiter. | |
template<typename Range , typename Body > | |
void | fcppt::algorithm::loop (Range &&_range, Body const &_body) |
Iterates through a range. | |
template<typename Range , typename Body > | |
void | fcppt::algorithm::loop_break (Range &&_range, Body const &_body) |
Iterates through a range with the ability to break out of the loop. | |
template<typename TargetContainer , typename SourceRange , typename Function > | |
TargetContainer | fcppt::algorithm::map (SourceRange &&_source, Function const &_function) |
Transforms a range to another container by applying a function to every element. | |
template<typename TargetContainer , typename Range , typename Function > | |
TargetContainer | fcppt::algorithm::map_concat (Range &&_range, Function const &_function) |
Maps a range to other sequences and joins them. | |
template<typename Map , typename UpdateAction > | |
void | fcppt::algorithm::map_iteration (Map &_map, UpdateAction const &_update_action) |
Iterates over a map with the possibility of erasing elements. | |
template<typename Map , typename UpdateAction > | |
void | fcppt::algorithm::map_iteration_second (Map &_map, UpdateAction const &_update_action) |
Iterates over a map with the possibility of erasing elements, passing second. | |
template<typename TargetContainer , typename Source , typename Function > | |
TargetContainer | fcppt::algorithm::map_optional (Source &&_source, Function const &_function) |
Transforms a range to another container by applying a function to every element, only inserting the results that are not empty optionals. | |
template<typename Container > | |
bool | fcppt::algorithm::remove (Container &_container, typename Container::const_reference _element) |
Removes all occurrences of a value from a container. | |
template<typename Container , typename Predicate > | |
bool | fcppt::algorithm::remove_if (Container &_container, Predicate const &_predicate) |
Removes all elements from a container matching a predicate. | |
template<typename Count , typename Function > | |
void | fcppt::algorithm::repeat (Count const _count, Function const &_function) |
Calls a function a number of times. | |
template<typename Container > | |
std::remove_cvref_t< Container > | fcppt::algorithm::reverse (Container &&_container) |
Reverses a container. | |
template<typename Sequence , typename UpdateAction > | |
void | fcppt::algorithm::sequence_iteration (Sequence &_sequence, UpdateAction const &_update_action) |
Iterates over a sequence with the possibility of erasing elements. | |
template<typename String > | |
std::vector< String > | fcppt::algorithm::split_string (String const &_string, fcppt::type_traits::value_type< String > const _delim) |
Splits a string, using a delimiter. | |
template<typename Container > | |
void | fcppt::algorithm::unique (Container &_container) |
Removes duplicate elements from a container. | |
template<typename Container , typename BinaryPredicate > | |
void | fcppt::algorithm::unique_if (Container &_container, BinaryPredicate const &_predicate) |
Removes duplicate elements from a container. Compares using a predicate. | |
using fcppt::algorithm::range_element_type = decltype(*std::declval<Range>().begin()) |
The type of a range element.
Depending on the range, this can be a reference, const reference or an object type.
|
strong |
|
inline |
Checks if a given predicate is true for all elements of a range.
Pred | A function callable as bool (Range::value_type) . |
fcppt::optional::object< fcppt::container::to_iterator_type< std::remove_reference_t< Range > > > fcppt::algorithm::binary_search | ( | Range && | _range, |
T const & | _value ) |
Finds an element by binary search.
If there is exactly one element that is uncomparable to _value, returns an iterator to that element. Otherwise, returns the empty optional.
T | Must be less-than comparable to the range's value type. |
|
inline |
Checks if a given value is inside a range.
T | Must be equality comparable to the range's value type. |
|
inline |
Checks if a given value is inside a range, using a predicate.
Pred | A function callable as bool (T) for all types T that appear in Range. |
|
inline |
Compares two ranges for equality.
|
inline |
Returns the equal range of a given range and a value.
T | Must be less-than comparable to the range's value type. |
|
inline |
Optionally finds an element and transforms it.
Returns the first element in _range for which _function does not return an empty optional, if there is any. Otherwise, returns the empty optional.
Function | A function callable as fcppt::optional::object<Result> (V) for every type V in Range. |
|
inline |
Like std::find_if
but returns an empty optional on failure.
Comp | Must be a function callable as bool (Range::value_type) . |
fcppt::optional::object< fcppt::container::to_iterator_type< std::remove_reference_t< Range > > > fcppt::algorithm::find_opt | ( | Range && | _range, |
T const & | _value ) |
Like std::find
but returns an empty optional on failure.
T | Must be equality-comparable to Range::value_type . |
|
inline |
Folds a range into a value.
Like fold_left
in a functional programming language, this function starts with _state as cur_state
, and calls cur_state = _function(element, cur_state)
for every element of _range.
State | Has to be movable |
Function | Must be callable as State (Range::value_type, State) . |
|
inline |
Folds a range into a value, can break out early.
Let [e_1,...,e_n]
be the input range and s_0 = _state
.
This function calls (l_i, s_i) = function(e_i,s{i-1})
for i = 1,...,x
where x <= n
is the largest number such that l_j = loop::continue_
for all 1 <= j < x
.
State | Has to be movable |
Function | Must be callable as std::pair<fcppt::loop, State> (Range::value_type,State) . |
TargetContainer fcppt::algorithm::generate_n | ( | std::size_t const | _count, |
Function const & | _function ) |
Generates a container by calling a function a number of times.
Calls _function _count times and collects the results into a container.
Function | A function callable as TargetContainer::value_type() . |
|
inline |
Returns the index of the first element found in a sequence.
Searches for _value in _range and returns the index of the first occurrence if there is any, otherwise returns the empty optional.
Range | A random access range. |
T | A type equality-comparable to the ranges's value type. |
fcppt::type_traits::value_type< Range > fcppt::algorithm::join_strings | ( | Range const & | _range, |
fcppt::type_traits::value_type< Range > const & | _delim ) |
Joins a range of strings, using a delimiter.
Joins _range, inserting _delim between every pair of consecutive elements.
Example:
Range | A range of strings. |
|
inline |
Iterates through a range.
Iterates through _range, calling _body for every element of the range. The implementation for a specific range type is handled by fcppt::algorithm::loop_break_impl.
Body | A function callable as void (T) for every type T in Range. |
|
inline |
Iterates through a range with the ability to break out of the loop.
Iterates through _range, calling _body for every element of the range as long as _function returns fcppt::loop::continue_. The implementation for a specific range type is handled by fcppt::algorithm::loop_break_impl.
Body | A function callable as fcppt::loop (T) for every type T in Range |
TargetContainer fcppt::algorithm::map | ( | SourceRange && | _source, |
Function const & | _function ) |
Transforms a range to another container by applying a function to every element.
For every element e in _source, _function(e)
is inserted into the result container.
reserve
function, and the source range needs a size
function or must be a random-access range.The actual implementation of the algorithm is provided by fcppt::algorithm::map_impl which by default uses fcppt::algorithm::loop.
Function | A function callable as TargetContainer::value_type (SourceRange::value_type) . |
TargetContainer fcppt::algorithm::map_concat | ( | Range && | _range, |
Function const & | _function ) |
Maps a range to other sequences and joins them.
For every element in _range (a_1, ..., a_n)
, _function(a_1), ..., _function(a_n)
is called, yielding (r_1, ..., r_n)
. The result is join(r_1, r_2, ...,
r_n)
.
Function | A function callable as TargetContainer (Range::value_type) . |
void fcppt::algorithm::map_iteration | ( | Map & | _map, |
UpdateAction const & | _update_action ) |
Iterates over a map with the possibility of erasing elements.
Iterates over _map, applying _update_action to each element. If _update_action returns fcppt::algorithm::update_action::remove, the element is deleted from the map.
Map | A map-like container. |
UpdateAction | A function callable as fcppt::algorithm::update_action (Map::value_type) . |
void fcppt::algorithm::map_iteration_second | ( | Map & | _map, |
UpdateAction const & | _update_action ) |
Iterates over a map with the possibility of erasing elements, passing second.
Iterates over _map, applying _update_action to the mapped object of each element. If _update_action returns fcppt::algorithm::update_action::remove, the element is removed from the map.
Map | A map-like container. |
UpdateAction | A function callable as fcppt::algorithm::update_action (Map::mapped_type) . |
TargetContainer fcppt::algorithm::map_optional | ( | Source && | _source, |
Function const & | _function ) |
Transforms a range to another container by applying a function to every element, only inserting the results that are not empty optionals.
For every element e in _source, _function(e)
is called. If the result is not an empty optional, it is inserted into the result container.
Function | A function callable as fcppt::optional::object<TargetContainer::value_type> (Source::value_type) . |
TODO(philipp): concepts
|
inline |
Removes all occurrences of a value from a container.
Removes all elements from a _container
equal to _element
.
true
if something has been removed, false
otherwise. bool fcppt::algorithm::remove_if | ( | Container & | _container, |
Predicate const & | _predicate ) |
Removes all elements from a container matching a predicate.
Removes all elements from _container
matching _predicate
.
Predicate | A function callable as bool (Container::value_type) . |
true
if something has been removed, false
otherwise. void fcppt::algorithm::repeat | ( | Count const | _count, |
Function const & | _function ) |
Calls a function a number of times.
Calls _function _count times.
Count | An integer type |
Function | A function callable as void () . |
|
inline |
Reverses a container.
Container | Must be a bidirectional container |
void fcppt::algorithm::sequence_iteration | ( | Sequence & | _sequence, |
UpdateAction const & | _update_action ) |
Iterates over a sequence with the possibility of erasing elements.
Iterates over _sequence, applying _update_action to each element. If _update_action returns fcppt::algorithm::update_action::remove, the element is removed from the sequence.
UpdateAction | A function callable as fcppt::algorithm::update_action (Sequence::value_type) . |
std::vector< String > fcppt::algorithm::split_string | ( | String const & | _string, |
fcppt::type_traits::value_type< String > const | _delim ) |
Splits a string, using a delimiter.
Let , where and , where , be the positions of _string with value _delim. Also, let . The result is
Note that in case m is 0, the string itself is returned as a single element.
String | Must be a container, constructible with a pair of iterators. |
|
inline |
Removes duplicate elements from a container.
Removes duplicate elements from _container
.
void fcppt::algorithm::unique_if | ( | Container & | _container, |
BinaryPredicate const & | _predicate ) |
Removes duplicate elements from a container. Compares using a predicate.
Removes duplicate elements from _container
. Elements are compared using _predicate
.
BinaryPredicate | A function callable as bool (Container::value_type, Container::value_type) . |