Chris@16: /** Chris@16: * \file size.hpp Chris@16: * Chris@16: * \brief The family of \c size operations. Chris@16: * Chris@16: * Copyright (c) 2009-2010, Marco Guazzone 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: * \author Marco Guazzone, marco.guazzone@gmail.com Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_NUMERIC_UBLAS_OPERATION_SIZE_HPP Chris@16: #define BOOST_NUMERIC_UBLAS_OPERATION_SIZE_HPP Chris@16: Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: Chris@16: namespace boost { namespace numeric { namespace ublas { Chris@16: Chris@16: namespace detail { namespace /**/ { Chris@16: Chris@16: /// Define a \c has_size_type trait class. Chris@16: BOOST_MPL_HAS_XXX_TRAIT_DEF(size_type) Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief Wrapper type-traits used in \c boost::lazy_enabled_if for getting the Chris@16: * size type (see below). Chris@16: * \tparam VectorT A vector type. Chris@16: */ Chris@16: template Chris@16: struct vector_size_type Chris@16: { Chris@16: /// The size type. Chris@16: typedef typename vector_traits::size_type type; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * \brief Wrapper type-traits used in \c boost::lazy_enabled_if for getting the Chris@16: * size type (see below). Chris@16: * \tparam MatrixT A matrix type. Chris@16: */ Chris@16: template Chris@16: struct matrix_size_type Chris@16: { Chris@16: /// The size type. Chris@16: typedef typename matrix_traits::size_type type; Chris@16: }; Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief Auxiliary class for computing the size of the given dimension for Chris@16: * a container of the given category. Chris@16: * \tparam Dim The dimension number (starting from 1). Chris@16: * \tparam CategoryT The category type (e.g., vector_tag). Chris@16: */ Chris@16: template Chris@16: struct size_by_dim_impl; Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief Auxiliary class for computing the size of the given dimension for Chris@16: * a container of the given category and with the given orientation. Chris@16: * \tparam Dim The dimension number (starting from 1). Chris@16: * \tparam CategoryT The category type (e.g., vector_tag). Chris@16: * \tparam OrientationT The orientation category type (e.g., row_major_tag). Chris@16: */ Chris@16: template Chris@16: struct size_by_tag_impl; Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief Specialization of \c size_by_dim_impl for computing the size of a Chris@16: * vector. Chris@16: */ Chris@16: template <> Chris@16: struct size_by_dim_impl<1, vector_tag> Chris@16: { Chris@16: /** Chris@16: * \brief Compute the size of the given vector. Chris@16: * \tparam ExprT A vector expression type. Chris@16: * \pre ExprT must be a model of VectorExpression. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static typename vector_traits::size_type apply(vector_expression const& ve) Chris@16: { Chris@16: return ve().size(); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief Specialization of \c size_by_dim_impl for computing the number of Chris@16: * rows of a matrix Chris@16: */ Chris@16: template <> Chris@16: struct size_by_dim_impl<1, matrix_tag> Chris@16: { Chris@16: /** Chris@16: * \brief Compute the number of rows of the given matrix. Chris@16: * \tparam ExprT A matrix expression type. Chris@16: * \pre ExprT must be a model of MatrixExpression. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static typename matrix_traits::size_type apply(matrix_expression const& me) Chris@16: { Chris@16: return me().size1(); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief Specialization of \c size_by_dim_impl for computing the number of Chris@16: * columns of a matrix Chris@16: */ Chris@16: template <> Chris@16: struct size_by_dim_impl<2, matrix_tag> Chris@16: { Chris@16: /** Chris@16: * \brief Compute the number of columns of the given matrix. Chris@16: * \tparam ExprT A matrix expression type. Chris@16: * \pre ExprT must be a model of MatrixExpression. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static typename matrix_traits::size_type apply(matrix_expression const& me) Chris@16: { Chris@16: return me().size2(); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief Specialization of \c size_by_tag_impl for computing the size of the Chris@16: * major dimension of a row-major oriented matrix. Chris@16: */ Chris@16: template <> Chris@16: struct size_by_tag_impl Chris@16: { Chris@16: /** Chris@16: * \brief Compute the number of rows of the given matrix. Chris@16: * \tparam ExprT A matrix expression type. Chris@16: * \pre ExprT must be a model of MatrixExpression. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static typename matrix_traits::size_type apply(matrix_expression const& me) Chris@16: { Chris@16: return me().size1(); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief Specialization of \c size_by_tag_impl for computing the size of the Chris@16: * minor dimension of a row-major oriented matrix. Chris@16: */ Chris@16: template <> Chris@16: struct size_by_tag_impl Chris@16: { Chris@16: /** Chris@16: * \brief Compute the number of columns of the given matrix. Chris@16: * \tparam ExprT A matrix expression type. Chris@16: * \pre ExprT must be a model of MatrixExpression. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static typename matrix_traits::size_type apply(matrix_expression const& me) Chris@16: { Chris@16: return me().size2(); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief Specialization of \c size_by_tag_impl for computing the size of the Chris@16: * leading dimension of a row-major oriented matrix. Chris@16: */ Chris@16: template <> Chris@16: struct size_by_tag_impl Chris@16: { Chris@16: /** Chris@16: * \brief Compute the number of columns of the given matrix. Chris@16: * \tparam ExprT A matrix expression type. Chris@16: * \pre ExprT must be a model of MatrixExpression. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static typename matrix_traits::size_type apply(matrix_expression const& me) Chris@16: { Chris@16: return me().size2(); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: /// \brief Specialization of \c size_by_tag_impl for computing the size of the Chris@16: /// major dimension of a column-major oriented matrix. Chris@16: template <> Chris@16: struct size_by_tag_impl Chris@16: { Chris@16: /** Chris@16: * \brief Compute the number of columns of the given matrix. Chris@16: * \tparam ExprT A matrix expression type. Chris@16: * \pre ExprT must be a model of MatrixExpression. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static typename matrix_traits::size_type apply(matrix_expression const& me) Chris@16: { Chris@16: return me().size2(); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: /// \brief Specialization of \c size_by_tag_impl for computing the size of the Chris@16: /// minor dimension of a column-major oriented matrix. Chris@16: template <> Chris@16: struct size_by_tag_impl Chris@16: { Chris@16: /** Chris@16: * \brief Compute the number of rows of the given matrix. Chris@16: * \tparam ExprT A matrix expression type. Chris@16: * \pre ExprT must be a model of MatrixExpression. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static typename matrix_traits::size_type apply(matrix_expression const& me) Chris@16: { Chris@16: return me().size1(); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: /// \brief Specialization of \c size_by_tag_impl for computing the size of the Chris@16: /// leading dimension of a column-major oriented matrix. Chris@16: template <> Chris@16: struct size_by_tag_impl Chris@16: { Chris@16: /** Chris@16: * \brief Compute the number of rows of the given matrix. Chris@16: * \tparam ExprT A matrix expression type. Chris@16: * \pre ExprT must be a model of MatrixExpression. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: static typename matrix_traits::size_type apply(matrix_expression const& me) Chris@16: { Chris@16: return me().size1(); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: /// \brief Specialization of \c size_by_tag_impl for computing the size of the Chris@16: /// given dimension of a unknown oriented expression. Chris@16: template Chris@16: struct size_by_tag_impl: size_by_tag_impl Chris@16: { Chris@16: // Empty Chris@16: }; Chris@16: Chris@16: }} // Namespace detail:: Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief Return the number of columns. Chris@16: * \tparam VectorExprT A type which models the vector expression concept. Chris@16: * \param ve A vector expression. Chris@16: * \return The length of the input vector expression. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: typename ::boost::lazy_enable_if_c< Chris@16: detail::has_size_type::value, Chris@16: detail::vector_size_type Chris@16: >::type size(vector_expression const& ve) Chris@16: { Chris@16: return ve().size(); Chris@16: } Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief Return the size of the given dimension for the given vector Chris@16: * expression. Chris@16: * \tparam Dim The dimension number (starting from 1). Chris@16: * \tparam VectorExprT A vector expression type. Chris@16: * \param ve A vector expression. Chris@16: * \return The length of the input vector expression. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: typename vector_traits::size_type size(vector_expression const& ve) Chris@16: { Chris@16: return detail::size_by_dim_impl::apply(ve); Chris@16: } Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief Return the size of the given dimension for the given matrix Chris@16: * expression. Chris@16: * \tparam Dim The dimension number (starting from 1). Chris@16: * \tparam MatrixExprT A matrix expression type. Chris@16: * \param e A matrix expression. Chris@16: * \return The size of the input matrix expression associated to the dimension Chris@16: * \a Dim. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: typename matrix_traits::size_type size(matrix_expression const& me) Chris@16: { Chris@16: return detail::size_by_dim_impl::apply(me); Chris@16: } Chris@16: Chris@16: Chris@16: /** Chris@16: * \brief Return the size of the given dimension tag for the given matrix Chris@16: * expression. Chris@16: * \tparam TagT The dimension tag type (e.g., tag::major). Chris@16: * \tparam MatrixExprT A matrix expression type. Chris@16: * \param e A matrix expression. Chris@16: * \return The size of the input matrix expression associated to the dimension Chris@16: * tag \a TagT. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: typename ::boost::lazy_enable_if_c< Chris@16: detail::has_size_type::value, Chris@16: detail::matrix_size_type Chris@16: >::type size(matrix_expression const& me) Chris@16: { Chris@16: return detail::size_by_tag_impl::orientation_category>::apply(me); Chris@16: } Chris@16: Chris@16: }}} // Namespace boost::numeric::ublas Chris@16: Chris@16: Chris@16: #endif // BOOST_NUMERIC_UBLAS_OPERATION_SIZE_HPP