0.12.0
Freundlich's C++ toolkit
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Classes | Functions
fcppt.algorithm
fcppt

Description

General-purpose algorithms that don't really fit into a common category.

Classes

class  fcppt::algorithm::element_not_found
 May be thrown by the _exn algorithms like fcppt::algorithm::find_exn() More...
 
class  fcppt::algorithm::exception
 The exception thrown by some algorithm operations. More...
 

Functions

template<typename DestContainer , typename SourceContainer >
void fcppt::algorithm::append (DestContainer &dest, SourceContainer const &src)
 Appends the sequence src to dest.
 
template<typename TargetArray , typename SourceArray , typename Functor >
TargetArray const fcppt::algorithm::array_map (SourceArray const &source, Functor const &f)
 Applies a functor to an array and returns a new array containing the results.
 
template<typename In , typename T >
bool fcppt::algorithm::contains (In const beg, In const end, T const &value)
 Checks if a given value is inside the range [beg,end].
 
template<typename Container , typename T >
bool fcppt::algorithm::contains (Container const &container, T const &value)
 Checks if a given value is inside a range.
 
template<typename In , typename Pred >
bool fcppt::algorithm::contains_if (In const beg, In const end, Pred const &pred)
 Checks if a given value matches pred inside the range [beg end].
 
template<typename Container , typename Pred >
bool fcppt::algorithm::contains_if (Container const &container, Pred const &pred)
 Checks if a given value is inside a range.
 
template<typename InputIterator , typename OutputIterator , typename Predicate >
OutputIterator fcppt::algorithm::copy_if (InputIterator first, InputIterator last, OutputIterator result, Predicate pred)
 Assigns the values of elements from a source range satisfying a predicate to a destination range,.
 
template<typename In , typename Out , typename Size >
Out fcppt::algorithm::copy_n (In const _beg, Size const _sz, Out const _out)
 Copies sz elements from _beg to _out.
 
template<typename In , typename T >
In fcppt::algorithm::find_exn (In const begin, In const end, T const &t)
 Like std::find but throws fcppt::algorithm::element_not_found.
 
template<typename In , typename Comp >
In fcppt::algorithm::find_if_exn (In const begin, In const end, Comp const &comp)
 Like std::find_if but throws fcppt::algorithm::element_not_found.
 
template<typename Container >
Container const fcppt::algorithm::join (Container _left, Container const &_right)
 Joins two containers.
 
template<typename Range >
Range::value_type const fcppt::algorithm::join_strings (Range const &_range, typename Range::value_type const &_delim)
 Joins a range of items delimited by delim.
 
template<typename Range >
boost::range_size< Range >::type fcppt::algorithm::levenshtein (Range const &source, Range const &target)
 Calculates the Levenshtein distance.
 
template<typename TargetContainer , typename SourceContainer , typename Functor >
TargetContainer const fcppt::algorithm::map (SourceContainer const &s, Functor const &f)
 Transforms a container using a functor to another container.
 
template<typename Container , typename Ptr >
boost::enable_if
< boost::is_same< typename
boost::add_pointer< typename
boost::remove_const< typename
boost::remove_pointer< Ptr >
::type >::type >::type,
typename Container::value_type >
, bool >::type 
fcppt::algorithm::ptr_container_erase (Container &_container, Ptr const _element)
 Removes pointer _element from the ptr_container _container.
 
template<typename Container , typename Predicate >
bool fcppt::algorithm::ptr_container_erase_if (Container &_container, Predicate const &_predicate)
 Removes pointer from the ptr_container _container if _predicate matches.
 
template<typename Container >
bool fcppt::algorithm::remove (Container &_container, typename Container::const_reference _element)
 Tries to remove all elements from _container matching _element.
 
template<typename Container , typename Predicate >
bool fcppt::algorithm::remove_if (Container &_container, Predicate const &_predicate)
 Tries to remove all elements from _container matching _predicate.
 
template<typename Set >
Set const fcppt::algorithm::set_intersection (Set const &a, Set const &b)
 Calculates the intersection of two sets.
 
