Chris@16: // Chris@16: // Copyright (c) 2010 Athanasios Iliopoulos Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef ASSIGNMENT_HPP Chris@16: #define ASSIGNMENT_HPP Chris@16: #include Chris@16: #include Chris@16: Chris@16: /*! \file assignment.hpp Chris@16: \brief uBlas assignment operator <<=. Chris@16: */ Chris@16: Chris@16: namespace boost { namespace numeric { namespace ublas { Chris@16: Chris@16: /** \brief A CRTP and Barton-Nackman trick index manipulator wrapper class. Chris@16: * Chris@16: * This class is not meant to be used directly. Chris@16: */ Chris@16: template Chris@16: class index_manipulator { Chris@16: public: Chris@16: typedef TV type; Chris@16: BOOST_UBLAS_INLINE Chris@16: const type &operator () () const { Chris@16: return *static_cast (this); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: type &operator () () { Chris@16: return *static_cast (this); Chris@16: } Chris@16: }; Chris@16: Chris@16: /** \brief A move_to vector index manipulator. Chris@16: * Chris@16: * When member function \c manip is called the referenced Chris@16: * index will be set to the manipulators' index. Chris@16: * Chris@16: * \sa move_to(T i) Chris@16: */ Chris@16: template Chris@16: class vector_move_to_manip: public index_manipulator > { Chris@16: public: Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_move_to_manip(const T &k): i(k) { } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: void manip(V &k) const { k=i; } Chris@16: private: Chris@16: T i; Chris@16: }; Chris@16: Chris@16: /** \brief An object generator that returns a move_to vector index manipulator Chris@16: * Chris@16: * \param i The element number the manipulator will move to when \c manip member function is called Chris@16: * \return A move_to vector manipulator Chris@16: * Chris@16: * Example usage: Chris@16: * \code Chris@16: * vector a(6, 0); Chris@16: * a <<= 1, 2, move_to(5), 3; Chris@16: * \endcode Chris@16: * will result in: Chris@16: * \code Chris@16: * 1 2 0 0 0 3 Chris@16: * \endcode Chris@16: * Chris@16: * \tparam T Size type Chris@16: * \sa move_to() Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE vector_move_to_manip move_to(T i) { Chris@16: return vector_move_to_manip(i); Chris@16: } Chris@16: Chris@16: /** \brief A static move to vector manipulator. Chris@16: * Chris@16: * When member function \c manip is called the referenced Chris@16: * index will be set to the manipulators' index Chris@16: * Chris@16: * \sa move_to(T i) and move_to() Chris@16: */ Chris@16: template Chris@16: class static_vector_move_to_manip: public index_manipulator > { Chris@16: public: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: void manip(V &k) const { k=I; } Chris@16: }; Chris@16: Chris@16: /** \brief An object generator that returns a static move_to vector index manipulator. Chris@16: * Chris@16: * Typically faster than the dynamic version, but can be used only when the Chris@16: * values are known at compile time. Chris@16: * Chris@16: * \return A static move_to vector manipulator Chris@16: * Chris@16: * Example usage: Chris@16: * \code Chris@16: * vector a(6, 0); Chris@16: * a <<= 1, 2, move_to<5>(), 3; Chris@16: * \endcode Chris@16: * will result in: Chris@16: * \code Chris@16: * 1 2 0 0 0 3 Chris@16: * \endcode Chris@16: * Chris@16: * \tparam I The number of elements the manipulator will traverse the index when \c manip function is called Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE static_vector_move_to_manip move_to() { Chris@16: return static_vector_move_to_manip(); Chris@16: } Chris@16: Chris@16: /** \brief A move vector index manipulator. Chris@16: * Chris@16: * When member function traverse is called the manipulators' Chris@16: * index will be added to the referenced index. Chris@16: * Chris@16: * \see move(T i) Chris@16: */ Chris@16: template Chris@16: class vector_move_manip: public index_manipulator > { Chris@16: public: Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_move_manip(const T &k): i(k) { } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE void manip(V &k) const { k+=i; } Chris@16: private: Chris@16: T i; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief An object generator that returns a move vector index manipulator Chris@16: * Chris@16: * \tparam T Size type Chris@16: * \param i The number of elements the manipulator will traverse the index when \c manip Chris@16: * member function is called. Negative values can be used. Chris@16: * \return A move vector manipulator Chris@16: * Chris@16: * Example usage: Chris@16: * \code Chris@16: * vector a(6, 0); Chris@16: * a <<= 1, 2, move(3), 3; Chris@16: * \endcode Chris@16: * will result in: Chris@16: * \code Chris@16: * 1 2 0 0 0 3 Chris@16: * \endcode Chris@16: * Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE vector_move_manip move(T i) { Chris@16: return vector_move_manip(i); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief A static move vector manipulator Chris@16: * Chris@16: * When member function \c manip is called the manipulators Chris@16: * index will be added to the referenced index Chris@16: * Chris@16: * \sa move() Chris@16: * Chris@16: * \todo Doxygen has some problems with similar template functions. Correct that. Chris@16: */ Chris@101: template Chris@16: class static_vector_move_manip: public index_manipulator > { Chris@16: public: Chris@16: template Chris@16: BOOST_UBLAS_INLINE void manip(V &k) const { k+=I; } Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief An object generator that returns a static move vector index manipulator. Chris@16: * Chris@16: * Typically faster than the dynamic version, but can be used only when the Chris@16: * values are known at compile time. Chris@16: * \tparam I The Number of elements the manipulator will traverse the index when \c manip Chris@16: * function is called.Negative values can be used. Chris@16: * \return A static move vector manipulator Chris@16: * Chris@16: * Example usage: Chris@16: * \code Chris@16: * vector a(6, 0); Chris@16: * a <<= 1, 2, move<3>(), 3; Chris@16: * \endcode Chris@16: * will result in: Chris@16: * \code Chris@16: * 1 2 0 0 0 3 Chris@16: * \endcode Chris@16: * Chris@16: * \todo Doxygen has some problems with similar template functions. Correct that. Chris@16: */ Chris@101: template Chris@101: static_vector_move_manip move() { Chris@16: return static_vector_move_manip(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief A move_to matrix manipulator Chris@16: * Chris@16: * When member function \c manip is called the referenced Chris@16: * index will be set to the manipulators' index Chris@16: * Chris@16: * \sa move_to(T i, T j) Chris@16: * Chris@16: * \todo Doxygen has some problems with similar template functions. Correct that. Chris@16: */ Chris@16: template Chris@16: class matrix_move_to_manip: public index_manipulator > { Chris@16: public: Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_move_to_manip(T k, T l): i(k), j(l) { } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: void manip(V1 &k, V2 &l) const { Chris@16: k=i; Chris@16: l=j; Chris@16: } Chris@16: private: Chris@16: T i, j; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief An object generator that returns a "move_to" matrix index manipulator Chris@16: * Chris@16: * \tparam size type Chris@16: * \param i The row number the manipulator will move to when \c manip Chris@16: * member function is called Chris@16: * \param j The column number the manipulator will move to when \c manip Chris@16: * member function is called Chris@16: * \return A move matrix manipulator Chris@16: * Chris@16: * Example usage: Chris@16: * \code: Chris@16: * matrix A(3, 3, 0); Chris@16: * A <<= 1, 2, move_to(A.size1()-1, A.size1()-1), 3; Chris@16: * \endcode Chris@16: * will result in: Chris@16: * \code Chris@16: * 1 2 0 Chris@16: * 0 0 0 Chris@16: * 0 0 3 Chris@16: * \endcode Chris@16: * \sa move_to(T i, T j) and static_matrix_move_to_manip Chris@16: * Chris@16: * \todo Doxygen has some problems with similar template functions. Correct that. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE matrix_move_to_manip move_to(T i, T j) { Chris@16: return matrix_move_to_manip(i, j); Chris@16: } Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief A static move_to matrix manipulator Chris@16: * When member function traverse is called the referenced Chris@16: * index will be set to the manipulators' index Chris@16: * Chris@16: * \sa move_to() Chris@16: * Chris@16: * \todo Doxygen has some problems with similar template functions. Correct that. Chris@16: */ Chris@101: template Chris@16: class static_matrix_move_to_manip: public index_manipulator > { Chris@16: public: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: void manip(V &k, K &l) const { Chris@16: k=I; Chris@16: l=J; Chris@16: } Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief An object generator that returns a static move_to matrix index manipulator. Chris@16: * Chris@16: * Typically faster than the dynamic version, but can be used only when the Chris@16: * values are known at compile time. Chris@16: * \tparam I The row number the manipulator will set the matrix assigner index to. Chris@16: * \tparam J The column number the manipulator will set the matrix assigner index to. Chris@16: * \return A static move_to matrix manipulator Chris@16: * Chris@16: * Example usage: Chris@16: * \code: Chris@16: * matrix A(3, 3, 0); Chris@16: * A <<= 1, 2, move_to<2,2>, 3; Chris@16: * \endcode Chris@16: * will result in: Chris@16: * \code Chris@16: * 1 2 0 Chris@16: * 0 0 0 Chris@16: * 0 0 3 Chris@16: * \endcode Chris@16: * \sa move_to(T i, T j) and static_matrix_move_to_manip Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE static_matrix_move_to_manip move_to() { Chris@16: return static_matrix_move_to_manip(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief A move matrix index manipulator. Chris@16: * Chris@16: * When member function \c manip is called the manipulator's Chris@16: * index will be added to the referenced' index. Chris@16: * Chris@16: * \sa move(T i, T j) Chris@16: */ Chris@16: template Chris@16: class matrix_move_manip: public index_manipulator > { Chris@16: public: Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_move_manip(T k, T l): i(k), j(l) { } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: void manip(V &k, K &l) const { Chris@16: k+=i; Chris@16: l+=j; Chris@16: } Chris@16: private: Chris@16: T i, j; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief An object generator that returns a move matrix index manipulator Chris@16: * Chris@16: * \tparam size type Chris@16: * \param i The number of rows the manipulator will traverse the index when "manip" Chris@16: * member function is called Chris@16: * \param j The number of columns the manipulator will traverse the index when "manip" Chris@16: * member function is called Chris@16: * \return A move matrix manipulator Chris@16: * Chris@16: * Example: Chris@16: * \code: Chris@16: * matrix A(3, 3, 0); Chris@16: * A <<= 1, 2, move(1,0), Chris@16: * 3,; Chris@16: * \endcode Chris@16: * will result in: Chris@16: * \code Chris@16: * 1 2 0 Chris@16: * 0 0 3 Chris@16: * 0 0 0 Chris@16: * \endcode Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE matrix_move_manip move(T i, T j) { Chris@16: return matrix_move_manip(i, j); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief A static move matrix index manipulator. Chris@16: * Chris@16: * When member function traverse is called the manipulator's Chris@16: * index will be added to the referenced' index. Chris@16: * Chris@16: * \sa move() Chris@16: * Chris@16: * \todo Doxygen has some problems with similar template functions. Correct that. Chris@16: */ Chris@101: template Chris@16: class static_matrix_move_manip: public index_manipulator > { Chris@16: public: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: void manip(V &k, K &l) const { Chris@16: k+=I; Chris@16: l+=J; Chris@16: } Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief An object generator that returns a static "move" matrix index manipulator. Chris@16: * Chris@16: * Typically faster than the dynamic version, but can be used only when the Chris@16: * values are known at compile time. Negative values can be used. Chris@16: * \tparam I The number of rows the manipulator will trasverse the matrix assigner index. Chris@16: * \tparam J The number of columns the manipulator will trasverse the matrix assigner index. Chris@16: * \tparam size type Chris@16: * \return A static move matrix manipulator Chris@16: * Chris@16: * Example: Chris@16: * \code: Chris@16: * matrix A(3, 3, 0); Chris@16: * A <<= 1, 2, move<1,0>(), Chris@16: * 3,; Chris@16: * \endcode Chris@16: * will result in: Chris@16: * \code Chris@16: * 1 2 0 Chris@16: * 0 0 3 Chris@16: * 0 0 0 Chris@16: * \endcode Chris@16: * Chris@16: * \sa move_to() Chris@16: * Chris@16: * \todo Doxygen has some problems with similar template functions. Correct that. Chris@16: */ Chris@101: template Chris@16: BOOST_UBLAS_INLINE static_matrix_move_manip move() { Chris@16: return static_matrix_move_manip(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief A begining of row manipulator Chris@16: * Chris@16: * When member function \c manip is called the referenced Chris@16: * index will be be set to the begining of the row (i.e. column = 0) Chris@16: * Chris@16: * \sa begin1() Chris@16: */ Chris@16: class begin1_manip: public index_manipulator { Chris@16: public: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: void manip(V & k, K &/*l*/) const { Chris@16: k=0; Chris@16: } Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief An object generator that returns a begin1 manipulator. Chris@16: * Chris@16: * The resulted manipulator will traverse the index to the begining Chris@16: * of the current column when its' \c manip member function is called. Chris@16: * Chris@16: * \return A begin1 matrix index manipulator Chris@16: * Chris@16: * Example usage: Chris@16: * \code: Chris@16: * matrix A(3, 3, 0); Chris@16: * A <<= 1, 2, next_row(), Chris@16: * 3, 4, begin1(), 1; Chris@16: * \endcode Chris@16: * will result in: Chris@16: * \code Chris@16: * 1 2 1 Chris@16: * 3 4 0 Chris@16: * 0 0 0 Chris@16: * \endcode Chris@16: * \sa begin2() Chris@16: */ Chris@16: inline begin1_manip begin1() { Chris@16: return begin1_manip(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief A begining of column manipulator Chris@16: * Chris@16: * When member function \c manip is called the referenced Chris@16: * index will be be set to the begining of the column (i.e. row = 0). Chris@16: * Chris@16: * Chris@16: * \sa begin2() Chris@16: */ Chris@16: class begin2_manip: public index_manipulator { Chris@16: public: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: void manip(V &/*k*/, K &l) const { Chris@16: l=0; Chris@16: } Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief An object generator that returns a begin2 manipulator to be used to traverse a matrix. Chris@16: * Chris@16: * The resulted manipulator will traverse the index to the begining Chris@16: * of the current row when its' \c manip member function is called. Chris@16: * Chris@16: * \return A begin2 matrix manipulator Chris@16: * Chris@16: * Example: Chris@16: * \code: Chris@16: * matrix A(3, 3, 0); Chris@16: * A <<= 1, 2, move<1,0>(), Chris@16: * 3, begin2(), 1; Chris@16: * \endcode Chris@16: * will result in: Chris@16: * \code Chris@16: * 1 2 0 Chris@16: * 1 0 3 Chris@16: * 0 0 0 Chris@16: * \endcode Chris@16: * \sa begin1() begin2_manip Chris@16: */ Chris@16: inline begin2_manip begin2() { Chris@16: return begin2_manip(); Chris@16: } Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief A next row matrix manipulator. Chris@16: * Chris@16: * When member function traverse is called the referenced Chris@16: * index will be traveresed to the begining of next row. Chris@16: * Chris@16: * \sa next_row() Chris@16: */ Chris@16: class next_row_manip: public index_manipulator { Chris@16: public: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: void manip(V &k, K &l) const { Chris@16: k++; Chris@16: l=0; Chris@16: } Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief An object generator that returns a next_row manipulator. Chris@16: * Chris@16: * The resulted manipulator will traverse the index to the begining Chris@16: * of the next row when it's manip member function is called. Chris@16: * Chris@16: * \return A next_row matrix manipulator. Chris@16: * Chris@16: * Example: Chris@16: * \code: Chris@16: * matrix A(3, 3, 0); Chris@16: * A <<= 1, 2, next_row(), Chris@16: * 3, 4; Chris@16: * \endcode Chris@16: * will result in: Chris@16: * \code Chris@16: * 1 2 0 Chris@16: * 3 4 0 Chris@16: * 0 0 0 Chris@16: * \endcode Chris@16: * \sa next_column() Chris@16: */ Chris@16: inline next_row_manip next_row() { Chris@16: return next_row_manip(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief A next column matrix manipulator. Chris@16: * Chris@16: * When member function traverse is called the referenced Chris@16: * index will be traveresed to the begining of next column. Chris@16: * Chris@16: * \sa next_column() Chris@16: */ Chris@16: class next_column_manip: public index_manipulator { Chris@16: public: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: void manip(V &k, K &l) const { Chris@16: k=0; Chris@16: l++; Chris@16: } Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief An object generator that returns a next_row manipulator. Chris@16: * Chris@16: * The resulted manipulator will traverse the index to the begining Chris@16: * of the next column when it's manip member function is called. Chris@16: * Chris@16: * \return A next_column matrix manipulator. Chris@16: * Chris@16: * Example: Chris@16: * \code: Chris@16: * matrix A(3, 3, 0); Chris@16: * A <<= 1, 2, 0, Chris@16: * 3, next_column(), 4; Chris@16: * \endcode Chris@16: * will result in: Chris@16: * \code Chris@16: * 1 2 4 Chris@16: * 3 0 0 Chris@16: * 0 0 0 Chris@16: * \endcode Chris@16: * Chris@16: */ Chris@16: inline next_column_manip next_column() { Chris@16: return next_column_manip(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief A wrapper for fill policy classes Chris@16: * Chris@16: */ Chris@16: template Chris@16: class fill_policy_wrapper { Chris@16: public: Chris@16: typedef T type; Chris@16: }; Chris@16: Chris@16: // Collection of the fill policies Chris@16: namespace fill_policy { Chris@16: Chris@16: /** Chris@16: * \brief An index assign policy Chris@16: * Chris@16: * This policy is used to for the simplified ublas assign through Chris@16: * normal indexing. Chris@16: * Chris@16: * Chris@16: */ Chris@16: class index_assign :public fill_policy_wrapper { Chris@16: public: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void apply(T &e, const S &i, const V &v) { Chris@16: e()(i) = v; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void apply(T &e, const S &i, const S &j, const V &v) { Chris@16: e()(i, j) = v; Chris@16: } Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief An index plus assign policy Chris@16: * Chris@16: * This policy is used when the assignment is desired to be followed Chris@16: * by an addition. Chris@16: * Chris@16: * Chris@16: */ Chris@16: class index_plus_assign :public fill_policy_wrapper { Chris@16: public: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void apply(T &e, const S &i, const V &v) { Chris@16: e()(i) += v; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void apply(T &e, const S &i, const S &j, const V &v) { Chris@16: e()(i, j) += v; Chris@16: } Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief An index minus assign policy Chris@16: * Chris@16: * This policy is used when the assignment is desired to be followed Chris@16: * by a substraction. Chris@16: * Chris@16: * Chris@16: */ Chris@16: class index_minus_assign :public fill_policy_wrapper { Chris@16: public: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void apply(T &e, const S &i, const V &v) { Chris@16: e()(i) -= v; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void apply(T &e, const S &i, const S &j, const V &v) { Chris@16: e()(i, j) -= v; Chris@16: } Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief The sparse push_back fill policy. Chris@16: * Chris@16: * This policy is adequate for sparse types, when fast filling is required, where indexing Chris@16: * assign is pretty slow. Chris@16: Chris@16: * It is important to note that push_back assign cannot be used to add elements before elements Chris@16: * already existing in a sparse container. To achieve that please use the sparse_insert fill policy. Chris@16: */ Chris@16: class sparse_push_back :public fill_policy_wrapper { Chris@16: public: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void apply(T &e, const S &i, const V &v) { Chris@16: e().push_back(i, v); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void apply(T &e, const S &i, const S &j, const V &v) { Chris@16: e().push_back(i,j, v); Chris@16: } Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief The sparse insert fill policy. Chris@16: * Chris@16: * This policy is adequate for sparse types, when fast filling is required, where indexing Chris@16: * assign is pretty slow. It is slower than sparse_push_back fill policy, but it can be used to Chris@16: * insert elements anywhere inside the container. Chris@16: */ Chris@16: class sparse_insert :public fill_policy_wrapper { Chris@16: public: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void apply(T &e, const S &i, const V &v) { Chris@16: e().insert_element(i, v); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void apply(T &e, const S &i, const S &j, const V &v) { Chris@16: e().insert_element(i,j, v); Chris@16: } Chris@16: }; Chris@16: Chris@16: } Chris@16: Chris@16: /** \brief A wrapper for traverse policy classes Chris@16: * Chris@16: */ Chris@16: template Chris@16: class traverse_policy_wrapper { Chris@16: public: Chris@16: typedef T type; Chris@16: }; Chris@16: Chris@16: // Collection of the traverse policies Chris@16: namespace traverse_policy { Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief The no wrap policy. Chris@16: * Chris@16: * The no wrap policy does not allow wrapping when assigning to a matrix Chris@16: */ Chris@16: struct no_wrap { Chris@16: /** Chris@16: * \brief Element wrap method Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void apply1(const S1 &/*s*/, S2 &/*i*/, S3 &/*j*/) { Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief Matrix block wrap method Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void apply2(const S1 &/*s1*/, const S1 &/*s2*/, S2 &/*i1*/, S3 &/*i2*/) { Chris@16: } Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief The wrap policy. Chris@16: * Chris@16: * The wrap policy enables element wrapping when assigning to a matrix Chris@16: */ Chris@16: struct wrap { Chris@16: /** Chris@16: * \brief Element wrap method Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void apply1(const S1 &s, S2 &i1, S3 &i2) { Chris@16: if (i2>=s) { Chris@16: i1++; Chris@16: i2=0; Chris@16: } Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief Matrix block wrap method Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void apply2(const S1 &s1, const S1 &s2, S2 &i1, S3 &i2) { Chris@16: if (i2>=s2) i2=0; // Wrap to the next block Chris@16: else i1-=s1; // Move up (or right) one block Chris@16: } Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief The row_by_row traverse policy Chris@16: * Chris@16: * This policy is used when the assignment is desired to happen Chris@16: * row_major wise for performance or other reasons. Chris@16: * Chris@16: * This is the default behaviour. To change it globally please define BOOST_UBLAS_DEFAULT_ASSIGN_BY_COLUMN Chris@16: * in the compilation options or in an adequate header file. Chris@16: * Chris@16: * Please see EXAMPLES_LINK for usage information. Chris@16: * Chris@16: * \todo Add examples link Chris@16: */ Chris@16: template Chris@16: class by_row_policy :public traverse_policy_wrapper > { Chris@16: public: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void advance(S1 &/*i*/, S2 &j) { j++;} Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static bool next(const E1 &e, const E2 &me, S1 &i, S2 &j, const S3 &/*i0*/, const S3 &j0, S4 &k, S5 &l) { Chris@16: l++; j++; Chris@16: if (l>=e().size2()) { Chris@16: l=0; k++; j=j0; i++; Chris@101: // It is assumed that the iteration starts from 0 and progresses only using this function from within Chris@16: // an assigner object. Chris@16: // Otherwise (i.e. if it is called outside the assigner object) apply2 should have been Chris@16: // outside the if statement. Chris@16: if (k>=e().size1()) { Chris@16: j=j0+e().size2(); Chris@16: Wrap::apply2(e().size1(), me().size2(), i, j); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void apply_wrap(const E& e, S1 &i, S2 &j) { Chris@16: Wrap::apply1(e().size2(), i, j); Chris@16: } Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief The column_by_column traverse policy Chris@16: * Chris@16: * This policy is used when the assignment is desired to happen Chris@16: * column_major wise, for performance or other reasons. Chris@16: * Chris@16: * This is the NOT the default behaviour. To set this as the default define BOOST_UBLAS_DEFAULT_ASSIGN_BY_COLUMN Chris@16: * in the compilation options or in an adequate header file. Chris@16: * Chris@16: * Please see EXAMPLES_LINK for usage information. Chris@16: * Chris@16: * \todo Add examples link Chris@16: */ Chris@16: template Chris@16: class by_column_policy :public traverse_policy_wrapper > { Chris@16: public: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void advance(S1 &i, S2 &/*j*/) { i++;} Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static bool next(const E1 &e, const E2 &me, S1 &i, S2 &j, const S3 &i0, const S3 &/*j0*/, S4 &k, S5 &l) { Chris@16: k++; i++; Chris@16: if (k>=e().size1()) { Chris@16: k=0; l++; i=i0; j++; Chris@101: // It is assumed that the iteration starts from 0 and progresses only using this function from within Chris@16: // an assigner object. Chris@16: // Otherwise (i.e. if it is called outside the assigner object) apply2 should have been Chris@16: // outside the if statement. Chris@16: if (l>=e().size2()) { Chris@16: i=i0+e().size1(); Chris@16: Wrap::apply2(e().size2(), me().size1(), j, i); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static void apply_wrap(const E& e, S1 &i, S2 &j) { Chris@16: Wrap::apply1(e().size1(), j, i); Chris@16: } Chris@16: }; Chris@16: } Chris@16: #ifndef BOOST_UBLAS_DEFAULT_NO_WRAP_POLICY Chris@16: typedef traverse_policy::wrap DEFAULT_WRAP_POLICY; Chris@16: #else Chris@16: typedef traverse_policy::no_wrap DEFAULT_WRAP_POLICY; Chris@16: #endif Chris@16: Chris@16: #ifndef BOOST_UBLAS_DEFAULT_ASSIGN_BY_COLUMN Chris@16: typedef traverse_policy::by_row_policy DEFAULT_TRAVERSE_POLICY; Chris@16: #else Chris@16: typedef traverse_policy::by_column DEFAULT_TRAVERSE_POLICY; Chris@16: #endif Chris@16: Chris@16: // Traverse policy namespace Chris@16: namespace traverse_policy { Chris@16: Chris@16: inline by_row_policy by_row() { Chris@16: return by_row_policy(); Chris@16: } Chris@16: Chris@16: inline by_row_policy by_row_wrap() { Chris@16: return by_row_policy(); Chris@16: } Chris@16: Chris@16: inline by_row_policy by_row_no_wrap() { Chris@16: return by_row_policy(); Chris@16: } Chris@16: Chris@16: inline by_column_policy by_column() { Chris@16: return by_column_policy(); Chris@16: } Chris@16: Chris@16: inline by_column_policy by_column_wrap() { Chris@16: return by_column_policy(); Chris@16: } Chris@16: Chris@16: inline by_column_policy by_column_no_wrap() { Chris@16: return by_column_policy(); Chris@16: } Chris@16: Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief An assigner object used to fill a vector using operator <<= and operator, (comma) Chris@16: * Chris@16: * This object is meant to be created by appropriate object generators. Chris@16: * Please see EXAMPLES_LINK for usage information. Chris@16: * Chris@16: * \todo Add examples link Chris@16: */ Chris@16: template Chris@16: class vector_expression_assigner { Chris@16: public: Chris@16: typedef typename E::expression_type::value_type value_type; Chris@16: typedef typename E::expression_type::size_type size_type; Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_expression_assigner(E &e):ve(e), i(0) { Chris@16: } Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_expression_assigner(size_type k, E &e):ve(e), i(k) { Chris@16: // Overloaded like that so it can be differentiated from (E, val). Chris@16: // Otherwise there would be an ambiquity when value_type == size_type. Chris@16: } Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_expression_assigner(E &e, value_type val):ve(e), i(0) { Chris@16: operator,(val); Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_expression_assigner(E &e, const vector_expression &nve):ve(e), i(0) { Chris@16: operator,(nve); Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_expression_assigner(E &e, const index_manipulator &ta):ve(e), i(0) { Chris@16: operator,(ta); Chris@16: } Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_expression_assigner &operator, (const value_type& val) { Chris@16: apply(val); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_expression_assigner &operator, (const vector_expression &nve) { Chris@16: for (typename AE::size_type k = 0; k!= nve().size(); k++) Chris@16: operator,(nve()(k)); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_expression_assigner &operator, (const index_manipulator &ta) { Chris@16: ta().manip(i); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_expression_assigner operator, (fill_policy_wrapper) const { Chris@16: return vector_expression_assigner(i, ve); Chris@16: } Chris@16: Chris@16: private: Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_expression_assigner &apply(const typename E::expression_type::value_type& val) { Chris@16: Fill_Policy::apply(ve, i++, val); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: private: Chris@16: E &ve; Chris@16: size_type i; Chris@16: }; Chris@16: Chris@16: /* Chris@16: // The following static assigner is about 30% slower than the dynamic one, probably due to the recursive creation of assigner objects. Chris@16: // It remains commented here for future reference. Chris@16: Chris@16: template Chris@16: class static_vector_expression_assigner { Chris@16: public: Chris@16: typedef typename E::expression_type::value_type value_type; Chris@16: typedef typename E::expression_type::size_type size_type; Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: static_vector_expression_assigner(E &e):ve(e) { Chris@16: } Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: static_vector_expression_assigner(E &e, value_type val):ve(e) { Chris@16: operator,(val); Chris@16: } Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: static_vector_expression_assigner operator, (const value_type& val) { Chris@16: return apply(val); Chris@16: } Chris@16: Chris@16: private: Chris@16: BOOST_UBLAS_INLINE Chris@16: static_vector_expression_assigner apply(const typename E::expression_type::value_type& val) { Chris@16: ve()(I)=val; Chris@16: return static_vector_expression_assigner(ve); Chris@16: } Chris@16: Chris@16: private: Chris@16: E &ve; Chris@16: }; Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static_vector_expression_assigner, 1 > test_static(vector_expression &v, const typename E::value_type &val) { Chris@16: v()(0)=val; Chris@16: return static_vector_expression_assigner, 1 >(v); Chris@16: } Chris@16: */ Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief A vector_expression_assigner generator used with operator<<= for simple types Chris@16: * Chris@16: * Please see EXAMPLES_LINK for usage information. Chris@16: * Chris@16: * \todo Add examples link Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_expression_assigner > operator<<=(vector_expression &v, const typename E::value_type &val) { Chris@16: return vector_expression_assigner >(v,val); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief ! A vector_expression_assigner generator used with operator<<= for vector expressions Chris@16: * Chris@16: * Please see EXAMPLES_LINK for usage information. Chris@16: * Chris@16: * \todo Add examples link Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_expression_assigner > operator<<=(vector_expression &v, const vector_expression &ve) { Chris@16: return vector_expression_assigner >(v,ve); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief A vector_expression_assigner generator used with operator<<= for traverse manipulators Chris@16: * Chris@16: * Please see EXAMPLES_LINK for usage information. Chris@16: * Chris@16: * \todo Add examples link Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_expression_assigner > operator<<=(vector_expression &v, const index_manipulator &nv) { Chris@16: return vector_expression_assigner >(v,nv); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief A vector_expression_assigner generator used with operator<<= for choice of fill policy Chris@16: * Chris@16: * Please see EXAMPLES_LINK for usage information. Chris@16: * Chris@16: * \todo Add examples link Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_expression_assigner, T> operator<<=(vector_expression &v, fill_policy_wrapper) { Chris@16: return vector_expression_assigner, T>(v); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief An assigner object used to fill a vector using operator <<= and operator, (comma) Chris@16: * Chris@16: * This object is meant to be created by appropriate object generators. Chris@16: * Please see EXAMPLES_LINK for usage information. Chris@16: * Chris@16: * \todo Add examples link Chris@16: */ Chris@16: template Chris@16: class matrix_expression_assigner { Chris@16: public: Chris@16: typedef typename E::expression_type::size_type size_type; Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner(E &e): me(e), i(0), j(0) { Chris@16: } Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner(E &e, size_type k, size_type l): me(e), i(k), j(l) { Chris@16: } Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner(E &e, typename E::expression_type::value_type val): me(e), i(0), j(0) { Chris@16: operator,(val); Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner(E &e, const vector_expression &nve):me(e), i(0), j(0) { Chris@16: operator,(nve); Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner(E &e, const matrix_expression &nme):me(e), i(0), j(0) { Chris@16: operator,(nme); Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner(E &e, const index_manipulator &ta):me(e), i(0), j(0) { Chris@16: operator,(ta); Chris@16: } Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner &operator, (const typename E::expression_type::value_type& val) { Chris@16: Traverse_Policy::apply_wrap(me, i ,j); Chris@16: return apply(val); Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner &operator, (const vector_expression &nve) { Chris@16: for (typename AE::size_type k = 0; k!= nve().size(); k++) { Chris@16: operator,(nve()(k)); Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner &operator, (const matrix_expression &nme) { Chris@16: return apply(nme); Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner &operator, (const index_manipulator &ta) { Chris@16: ta().manip(i, j); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner operator, (fill_policy_wrapper) const { Chris@16: return matrix_expression_assigner(me, i, j); Chris@16: } Chris@16: Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner operator, (traverse_policy_wrapper) { Chris@16: Traverse_Policy::apply_wrap(me, i ,j); Chris@16: return matrix_expression_assigner(me, i, j); Chris@16: } Chris@16: Chris@16: private: Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner &apply(const typename E::expression_type::value_type& val) { Chris@16: Fill_Policy::apply(me, i, j, val); Chris@16: Traverse_Policy::advance(i,j); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner &apply(const matrix_expression &nme) { Chris@16: size_type bi = i; Chris@16: size_type bj = j; Chris@16: typename AE::size_type k=0, l=0; Chris@16: Fill_Policy::apply(me, i, j, nme()(k, l)); Chris@16: while (Traverse_Policy::next(nme, me, i, j, bi, bj, k, l)) Chris@16: Fill_Policy::apply(me, i, j, nme()(k, l)); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: private: Chris@16: E &me; Chris@16: size_type i, j; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief A matrix_expression_assigner generator used with operator<<= for simple types Chris@16: * Chris@16: * Please see EXAMPLES_LINK for usage information. Chris@16: * Chris@16: * \todo Add examples link Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner > operator<<=(matrix_expression &me, const typename E::value_type &val) { Chris@16: return matrix_expression_assigner >(me,val); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief A matrix_expression_assigner generator used with operator<<= for choice of fill policy Chris@16: * Chris@16: * Please see EXAMPLES_LINK for usage information. Chris@16: * Chris@16: * \todo Add examples link Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner, T> operator<<=(matrix_expression &me, fill_policy_wrapper) { Chris@16: return matrix_expression_assigner, T>(me); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief A matrix_expression_assigner generator used with operator<<= for traverse manipulators Chris@16: * Chris@16: * Please see EXAMPLES_LINK for usage information. Chris@16: * Chris@16: * \todo Add examples link Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner > operator<<=(matrix_expression &me, const index_manipulator &ta) { Chris@16: return matrix_expression_assigner >(me,ta); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief A matrix_expression_assigner generator used with operator<<= for traverse manipulators Chris@16: * Chris@16: * Please see EXAMPLES_LINK for usage information. Chris@16: * Chris@16: * \todo Add examples link Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner, fill_policy::index_assign, T> operator<<=(matrix_expression &me, traverse_policy_wrapper) { Chris@16: return matrix_expression_assigner, fill_policy::index_assign, T>(me); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief A matrix_expression_assigner generator used with operator<<= for vector expressions Chris@16: * Chris@16: * Please see EXAMPLES_LINK for usage information. Chris@16: * Chris@16: * \todo Add examples link Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner > operator<<=(matrix_expression &me, const vector_expression &ve) { Chris@16: return matrix_expression_assigner >(me,ve); Chris@16: } Chris@16: Chris@16: /** Chris@16: * \brief A matrix_expression_assigner generator used with operator<<= for matrix expressions Chris@16: * Chris@16: * Please see EXAMPLES_LINK for usage information. Chris@16: * Chris@16: * \todo Add examples link Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: matrix_expression_assigner > operator<<=(matrix_expression &me1, const matrix_expression &me2) { Chris@16: return matrix_expression_assigner >(me1,me2); Chris@16: } Chris@16: Chris@16: } } } Chris@16: Chris@16: #endif // ASSIGNMENT_HPP