1 // Copyright 2002 The Trustees of Indiana University.
2
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 // Boost.MultiArray Library
8 // Authors: Ronald Garcia
9 // Jeremy Siek
10 // Andrew Lumsdaine
11 // See http://www.boost.org/libs/multi_array for documentation.
12
13 #ifndef FOR_EACH_HPP
14 #define FOR_EACH_HPP
15
16 //
17 // for_each.hpp - Writing an algorithm to transform each element of
18 // a multi_array
19 //
20
21 #include "boost/type.hpp"
22
23 template <typename Array, typename Element, typename Functor>
for_each(const boost::type<Element> & type_dispatch,Array A,Functor & xform)24 void for_each (const boost::type<Element>& type_dispatch,
25 Array A, Functor& xform) {
26 for_each(type_dispatch,A.begin(),A.end(),xform);
27 }
28
29 template <typename Element, typename Functor>
for_each(const boost::type<Element> &,Element & Val,Functor & xform)30 void for_each (const boost::type<Element>&,Element& Val, Functor& xform) {
31 Val = xform(Val);
32 }
33
34 template <typename Element, typename Iterator, typename Functor>
for_each(const boost::type<Element> & type_dispatch,Iterator begin,Iterator end,Functor & xform)35 void for_each (const boost::type<Element>& type_dispatch,
36 Iterator begin, Iterator end,
37 Functor& xform) {
38 while (begin != end) {
39 for_each(type_dispatch,*begin,xform);
40 ++begin;
41 }
42 }
43
44
45 template <typename Array, typename Functor>
for_each(Array & A,Functor xform)46 void for_each (Array& A, Functor xform) {
47 // Dispatch to the proper function
48 for_each(boost::type<typename Array::element>(),A.begin(),A.end(),xform);
49 }
50
51
52 #endif // FOR_EACH_HPP
53