
Hi, Is there any interest in a library which unrolls loops when traversing N times through containers? (where N is known in compile time) For example, 'fixed_size_for_each': // IT - Iterator // N - The number of elements template <typename IT, int N> struct fixed_size_for_each_impl; // ================ // Specialization 1: template <typename IT, int N> struct fixed_size_for_each_impl { template <class F> void operator()(IT it, F& op) { op(*it); fixed_size_for_each_impl<IT, N-1> tail; tail(++it, op); } }; // ================ // Specialization 2 ('stop condition'): template <typename IT> struct fixed_size_for_each_impl<IT, 1> { template <class F> void operator()(IT it, F& op) { op(*it); } }; // ================ // A sort of 'for_each' algorithm // that traverses N times through some // container while unrolling the loop template<int N, typename IT, typename F> F fixed_size_for_each(IT it, F op) { fixed_size_for_each_impl<IT, N>()(it, op); return op; } // ================ // Overloaded method especially for boost::array // because we know the number of elements (static_size) template<typename F, typename T, int N> F fixed_size_for_each(boost::array<T, N>& a, F op) { typedef boost::array<T, N> boost_array; return fixed_size_for_each<boost_array::static_size>(a.begin(), op); } Possible use: boost::array<Shape *, 4> a; .... fixed_size_for_each(a, mem_fun(&Shape::draw)); Other algorithms (e.g. find) can be done too, if there is a need I'll be happy to submit a suggestion. Thanks, Guy This message and the information contained herein is proprietary and confidential and subject to the Amdocs policy statement, you may review at http://www.amdocs.com/email_disclaimer.asp

On 09/11/06, Guy Peleg <guy.peleg@amdocs.com> wrote:
Is there any interest in a library which unrolls loops when traversing N times through containers? (where N is known in compile time)
// Specialization 2 ('stop condition'):
template <typename IT>
struct fixed_size_for_each_impl<IT, 1>
I would modify this to a 0-based recursion that does nothing. That way, for_each also works with defines or compile-time constants that could equal 0, such as temporarily removing some functionality could normally do.
participants (2)
-
Guy Peleg
-
Peter Bindels