Index: boost/multi_array/multi_array_ref.hpp =================================================================== --- boost/multi_array/multi_array_ref.hpp (revision 58069) +++ boost/multi_array/multi_array_ref.hpp (working copy) @@ -30,6 +30,7 @@ #include "boost/concept_check.hpp" #include "boost/functional.hpp" #include "boost/limits.hpp" +#include "boost/swap.hpp" #include #include #include @@ -262,6 +263,7 @@ } + template bool operator==(const const_multi_array_ref& rhs) @@ -348,7 +350,6 @@ } } - TPtr base_; storage_order_type storage_; size_list extent_list_; @@ -358,6 +359,19 @@ index directional_offset_; size_type num_elements_; +protected: + // const_multi_array_ref cannot be swapped, but we need swap for subclasses + void swap(const_multi_array_ref& other) { + boost::swap(base_, other.base_); + boost::swap(storage_, other.storage_); + boost::swap(extent_list_, other.extent_list_); + boost::swap(stride_list_, other.stride_list_); + boost::swap(index_base_list_, other.index_base_list_); + boost::swap(origin_offset_, other.origin_offset_); + boost::swap(directional_offset_, other.directional_offset_); + boost::swap(num_elements_, other.num_elements_); + } + private: // const_multi_array_ref cannot be assigned to (no deep copies!) const_multi_array_ref& operator=(const const_multi_array_ref& other); @@ -618,6 +632,10 @@ return super_type::rend(); } + void swap(multi_array_ref& other) { + super_type::swap(other); + } + protected: // This is only supplied to support multi_array's default constructor explicit multi_array_ref(T* base, @@ -628,6 +646,13 @@ }; +// global swap() +template +inline void swap(multi_array_ref& x, + multi_array_ref& y) { + x.swap(y); +} + } // namespace boost #endif // BOOST_MULTI_ARRAY_REF_RG071801_HPP Index: boost/multi_array.hpp =================================================================== --- boost/multi_array.hpp (revision 58069) +++ boost/multi_array.hpp (working copy) @@ -27,6 +27,7 @@ #include "boost/multi_array/algorithm.hpp" #include "boost/array.hpp" #include "boost/mpl/if.hpp" +#include "boost/swap.hpp" #include "boost/type_traits.hpp" #include #include @@ -446,23 +447,18 @@ // Set the right portion of the new array view_new = view_old; - using std::swap; // Swap the internals of these arrays. - swap(this->super_type::base_,new_array.super_type::base_); - swap(this->storage_,new_array.storage_); - swap(this->extent_list_,new_array.extent_list_); - swap(this->stride_list_,new_array.stride_list_); - swap(this->index_base_list_,new_array.index_base_list_); - swap(this->origin_offset_,new_array.origin_offset_); - swap(this->directional_offset_,new_array.directional_offset_); - swap(this->num_elements_,new_array.num_elements_); - swap(this->allocator_,new_array.allocator_); - swap(this->base_,new_array.base_); - swap(this->allocated_elements_,new_array.allocated_elements_); + swap(new_array); return *this; } + void swap(multi_array& other) { + super_type::swap(other); + boost::swap(allocator_, other.allocator_); + boost::swap(base_, other.base_); + boost::swap(allocated_elements_, other.allocated_elements_); + } ~multi_array() { deallocate_space(); @@ -494,6 +490,12 @@ enum {initial_base_ = 0}; }; +// global swap() +template +inline void swap(multi_array& x, multi_array& y) { + x.swap(y); +} + } // namespace boost #endif // BOOST_MULTI_ARRAY_RG071801_HPP Index: libs/multi_array/test/regression.cfg =================================================================== --- libs/multi_array/test/regression.cfg (revision 58069) +++ libs/multi_array/test/regression.cfg (working copy) @@ -12,6 +12,7 @@ run libs/multi_array/test/range1.cpp run libs/multi_array/test/idxgen1.cpp run libs/multi_array/test/stl_interaction.cpp +run libs/multi_array/test/swap.cpp compile libs/multi_array/test/concept_checks.cpp compile-fail libs/multi_array/test/fail_cbracket.cpp Index: libs/multi_array/test/swap.cpp =================================================================== --- libs/multi_array/test/swap.cpp (revision 0) +++ libs/multi_array/test/swap.cpp (revision 0) @@ -0,0 +1,126 @@ +// Copyright 2002 The Trustees of Indiana University. + +// Use, modification and distribution is subject to the Boost Software +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Boost.MultiArray Library +// Authors: Ronald Garcia +// Jeremy Siek +// Andrew Lumsdaine +// See http://www.boost.org/libs/multi_array for documentation. + +// +// swap.cpp - Test swap member for multi_array, multi_array_ref +// + +#include "boost/test/minimal.hpp" + +#include "boost/multi_array.hpp" +#include +#include + +void check_shape(const double&, std::size_t*, int*, unsigned int) +{} + +template +void check_shape(const Array& A, + std::size_t* sizes, + int* strides, + unsigned int num_elements) +{ + BOOST_CHECK(A.num_elements() == num_elements); + BOOST_CHECK(A.size() == *sizes); + BOOST_CHECK(std::equal(sizes, sizes + A.num_dimensions(), A.shape())); + BOOST_CHECK(std::equal(strides, strides + A.num_dimensions(), A.strides())); + check_shape(A[0], ++sizes, ++strides, num_elements / A.size()); +} + + +bool equal(const double& a, const double& b) +{ + return a == b; +} + +template +bool equal(const ArrayA& A, const ArrayB& B) +{ + typename ArrayA::const_iterator ia; + typename ArrayB::const_iterator ib = B.begin(); + for (ia = A.begin(); ia != A.end(); ++ia, ++ib) + if (!equal(*ia, *ib)) + return false; + return true; +} + + +int +test_main(int, char*[]) +{ + typedef boost::multi_array::size_type size_type; + boost::array sizes[2] = { {{4, 5, 6}}, {{1, 2, 3}} }; + int strides[2][3] = { { 30, 6, 1 }, { 6, 3, 1 }}; + size_type num_elements[2] = { 4*5*6, 1*2*3 }; + + // multi_array_ref::swap, default storage order and allocator + { + double aptr[] = { 777 }; + boost::multi_array_ref A(aptr,sizes[0]); + check_shape(A, &sizes[0][0], strides[0], num_elements[0]); + + double bptr[] = { 888 }; + boost::multi_array_ref B(bptr,sizes[1]); + check_shape(B, &sizes[1][0], strides[1], num_elements[1]); + + A.swap(B); + + check_shape(A, &sizes[1][0], strides[1], num_elements[1]); + check_shape(B, &sizes[0][0], strides[0], num_elements[0]); + BOOST_CHECK(A.data() == bptr); + BOOST_CHECK(B.data() == aptr); + } + + // multi_array_ref::swap, self swap + { + double aptr[] = { 777 }; + boost::multi_array_ref A(aptr,sizes[0]); + check_shape(A, &sizes[0][0], strides[0], num_elements[0]); + + A.swap(A); + + check_shape(A, &sizes[0][0], strides[0], num_elements[0]); + BOOST_CHECK(A.data() == aptr); + } + + // multi_array::swap, default storage order and allocator + { + boost::multi_array A(sizes[0]); + check_shape(A, &sizes[0][0], strides[0], num_elements[0]); + A[0][0][0] = 777.0; + + boost::multi_array B(sizes[1]); + check_shape(B, &sizes[1][0], strides[1], num_elements[1]); + B[0][0][0] = 888.0; + + A.swap(B); + + check_shape(A, &sizes[1][0], strides[1], num_elements[1]); + check_shape(B, &sizes[0][0], strides[0], num_elements[0]); + BOOST_CHECK(A[0][0][0] == 888.0); + BOOST_CHECK(B[0][0][0] == 777.0); + } + + // multi_array::swap, self swap + { + boost::multi_array A(sizes[0]); + check_shape(A, &sizes[0][0], strides[0], num_elements[0]); + A[0][0][0] = 777.0; + + A.swap(A); + + check_shape(A, &sizes[0][0], strides[0], num_elements[0]); + BOOST_CHECK(A[0][0][0] == 777.0); + } + + return boost::exit_success; +} Property changes on: libs/multi_array/test/swap.cpp ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Id Added: svn:eol-style + native Index: libs/multi_array/test/Jamfile.v2 =================================================================== --- libs/multi_array/test/Jamfile.v2 (revision 58069) +++ libs/multi_array/test/Jamfile.v2 (working copy) @@ -45,5 +45,6 @@ [ run stl_interaction.cpp ../../test/build//boost_test_exec_monitor ] [ run resize.cpp ../../test/build//boost_test_exec_monitor ] [ run assert.cpp ../../test/build//boost_test_exec_monitor ] + [ run swap.cpp ../../test/build//boost_test_exec_monitor ] [ compile concept_checks.cpp ] ; Index: libs/multi_array/doc/xml/multi_array_ref.xml =================================================================== --- libs/multi_array/doc/xml/multi_array_ref.xml (revision 58069) +++ libs/multi_array/doc/xml/multi_array_ref.xml (working copy) @@ -124,6 +124,7 @@ void reshape(const SizeList& sizes) template void reindex(const BaseList& values); void reindex(index value); + void swap(multi_array_ref &other); }; ]]> Index: libs/multi_array/doc/xml/multi_array.xml =================================================================== --- libs/multi_array/doc/xml/multi_array.xml (revision 58069) +++ libs/multi_array/doc/xml/multi_array.xml (working copy) @@ -140,6 +140,7 @@ template multi_array& resize(const ExtentList& extents); multi_array& resize(extents_tuple& extents); + void swap(multi_array& other); }; ]]> Index: libs/multi_array/doc/reference.html =================================================================== --- libs/multi_array/doc/reference.html (revision 58069) +++ libs/multi_array/doc/reference.html (working copy) @@ -697,6 +697,7 @@ template <typename ExtentList> multi_array& resize(const ExtentList& extents); multi_array& resize(extents_tuple& extents); + void swap(multi_array& other); };

Constructors. 

template <typename ExtentList>
@@ -908,6 +909,7 @@
     void			reshape(const SizeList& sizes)
   template <typename BaseList>	void reindex(const BaseList& values);
   void				reindex(index value);
+  void              swap(multi_array_ref& other);
 };
 
 

Constructors. 

template <typename ExtentList>