template<typename Container1 , typename Container2 , typename Equality >
bool fcppt::algorithm::shift_compare (Container1 const &a, Container2 b, Equality const &is_equal)
 Tests if a is equal b up to rotation.
 

Function Documentation

template<typename DestContainer , typename SourceContainer >
void fcppt::algorithm::append ( DestContainer &  dest,
SourceContainer const &  src 
)

Appends the sequence src to dest.

Template Parameters
DestContainerMust have an insert function taking three integers (like the std containers all do).
SourceContainerMust have a begin and end member function.

Example:

std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
std::vector<int> w;
w.push_back(4);
w.push_back(5);
v,
w);
// Now v contains: 1,2,3,4,5
template<typename TargetArray , typename SourceArray , typename Functor >
TargetArray const fcppt::algorithm::array_map ( SourceArray const &  source,
Functor const &  f 
)

Applies a functor to an array and returns a new array containing the results.

Template Parameters
TargetArrayMust be default-constructible

Example:

typedef
three_ints;
three_ints const a = {{ 1,2,3 }};
three_ints const b(
fcppt::algorithm::array_map<three_ints>(
a,
boost::phoenix::arg_names::arg1 * 3));
// b now contains: 3, 6, 9
template<typename In , typename T >
bool fcppt::algorithm::contains ( In const  beg,
In const  end,
T const &  value 
)

Checks if a given value is inside the range [beg,end].

Template Parameters
InA forward iterator
TA type compatible with the iterator's value type

This is equivalent to

std::find(beg, end, value) != end
template<typename Container , typename T >
bool fcppt::algorithm::contains ( Container const &  container,
T const &  value 
)

Checks if a given value is inside a range.

Template Parameters
InA container type having begin and end member functions
TThe container's value type

This is equivalent to

contains(container.begin(), container.end(), value)
template<typename In , typename Pred >
bool fcppt::algorithm::contains_if ( In const  beg,
In const  end,
Pred const &  pred 
)

Checks if a given value matches pred inside the range [beg end].

Template Parameters
InA forward iterator
PredA predicate (a unary function returning a bool)

This is equivalent to

std::find_if(beg, end, pred) != end
template<typename Container , typename Pred >
bool fcppt::algorithm::contains_if ( Container const &  container,
Pred const &  pred 
)

Checks if a given value is inside a range.

Template Parameters
InA container type having begin and end member functions
PredA predicate (a unary function returning a bool)

This is equivalent to

This is equivalent to

contains_if(container.begin(), container.end(), value)
template<typename InputIterator , typename OutputIterator , typename Predicate >
OutputIterator fcppt::algorithm::copy_if ( InputIterator  first,
InputIterator  last,
OutputIterator  result,
Predicate  pred 
)

Assigns the values of elements from a source range satisfying a predicate to a destination range,.

Template Parameters
InputIteratorAn input iterator type
OutputIteratorAn output iterator type
PredicateAn unary predicate
Parameters
firstAn input iterator addressing the position of the first element in the source range.
lastAn input iterator addressing the position that is one past the final element in the source range.
resultAn output iterator addressing the position of the first element in the destination range.
predUser-defined predicate function object that defines the condition to be satisfied if an element is to be counted. A predicate takes single argument and returns true or false.
Returns
An iterator pointing to one past the last position where an element was inserted into the destination range

The C++03's standard library is missing this function. C++11 includes it in the standard library, but for compatibility, you can use fcppt's version.

template<typename In , typename Out , typename Size >
Out fcppt::algorithm::copy_n ( In const  _beg,
Size const  _sz,
Out const  _out 
)

Copies sz elements from _beg to _out.

Equivalent to

std::copy(_beg, _beg + _sz, _out)
template<typename In , typename T >
In fcppt::algorithm::find_exn ( In const  begin,
In const  end,
T const &  t 
)

Like std::find but throws fcppt::algorithm::element_not_found.

Exceptions
fcppt::algorithm::element_not_foundIf the element was not found
template<typename In , typename Comp >
In fcppt::algorithm::find_if_exn ( In const  begin,
In const  end,
Comp const &  comp 
)

Like std::find_if but throws fcppt::algorithm::element_not_found.

