diff options
Diffstat (limited to '')
-rw-r--r-- | src/boost/libs/multi_array/example/basic1.cpp | 25 | ||||
-rw-r--r-- | src/boost/libs/multi_array/example/basic2.cpp | 27 | ||||
-rw-r--r-- | src/boost/libs/multi_array/example/for_each.hpp | 52 | ||||
-rw-r--r-- | src/boost/libs/multi_array/example/foreach_test.cpp | 54 | ||||
-rw-r--r-- | src/boost/libs/multi_array/example/foreach_test2.cpp | 52 | ||||
-rw-r--r-- | src/boost/libs/multi_array/example/op_paren.cpp | 29 | ||||
-rw-r--r-- | src/boost/libs/multi_array/example/print_array.cpp | 50 | ||||
-rw-r--r-- | src/boost/libs/multi_array/example/resize_from_other.cpp | 57 | ||||
-rw-r--r-- | src/boost/libs/multi_array/example/subview.cpp | 53 | ||||
-rw-r--r-- | src/boost/libs/multi_array/example/subview2.cpp | 54 |
10 files changed, 453 insertions, 0 deletions
diff --git a/src/boost/libs/multi_array/example/basic1.cpp b/src/boost/libs/multi_array/example/basic1.cpp new file mode 100644 index 00000000..9c9f22fc --- /dev/null +++ b/src/boost/libs/multi_array/example/basic1.cpp @@ -0,0 +1,25 @@ +// 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. + +#include <cassert> +#include "boost/multi_array.hpp" +#include "boost/cstdlib.hpp" + +int main () { + // Create a 3D array that is 3 x 4 x 2 + typedef boost::multi_array<double, 3> array; + array A(boost::extents[3][4][2]); + // Assign a value to an element in the array + A[0][0][0] = 3.14; + assert(A[0][0][0] == 3.14); + return boost::exit_success; +} diff --git a/src/boost/libs/multi_array/example/basic2.cpp b/src/boost/libs/multi_array/example/basic2.cpp new file mode 100644 index 00000000..5ab48487 --- /dev/null +++ b/src/boost/libs/multi_array/example/basic2.cpp @@ -0,0 +1,27 @@ +// 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. + + +#include <cassert> +#include "boost/multi_array.hpp" +#include "boost/array.hpp" +#include "boost/cstdlib.hpp" + +int main () { + // Create a 3D array that is 3 x 4 x 2 + boost::array<int, 3> shape = {{ 3, 4, 2 }}; + boost::multi_array<double, 3> A(shape); + // Assign a value to an element in the array + A[0][0][0] = 3.14; + assert(A[0][0][0] == 3.14); + return boost::exit_success; +} diff --git a/src/boost/libs/multi_array/example/for_each.hpp b/src/boost/libs/multi_array/example/for_each.hpp new file mode 100644 index 00000000..c0496c58 --- /dev/null +++ b/src/boost/libs/multi_array/example/for_each.hpp @@ -0,0 +1,52 @@ +// 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. + +#ifndef FOR_EACH_HPP +#define FOR_EACH_HPP + +// +// for_each.hpp - Writing an algorithm to transform each element of +// a multi_array +// + +#include "boost/type.hpp" + +template <typename Array, typename Element, typename Functor> +void for_each (const boost::type<Element>& type_dispatch, + Array A, Functor& xform) { + for_each(type_dispatch,A.begin(),A.end(),xform); +} + +template <typename Element, typename Functor> +void for_each (const boost::type<Element>&,Element& Val, Functor& xform) { + Val = xform(Val); +} + +template <typename Element, typename Iterator, typename Functor> +void for_each (const boost::type<Element>& type_dispatch, + Iterator begin, Iterator end, + Functor& xform) { + while (begin != end) { + for_each(type_dispatch,*begin,xform); + ++begin; + } +} + + +template <typename Array, typename Functor> +void for_each (Array& A, Functor xform) { + // Dispatch to the proper function + for_each(boost::type<typename Array::element>(),A.begin(),A.end(),xform); +} + + +#endif // FOR_EACH_HPP diff --git a/src/boost/libs/multi_array/example/foreach_test.cpp b/src/boost/libs/multi_array/example/foreach_test.cpp new file mode 100644 index 00000000..4330d18a --- /dev/null +++ b/src/boost/libs/multi_array/example/foreach_test.cpp @@ -0,0 +1,54 @@ +// 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. + +// foreach_test.cpp +// Let's see if this stuff works + +#include "boost/multi_array.hpp" +#include "for_each.hpp" +#include <algorithm> + +struct times_five { + double operator()(const int& val) { return val*5.0; } +}; + + +int main() { + + typedef boost::multi_array<double,2> array; + + double data[] = { + 1.0, 2.0, 3.0, + 4.0, 5.0, 6.0, + 7.0, 8.0, 9.0 + }; + const int data_size=9; + + array A(boost::extents[3][3]); + A.assign(data,data+data_size); + +#if 0 + std::copy(A.data(),A.data()+A.num_elements(), + std::ostream_iterator<double>(std::cout,",")); + + std::cout << "\n"; +#endif + for_each(A,times_five()); + +#if 0 + std::copy(A.data(),A.data()+A.num_elements(), + std::ostream_iterator<double>(std::cout,",")); + + std::cout << "\n"; +#endif + return 0; +} diff --git a/src/boost/libs/multi_array/example/foreach_test2.cpp b/src/boost/libs/multi_array/example/foreach_test2.cpp new file mode 100644 index 00000000..b1161126 --- /dev/null +++ b/src/boost/libs/multi_array/example/foreach_test2.cpp @@ -0,0 +1,52 @@ +// 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. + +#include "boost/multi_array.hpp" +#include "for_each.hpp" +#include <algorithm> + +struct times_five { + double operator()(const int& val) { return val*5.0; } +}; + + +int main() { + + typedef boost::multi_array<double,1> array; + + double data[] = { + 1.0, 2.0, 3.0, + 4.0, 5.0, 6.0, + 7.0, 8.0, 9.0 + }; + const int data_size=9; + + array A(boost::extents[9]); + A.assign(data,data+data_size); + +#if 0 + std::copy(A.begin(),A.end(), + std::ostream_iterator<double>(std::cout,",")); + + std::cout << "\n"; +#endif + + for_each(A,times_five()); + +#if 0 + std::copy(A.begin(),A.end(), + std::ostream_iterator<double>(std::cout,",")); + + std::cout << "\n"; +#endif + return 0; +} diff --git a/src/boost/libs/multi_array/example/op_paren.cpp b/src/boost/libs/multi_array/example/op_paren.cpp new file mode 100644 index 00000000..6dad7113 --- /dev/null +++ b/src/boost/libs/multi_array/example/op_paren.cpp @@ -0,0 +1,29 @@ +// 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. + + +#include <cassert> +#include "boost/multi_array.hpp" +#include "boost/array.hpp" +#include "boost/cstdlib.hpp" + +int main () { + // Create a 3D array that is 3 x 4 x 2 + boost::array<int, 3> shape = {{ 3, 4, 2 }}; + boost::multi_array<double, 3> A(shape); + typedef boost::multi_array<double, 3>::index index; + // Assign a value to an element in the array + boost::array<index, 3> idx = {{ 0, 0, 0 }}; + A(idx) = 3.14; + assert(A(idx) == 3.14); + return boost::exit_success; +} diff --git a/src/boost/libs/multi_array/example/print_array.cpp b/src/boost/libs/multi_array/example/print_array.cpp new file mode 100644 index 00000000..5ce3c8a2 --- /dev/null +++ b/src/boost/libs/multi_array/example/print_array.cpp @@ -0,0 +1,50 @@ +// 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. + + +#include <iostream> +#include "boost/multi_array.hpp" +#include "boost/array.hpp" +#include "boost/cstdlib.hpp" + +template <typename Array> +void print(std::ostream& os, const Array& A) +{ + typename Array::const_iterator i; + os << "["; + for (i = A.begin(); i != A.end(); ++i) { + print(os, *i); + if (boost::next(i) != A.end()) + os << ','; + } + os << "]"; +} +void print(std::ostream& os, const double& x) +{ + os << x; +} +int main() +{ + typedef boost::multi_array<double, 2> array; + double values[] = { + 0, 1, 2, + 3, 4, 5 + }; + const int values_size=6; + array A(boost::extents[2][3]); + A.assign(values,values+values_size); + print(std::cout, A); + return boost::exit_success; +} + +// The output is: +// [[0,1,2],[3,4,5]] diff --git a/src/boost/libs/multi_array/example/resize_from_other.cpp b/src/boost/libs/multi_array/example/resize_from_other.cpp new file mode 100644 index 00000000..e272c286 --- /dev/null +++ b/src/boost/libs/multi_array/example/resize_from_other.cpp @@ -0,0 +1,57 @@ +// Copyright 2008 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. + +// +// resize_from_other.cpp - an experiment in writing a resize function for +// multi_arrays that will use the extents from another to build itself. +// + +#include <boost/multi_array.hpp> +#include <boost/static_assert.hpp> +#include <boost/array.hpp> +#include <algorithm> + +template <typename T, typename U, std::size_t N> +void +resize_from_MultiArray(boost::multi_array<T,N>& marray, U& other) { + + // U must be a model of MultiArray + boost::function_requires< + boost::multi_array_concepts::ConstMultiArrayConcept<U,U::dimensionality> >(); + // U better have U::dimensionality == N + BOOST_STATIC_ASSERT(U::dimensionality == N); + + boost::array<typename boost::multi_array<T,N>::size_type, N> shape; + + std::copy(other.shape(), other.shape()+N, shape.begin()); + + marray.resize(shape); + +} + +#include <iostream> + + +int main () { + + boost::multi_array<int,2> A(boost::extents[5][4]), B; + boost::multi_array<int,3> C; + + resize_from_MultiArray(B,A); + +#if 0 + resize_from_MultiArray(C,A); // Compile-time error +#endif + + std::cout << B.shape()[0] << ", " << B.shape()[1] << '\n'; + +} diff --git a/src/boost/libs/multi_array/example/subview.cpp b/src/boost/libs/multi_array/example/subview.cpp new file mode 100644 index 00000000..5e85640b --- /dev/null +++ b/src/boost/libs/multi_array/example/subview.cpp @@ -0,0 +1,53 @@ +// 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. + + +#include "boost/multi_array.hpp" +#include "boost/cstdlib.hpp" + +int +main() +{ + const int ndims=3; + typedef boost::multi_array<int,ndims> array; + + int data[] = { + 0,1,2,3, + 4,5,6,7, + 8,9,10,11, + + 12,13,14,15, + 16,17,18,19, + 20,21,22,23 + }; + const int data_size=24; + + array myarray(boost::extents[2][3][4]); + myarray.assign(data,data+data_size); + + // + // array_view dims: + // [base,stride,bound) + // [0,1,2), [1,1,3), [0,2,4) + // + + typedef array::index_range range; + array::array_view<ndims>::type myview = + myarray[boost::indices[range(0,2)][range(1,3)][range(0,4,2)]]; + + for (array::index i = 0; i != 2; ++i) + for (array::index j = 0; j != 2; ++j) + for (array::index k = 0; k != 2; ++k) + assert(myview[i][j][k] == myarray[i][j+1][k*2]); + + return boost::exit_success; +} diff --git a/src/boost/libs/multi_array/example/subview2.cpp b/src/boost/libs/multi_array/example/subview2.cpp new file mode 100644 index 00000000..925d59d1 --- /dev/null +++ b/src/boost/libs/multi_array/example/subview2.cpp @@ -0,0 +1,54 @@ +// 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. + + +#include "boost/multi_array.hpp" +#include "boost/cstdlib.hpp" + +int +main() +{ + using boost::extents; + using boost::indices; + typedef boost::multi_array<int,3> array; + + int data[] = { + 0,1,2,3, + 4,5,6,7, + 8,9,10,11, + + 12,13,14,15, + 16,17,18,19, + 20,21,22,23 + }; + const int data_size=24; + + array myarray(extents[2][3][4]); + myarray.assign(data,data+data_size); + + // + // array_view dims: + // [base,stride,bound) + // [0,1,2), [1,1,3), [0,2,4) + // + + typedef boost::multi_array_types::index_range range; + array::array_view<3>::type myview = + myarray[indices[range(0,2)][range(1,3)][range(0,4,2)]]; + + for (array::index i = 0; i != 2; ++i) + for (array::index j = 0; j != 2; ++j) + for (array::index k = 0; k != 2; ++k) + assert(myview[i][j][k] == myarray[i][j+1][k*2]); + + return boost::exit_success; +} |