2
0
mirror of https://github.com/boostorg/ublas.git synced 2026-02-22 03:42:19 +00:00

final patch for size operation

svn path=/trunk/boost/numeric/ublas/; revision=65783
This commit is contained in:
David Bellot
2010-10-06 13:27:27 +00:00
parent 401edb1e66
commit 051bd10e49
3 changed files with 354 additions and 287 deletions

View File

@@ -13,13 +13,11 @@
/*! \file assignment.hpp
\brief uBlas assignment operator <<=.
*/
namespace boost { namespace numeric { namespace ublas {
/**
* \brief A CRTP and Barton-Nackman trick index manipulator wrapper class.
/** \brief A CRTP and Barton-Nackman trick index manipulator wrapper class.
*
* This class is not meant to be used directly.
*/
@@ -37,14 +35,13 @@ public:
}
};
/**
* \brief A move_to vector index manipulator.
*
* When member function \c manip is called the referenced
* index will be set to the manipulators' index.
*
* \sa move_to(T i)
*/
/** \brief A move_to vector index manipulator.
*
* When member function \c manip is called the referenced
* index will be set to the manipulators' index.
*
* \sa move_to(T i)
*/
template <typename T>
class vector_move_to_manip: public index_manipulator<vector_move_to_manip<T> > {
public:
@@ -58,38 +55,35 @@ private:
T i;
};
/**
* \brief An object generator that returns a move_to vector index manipulator
*
* \tparam T Size type
* \param i The element number the manipulator will move to when \c manip
* member function is called
* \return A move_to vector manipulator
*
* Example usage:
* \code
* vector<double> a(6, 0);
* a <<= 1, 2, move_to(5), 3;
* \endcode
* will result in:
* \code
* 1 2 0 0 0 3
* \endcode
*
* \sa move_to()
*/
/** \brief An object generator that returns a move_to vector index manipulator
*
* \param i The element number the manipulator will move to when \c manip member function is called
* \return A move_to vector manipulator
*
* Example usage:
* \code
* vector<double> a(6, 0);
* a <<= 1, 2, move_to(5), 3;
* \endcode
* will result in:
* \code
* 1 2 0 0 0 3
* \endcode
*
* \tparam T Size type
* \sa move_to()
*/
template <typename T>
BOOST_UBLAS_INLINE vector_move_to_manip<T> move_to(T i) {
return vector_move_to_manip<T>(i);
}
/**
* \brief A static move to vector manipulator.
*
* When member function \c manip is called the referenced
* index will be set to the manipulators' index
*
* \sa move_to(T i) and move_to()
/** \brief A static move to vector manipulator.
*
* When member function \c manip is called the referenced
* index will be set to the manipulators' index
*
* \sa move_to(T i) and move_to()
*/
template <std::size_t I>
class static_vector_move_to_manip: public index_manipulator<static_vector_move_to_manip<I> > {
@@ -99,40 +93,37 @@ public:
void manip(V &k) const { k=I; }
};
/**
* \brief An object generator that returns a static move_to vector index manipulator.
*
* Typically faster than the dynamic version, but can be used only when the
* values are known at compile time.
*
* \tparam I The number of elements the manipulator will traverse the index when \c manip
* function is called
* \return A static move_to vector manipulator
*
* Example usage:
* \code
* vector<double> a(6, 0);
* a <<= 1, 2, move_to<5>(), 3;
* \endcode
* will result in:
* \code
* 1 2 0 0 0 3
* \endcode
*
*/
/** \brief An object generator that returns a static move_to vector index manipulator.
*
* Typically faster than the dynamic version, but can be used only when the
* values are known at compile time.
*
* \return A static move_to vector manipulator
*
* Example usage:
* \code
* vector<double> a(6, 0);
* a <<= 1, 2, move_to<5>(), 3;
* \endcode
* will result in:
* \code
* 1 2 0 0 0 3
* \endcode
*
* \tparam I The number of elements the manipulator will traverse the index when \c manip function is called
*/
template <std::size_t I>
BOOST_UBLAS_INLINE static_vector_move_to_manip<I> move_to() {
return static_vector_move_to_manip<I>();
}
/**
* \brief A move vector index manipulator.
*
* When member function traverse is called the manipulators'
* index will be added to the referenced index.
*
* \sa move(T i)
*/
/** \brief A move vector index manipulator.
*
* When member function traverse is called the manipulators'
* index will be added to the referenced index.
*
* \see move(T i)
*/
template <typename T>
class vector_move_manip: public index_manipulator<vector_move_manip<T> > {
public:

View File

@@ -1,6 +1,6 @@
//
// Copyright (c) 2000-2002
// Joerg Walter, Mathias Koch
// Copyright (c) 2000-2010
// Joerg Walter, Mathias Koch. David Bellot
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
@@ -22,10 +22,10 @@
namespace boost { namespace numeric { namespace ublas {
/** \brief Base class for uBLAS staticaly derived expressions using the the Barton Nackman trick
/** \brief Base class for uBLAS statically derived expressions using the the Barton Nackman trick
*
* This class provides the numeric properties for linear algebra.
* This is a NonAssignable class
* Directly implement nonassignable - simplifes debugging call trace!
*
* \tparam E an expression type
*/
@@ -38,7 +38,6 @@ namespace boost { namespace numeric { namespace ublas {
typedef typename E::value_type value_type;
*/
// Directly implement nonassignable - simplifes debugging call trace!
protected:
ublas_expression () {}
~ublas_expression () {}

View File

@@ -1,11 +1,9 @@
/**
* -*- c++ -*-
*
* \file size.hpp
*
* \brief The \c size operation.
* \brief The family of \c size operations.
*
* Copyright (c) 2009, Marco Guazzone
* Copyright (c) 2009-2010, Marco Guazzone
*
* Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE_1_0.txt or copy at
@@ -18,254 +16,333 @@
#define BOOST_NUMERIC_UBLAS_OPERATION_SIZE_HPP
#include <boost/mpl/has_xxx.hpp>
#include <boost/mpl/if.hpp>
#include <boost/numeric/ublas/detail/config.hpp>
#include <boost/numeric/ublas/expression_types.hpp>
#include <boost/numeric/ublas/fwd.hpp>
#include <boost/numeric/ublas/tags.hpp>
#include <boost/numeric/ublas/traits.hpp>
#include <boost/utility/enable_if.hpp>
#include <cstddef>
namespace boost { namespace numeric { namespace ublas {
namespace detail {
namespace detail { namespace /*<unnamed>*/ {
/**
* \brief Auxiliary class for computing the size of the given dimension for
* a container of the given category..
* \tparam Dim The dimension number (starting from 1).
* \tparam CategoryT The category type (e.g., vector_tag).
*/
template <size_t Dim, typename CategoryT>
struct size_by_dim_impl;
/// Define a \c has_size_type trait class.
BOOST_MPL_HAS_XXX_TRAIT_DEF(size_type)
/// \brief Specialization of \c size_by_dim_impl for computing the size of a
/// vector
template <>
struct size_by_dim_impl<1, vector_tag>
{
/**
* \brief Compute the size of the given vector.
* \tparam ExprT A vector expression type.
* \pre ExprT must be a model of VectorExpression.
*/
template <typename ExprT>
BOOST_UBLAS_INLINE
static typename ExprT::size_type apply(ExprT const& e)
{
return e.size();
}
};
/**
* \brief Wrapper type-traits used in \c boost::lazy_enabled_if for getting the
* size type (see below).
* \tparam VectorT A vector type.
*/
template <typename VectorT>
struct vector_size_type
{
/// The size type.
typedef typename vector_traits<VectorT>::size_type type;
};
/**
* \brief Wrapper type-traits used in \c boost::lazy_enabled_if for getting the
* size type (see below).
* \tparam MatrixT A matrix type.
*/
template <typename MatrixT>
struct matrix_size_type
{
/// The size type.
typedef typename matrix_traits<MatrixT>::size_type type;
};
/// \brief Specialization of \c size_by_dim_impl for computing the number of
/// rows of a matrix
template <>
struct size_by_dim_impl<1, matrix_tag>
{
/**
* \brief Compute the number of rows of the given matrix.
* \tparam ExprT A matrix expression type.
* \pre ExprT must be a model of MatrixExpression.
*/
template <typename ExprT>
BOOST_UBLAS_INLINE
static typename ExprT::size_type apply(ExprT const& e)
{
return e.size1();
}
};
/**
* \brief Auxiliary class for computing the size of the given dimension for
* a container of the given category.
* \tparam Dim The dimension number (starting from 1).
* \tparam CategoryT The category type (e.g., vector_tag).
*/
template <std::size_t Dim, typename CategoryT>
struct size_by_dim_impl;
/// \brief Specialization of \c size_by_dim_impl for computing the number of
/// columns of a matrix
template <>
struct size_by_dim_impl<2, matrix_tag>
{
/**
* \brief Compute the number of columns of the given matrix.
* \tparam ExprT A matrix expression type.
* \pre ExprT must be a model of MatrixExpression.
*/
template <typename ExprT>
BOOST_UBLAS_INLINE
static typename ExprT::size_type apply(ExprT const& e)
{
return e.size2();
}
};
/**
* \brief Auxiliary class for computing the size of the given dimension for
* a container of the given category and with the given orientation..
* \tparam Dim The dimension number (starting from 1).
* \tparam CategoryT The category type (e.g., vector_tag).
* \tparam OrientationT The orientation category type (e.g., row_major_tag).
*/
template <typename TagT, typename CategoryT, typename OrientationT>
struct size_by_tag_impl;
/// \brief Specialization of \c size_by_tag_impl for computing the size of the
/// major dimension of a row-major oriented matrix.
template <>
struct size_by_tag_impl<tag::major, matrix_tag, row_major_tag>
{
/**
* \brief Compute the number of rows of the given matrix.
* \tparam ExprT A matrix expression type.
* \pre ExprT must be a model of MatrixExpression.
*/
template <typename ExprT>
BOOST_UBLAS_INLINE
static typename ExprT::size_type apply(ExprT const& e)
{
return e.size1();
}
};
/// \brief Specialization of \c size_by_tag_impl for computing the size of the
/// minor dimension of a row-major oriented matrix.
template <>
struct size_by_tag_impl<tag::minor, matrix_tag, row_major_tag>
{
/**
* \brief Compute the number of columns of the given matrix.
* \tparam ExprT A matrix expression type.
* \pre ExprT must be a model of MatrixExpression.
*/
template <typename ExprT>
BOOST_UBLAS_INLINE
static typename ExprT::size_type apply(ExprT const& e)
{
return e.size2();
}
};
/// \brief Specialization of \c size_by_tag_impl for computing the size of the
/// leading dimension of a row-major oriented matrix.
template <>
struct size_by_tag_impl<tag::leading, matrix_tag, row_major_tag>
{
/**
* \brief Compute the number of columns of the given matrix.
* \tparam ExprT A matrix expression type.
* \pre ExprT must be a model of MatrixExpression.
*/
template <typename ExprT>
BOOST_UBLAS_INLINE
static typename ExprT::size_type apply(ExprT const& e)
{
return e.size2();
}
};
/// \brief Specialization of \c size_by_tag_impl for computing the size of the
/// major dimension of a column-major oriented matrix.
template <>
struct size_by_tag_impl<tag::major, matrix_tag, column_major_tag>
{
/**
* \brief Compute the number of columns of the given matrix.
* \tparam ExprT A matrix expression type.
* \pre ExprT must be a model of MatrixExpression.
*/
template <typename ExprT>
BOOST_UBLAS_INLINE
static typename ExprT::size_type apply(ExprT const& e)
{
return e.size2();
}
};
/// \brief Specialization of \c size_by_tag_impl for computing the size of the
/// minor dimension of a column-major oriented matrix.
template <>
struct size_by_tag_impl<tag::minor, matrix_tag, column_major_tag>
{
/**
* \brief Compute the number of rows of the given matrix.
* \tparam ExprT A matrix expression type.
* \pre ExprT must be a model of MatrixExpression.
*/
template <typename ExprT>
BOOST_UBLAS_INLINE
static typename ExprT::size_type apply(ExprT const& e)
{
return e.size1();
}
};
/// \brief Specialization of \c size_by_tag_impl for computing the size of the
/// leading dimension of a column-major oriented matrix.
template <>
struct size_by_tag_impl<tag::leading, matrix_tag, column_major_tag>
{
/**
* \brief Compute the number of rows of the given matrix.
* \tparam ExprT A matrix expression type.
* \pre ExprT must be a model of MatrixExpression.
*/
template <typename ExprT>
BOOST_UBLAS_INLINE
static typename ExprT::size_type apply(ExprT const& e)
{
return e.size1();
}
};
} // Namespace detail
/**
* \brief Auxiliary class for computing the size of the given dimension for
* a container of the given category and with the given orientation.
* \tparam Dim The dimension number (starting from 1).
* \tparam CategoryT The category type (e.g., vector_tag).
* \tparam OrientationT The orientation category type (e.g., row_major_tag).
*/
template <typename TagT, typename CategoryT, typename OrientationT>
struct size_by_tag_impl;
/**
* \brief Specialization of \c size_by_dim_impl for computing the size of a
* vector.
*/
template <>
struct size_by_dim_impl<1, vector_tag>
{
/**
* \brief Return the number of columns.
* \tparam MatrixExprT A type which models the matrix expression concept.
* \param m A matrix expression.
* \return The number of columns.
* \brief Compute the size of the given vector.
* \tparam ExprT A vector expression type.
* \pre ExprT must be a model of VectorExpression.
*/
template <typename VectorExprT>
template <typename ExprT>
BOOST_UBLAS_INLINE
typename VectorExprT::size_type size(VectorExprT const& v)
static typename vector_traits<ExprT>::size_type apply(vector_expression<ExprT> const& ve)
{
return v.size();
return ve().size();
}
};
/**
* \brief Specialization of \c size_by_dim_impl for computing the number of
* rows of a matrix
*/
template <>
struct size_by_dim_impl<1, matrix_tag>
{
/**
* \brief Return the size of the given dimension for the given expression.
* \tparam Dim The dimension number (starting from 1).
* \tparam ExprT An expression type.
* \param e An expression.
* \return The number of columns.
* \return The size associated to the dimension \a Dim.
* \brief Compute the number of rows of the given matrix.
* \tparam ExprT A matrix expression type.
* \pre ExprT must be a model of MatrixExpression.
*/
template <std::size_t Dim, typename ExprT>
template <typename ExprT>
BOOST_UBLAS_INLINE
typename ExprT::size_type size(ExprT const& e)
static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
{
return detail::size_by_dim_impl<Dim, typename ExprT::type_category>::apply(e);
return me().size1();
}
};
/**
* \brief Specialization of \c size_by_dim_impl for computing the number of
* columns of a matrix
*/
template <>
struct size_by_dim_impl<2, matrix_tag>
{
/**
* \brief Return the size of the given dimension tag for the given expression.
* \tparam TagT The dimension tag type (e.g., tag::major).
* \tparam ExprT An expression type.
* \param e An expression.
* \return The size associated to the dimension tag \a TagT.
* \brief Compute the number of columns of the given matrix.
* \tparam ExprT A matrix expression type.
* \pre ExprT must be a model of MatrixExpression.
*/
template <typename TagT, typename ExprT>
template <typename ExprT>
BOOST_UBLAS_INLINE
typename ExprT::size_type size(ExprT const& e)
static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
{
return detail::size_by_tag_impl<TagT, typename ExprT::type_category, typename ExprT::orientation_category>::apply(e);
return me().size2();
}
};
/**
* \brief Specialization of \c size_by_tag_impl for computing the size of the
* major dimension of a row-major oriented matrix.
*/
template <>
struct size_by_tag_impl<tag::major, matrix_tag, row_major_tag>
{
/**
* \brief Compute the number of rows of the given matrix.
* \tparam ExprT A matrix expression type.
* \pre ExprT must be a model of MatrixExpression.
*/
template <typename ExprT>
BOOST_UBLAS_INLINE
static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
{
return me().size1();
}
};
/**
* \brief Specialization of \c size_by_tag_impl for computing the size of the
* minor dimension of a row-major oriented matrix.
*/
template <>
struct size_by_tag_impl<tag::minor, matrix_tag, row_major_tag>
{
/**
* \brief Compute the number of columns of the given matrix.
* \tparam ExprT A matrix expression type.
* \pre ExprT must be a model of MatrixExpression.
*/
template <typename ExprT>
BOOST_UBLAS_INLINE
static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
{
return me().size2();
}
};
/**
* \brief Specialization of \c size_by_tag_impl for computing the size of the
* leading dimension of a row-major oriented matrix.
*/
template <>
struct size_by_tag_impl<tag::leading, matrix_tag, row_major_tag>
{
/**
* \brief Compute the number of columns of the given matrix.
* \tparam ExprT A matrix expression type.
* \pre ExprT must be a model of MatrixExpression.
*/
template <typename ExprT>
BOOST_UBLAS_INLINE
static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
{
return me().size2();
}
};
/// \brief Specialization of \c size_by_tag_impl for computing the size of the
/// major dimension of a column-major oriented matrix.
template <>
struct size_by_tag_impl<tag::major, matrix_tag, column_major_tag>
{
/**
* \brief Compute the number of columns of the given matrix.
* \tparam ExprT A matrix expression type.
* \pre ExprT must be a model of MatrixExpression.
*/
template <typename ExprT>
BOOST_UBLAS_INLINE
static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
{
return me().size2();
}
};
/// \brief Specialization of \c size_by_tag_impl for computing the size of the
/// minor dimension of a column-major oriented matrix.
template <>
struct size_by_tag_impl<tag::minor, matrix_tag, column_major_tag>
{
/**
* \brief Compute the number of rows of the given matrix.
* \tparam ExprT A matrix expression type.
* \pre ExprT must be a model of MatrixExpression.
*/
template <typename ExprT>
BOOST_UBLAS_INLINE
static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
{
return me().size1();
}
};
/// \brief Specialization of \c size_by_tag_impl for computing the size of the
/// leading dimension of a column-major oriented matrix.
template <>
struct size_by_tag_impl<tag::leading, matrix_tag, column_major_tag>
{
/**
* \brief Compute the number of rows of the given matrix.
* \tparam ExprT A matrix expression type.
* \pre ExprT must be a model of MatrixExpression.
*/
template <typename ExprT>
BOOST_UBLAS_INLINE
static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
{
return me().size1();
}
};
/// \brief Specialization of \c size_by_tag_impl for computing the size of the
/// given dimension of a unknown oriented expression.
template <typename TagT, typename CategoryT>
struct size_by_tag_impl<TagT, CategoryT, unknown_orientation_tag>: size_by_tag_impl<TagT, CategoryT, row_major_tag>
{
// Empty
};
}} // Namespace detail::<unnamed>
/**
* \brief Return the number of columns.
* \tparam VectorExprT A type which models the vector expression concept.
* \param ve A vector expression.
* \return The length of the input vector expression.
*/
template <typename VectorExprT>
BOOST_UBLAS_INLINE
typename ::boost::lazy_enable_if_c<
detail::has_size_type<VectorExprT>::value,
detail::vector_size_type<VectorExprT>
>::type size(vector_expression<VectorExprT> const& ve)
{
return ve().size();
}
/**
* \brief Return the size of the given dimension for the given vector
* expression.
* \tparam Dim The dimension number (starting from 1).
* \tparam VectorExprT A vector expression type.
* \param ve A vector expression.
* \return The length of the input vector expression.
*/
template <std::size_t Dim, typename VectorExprT>
BOOST_UBLAS_INLINE
typename vector_traits<VectorExprT>::size_type size(vector_expression<VectorExprT> const& ve)
{
return detail::size_by_dim_impl<Dim, vector_tag>::template apply(ve);
}
/**
* \brief Return the size of the given dimension for the given matrix
* expression.
* \tparam Dim The dimension number (starting from 1).
* \tparam MatrixExprT A matrix expression type.
* \param e A matrix expression.
* \return The size of the input matrix expression associated to the dimension
* \a Dim.
*/
template <std::size_t Dim, typename MatrixExprT>
BOOST_UBLAS_INLINE
typename matrix_traits<MatrixExprT>::size_type size(matrix_expression<MatrixExprT> const& me)
{
return detail::size_by_dim_impl<Dim, matrix_tag>::template apply(me);
}
/**
* \brief Return the size of the given dimension tag for the given matrix
* expression.
* \tparam TagT The dimension tag type (e.g., tag::major).
* \tparam MatrixExprT A matrix expression type.
* \param e A matrix expression.
* \return The size of the input matrix expression associated to the dimension
* tag \a TagT.
*/
template <typename TagT, typename MatrixExprT>
BOOST_UBLAS_INLINE
typename ::boost::lazy_enable_if_c<
detail::has_size_type<MatrixExprT>::value,
detail::matrix_size_type<MatrixExprT>
>::type size(matrix_expression<MatrixExprT> const& me)
{
return detail::size_by_tag_impl<TagT, matrix_tag, typename matrix_traits<MatrixExprT>::orientation_category>::template apply(me);
}
}}} // Namespace boost::numeric::ublas