|
Boost : |
From: Guy Peleg (guy.peleg_at_[hidden])
Date: 2006-11-09 09:56:55
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
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk