1 /**
2  * -*- c++ -*-
3  *
4  * \file end.hpp
5  *
6  * \brief The \c end 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 
18 #ifndef BOOST_NUMERIC_UBLAS_OPERATION_END_HPP
19 #define BOOST_NUMERIC_UBLAS_OPERATION_END_HPP
20 
21 
22 #include <boost/numeric/ublas/expression_types.hpp>
23 #include <boost/numeric/ublas/fwd.hpp>
24 #include <boost/numeric/ublas/traits/const_iterator_type.hpp>
25 #include <boost/numeric/ublas/traits/iterator_type.hpp>
26 
27 
28 namespace boost { namespace numeric { namespace ublas {
29 
30     namespace detail {
31 
32         /**
33          * \brief Auxiliary class for implementing the \c end operation.
34          * \tparam CategoryT The expression category type (e.g., vector_tag).
35          * \tparam TagT The dimension type tag (e.g., tag::major).
36          * \tparam OrientationT The orientation category type (e.g., row_major_tag).
37          */
38         template <typename CategoryT, typename TagT=void, typename OrientationT=void>
39         struct end_impl;
40 
41 
42         /// \brief Specialization of \c end_impl for iterating vector expressions.
43         template <>
44         struct end_impl<vector_tag,void,void>
45         {
46             /**
47              * \brief Return an iterator to the last element of the given vector
48              *  expression.
49              * \tparam ExprT A model of VectorExpression type.
50              * \param e A vector expression.
51              * \return An iterator over the given vector expression.
52              */
53             template <typename ExprT>
applyboost::numeric::ublas::detail::end_impl54             static typename ExprT::iterator apply(ExprT& e)
55             {
56                 return e.end();
57             }
58 
59 
60             /**
61              * \brief Return a const iterator to the last element of the given vector
62              *  expression.
63              * \tparam ExprT A model of VectorExpression type.
64              * \param e A vector expression.
65              * \return A const iterator to the first element of the given vector
66              *  expression.
67              */
68             template <typename ExprT>
applyboost::numeric::ublas::detail::end_impl69             static typename ExprT::const_iterator apply(ExprT const& e)
70             {
71                 return e.end();
72             }
73         };
74 
75 
76         /// \brief Specialization of \c end_impl for iterating matrix expressions with a
77         ///  row-major orientation over the major dimension.
78         template <>
79         struct end_impl<matrix_tag,tag::major,row_major_tag>
80         {
81             /**
82              * \brief Return an iterator to the last element of the given row-major
83              *  matrix expression over the major dimension.
84              * \tparam ExprT A model of MatrixExpression type.
85              * \param e A matrix expression.
86              * \return An iterator over the major dimension of the given matrix
87              *  expression.
88              */
89             template <typename ExprT>
applyboost::numeric::ublas::detail::end_impl90             static typename ExprT::iterator1 apply(ExprT& e)
91             {
92                 return e.end1();
93             }
94 
95 
96             /**
97              * \brief Return a const iterator to the last element of the given row-major
98              *  matrix expression over the major dimension.
99              * \tparam ExprT A model of MatrixExpression type.
100              * \param e A matrix expression.
101              * \return A const iterator over the major dimension of the given matrix
102              *  expression.
103              */
104             template <typename ExprT>
applyboost::numeric::ublas::detail::end_impl105             static typename ExprT::const_iterator1 apply(ExprT const& e)
106             {
107                 return e.end1();
108             }
109         };
110 
111 
112         /// \brief Specialization of \c end_impl for iterating matrix expressions with a
113         ///  column-major orientation over the major dimension.
114         template <>
115         struct end_impl<matrix_tag,tag::major,column_major_tag>
116         {
117             /**
118              * \brief Return an iterator to the last element of the given column-major
119              *  matrix expression over the major dimension.
120              * \tparam ExprT A model of MatrixExpression type.
121              * \param e A matrix expression.
122              * \return An iterator over the major dimension of the given matrix
123              *  expression.
124              */
125             template <typename ExprT>
applyboost::numeric::ublas::detail::end_impl126             static typename ExprT::iterator2 apply(ExprT& e)
127             {
128                 return e.end2();
129             }
130 
131 
132             /**
133              * \brief Return a const iterator to the last element of the given
134              *  column-major matrix expression over the major dimension.
135              * \tparam ExprT A model of MatrixExpression type.
136              * \param e A matrix expression.
137              * \return A const iterator over the major dimension of the given matrix
138              *  expression.
139              */
140             template <typename ExprT>
applyboost::numeric::ublas::detail::end_impl141             static typename ExprT::const_iterator2 apply(ExprT const& e)
142             {
143                 return e.end2();
144             }
145         };
146 
147 
148         /// \brief Specialization of \c end_impl for iterating matrix expressions with a
149         ///  row-major orientation over the minor dimension.
150         template <>
151         struct end_impl<matrix_tag,tag::minor,row_major_tag>
152         {
153             /**
154              * \brief Return an iterator to the last element of the given row-major
155              *  matrix expression over the minor dimension.
156              * \tparam ExprT A model of MatrixExpression type.
157              * \param e A matrix expression.
158              * \return An iterator over the minor dimension of the given matrix
159              *  expression.
160              */
161             template <typename ExprT>
applyboost::numeric::ublas::detail::end_impl162             static typename ExprT::iterator2 apply(ExprT& e)
163             {
164                 return e.end2();
165             }
166 
167 
168             /**
169              * \brief Return a const iterator to the last element of the given
170              *  row-minor matrix expression over the major dimension.
171              * \tparam ExprT A model of MatrixExpression type.
172              * \param e A matrix expression.
173              * \return A const iterator over the minor dimension of the given matrix
174              *  expression.
175              */
176             template <typename ExprT>
applyboost::numeric::ublas::detail::end_impl177             static typename ExprT::const_iterator2 apply(ExprT const& e)
178             {
179                 return e.end2();
180             }
181         };
182 
183 
184         /// \brief Specialization of \c end_impl for iterating matrix expressions with a
185         ///  column-major orientation over the minor dimension.
186         template <>
187         struct end_impl<matrix_tag,tag::minor,column_major_tag>
188         {
189             /**
190              * \brief Return an iterator to the last element of the given column-major
191              *  matrix expression over the minor dimension.
192              * \tparam ExprT A model of MatrixExpression type.
193              * \param e A matrix expression.
194              * \return An iterator over the minor dimension of the given matrix
195              *  expression.
196              */
197             template <typename ExprT>
applyboost::numeric::ublas::detail::end_impl198             static typename ExprT::iterator1 apply(ExprT& e)
199             {
200                 return e.end1();
201             }
202 
203 
204             /**
205              * \brief Return a const iterator to the last element of the given
206              *  column-minor matrix expression over the major dimension.
207              * \tparam ExprT A model of MatrixExpression type.
208              * \param e A matrix expression.
209              * \return A const iterator over the minor dimension of the given matrix
210              *  expression.
211              */
212             template <typename ExprT>
applyboost::numeric::ublas::detail::end_impl213             static typename ExprT::const_iterator1 apply(ExprT const& e)
214             {
215                 return e.end1();
216             }
217         };
218 
219     } // Namespace detail
220 
221 
222     /**
223      * \brief An iterator to the last element of the given vector expression.
224      * \tparam ExprT A model of VectorExpression type.
225      * \param e A vector expression.
226      * \return An iterator to the last element of the given vector expression.
227      */
228     template <typename ExprT>
229     BOOST_UBLAS_INLINE
end(vector_expression<ExprT> & e)230     typename ExprT::iterator end(vector_expression<ExprT>& e)
231     {
232         return detail::end_impl<typename ExprT::type_category>::apply(e());
233     }
234 
235 
236     /**
237      * \brief A const iterator to the last element of the given vector expression.
238      * \tparam ExprT A model of VectorExpression type.
239      * \param e A vector expression.
240      * \return A const iterator to the last element of the given vector expression.
241      */
242     template <typename ExprT>
243     BOOST_UBLAS_INLINE
end(vector_expression<ExprT> const & e)244     typename ExprT::const_iterator end(vector_expression<ExprT> const& e)
245     {
246         return detail::end_impl<typename ExprT::type_category>::apply(e());
247     }
248 
249 
250     /**
251      * \brief An iterator to the last element of the given matrix expression
252      *  according to its orientation.
253      * \tparam DimTagT A dimension tag type (e.g., tag::major).
254      * \tparam ExprT A model of MatrixExpression type.
255      * \param e A matrix expression.
256      * \return An iterator to the last element of the given matrix expression
257      *  according to its orientation.
258      */
259     template <typename TagT, typename ExprT>
260     BOOST_UBLAS_INLINE
end(matrix_expression<ExprT> & e)261     typename iterator_type<ExprT,TagT>::type end(matrix_expression<ExprT>& e)
262     {
263         return detail::end_impl<typename ExprT::type_category, TagT, typename ExprT::orientation_category>::apply(e());
264     }
265 
266 
267     /**
268      * \brief A const iterator to the last element of the given matrix expression
269      *  according to its orientation.
270      * \tparam TagT A dimension tag type (e.g., tag::major).
271      * \tparam ExprT A model of MatrixExpression type.
272      * \param e A matrix expression.
273      * \return A const iterator to the last element of the given matrix expression
274      *  according to its orientation.
275      */
276     template <typename TagT, typename ExprT>
277     BOOST_UBLAS_INLINE
end(matrix_expression<ExprT> const & e)278     typename const_iterator_type<ExprT,TagT>::type end(matrix_expression<ExprT> const& e)
279     {
280         return detail::end_impl<typename ExprT::type_category, TagT, typename ExprT::orientation_category>::apply(e());
281     }
282 
283 
284     /**
285      * \brief An iterator to the last element over the dual dimension of the given
286      *  iterator.
287      * \tparam IteratorT A model of Iterator type.
288      * \param it An iterator.
289      * \return An iterator to the last element over the dual dimension of the given
290      *  iterator.
291      */
292     template <typename IteratorT>
293     BOOST_UBLAS_INLINE
end(IteratorT & it)294     typename IteratorT::dual_iterator_type end(IteratorT& it)
295     {
296         return it.end();
297     }
298 
299 
300     /**
301      * \brief A const iterator to the last element over the dual dimension of the
302      *  given iterator.
303      * \tparam IteratorT A model of Iterator type.
304      * \param it An iterator.
305      * \return A const iterator to the last element over the dual dimension of the
306      *  given iterator.
307      */
308     template <typename IteratorT>
309     BOOST_UBLAS_INLINE
end(IteratorT const & it)310     typename IteratorT::dual_iterator_type end(IteratorT const& it)
311     {
312         return it.end();
313     }
314 
315 }}} // Namespace boost::numeric::ublas
316 
317 
318 #endif // BOOST_NUMERIC_UBLAS_OPERATION_END_HPP
319