/*******************************************************************************
*
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
*
* Copyright (C) 2012 by O. Parcollet
*
* TRIQS is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* TRIQS. If not, see .
*
******************************************************************************/
#ifndef TRIQS_ARRAYS_EXPRESSION_FOLD_H
#define TRIQS_ARRAYS_EXPRESSION_FOLD_H
#include
#include
#include
#include "../array.hpp"
namespace triqs { namespace arrays {
template
class fold_worker {
F f;
typedef typename boost::remove_const::type>::type result_type;
template
struct fold_func_adaptor {
A const & b;
F f; result_type r;
fold_func_adaptor(F f_, A const & a_,typename A::value_type init ):b(a_), f(f_) { r= init;}
template void operator()(Args const & ... args) { r = f(r,b(args...)); }
};
public:
fold_worker ( F const & f_):f(f_) {}
template
result_type operator() (A const & a, typename A::value_type init = typename A::value_type() ) const {
fold_func_adaptor func(f,a,init);
//fold_func_adaptor func(f,init);
foreach(a,std::ref(func));
//foreach(a,boost::ref(func));
return func.r;
}
};
template fold_worker fold (F const & f) { return fold_worker(f);}
template
fold_worker< boost::function >
fold (R (*f)(A1,A1)) { return fold( boost::function(f)); }
}}//namespace triqs::arrays
#endif