Exceptions
fcppt::algorithm::element_not_foundIf the element was not found
template<typename Container >
Container const fcppt::algorithm::join ( Container  _left,
Container const &  _right 
)

Joins two containers.

Joins containers _left and _right, by inserting _right into _left.

Parameters
_leftThe left container
_rightThe right container, which will be inserted into the left container
Template Parameters
ContainerA container class that supports insert of iterator ranges
template<typename Range >
Range::value_type const fcppt::algorithm::join_strings ( Range const &  _range,
typename Range::value_type const &  _delim 
)

Joins a range of items delimited by delim.

Parameters
_rangeA forward-iterable range. The range's value_type must have an operator+=
_delimThe delimiting value

Example:

std::vector<std::string> strings;
strings.push_back("lol");
strings.push_back("rofl");
strings.push_back("wololololooo");
std::string const result =
strings,
",");
// Outputs "lol,rofl,wololololooo"
std::cout << result << "\n";
template<typename Range >
boost::range_size<Range>::type fcppt::algorithm::levenshtein ( Range const &  source,
Range const &  target 
)

Calculates the Levenshtein distance.

See http://en.wikipedia.org/wiki/Levenshtein_distance for an explanation of the algorithm.

Precondition
  • Range::size_type and Range::value_type exist
  • bool Range::empty() const exists
  • size_type Range::size() const exists
  • Range::operator[] exists
  • Range::value_type has to have an operator==
Note
The code is taken quite literally from: http://www.merriampark.com/ldcpp.htm
template<typename TargetContainer , typename SourceContainer , typename Functor >
TargetContainer const fcppt::algorithm::map ( SourceContainer const &  s,
Functor const &  f 
)

Transforms a container using a functor to another container.

Template Parameters
TargetContainerMust be default-constructible

This is equivalent to:

std::transform(source.begin(),source.end(),inserter,f)

with an appropriate inserter.

template<typename Container , typename Ptr >
boost::enable_if< boost::is_same< typename boost::add_pointer< typename boost::remove_const< typename boost::remove_pointer< Ptr >::type >::type >::type, typename Container::value_type >, bool>::type fcppt::algorithm::ptr_container_erase ( Container &  _container,
Ptr const  _element 
)

Removes pointer _element from the ptr_container _container.

Template Parameters
PtrMay be Container::value_type or a const version of it
Returns
true if the element was found/erased, false otherwise
template<typename Container , typename Predicate >
bool fcppt::algorithm::ptr_container_erase_if ( Container &  _container,
Predicate const &  _predicate 
)

Removes pointer from the ptr_container _container if _predicate matches.

Returns
true if the element was found/erased, false otherwise

This is different from the erase_if member functions because it returns if something has been erased.

template<typename Container >
bool fcppt::algorithm::remove ( Container &  _container,
typename Container::const_reference  _element 
)

Tries to remove all elements from _container matching _element.

Returns
true if something has been removed, false otherwise
template<typename Container , typename Predicate >
bool fcppt::algorithm::remove_if ( Container &  _container,
Predicate const &  _predicate 
)

Tries to remove all elements from _container matching _predicate.

Returns
true if something has been removed, false otherwise
template<typename Set >
Set const fcppt::algorithm::set_intersection ( Set const &  a,
Set const &  b 
)

Calculates the intersection of two sets.

Template Parameters
SetMust be default-constructible

This just calls std::set_intersection writes the result to a new container. </pre

template<typename Container1 , typename Container2 , typename Equality >
bool fcppt::algorithm::shift_compare ( Container1 const &  a,
Container2  b,
Equality const &  is_equal 
)

Tests if a is equal b up to rotation.

Precondition
Container1::value_type and Container2::value_type have to be equality-comparable.

Example:

std::vector<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
std::vector<int> b;
b.push_back(3);
b.push_back(1);
b.push_back(2);
std::vector<int> c;
c.push_back(1);
c.push_back(3);
c.push_back(2);
bool const first_result =
a,
b,
::std::equal_to<int>());
bool const second_result =
a,
c,
::std::equal_to<int>());
// Outputs true
std::cout << "first_result: " << first_result << "\n";
// Outputs false
std::cout << "second_result: " << second_result << "\n";