Mirror reflection library - Lagoon run-time layer 0.5.13

lagoon/range/fold.hpp

Go to the documentation of this file.
00001 
00011 #ifndef LAGOON_RANGE_ACCUMULATE_1011291729_HPP
00012 #define LAGOON_RANGE_ACCUMULATE_1011291729_HPP
00013 
00014 #include <lagoon/lagoon_fwd.hpp>
00015 
00016 LAGOON_NAMESPACE_BEGIN
00017 
00019 
00029 template <typename State, class Range, typename BinaryOp>
00030 State fold(Range range, State state, BinaryOp op)
00031 {
00032     while(!range.empty())
00033     {
00034         state = op(state, range.front());
00035         range.step_front();
00036     }
00037     return state;
00038 }
00039 
00041 
00051 template <typename State, class Range, typename BinaryOp>
00052 State& fold_ref(Range range, State& state_ref, BinaryOp op)
00053 {
00054     while(!range.empty())
00055     {
00056         op(state_ref, range.front());
00057         range.step_front();
00058     }
00059     return state_ref;
00060 }
00061 
00062 
00064 
00080 template <
00081     typename State,
00082     class Range,
00083     typename InitialOp,
00084     typename DefaultOp,
00085     typename FinalOp
00086 > State& fold_ref(
00087     Range range,
00088     State& state_ref,
00089     InitialOp initial,
00090     DefaultOp op,
00091     FinalOp final
00092 )
00093 {
00094     if(!range.empty())
00095     {
00096         initial(state_ref, range.front());
00097         range.step_front();
00098         bool done = range.empty();
00099         while(!done)
00100         {
00101             auto front = range.front();
00102             range.step_front();
00103             done = range.empty();
00104             if(done)
00105             {
00106                 final(state_ref, front);
00107                 break;
00108             }
00109             else op(state_ref, front);
00110         }
00111     }
00112     return state_ref;
00113 }
00114 
00115 LAGOON_NAMESPACE_END
00116 
00117 #endif //include guard
00118 

Copyright © 2006-2011 Matus Chochlik, University of Zilina, Zilina, Slovakia.
<matus.chochlik -at- fri.uniza.sk>
<chochlik -at -gmail.com>
Documentation generated on Fri Dec 16 2011 by Doxygen (version 1.7.3).
Important note: Although the 'boostified' version of Mirror uses the Boost C++ libraries Coding Guidelines and is implemented inside of the boost namespace, it IS NOT an officially reviewed and accepted Boost library. Mirror is being developed with the intention to be submitted for review for inclusion to the Boost C++ libraries.