Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of boost. Click here for the latest Boost documentation.

[Home]fold_backward

Synopsis

template<
      typename Sequence
    , typename State
    , typename BackwardOp
    , typename ForwardOp = _1
    >
struct fold_backward
{
    typedef unspecified type;
};

Description

Returns the result of the successive application of binary BackwardOp to the result of the previous BackwardOp invocation (State if it's the first call) and every element in the range [begin<Sequence>::type,end<Sequence>::type) in the reverse order. If ForwardOp is provided, then it's applied on forward traversal to form the result which is passed to the first BackwardOp call.

Definition

#include "boost/mpl/fold_backward.hpp"

Parameters

 Parameter  Requirement  Description  Default value  
SequenceA model of SequenceA sequence to iterate.
StateA typeThe initial state for the first BackwardOp/ForwardOp application.
BackwardOpA model of [Lambda Function]The operation to be executed on backward traversal.
ForwardOpA model of [Lambda Function]The operation to be executed on forward traversal.arg<1>

Expression semantics

 Expression  Expression type  Precondition  Semantics  Postcondition 
typedef fold_backward< Sequence,T,BackwardOp >::type t;A typeEquivalent to typedef lambda<BackwardOp>::type bk_op; typedef begin<Sequence>::type i1; typedef i1::next i2; ...; typedef in::next last; typedef apply<bk_op,T,in::type>::type tn; typedef apply<bk_op,tn,in-1::type>::type tn-1; ...; typedef apply<bk_op,t2,i1::type>::type t1; typedef t1 t, where n == size<Sequence>::type::value and last is identical to end<Sequence>::type; Equivalent to typedef T t; if the sequence is empty.
typedef fold_backward< Sequence,T,BackwardOp,ForwardOp >::type t;A typeEquivalent to typedef fold_backward<Sequence, fold<Sequence,State,ForwardOp>::type, BackwardOp>::type t;.

Complexity

Linear. Exactly size<Sequence>::type::value applications of BackwardOp and ForwardOp.

Example

Removes negative elements from a sequence [1].

typedef list_c<int,5,-1,0,-7,-2,0,-5,4> numbers;
typedef list_c<int,-1,-7,-2,-5> negatives;
typedef fold_backward<
      numbers
    , list_c<int>
    , if_< less< _2,int_<0> >, push_front<_1,_2,>, _1 >
    >::type result;

BOOST_STATIC_ASSERT(equal< negatives,result >::type::value);

Notes

[1] See remove_if for a more compact way to do this.

See also

Algorithms, fold, iter_fold_backward, iter_fold, copy, copy_backward


Table of Contents
Last edited March 12, 2003 6:30 am