Chris@16
|
1 /**
|
Chris@16
|
2 * \file size.hpp
|
Chris@16
|
3 *
|
Chris@16
|
4 * \brief The family of \c size operations.
|
Chris@16
|
5 *
|
Chris@16
|
6 * Copyright (c) 2009-2010, Marco Guazzone
|
Chris@16
|
7 *
|
Chris@16
|
8 * Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
9 * accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
10 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
11 *
|
Chris@16
|
12 * \author Marco Guazzone, marco.guazzone@gmail.com
|
Chris@16
|
13 */
|
Chris@16
|
14
|
Chris@16
|
15 #ifndef BOOST_NUMERIC_UBLAS_OPERATION_SIZE_HPP
|
Chris@16
|
16 #define BOOST_NUMERIC_UBLAS_OPERATION_SIZE_HPP
|
Chris@16
|
17
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/mpl/has_xxx.hpp>
|
Chris@16
|
20 #include <boost/mpl/if.hpp>
|
Chris@16
|
21 #include <boost/numeric/ublas/detail/config.hpp>
|
Chris@16
|
22 #include <boost/numeric/ublas/expression_types.hpp>
|
Chris@16
|
23 #include <boost/numeric/ublas/fwd.hpp>
|
Chris@16
|
24 #include <boost/numeric/ublas/tags.hpp>
|
Chris@16
|
25 #include <boost/numeric/ublas/traits.hpp>
|
Chris@16
|
26 #include <boost/utility/enable_if.hpp>
|
Chris@16
|
27 #include <cstddef>
|
Chris@16
|
28
|
Chris@16
|
29
|
Chris@16
|
30 namespace boost { namespace numeric { namespace ublas {
|
Chris@16
|
31
|
Chris@16
|
32 namespace detail { namespace /*<unnamed>*/ {
|
Chris@16
|
33
|
Chris@16
|
34 /// Define a \c has_size_type trait class.
|
Chris@16
|
35 BOOST_MPL_HAS_XXX_TRAIT_DEF(size_type)
|
Chris@16
|
36
|
Chris@16
|
37
|
Chris@16
|
38 /**
|
Chris@16
|
39 * \brief Wrapper type-traits used in \c boost::lazy_enabled_if for getting the
|
Chris@16
|
40 * size type (see below).
|
Chris@16
|
41 * \tparam VectorT A vector type.
|
Chris@16
|
42 */
|
Chris@16
|
43 template <typename VectorT>
|
Chris@16
|
44 struct vector_size_type
|
Chris@16
|
45 {
|
Chris@16
|
46 /// The size type.
|
Chris@16
|
47 typedef typename vector_traits<VectorT>::size_type type;
|
Chris@16
|
48 };
|
Chris@16
|
49
|
Chris@16
|
50 /**
|
Chris@16
|
51 * \brief Wrapper type-traits used in \c boost::lazy_enabled_if for getting the
|
Chris@16
|
52 * size type (see below).
|
Chris@16
|
53 * \tparam MatrixT A matrix type.
|
Chris@16
|
54 */
|
Chris@16
|
55 template <typename MatrixT>
|
Chris@16
|
56 struct matrix_size_type
|
Chris@16
|
57 {
|
Chris@16
|
58 /// The size type.
|
Chris@16
|
59 typedef typename matrix_traits<MatrixT>::size_type type;
|
Chris@16
|
60 };
|
Chris@16
|
61
|
Chris@16
|
62
|
Chris@16
|
63 /**
|
Chris@16
|
64 * \brief Auxiliary class for computing the size of the given dimension for
|
Chris@16
|
65 * a container of the given category.
|
Chris@16
|
66 * \tparam Dim The dimension number (starting from 1).
|
Chris@16
|
67 * \tparam CategoryT The category type (e.g., vector_tag).
|
Chris@16
|
68 */
|
Chris@16
|
69 template <std::size_t Dim, typename CategoryT>
|
Chris@16
|
70 struct size_by_dim_impl;
|
Chris@16
|
71
|
Chris@16
|
72
|
Chris@16
|
73 /**
|
Chris@16
|
74 * \brief Auxiliary class for computing the size of the given dimension for
|
Chris@16
|
75 * a container of the given category and with the given orientation.
|
Chris@16
|
76 * \tparam Dim The dimension number (starting from 1).
|
Chris@16
|
77 * \tparam CategoryT The category type (e.g., vector_tag).
|
Chris@16
|
78 * \tparam OrientationT The orientation category type (e.g., row_major_tag).
|
Chris@16
|
79 */
|
Chris@16
|
80 template <typename TagT, typename CategoryT, typename OrientationT>
|
Chris@16
|
81 struct size_by_tag_impl;
|
Chris@16
|
82
|
Chris@16
|
83
|
Chris@16
|
84 /**
|
Chris@16
|
85 * \brief Specialization of \c size_by_dim_impl for computing the size of a
|
Chris@16
|
86 * vector.
|
Chris@16
|
87 */
|
Chris@16
|
88 template <>
|
Chris@16
|
89 struct size_by_dim_impl<1, vector_tag>
|
Chris@16
|
90 {
|
Chris@16
|
91 /**
|
Chris@16
|
92 * \brief Compute the size of the given vector.
|
Chris@16
|
93 * \tparam ExprT A vector expression type.
|
Chris@16
|
94 * \pre ExprT must be a model of VectorExpression.
|
Chris@16
|
95 */
|
Chris@16
|
96 template <typename ExprT>
|
Chris@16
|
97 BOOST_UBLAS_INLINE
|
Chris@16
|
98 static typename vector_traits<ExprT>::size_type apply(vector_expression<ExprT> const& ve)
|
Chris@16
|
99 {
|
Chris@16
|
100 return ve().size();
|
Chris@16
|
101 }
|
Chris@16
|
102 };
|
Chris@16
|
103
|
Chris@16
|
104
|
Chris@16
|
105 /**
|
Chris@16
|
106 * \brief Specialization of \c size_by_dim_impl for computing the number of
|
Chris@16
|
107 * rows of a matrix
|
Chris@16
|
108 */
|
Chris@16
|
109 template <>
|
Chris@16
|
110 struct size_by_dim_impl<1, matrix_tag>
|
Chris@16
|
111 {
|
Chris@16
|
112 /**
|
Chris@16
|
113 * \brief Compute the number of rows of the given matrix.
|
Chris@16
|
114 * \tparam ExprT A matrix expression type.
|
Chris@16
|
115 * \pre ExprT must be a model of MatrixExpression.
|
Chris@16
|
116 */
|
Chris@16
|
117 template <typename ExprT>
|
Chris@16
|
118 BOOST_UBLAS_INLINE
|
Chris@16
|
119 static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
|
Chris@16
|
120 {
|
Chris@16
|
121 return me().size1();
|
Chris@16
|
122 }
|
Chris@16
|
123 };
|
Chris@16
|
124
|
Chris@16
|
125
|
Chris@16
|
126 /**
|
Chris@16
|
127 * \brief Specialization of \c size_by_dim_impl for computing the number of
|
Chris@16
|
128 * columns of a matrix
|
Chris@16
|
129 */
|
Chris@16
|
130 template <>
|
Chris@16
|
131 struct size_by_dim_impl<2, matrix_tag>
|
Chris@16
|
132 {
|
Chris@16
|
133 /**
|
Chris@16
|
134 * \brief Compute the number of columns of the given matrix.
|
Chris@16
|
135 * \tparam ExprT A matrix expression type.
|
Chris@16
|
136 * \pre ExprT must be a model of MatrixExpression.
|
Chris@16
|
137 */
|
Chris@16
|
138 template <typename ExprT>
|
Chris@16
|
139 BOOST_UBLAS_INLINE
|
Chris@16
|
140 static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
|
Chris@16
|
141 {
|
Chris@16
|
142 return me().size2();
|
Chris@16
|
143 }
|
Chris@16
|
144 };
|
Chris@16
|
145
|
Chris@16
|
146
|
Chris@16
|
147 /**
|
Chris@16
|
148 * \brief Specialization of \c size_by_tag_impl for computing the size of the
|
Chris@16
|
149 * major dimension of a row-major oriented matrix.
|
Chris@16
|
150 */
|
Chris@16
|
151 template <>
|
Chris@16
|
152 struct size_by_tag_impl<tag::major, matrix_tag, row_major_tag>
|
Chris@16
|
153 {
|
Chris@16
|
154 /**
|
Chris@16
|
155 * \brief Compute the number of rows of the given matrix.
|
Chris@16
|
156 * \tparam ExprT A matrix expression type.
|
Chris@16
|
157 * \pre ExprT must be a model of MatrixExpression.
|
Chris@16
|
158 */
|
Chris@16
|
159 template <typename ExprT>
|
Chris@16
|
160 BOOST_UBLAS_INLINE
|
Chris@16
|
161 static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
|
Chris@16
|
162 {
|
Chris@16
|
163 return me().size1();
|
Chris@16
|
164 }
|
Chris@16
|
165 };
|
Chris@16
|
166
|
Chris@16
|
167
|
Chris@16
|
168 /**
|
Chris@16
|
169 * \brief Specialization of \c size_by_tag_impl for computing the size of the
|
Chris@16
|
170 * minor dimension of a row-major oriented matrix.
|
Chris@16
|
171 */
|
Chris@16
|
172 template <>
|
Chris@16
|
173 struct size_by_tag_impl<tag::minor, matrix_tag, row_major_tag>
|
Chris@16
|
174 {
|
Chris@16
|
175 /**
|
Chris@16
|
176 * \brief Compute the number of columns of the given matrix.
|
Chris@16
|
177 * \tparam ExprT A matrix expression type.
|
Chris@16
|
178 * \pre ExprT must be a model of MatrixExpression.
|
Chris@16
|
179 */
|
Chris@16
|
180 template <typename ExprT>
|
Chris@16
|
181 BOOST_UBLAS_INLINE
|
Chris@16
|
182 static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
|
Chris@16
|
183 {
|
Chris@16
|
184 return me().size2();
|
Chris@16
|
185 }
|
Chris@16
|
186 };
|
Chris@16
|
187
|
Chris@16
|
188
|
Chris@16
|
189 /**
|
Chris@16
|
190 * \brief Specialization of \c size_by_tag_impl for computing the size of the
|
Chris@16
|
191 * leading dimension of a row-major oriented matrix.
|
Chris@16
|
192 */
|
Chris@16
|
193 template <>
|
Chris@16
|
194 struct size_by_tag_impl<tag::leading, matrix_tag, row_major_tag>
|
Chris@16
|
195 {
|
Chris@16
|
196 /**
|
Chris@16
|
197 * \brief Compute the number of columns of the given matrix.
|
Chris@16
|
198 * \tparam ExprT A matrix expression type.
|
Chris@16
|
199 * \pre ExprT must be a model of MatrixExpression.
|
Chris@16
|
200 */
|
Chris@16
|
201 template <typename ExprT>
|
Chris@16
|
202 BOOST_UBLAS_INLINE
|
Chris@16
|
203 static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
|
Chris@16
|
204 {
|
Chris@16
|
205 return me().size2();
|
Chris@16
|
206 }
|
Chris@16
|
207 };
|
Chris@16
|
208
|
Chris@16
|
209
|
Chris@16
|
210 /// \brief Specialization of \c size_by_tag_impl for computing the size of the
|
Chris@16
|
211 /// major dimension of a column-major oriented matrix.
|
Chris@16
|
212 template <>
|
Chris@16
|
213 struct size_by_tag_impl<tag::major, matrix_tag, column_major_tag>
|
Chris@16
|
214 {
|
Chris@16
|
215 /**
|
Chris@16
|
216 * \brief Compute the number of columns of the given matrix.
|
Chris@16
|
217 * \tparam ExprT A matrix expression type.
|
Chris@16
|
218 * \pre ExprT must be a model of MatrixExpression.
|
Chris@16
|
219 */
|
Chris@16
|
220 template <typename ExprT>
|
Chris@16
|
221 BOOST_UBLAS_INLINE
|
Chris@16
|
222 static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
|
Chris@16
|
223 {
|
Chris@16
|
224 return me().size2();
|
Chris@16
|
225 }
|
Chris@16
|
226 };
|
Chris@16
|
227
|
Chris@16
|
228
|
Chris@16
|
229 /// \brief Specialization of \c size_by_tag_impl for computing the size of the
|
Chris@16
|
230 /// minor dimension of a column-major oriented matrix.
|
Chris@16
|
231 template <>
|
Chris@16
|
232 struct size_by_tag_impl<tag::minor, matrix_tag, column_major_tag>
|
Chris@16
|
233 {
|
Chris@16
|
234 /**
|
Chris@16
|
235 * \brief Compute the number of rows of the given matrix.
|
Chris@16
|
236 * \tparam ExprT A matrix expression type.
|
Chris@16
|
237 * \pre ExprT must be a model of MatrixExpression.
|
Chris@16
|
238 */
|
Chris@16
|
239 template <typename ExprT>
|
Chris@16
|
240 BOOST_UBLAS_INLINE
|
Chris@16
|
241 static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
|
Chris@16
|
242 {
|
Chris@16
|
243 return me().size1();
|
Chris@16
|
244 }
|
Chris@16
|
245 };
|
Chris@16
|
246
|
Chris@16
|
247
|
Chris@16
|
248 /// \brief Specialization of \c size_by_tag_impl for computing the size of the
|
Chris@16
|
249 /// leading dimension of a column-major oriented matrix.
|
Chris@16
|
250 template <>
|
Chris@16
|
251 struct size_by_tag_impl<tag::leading, matrix_tag, column_major_tag>
|
Chris@16
|
252 {
|
Chris@16
|
253 /**
|
Chris@16
|
254 * \brief Compute the number of rows of the given matrix.
|
Chris@16
|
255 * \tparam ExprT A matrix expression type.
|
Chris@16
|
256 * \pre ExprT must be a model of MatrixExpression.
|
Chris@16
|
257 */
|
Chris@16
|
258 template <typename ExprT>
|
Chris@16
|
259 BOOST_UBLAS_INLINE
|
Chris@16
|
260 static typename matrix_traits<ExprT>::size_type apply(matrix_expression<ExprT> const& me)
|
Chris@16
|
261 {
|
Chris@16
|
262 return me().size1();
|
Chris@16
|
263 }
|
Chris@16
|
264 };
|
Chris@16
|
265
|
Chris@16
|
266
|
Chris@16
|
267 /// \brief Specialization of \c size_by_tag_impl for computing the size of the
|
Chris@16
|
268 /// given dimension of a unknown oriented expression.
|
Chris@16
|
269 template <typename TagT, typename CategoryT>
|
Chris@16
|
270 struct size_by_tag_impl<TagT, CategoryT, unknown_orientation_tag>: size_by_tag_impl<TagT, CategoryT, row_major_tag>
|
Chris@16
|
271 {
|
Chris@16
|
272 // Empty
|
Chris@16
|
273 };
|
Chris@16
|
274
|
Chris@16
|
275 }} // Namespace detail::<unnamed>
|
Chris@16
|
276
|
Chris@16
|
277
|
Chris@16
|
278 /**
|
Chris@16
|
279 * \brief Return the number of columns.
|
Chris@16
|
280 * \tparam VectorExprT A type which models the vector expression concept.
|
Chris@16
|
281 * \param ve A vector expression.
|
Chris@16
|
282 * \return The length of the input vector expression.
|
Chris@16
|
283 */
|
Chris@16
|
284 template <typename VectorExprT>
|
Chris@16
|
285 BOOST_UBLAS_INLINE
|
Chris@16
|
286 typename ::boost::lazy_enable_if_c<
|
Chris@16
|
287 detail::has_size_type<VectorExprT>::value,
|
Chris@16
|
288 detail::vector_size_type<VectorExprT>
|
Chris@16
|
289 >::type size(vector_expression<VectorExprT> const& ve)
|
Chris@16
|
290 {
|
Chris@16
|
291 return ve().size();
|
Chris@16
|
292 }
|
Chris@16
|
293
|
Chris@16
|
294
|
Chris@16
|
295 /**
|
Chris@16
|
296 * \brief Return the size of the given dimension for the given vector
|
Chris@16
|
297 * expression.
|
Chris@16
|
298 * \tparam Dim The dimension number (starting from 1).
|
Chris@16
|
299 * \tparam VectorExprT A vector expression type.
|
Chris@16
|
300 * \param ve A vector expression.
|
Chris@16
|
301 * \return The length of the input vector expression.
|
Chris@16
|
302 */
|
Chris@16
|
303 template <std::size_t Dim, typename VectorExprT>
|
Chris@16
|
304 BOOST_UBLAS_INLINE
|
Chris@16
|
305 typename vector_traits<VectorExprT>::size_type size(vector_expression<VectorExprT> const& ve)
|
Chris@16
|
306 {
|
Chris@16
|
307 return detail::size_by_dim_impl<Dim, vector_tag>::apply(ve);
|
Chris@16
|
308 }
|
Chris@16
|
309
|
Chris@16
|
310
|
Chris@16
|
311 /**
|
Chris@16
|
312 * \brief Return the size of the given dimension for the given matrix
|
Chris@16
|
313 * expression.
|
Chris@16
|
314 * \tparam Dim The dimension number (starting from 1).
|
Chris@16
|
315 * \tparam MatrixExprT A matrix expression type.
|
Chris@16
|
316 * \param e A matrix expression.
|
Chris@16
|
317 * \return The size of the input matrix expression associated to the dimension
|
Chris@16
|
318 * \a Dim.
|
Chris@16
|
319 */
|
Chris@16
|
320 template <std::size_t Dim, typename MatrixExprT>
|
Chris@16
|
321 BOOST_UBLAS_INLINE
|
Chris@16
|
322 typename matrix_traits<MatrixExprT>::size_type size(matrix_expression<MatrixExprT> const& me)
|
Chris@16
|
323 {
|
Chris@16
|
324 return detail::size_by_dim_impl<Dim, matrix_tag>::apply(me);
|
Chris@16
|
325 }
|
Chris@16
|
326
|
Chris@16
|
327
|
Chris@16
|
328 /**
|
Chris@16
|
329 * \brief Return the size of the given dimension tag for the given matrix
|
Chris@16
|
330 * expression.
|
Chris@16
|
331 * \tparam TagT The dimension tag type (e.g., tag::major).
|
Chris@16
|
332 * \tparam MatrixExprT A matrix expression type.
|
Chris@16
|
333 * \param e A matrix expression.
|
Chris@16
|
334 * \return The size of the input matrix expression associated to the dimension
|
Chris@16
|
335 * tag \a TagT.
|
Chris@16
|
336 */
|
Chris@16
|
337 template <typename TagT, typename MatrixExprT>
|
Chris@16
|
338 BOOST_UBLAS_INLINE
|
Chris@16
|
339 typename ::boost::lazy_enable_if_c<
|
Chris@16
|
340 detail::has_size_type<MatrixExprT>::value,
|
Chris@16
|
341 detail::matrix_size_type<MatrixExprT>
|
Chris@16
|
342 >::type size(matrix_expression<MatrixExprT> const& me)
|
Chris@16
|
343 {
|
Chris@16
|
344 return detail::size_by_tag_impl<TagT, matrix_tag, typename matrix_traits<MatrixExprT>::orientation_category>::apply(me);
|
Chris@16
|
345 }
|
Chris@16
|
346
|
Chris@16
|
347 }}} // Namespace boost::numeric::ublas
|
Chris@16
|
348
|
Chris@16
|
349
|
Chris@16
|
350 #endif // BOOST_NUMERIC_UBLAS_OPERATION_SIZE_HPP
|