for_each.hpp 1.4 KB

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