Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:43:02

0001 /**
0002  * \file size.hpp
0003  *
0004  * \brief The family of \c size operations.
0005  *
0006  * Copyright (c) 2009-2010, Marco Guazzone
0007  *
0008  * Distributed under the Boost Software License, Version 1.0. (See
0009  * accompanying file LICENSE_1_0.txt or copy at
0010  * http://www.boost.org/LICENSE_1_0.txt)
0011  *
0012  * \author Marco Guazzone, marco.guazzone@gmail.com
0013  */
0014 
0015 #ifndef BOOST_NUMERIC_UBLAS_OPERATION_SIZE_HPP
0016 #define BOOST_NUMERIC_UBLAS_OPERATION_SIZE_HPP
0017 
0018 
0019 #include <boost/mpl/has_xxx.hpp> 
0020 #include <boost/mpl/if.hpp>
0021 #include <boost/numeric/ublas/detail/config.hpp>
0022 #include <boost/numeric/ublas/expression_types.hpp>
0023 #include <boost/numeric/ublas/fwd.hpp>
0024 #include <boost/numeric/ublas/tags.hpp>
0025 #include <boost/numeric/ublas/traits.hpp>
0026 #include <boost/utility/enable_if.hpp>
0027 #include <cstddef>
0028 
0029 
0030 namespace boost { namespace numeric { namespace ublas {
0031 
0032 namespace detail { namespace /*<unnamed>*/ {
0033 
0034 /// Define a \c has_size_type trait class.
0035 BOOST_MPL_HAS_XXX_TRAIT_DEF(size_type)
0036 
0037 
0038 /**
0039  * \brief Wrapper type-traits used in \c boost::lazy_enabled_if for getting the
0040  *  size type (see below).
0041  * \tparam VectorT A vector type.
0042  */
0043 template <typename VectorT>
0044 struct vector_size_type
0045 {
0046     /// The size type.
0047     typedef typename vector_traits<VectorT>::size_type type;
0048 };
0049 
0050 /**
0051  * \brief Wrapper type-traits used in \c boost::lazy_enabled_if for getting the
0052  *  size type (see below).
0053  * \tparam MatrixT A matrix type.
0054  */
0055 template <typename MatrixT>
0056 struct matrix_size_type
0057 {
0058     /// The size type.
0059     typedef typename matrix_traits<MatrixT>::size_type type;
0060 };
0061 
0062 
0063 /**
0064  * \brief Auxiliary class for computing the size of the given dimension for
0065  *  a container of the given category.
0066  * \tparam Dim The dimension number (starting from 1).
0067  * \tparam CategoryT The category type (e.g., vector_tag).
0068  */
0069 template <std::size_t Dim, typename CategoryT>
0070 struct size_by_dim_impl;
0071 
0072 
0073 /**
0074  * \brief Auxiliary class for computing the size of the given dimension for
0075  *  a container of the given category and with the given orientation.
0076  * \tparam Dim The dimension number (starting from 1).
0077  * \tparam CategoryT The category type (e.g., vector_tag).
0078  * \tparam OrientationT The orientation category type (e.g., row_major_tag).
0079  */
0080 template <typename TagT, typename CategoryT, typename OrientationT>
0081 struct size_by_tag_impl;
0082 
0083 
0084 /**
0085  * \brief Specialization of \c size_by_dim_impl for computing the size of a
0086  *  vector.
0087  */
0088 template <>
0089 struct size_by_dim_impl<1, vector_tag>
0090 {
0091     /**
0092      * \brief Compute the size of the given vector.
0093      * \tparam ExprT A vector expression type.
0094      * \pre ExprT must be a model of VectorExpression.
0095      */
0096     template <typename ExprT>
0097     BOOST_UBLAS_INLINE
0098     static typename vector_traits<ExprT>::size_type apply(vector_expression<ExprT> const& ve)
0099     {
0100         return ve().size();
0101     }
0102 };
0103 
0104 
0105 /**
0106  * \brief Specialization of \c size_by_dim_impl for computing the number of
0107  *  rows of a matrix
0108  */
0109 template <>
0110 struct size_by_dim_impl<1, matrix_tag>
0111 {
0112     /**
0113      * \brief Compute the number of rows of the given matrix.
0114      * \tparam ExprT A matrix expression type.
0115      * \pre ExprT must be a model of MatrixExpression.
0116      */
0117     template <typename ExprT>
0118     BOOST_UBLAS_INLINE
0119     static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
0120     {
0121         return me().size1();
0122     }
0123 };
0124 
0125 
0126 /**
0127  * \brief Specialization of \c size_by_dim_impl for computing the number of
0128  *  columns of a matrix
0129  */
0130 template <>
0131 struct size_by_dim_impl<2, matrix_tag>
0132 {
0133     /**
0134      * \brief Compute the number of columns of the given matrix.
0135      * \tparam ExprT A matrix expression type.
0136      * \pre ExprT must be a model of MatrixExpression.
0137      */
0138     template <typename ExprT>
0139     BOOST_UBLAS_INLINE
0140     static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
0141     {
0142         return me().size2();
0143     }
0144 };
0145 
0146 
0147 /**
0148  * \brief Specialization of \c size_by_tag_impl for computing the size of the
0149  *  major dimension of a row-major oriented matrix.
0150  */
0151 template <>
0152 struct size_by_tag_impl<tag::major, matrix_tag, row_major_tag>
0153 {
0154     /**
0155      * \brief Compute the number of rows of the given matrix.
0156      * \tparam ExprT A matrix expression type.
0157      * \pre ExprT must be a model of MatrixExpression.
0158      */
0159     template <typename ExprT>
0160     BOOST_UBLAS_INLINE
0161     static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
0162     {
0163         return me().size1();
0164     }
0165 };
0166 
0167 
0168 /**
0169  * \brief Specialization of \c size_by_tag_impl for computing the size of the
0170  *  minor dimension of a row-major oriented matrix.
0171  */
0172 template <>
0173 struct size_by_tag_impl<tag::minor, matrix_tag, row_major_tag>
0174 {
0175     /**
0176      * \brief Compute the number of columns of the given matrix.
0177      * \tparam ExprT A matrix expression type.
0178      * \pre ExprT must be a model of MatrixExpression.
0179      */
0180     template <typename ExprT>
0181     BOOST_UBLAS_INLINE
0182     static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
0183     {
0184         return me().size2();
0185     }
0186 };
0187 
0188 
0189 /**
0190  * \brief Specialization of \c size_by_tag_impl for computing the size of the
0191  *  leading dimension of a row-major oriented matrix.
0192  */
0193 template <>
0194 struct size_by_tag_impl<tag::leading, matrix_tag, row_major_tag>
0195 {
0196     /**
0197      * \brief Compute the number of columns of the given matrix.
0198      * \tparam ExprT A matrix expression type.
0199      * \pre ExprT must be a model of MatrixExpression.
0200      */
0201     template <typename ExprT>
0202     BOOST_UBLAS_INLINE
0203     static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
0204     {
0205         return me().size2();
0206     }
0207 };
0208 
0209 
0210 /// \brief Specialization of \c size_by_tag_impl for computing the size of the
0211 ///  major dimension of a column-major oriented matrix.
0212 template <>
0213 struct size_by_tag_impl<tag::major, matrix_tag, column_major_tag>
0214 {
0215     /**
0216      * \brief Compute the number of columns of the given matrix.
0217      * \tparam ExprT A matrix expression type.
0218      * \pre ExprT must be a model of MatrixExpression.
0219      */
0220     template <typename ExprT>
0221     BOOST_UBLAS_INLINE
0222     static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
0223     {
0224         return me().size2();
0225     }
0226 };
0227 
0228 
0229 /// \brief Specialization of \c size_by_tag_impl for computing the size of the
0230 ///  minor dimension of a column-major oriented matrix.
0231 template <>
0232 struct size_by_tag_impl<tag::minor, matrix_tag, column_major_tag>
0233 {
0234     /**
0235      * \brief Compute the number of rows of the given matrix.
0236      * \tparam ExprT A matrix expression type.
0237      * \pre ExprT must be a model of MatrixExpression.
0238      */
0239     template <typename ExprT>
0240     BOOST_UBLAS_INLINE
0241     static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
0242     {
0243         return me().size1();
0244     }
0245 };
0246 
0247 
0248 /// \brief Specialization of \c size_by_tag_impl for computing the size of the
0249 ///  leading dimension of a column-major oriented matrix.
0250 template <>
0251 struct size_by_tag_impl<tag::leading, matrix_tag, column_major_tag>
0252 {
0253     /**
0254      * \brief Compute the number of rows of the given matrix.
0255      * \tparam ExprT A matrix expression type.
0256      * \pre ExprT must be a model of MatrixExpression.
0257      */
0258     template <typename ExprT>
0259     BOOST_UBLAS_INLINE
0260     static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
0261     {
0262         return me().size1();
0263     }
0264 };
0265 
0266 
0267 /// \brief Specialization of \c size_by_tag_impl for computing the size of the
0268 ///  given dimension of a unknown oriented expression.
0269 template <typename TagT, typename CategoryT>
0270 struct size_by_tag_impl<TagT, CategoryT, unknown_orientation_tag>: size_by_tag_impl<TagT, CategoryT, row_major_tag>
0271 {
0272     // Empty
0273 };
0274 
0275 }} // Namespace detail::<unnamed>
0276 
0277 
0278 /**
0279  * \brief Return the number of columns.
0280  * \tparam VectorExprT A type which models the vector expression concept.
0281  * \param ve A vector expression.
0282  * \return The length of the input vector expression.
0283  */
0284 template <typename VectorExprT>
0285 BOOST_UBLAS_INLINE
0286 typename ::boost::lazy_enable_if_c<
0287     detail::has_size_type<VectorExprT>::value,
0288     detail::vector_size_type<VectorExprT>
0289 >::type size(vector_expression<VectorExprT> const& ve)
0290 {
0291     return ve().size();
0292 }
0293 
0294 
0295 /**
0296  * \brief Return the size of the given dimension for the given vector
0297  *  expression.
0298  * \tparam Dim The dimension number (starting from 1).
0299  * \tparam VectorExprT A vector expression type.
0300  * \param ve A vector expression.
0301  * \return The length of the input vector expression.
0302  */
0303 template <std::size_t Dim, typename VectorExprT>
0304 BOOST_UBLAS_INLINE
0305 typename vector_traits<VectorExprT>::size_type size(vector_expression<VectorExprT> const& ve)
0306 {
0307     return detail::size_by_dim_impl<Dim, vector_tag>::apply(ve);
0308 }
0309 
0310 
0311 /**
0312  * \brief Return the size of the given dimension for the given matrix
0313  *  expression.
0314  * \tparam Dim The dimension number (starting from 1).
0315  * \tparam MatrixExprT A matrix expression type.
0316  * \param e A matrix expression.
0317  * \return The size of the input matrix expression associated to the dimension
0318  *  \a Dim.
0319  */
0320 template <std::size_t Dim, typename MatrixExprT>
0321 BOOST_UBLAS_INLINE
0322 typename matrix_traits<MatrixExprT>::size_type size(matrix_expression<MatrixExprT> const& me)
0323 {
0324     return detail::size_by_dim_impl<Dim, matrix_tag>::apply(me);
0325 }
0326 
0327 
0328 /**
0329  * \brief Return the size of the given dimension tag for the given matrix
0330  *  expression.
0331  * \tparam TagT The dimension tag type (e.g., tag::major).
0332  * \tparam MatrixExprT A matrix expression type.
0333  * \param e A matrix expression.
0334  * \return The size of the input matrix expression associated to the dimension
0335  *  tag \a TagT.
0336  */
0337 template <typename TagT, typename MatrixExprT>
0338 BOOST_UBLAS_INLINE
0339 typename ::boost::lazy_enable_if_c<
0340     detail::has_size_type<MatrixExprT>::value,
0341     detail::matrix_size_type<MatrixExprT>
0342 >::type size(matrix_expression<MatrixExprT> const& me)
0343 {
0344     return detail::size_by_tag_impl<TagT, matrix_tag, typename matrix_traits<MatrixExprT>::orientation_category>::apply(me);
0345 }
0346 
0347 }}} // Namespace boost::numeric::ublas
0348 
0349 
0350 #endif // BOOST_NUMERIC_UBLAS_OPERATION_SIZE_HPP