end.hpp 11 KB

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