pstade

PrevUpHomeNext

Range Algorithms

STL Algorithms
STL Numerics
at
back
front
begin/end
copied
distance
equals
exists
forall
value_at
value_back
value_front

Oven provides range-based algorithms taking a range instead of two iterators.

Description

algorithm.hpp is provided as range-based <algorithm>.

Header
  • <pstade/oven/algorithm.hpp>
Model of
Valid expressions

Valid expression

Semantics

f1(_rng, x1,...,xN)

std::f1(_begin(_rng), _end(_rng), x1,...,xN)

f2(_rng1, _rng2, x1,...,xN)

std::f2(_begin(_rng1), _end(_rng1), _begin(_rng2), _end(_rng2), x1,...,xN)

f3(_rng, _fun, x1,...,xN)

std::f3(_begin(_rng), _fun(_rng), _end(_rng), x1,...,xN)

Preconditions
  • f1 and f2 is one of the function names defined in <algorithm> except for...
    • fill_n, generate_n, rotate, rotate_copy, partial_sort, nth_element and inplace_merge.
  • f3 is one of the following names...
    • rotate, rotate_copy, partial_sort, nth_element and inplace_merge.
  • std::f1(_begin(_rng), _end(_rng), x1,...,xN) is a valid exression.
  • std::f2(_begin(_rng1), _end(_rng1), _begin(_rng2), _end(_rng2), x1,...,xN) is a valid expression.
  • std::f3(_begin(_rng), m, _end(_rng), x1,...,xN) is a valid expression.
  • _fun(_rng) is a valid expression.
  • _typeof(_fun(_rng)) is _iter_of<_typeof(_rng)>::type.
[Note] Note

fill_n and generate_n are not considered as range algorithm, so they are excluded.

Example
std::string str;

// iterator-based
str = "gfedcba";
std::sort(str.begin(), str.end());
BOOST_CHECK( str == "abcdefg" );

// Oven range-based
str = "gfedcba";
sort(str);
BOOST_CHECK( str == "abcdefg" );
Description

numeric.hpp is provided as range-based <numeric>.

Header
  • <pstade/oven/numeric.hpp>
Model of
Valid expressions

Valid expression

Semantics

f(_rng, x1,...,xN)

std::f(_begin(_rng), _end(_rng), x1,...,xN)

Preconditions
  • f is one of the function names defined in <numeric>.
  • std::f(_begin(_rng), _end(_rng), x1,...,xN) is a valid expression.
[Important] Important

Unfortunately, Oven STL Algorithms and Numerics are not traversal-aware. A Random Access Range whose boost::range_reference type is not a reference type is regarded as an Input Iterator range.

Description

at returns the N-th referent from the beginning of the range.

Header
  • <pstade/oven/at.hpp>
Model of
Valid expressions

Valid expression

Semantics

at(_rndrng, n)

*(_begin(_rndrng) + n)

Preconditions
  • _rndrng is Readable or Lvalue.
  • If boost::range_reference<_typeof(_rndrng)>::type is a reference type, _rndrng is a LvalueRange.
  • _typeof(n) is convertible to boost::range_difference<_typeof(_rndrng)>::type.
  • 0 <= n && n < distance(_rndrng)
[Note] Note

you must use value_at if the second precondition can't be met.

Example
std::string str("f12344513215b");
BOOST_CHECK( at(str, 0) == 'f' );
BOOST_CHECK( (str|at(4)) == '4' );
See also
Description

back returns the last referent in the range.

Header
  • <pstade/oven/back.hpp>
Model of
Valid expressions

Valid expression

Semantics

back(_rng)

*boost::prior(_end(_bidrng))

Preconditions
  • _bidrng is Readable or Lvalue.
  • If boost::range_reference<_typeof(_bidrng)>::type is a reference type, _bidrng is a LvalueRange.
  • !boost::empty(_bidrng)
See also
Description

front returns the first referent in the range.

Header
  • <pstade/oven/front.hpp>
Model of
Valid expressions

Valid expression

Semantics

front(_rng)

*_begin(_rng)

Preconditions
  • _rng is Readable or Lvalue.
  • If boost::range_reference<_typeof(_rng)>::type is a reference type, _rng is a LvalueRange.
  • !boost::empty(_rng)
See also
Description

begin and end is Ambi Static Function Object which represents boost::begin and boost::end respectively.

Header
  • <pstade/oven/begin_end.hpp>
Model of
Valid expressions

Valid expression

Semantics

