1 | /** |
---|
2 | * -*- c++ -*- |
---|
3 | * |
---|
4 | * \file size.hpp |
---|
5 | * |
---|
6 | * \brief The \c size operation. |
---|
7 | * |
---|
8 | * Copyright (c) 2009, Marco Guazzone |
---|
9 | * |
---|
10 | * Distributed under the Boost Software License, Version 1.0. (See |
---|
11 | * accompanying file LICENSE_1_0.txt or copy at |
---|
12 | * http://www.boost.org/LICENSE_1_0.txt) |
---|
13 | * |
---|
14 | * \author Marco Guazzone, marco.guazzone@gmail.com |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef BOOST_NUMERIC_UBLAS_OPERATION_SIZE_HPP |
---|
18 | #define BOOST_NUMERIC_UBLAS_OPERATION_SIZE_HPP |
---|
19 | |
---|
20 | |
---|
21 | #include <boost/numeric/ublas/detail/config.hpp> |
---|
22 | #include <boost/numeric/ublas/expression_types.hpp> |
---|
23 | #include <boost/numeric/ublas/fwd.hpp> |
---|
24 | #include <boost/numeric/ublas/tags.hpp> |
---|
25 | #include <cstddef> |
---|
26 | |
---|
27 | |
---|
28 | namespace boost { namespace numeric { namespace ublas { |
---|
29 | |
---|
30 | namespace detail { |
---|
31 | |
---|
32 | /** |
---|
33 | * \brief Auxiliary class for computing the size of the given dimension for |
---|
34 | * a container of the given category.. |
---|
35 | * \tparam Dim The dimension number (starting from 1). |
---|
36 | * \tparam CategoryT The category type (e.g., vector_tag). |
---|
37 | */ |
---|
38 | template <size_t Dim, typename CategoryT> |
---|
39 | struct size_by_dim_impl; |
---|
40 | |
---|
41 | |
---|
42 | /// \brief Specialization of \c size_by_dim_impl for computing the size of a |
---|
43 | /// vector |
---|
44 | template <> |
---|
45 | struct size_by_dim_impl<1, vector_tag> |
---|
46 | { |
---|
47 | /** |
---|
48 | * \brief Compute the size of the given vector. |
---|
49 | * \tparam ExprT A vector expression type. |
---|
50 | * \pre ExprT must be a model of VectorExpression. |
---|
51 | */ |
---|
52 | template <typename ExprT> |
---|
53 | BOOST_UBLAS_INLINE |
---|
54 | static typename ExprT::size_type apply(ExprT const& e) |
---|
55 | { |
---|
56 | return e.size(); |
---|
57 | } |
---|
58 | }; |
---|
59 | |
---|
60 | |
---|
61 | /// \brief Specialization of \c size_by_dim_impl for computing the number of |
---|
62 | /// rows of a matrix |
---|
63 | template <> |
---|
64 | struct size_by_dim_impl<1, matrix_tag> |
---|
65 | { |
---|
66 | /** |
---|
67 | * \brief Compute the number of rows of the given matrix. |
---|
68 | * \tparam ExprT A matrix expression type. |
---|
69 | * \pre ExprT must be a model of MatrixExpression. |
---|
70 | */ |
---|
71 | template <typename ExprT> |
---|
72 | BOOST_UBLAS_INLINE |
---|
73 | static typename ExprT::size_type apply(ExprT const& e) |
---|
74 | { |
---|
75 | return e.size1(); |
---|
76 | } |
---|
77 | }; |
---|
78 | |
---|
79 | |
---|
80 | /// \brief Specialization of \c size_by_dim_impl for computing the number of |
---|
81 | /// columns of a matrix |
---|
82 | template <> |
---|
83 | struct size_by_dim_impl<2, matrix_tag> |
---|
84 | { |
---|
85 | /** |
---|
86 | * \brief Compute the number of columns of the given matrix. |
---|
87 | * \tparam ExprT A matrix expression type. |
---|
88 | * \pre ExprT must be a model of MatrixExpression. |
---|
89 | */ |
---|
90 | template <typename ExprT> |
---|
91 | BOOST_UBLAS_INLINE |
---|
92 | static typename ExprT::size_type apply(ExprT const& e) |
---|
93 | { |
---|
94 | return e.size2(); |
---|
95 | } |
---|
96 | }; |
---|
97 | |
---|
98 | |
---|
99 | /** |
---|
100 | * \brief Auxiliary class for computing the size of the given dimension for |
---|
101 | * a container of the given category and with the given orientation.. |
---|
102 | * \tparam Dim The dimension number (starting from 1). |
---|
103 | * \tparam CategoryT The category type (e.g., vector_tag). |
---|
104 | * \tparam OrientationT The orientation category type (e.g., row_major_tag). |
---|
105 | */ |
---|
106 | template <typename TagT, typename CategoryT, typename OrientationT> |
---|
107 | struct size_by_tag_impl; |
---|
108 | |
---|
109 | |
---|
110 | /// \brief Specialization of \c size_by_tag_impl for computing the size of the |
---|
111 | /// major dimension of a row-major oriented matrix. |
---|
112 | template <> |
---|
113 | struct size_by_tag_impl<tag::major, matrix_tag, row_major_tag> |
---|
114 | { |
---|
115 | /** |
---|
116 | * \brief Compute the number of rows of the given matrix. |
---|
117 | * \tparam ExprT A matrix expression type. |
---|
118 | * \pre ExprT must be a model of MatrixExpression. |
---|
119 | */ |
---|
120 | template <typename ExprT> |
---|
121 | BOOST_UBLAS_INLINE |
---|
122 | static typename ExprT::size_type apply(ExprT const& e) |
---|
123 | { |
---|
124 | return e.size1(); |
---|
125 | } |
---|
126 | }; |
---|
127 | |
---|
128 | |
---|
129 | /// \brief Specialization of \c size_by_tag_impl for computing the size of the |
---|
130 | /// minor dimension of a row-major oriented matrix. |
---|
131 | template <> |
---|
132 | struct size_by_tag_impl<tag::minor, matrix_tag, row_major_tag> |
---|
133 | { |
---|
134 | /** |
---|
135 | * \brief Compute the number of columns of the given matrix. |
---|
136 | * \tparam ExprT A matrix expression type. |
---|
137 | * \pre ExprT must be a model of MatrixExpression. |
---|
138 | */ |
---|
139 | template <typename ExprT> |
---|
140 | BOOST_UBLAS_INLINE |
---|
141 | static typename ExprT::size_type apply(ExprT const& e) |
---|
142 | { |
---|
143 | return e.size2(); |
---|
144 | } |
---|
145 | }; |
---|
146 | |
---|
147 | |
---|
148 | /// \brief Specialization of \c size_by_tag_impl for computing the size of the |
---|
149 | /// leading dimension of a row-major oriented matrix. |
---|
150 | template <> |
---|
151 | struct size_by_tag_impl<tag::leading, matrix_tag, row_major_tag> |
---|
152 | { |
---|
153 | /** |
---|
154 | * \brief Compute the number of columns of the given matrix. |
---|
155 | * \tparam ExprT A matrix expression type. |
---|
156 | * \pre ExprT must be a model of MatrixExpression. |
---|
157 | */ |
---|
158 | template <typename ExprT> |
---|
159 | BOOST_UBLAS_INLINE |
---|
160 | static typename ExprT::size_type apply(ExprT const& e) |
---|
161 | { |
---|
162 | return e.size2(); |
---|
163 | } |
---|
164 | }; |
---|
165 | |
---|
166 | |
---|
167 | /// \brief Specialization of \c size_by_tag_impl for computing the size of the |
---|
168 | /// major dimension of a column-major oriented matrix. |
---|
169 | template <> |
---|
170 | struct size_by_tag_impl<tag::major, matrix_tag, column_major_tag> |
---|
171 | { |
---|
172 | /** |
---|
173 | * \brief Compute the number of columns of the given matrix. |
---|
174 | * \tparam ExprT A matrix expression type. |
---|
175 | * \pre ExprT must be a model of MatrixExpression. |
---|
176 | */ |
---|
177 | template <typename ExprT> |
---|
178 | BOOST_UBLAS_INLINE |
---|
179 | static typename ExprT::size_type apply(ExprT const& e) |
---|
180 | { |
---|
181 | return e.size2(); |
---|
182 | } |
---|
183 | }; |
---|
184 | |
---|
185 | |
---|
186 | /// \brief Specialization of \c size_by_tag_impl for computing the size of the |
---|
187 | /// minor dimension of a column-major oriented matrix. |
---|
188 | template <> |
---|
189 | struct size_by_tag_impl<tag::minor, matrix_tag, column_major_tag> |
---|
190 | { |
---|
191 | /** |
---|
192 | * \brief Compute the number of rows of the given matrix. |
---|
193 | * \tparam ExprT A matrix expression type. |
---|
194 | * \pre ExprT must be a model of MatrixExpression. |
---|
195 | */ |
---|
196 | template <typename ExprT> |
---|
197 | BOOST_UBLAS_INLINE |
---|
198 | static typename ExprT::size_type apply(ExprT const& e) |
---|
199 | { |
---|
200 | return e.size1(); |
---|
201 | } |
---|
202 | }; |
---|
203 | |
---|
204 | |
---|
205 | /// \brief Specialization of \c size_by_tag_impl for computing the size of the |
---|
206 | /// leading dimension of a column-major oriented matrix. |
---|
207 | template <> |
---|
208 | struct size_by_tag_impl<tag::leading, matrix_tag, column_major_tag> |
---|
209 | { |
---|
210 | /** |
---|
211 | * \brief Compute the number of rows of the given matrix. |
---|
212 | * \tparam ExprT A matrix expression type. |
---|
213 | * \pre ExprT must be a model of MatrixExpression. |
---|
214 | */ |
---|
215 | template <typename ExprT> |
---|
216 | BOOST_UBLAS_INLINE |
---|
217 | static typename ExprT::size_type apply(ExprT const& e) |
---|
218 | { |
---|
219 | return e.size1(); |
---|
220 | } |
---|
221 | }; |
---|
222 | |
---|
223 | } // Namespace detail |
---|
224 | |
---|
225 | |
---|
226 | /** |
---|
227 | * \brief Return the number of columns. |
---|
228 | * \tparam MatrixExprT A type which models the matrix expression concept. |
---|
229 | * \param m A matrix expression. |
---|
230 | * \return The number of columns. |
---|
231 | */ |
---|
232 | template <typename VectorExprT> |
---|
233 | BOOST_UBLAS_INLINE |
---|
234 | typename VectorExprT::size_type size(VectorExprT const& v) |
---|
235 | { |
---|
236 | return v.size(); |
---|
237 | } |
---|
238 | |
---|
239 | |
---|
240 | /** |
---|
241 | * \brief Return the size of the given dimension for the given expression. |
---|
242 | * \tparam Dim The dimension number (starting from 1). |
---|
243 | * \tparam ExprT An expression type. |
---|
244 | * \param e An expression. |
---|
245 | * \return The number of columns. |
---|
246 | * \return The size associated to the dimension \a Dim. |
---|
247 | */ |
---|
248 | template <std::size_t Dim, typename ExprT> |
---|
249 | BOOST_UBLAS_INLINE |
---|
250 | typename ExprT::size_type size(ExprT const& e) |
---|
251 | { |
---|
252 | return detail::size_by_dim_impl<Dim, typename ExprT::type_category>::apply(e); |
---|
253 | } |
---|
254 | |
---|
255 | |
---|
256 | /** |
---|
257 | * \brief Return the size of the given dimension tag for the given expression. |
---|
258 | * \tparam TagT The dimension tag type (e.g., tag::major). |
---|
259 | * \tparam ExprT An expression type. |
---|
260 | * \param e An expression. |
---|
261 | * \return The size associated to the dimension tag \a TagT. |
---|
262 | */ |
---|
263 | template <typename TagT, typename ExprT> |
---|
264 | BOOST_UBLAS_INLINE |
---|
265 | typename ExprT::size_type size(ExprT const& e) |
---|
266 | { |
---|
267 | return detail::size_by_tag_impl<TagT, typename ExprT::type_category, typename ExprT::orientation_category>::apply(e); |
---|
268 | } |
---|
269 | |
---|
270 | }}} // Namespace boost::numeric::ublas |
---|
271 | |
---|
272 | |
---|
273 | #endif // BOOST_NUMERIC_UBLAS_OPERATION_SIZE_HPP |
---|