Lumiera  0.pre.03
»edit your freedom«
util-foreach.hpp File Reference

Go to the source code of this file.

Description

Perform operations "for each element" of a collection.

This header defines various flavours of these active iteration functions, which all take a functor and invoke it in some way over the collection's elements.

  • basic constructs
    • for_each
    • and_all (all quantisation)
    • has_any (existence quantisation)
  • kinds of collection
    • STL collections and anything which has an iterator, begin() and end()
    • "Lumiera Forward Iterator" instance to yield all the elements of the collection
  • support for on-the-fly binding up to 4 arguments of the functor
Warning
in the standard case (STL container) the collection to operate on is taken by const& – but the const is stripped silently.

Thus, within the iteration, the function passed in can modify the original collection. If you pass in a ref to a temporary, the compiler won't complain. Moreover, several kinds of wrappers are also stripped silently, including reference_wrapper, shared_ptr and lumiera::P. The rationale for this non-standard behaviour is to allow convenient writing of in-line iteration, where even the collection to iterate is yielded by function call.

See also
util-foreach-test.cpp

Definition in file util-foreach.hpp.

#include "lib/util.hpp"
#include "lib/meta/trait.hpp"
#include <functional>
#include <algorithm>

Functions

template<typename IT , typename FUN >
bool and_all (IT i, IT end, FUN predicate)
 All quantification: check if all elements of a collection satisfy the given predicate. More...
 
template<typename Container , typename FUN >
enable_if< can_STL_ForEach< Container >, bool > and_all (Container const &coll, FUN predicate)
 
template<typename IT , typename FUN >
enable_if< can_IterForEach< IT >, bool > and_all (IT const &ii, FUN predicate)
 
template<typename CON , typename FUN , typename P1 , typename... ARGS>
bool and_all (CON const &elements, FUN function, P1 &&bind1, ARGS &&...args)
 Accept binding for arbitrary function arguments. More...
 
template<typename Container , typename FUN >
disable_if< can_IterForEach< Container >, FUN > for_each (Container const &coll, FUN doIt)
 operate on all elements of a STL container. More...
 
template<typename IT , typename FUN >
enable_if< can_IterForEach< IT >, FUN > for_each (IT const &ii, FUN doIt)
 operate on a Lumiera Forward Iterator until exhaustion. More...
 
template<typename CON , typename FUN , typename P1 , typename... ARGS>
void for_each (CON const &elements, FUN function, P1 &&bind1, ARGS &&...args)
 Accept binding for arbitrary function arguments. More...
 
template<typename IT , typename FUN >
bool has_any (IT i, IT end, FUN predicate)
 Existential quantification: check if any element of a collection satisfies the given predicate. More...
 
template<typename Container , typename FUN >
enable_if< can_STL_ForEach< Container >, bool > has_any (Container const &coll, FUN predicate)
 
template<typename IT , typename FUN >
enable_if< can_IterForEach< IT >, bool > has_any (IT const &ii, FUN predicate)
 
template<typename CON , typename FUN , typename P1 , typename... ARGS>
bool has_any (CON const &elements, FUN function, P1 &&bind1, ARGS &&...args)
 Accept binding for arbitrary function arguments. More...
 

Function Documentation

◆ and_all() [1/2]

bool util::and_all ( IT  i,
IT  end,
FUN  predicate 
)
inline

All quantification: check if all elements of a collection satisfy the given predicate.

Actually a short-circuit evaluation is performed.

Definition at line 69 of file util-foreach.hpp.

References util::and_all().

Referenced by util::and_all().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ has_any() [1/2]

bool util::has_any ( IT  i,
IT  end,
FUN  predicate 
)
inline

Existential quantification: check if any element of a collection satisfies the given predicate.

Actually, a short-circuit evaluation is performed.

Definition at line 85 of file util-foreach.hpp.

References util::has_any().

Referenced by util::has_any().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ for_each() [1/3]

disable_if< can_IterForEach<Container>, FUN > util::for_each ( Container const &  coll,
FUN  doIt 
)
inline

operate on all elements of a STL container.

Note
the container is taken by const& and the const is stripped before iteration.
Todo:
reconsider if using rvalue references covers the "inline iteration" use case sufficiently, so that we can get rid of the unwrapping and thus get back to strict const correctness.
Note
this case is the default and kicks in unless we detect a Lumiera iterator. The handling is different for and_all

Definition at line 113 of file util-foreach.hpp.

References util::for_each().

Referenced by util::for_each().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ for_each() [2/3]

enable_if< can_IterForEach<IT>, FUN > util::for_each ( IT const &  ii,
FUN  doIt 
)
inline

operate on a Lumiera Forward Iterator until exhaustion.

Definition at line 129 of file util-foreach.hpp.

References util::for_each().

+ Here is the call graph for this function:

◆ for_each() [3/3]

void util::for_each ( CON const &  elements,
FUN  function,
P1 &&  bind1,
ARGS &&...  args 
)
inline

Accept binding for arbitrary function arguments.

Note
obviously one of those arguments must be a placeholder

Definition at line 199 of file util-foreach.hpp.

References util::for_each().

+ Here is the call graph for this function:

◆ and_all() [2/2]

bool util::and_all ( CON const &  elements,
FUN  function,
P1 &&  bind1,
ARGS &&...  args 
)
inline

Accept binding for arbitrary function arguments.

Note
obviously one of those arguments must be a placeholder

Definition at line 212 of file util-foreach.hpp.

References util::and_all().

+ Here is the call graph for this function:

◆ has_any() [2/2]

bool util::has_any ( CON const &  elements,
FUN  function,
P1 &&  bind1,
ARGS &&...  args 
)
inline

Accept binding for arbitrary function arguments.

Note
obviously one of those arguments must be a placeholder

Definition at line 225 of file util-foreach.hpp.

References util::has_any().

+ Here is the call graph for this function: