1 /** 2 * -*- c++ -*- 3 * 4 * \file begin.hpp 5 * 6 * \brief The \c begin 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_BEGIN_HPP 18 #define BOOST_NUMERIC_UBLAS_OPERATION_BEGIN_HPP 19 20 21 #include <boost/numeric/ublas/expression_types.hpp> 22 #include <boost/numeric/ublas/fwd.hpp> 23 #include <boost/numeric/ublas/traits/const_iterator_type.hpp> 24 #include <boost/numeric/ublas/traits/iterator_type.hpp> 25 26 27 namespace boost { namespace numeric { namespace ublas { 28 29 namespace detail { 30 31 /** 32 * \brief Auxiliary class for implementing the \c begin operation. 33 * \tparam CategoryT The expression category type (e.g., vector_tag). 34 * \tparam TagT The dimension type tag (e.g., tag::major). 35 * \tparam OrientationT The orientation category type (e.g., row_major_tag). 36 */ 37 template <typename CategoryT, typename TagT=void, typename OrientationT=void> 38 struct begin_impl; 39 40 41 /// \brief Specialization of \c begin_impl for iterating vector expressions. 42 template <> 43 struct begin_impl<vector_tag,void,void> 44 { 45 /** 46 * \brief Return an iterator to the first element of the given vector 47 * expression. 48 * \tparam ExprT A model of VectorExpression type. 49 * \param e A vector expression. 50 * \return An iterator over the given vector expression. 51 */ 52 template <typename ExprT> applyboost::numeric::ublas::detail::begin_impl53 static typename ExprT::iterator apply(ExprT& e) 54 { 55 return e.begin(); 56 } 57 58 59 /** 60 * \brief Return a const iterator to the first element of the given vector 61 * expression. 62 * \tparam ExprT A model of VectorExpression type. 63 * \param e A vector expression. 64 * \return A const iterator to the first element of the given vector 65 * expression. 66 */ 67 template <typename ExprT> applyboost::numeric::ublas::detail::begin_impl68 static typename ExprT::const_iterator apply(ExprT const& e) 69 { 70 return e.begin(); 71 } 72 }; 73 74 75 /// \brief Specialization of \c begin_impl for iterating matrix expressions with 76 /// a row-major orientation over the major dimension. 77 template <> 78 struct begin_impl<matrix_tag,tag::major,row_major_tag> 79 { 80 /** 81 * \brief Return an iterator to the first 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 An iterator over the major dimension of the given matrix 86 * expression. 87 */ 88 template <typename ExprT> applyboost::numeric::ublas::detail::begin_impl89 static typename ExprT::iterator1 apply(ExprT& e) 90 { 91 return e.begin1(); 92 } 93 94 95 /** 96 * \brief Return a const iterator to the first element of the given 97 * row-major matrix expression over the major dimension. 98 * \tparam ExprT A model of MatrixExpression type. 99 * \param e A matrix expression. 100 * \return A const iterator over the major dimension of the given matrix 101 * expression. 102 */ 103 template <typename ExprT> applyboost::numeric::ublas::detail::begin_impl104 static typename ExprT::const_iterator1 apply(ExprT const& e) 105 { 106 return e.begin1(); 107 } 108 }; 109 110 111 /// \brief Specialization of \c begin_impl for iterating matrix expressions with 112 /// a column-major orientation over the major dimension. 113 template <> 114 struct begin_impl<matrix_tag,tag::major,column_major_tag> 115 { 116 /** 117 * \brief Return an iterator to the first element of the given column-major 118 * matrix expression over the major dimension. 119 * \tparam ExprT A model of MatrixExpression type. 120 * \param e A matrix expression. 121 * \return An iterator over the major dimension of the given matrix 122 * expression. 123 */ 124 template <typename ExprT> applyboost::numeric::ublas::detail::begin_impl125 static typename ExprT::iterator2 apply(ExprT& e) 126 { 127 return e.begin2(); 128 } 129 130 131 /** 132 * \brief Return a const iterator to the first element of the given 133 * column-major matrix expression over the major dimension. 134 * \tparam ExprT A model of MatrixExpression type. 135 * \param e A matrix expression. 136 * \return A const iterator over the major dimension of the given matrix 137 * expression. 138 */ 139 template <typename ExprT> applyboost::numeric::ublas::detail::begin_impl140 static typename ExprT::const_iterator2 apply(ExprT const& e) 141 { 142 return e.begin2(); 143 } 144 }; 145 146 147 /// \brief Specialization of \c begin_impl for iterating matrix expressions with 148 /// a row-major orientation over the minor dimension. 149 template <> 150 struct begin_impl<matrix_tag,tag::minor,row_major_tag> 151 { 152 /** 153 * \brief Return an iterator to the first element of the given row-major 154 * matrix expression over the minor dimension. 155 * \tparam ExprT A model of MatrixExpression type. 156 * \param e A matrix expression. 157 * \return An iterator over the minor dimension of the given matrix 158 * expression. 159 */ 160 template <typename ExprT> applyboost::numeric::ublas::detail::begin_impl161 static typename ExprT::iterator2 apply(ExprT& e) 162 { 163 return e.begin2(); 164 } 165 166 167 /** 168 * \brief Return a const iterator to the first element of the given 169 * row-major matrix expression over the minor dimension. 170 * \tparam ExprT A model of MatrixExpression type. 171 * \param e A matrix expression. 172 * \return A const iterator over the minor dimension of the given matrix 173 * expression. 174 */ 175 template <typename ExprT> applyboost::numeric::ublas::detail::begin_impl176 static typename ExprT::const_iterator2 apply(ExprT const& e) 177 { 178 return e.begin2(); 179 } 180 }; 181 182 183 184 /// \brief Specialization of \c begin_impl for iterating matrix expressions with 185 /// a column-major orientation over the minor dimension. 186 template <> 187 struct begin_impl<matrix_tag,tag::minor,column_major_tag> 188 { 189 /** 190 * \brief Return an iterator to the first 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::begin_impl198 static typename ExprT::iterator1 apply(ExprT& e) 199 { 200 return e.begin1(); 201 } 202 203 204 /** 205 * \brief Return a const iterator to the first element of the given 206 * column-major matrix expression over the minor 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::begin_impl213 static typename ExprT::const_iterator1 apply(ExprT const& e) 214 { 215 return e.begin1(); 216 } 217 }; 218 219 } // Namespace detail 220 221 222 /** 223 * \brief An iterator to the first 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 first element of the given vector expression. 227 */ 228 template <typename ExprT> 229 BOOST_UBLAS_INLINE begin(vector_expression<ExprT> & e)230 typename ExprT::iterator begin(vector_expression<ExprT>& e) 231 { 232 return detail::begin_impl<typename ExprT::type_category>::apply(e()); 233 } 234 235 236 /** 237 * \brief A const iterator to the first 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 first element of the given vector expression. 241 */ 242 template <typename ExprT> 243 BOOST_UBLAS_INLINE begin(vector_expression<ExprT> const & e)244 typename ExprT::const_iterator begin(vector_expression<ExprT> const& e) 245 { 246 return detail::begin_impl<typename ExprT::type_category>::apply(e()); 247 } 248 249 250 /** 251 * \brief An iterator to the first 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 first element of the given matrix expression 257 * according to its orientation. 258 */ 259 template <typename TagT, typename ExprT> 260 BOOST_UBLAS_INLINE begin(matrix_expression<ExprT> & e)261 typename iterator_type<ExprT,TagT>::type begin(matrix_expression<ExprT>& e) 262 { 263 return detail::begin_impl<typename ExprT::type_category, TagT, typename ExprT::orientation_category>::apply(e()); 264 } 265 266 267 /** 268 * \brief A const iterator to the first 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 first element of the given matrix expression 274 * according to its orientation. 275 */ 276 template <typename TagT, typename ExprT> 277 BOOST_UBLAS_INLINE begin(matrix_expression<ExprT> const & e)278 typename const_iterator_type<ExprT,TagT>::type begin(matrix_expression<ExprT> const& e) 279 { 280 return detail::begin_impl<typename ExprT::type_category, TagT, typename ExprT::orientation_category>::apply(e()); 281 } 282 283 284 /** 285 * \brief An iterator to the first 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 first element over the dual dimension of the given 290 * iterator. 291 */ 292 template <typename IteratorT> 293 BOOST_UBLAS_INLINE begin(IteratorT & it)294 typename IteratorT::dual_iterator_type begin(IteratorT& it) 295 { 296 return it.begin(); 297 } 298 299 300 /** 301 * \brief A const iterator to the first 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 first element over the dual dimension of the 306 * given iterator. 307 */ 308 template <typename IteratorT> 309 BOOST_UBLAS_INLINE begin(IteratorT const & it)310 typename IteratorT::dual_iterator_type begin(IteratorT const& it) 311 { 312 return it.begin(); 313 } 314 315 }}} // Namespace boost::numeric::ublas 316 317 318 #endif // BOOST_NUMERIC_UBLAS_OPERATION_BEGIN_HPP 319