begin(_rng)

_begin(_rng)

end(_rng)

_end(_rng)

Example
std::string src("abcDefg");   
copy(src|reversed|upper_chars, src|reversed|begin);
BOOST_CHECK( equals(src, std::string("ABCDEFG")) );
Description

copied automagically calls the range-constructor of the target range without specifying the type.

Model of
Header
  • <pstade/oven/copied.hpp>
Notation
  • V1 is _value_of<_typeof(_rng1)>::type.
  • to_v1 is regular(boost::lambda::ll_static_cast<V1>(boost::lambda::_1)).
Valid expressions

Valid expression

Semantics

Rng1 _rng1 = _rng|copied;

Rng1 _rng1(_begin(r), _end(r)), where r = _rng|transformed(to_v1).

Rng2 _rng2(_rng0|copied);

Rng2 _rng2 = _rng0|copied;

Preconditions
  • _rng1 is a Sequence.
  • There is a type X_ such that boost::is_convertible<X_, Rng1>::value == false.
  • _value_of<_typeof(_rng)>::type is convertible to V1.
  • Rng2 is an aggregate type such that Rng2 _rng2 = _rng0|copied; is a valid expression.
  • copied isn't placed in a function default argument list.
[Note] Note

The first valid expression implies you can place copied everywhere it is used in copy-initialization, e.g. return statement. The last precondition comes from a GCC bug. Notice that Rng1 _rng1(_rng2|copied); is not always a valid expression.

Example
std::string rng("abcdefg");
std::vector<char> vec = rng|copied;
BOOST_CHECK( equals(vec, rng) );
See also
Description

distance is a range-based std::distance but traversal-aware.

Header
  • <pstade/oven/distance.hpp>
Model of
Valid expressions

Valid expression

Semantics

distance(_rng)

std::distance(_begin(_rng), _end(_rng))

Preconditions
  • std::distance(_begin(_rng), _end(_rng)) is a valid expression.
Description

equals is a range-based std::equal but sensitive to the distances.

Header
  • <pstade/oven/equals.hpp>
Model of
Notation
  • eq is (boost::lambda::_1 == boost::lambda::_2).
Valid expressions

Valid expression

Semantics

equals(_rng1, _rng2, _prd)

distance(_rng1) == distance(_rng2) ? std::equal(_begin(_rng1), _end(_rng1), _begin(_rng2), _prd) : false

equals(_rng1, _rng2)

equals(_rng1, _rng2, eq)

Preconditions
Description

exists returns true if a predicate is true for a referent in the range.

Header
  • <pstade/oven/exists.hpp>
Model of
Valid expressions

Valid expression

Semantics

exists(_rng, _prd)

std::find_if(_begin(_rng), _end(_rng), pred) != _end(_rng)

Preconditions
See also
Description

forall returns true if a predicate is true for all the referents in the range.

Header
  • <pstade/oven/forall.hpp>
Model of
Notation
  • not_ is an imaginary function to nagate a predicate.
Valid expressions

Valid expression

Semantics

forall(_rng, _prd)

std::find_if(_begin(_rng), _end(_rng), not_(pred)) == _end(_rng)

Preconditions
See also
Description

value_at returns a copy of N-th referent from the beginning of the range.

Header
  • <pstade/oven/at.hpp>
Model of
Notation
  • V is _value_of<_typeof(_rndrng)>::type.
Valid expressions

Valid expression

Semantics

value_at(_rndrng, n)

V(*(_begin(_rndrng) + n))

Preconditions
  • _rndrng is Readable.
  • _typeof(n) is convertible to boost::range_difference<_typeof(_rndrng)>::type.
  • 0 <= n && n < distance(_rndrng)
See also
Description

value_back returns a copy of the last referent in the range.

Header
  • <pstade/oven/back.hpp>
Model of
Notation
  • V is _value_of<_typeof(_bidrng)>::type.
Valid expressions

Valid expression

Semantics

value_back(_bidrng)

V(*boost::prior(_bidrng))

Preconditions
  • _bidrng is Readable.
  • !boost::empty(_bidrng)
See also
Description

value_front returns a copy of the first referent in the range.

Header
  • <pstade/oven/front.hpp>
Model of
Notation
  • V is _value_of<_typeof(_rng)>::type.
Valid expressions

Valid expression

Semantics

value_front(_rng)

V(*_begin(_rng))

Preconditions
See also
Copyright 2005 -2007 Shunsuke Sogame

PrevUpHomeNext