copy_array.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 COPY_ARRAY_RG092101_HPP
  11. #define COPY_ARRAY_RG092101_HPP
  12. //
  13. // copy_array.hpp - generic code for copying the contents of one
  14. // Basic_MultiArray to another. We assume that they are of the same
  15. // shape
  16. //
  17. #include "boost/type.hpp"
  18. #include "boost/assert.hpp"
  19. namespace boost {
  20. namespace detail {
  21. namespace multi_array {
  22. template <typename Element>
  23. class copy_dispatch {
  24. public:
  25. template <typename SourceIterator, typename DestIterator>
  26. static void copy_array (SourceIterator first, SourceIterator last,
  27. DestIterator result) {
  28. while (first != last) {
  29. copy_array(*first++,*result++);
  30. }
  31. }
  32. private:
  33. // Array2 has to be passed by VALUE here because subarray
  34. // pseudo-references are temporaries created by iterator::operator*()
  35. template <typename Array1, typename Array2>
  36. static void copy_array (const Array1& source, Array2 dest) {
  37. copy_array(source.begin(),source.end(),dest.begin());
  38. }
  39. static void copy_array (const Element& source, Element& dest) {
  40. dest = source;
  41. }
  42. };
  43. template <typename Array1, typename Array2>
  44. void copy_array (Array1& source, Array2& dest) {
  45. BOOST_ASSERT(std::equal(source.shape(),source.shape()+source.num_dimensions(),
  46. dest.shape()));
  47. // Dispatch to the proper function
  48. typedef typename Array1::element element_type;
  49. copy_dispatch<element_type>::
  50. copy_array(source.begin(),source.end(),dest.begin());
  51. }
  52. } // namespace multi_array
  53. } // namespace detail
  54. } // namespace boost
  55. #endif // COPY_ARRAY_RG092101_HPP