iterator.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 1,437 行 · 第 1/4 页

HPP
1,437
字号
////  Copyright (c) 2000-2002//  Joerg Walter, Mathias Koch////  Distributed under the Boost Software License, Version 1.0. (See//  accompanying file LICENSE_1_0.txt or copy at//  http://www.boost.org/LICENSE_1_0.txt)////  The authors gratefully acknowledge the support of//  GeNeSys mbH & Co. KG in producing this work.//#ifndef _BOOST_UBLAS_ITERATOR_#define _BOOST_UBLAS_ITERATOR_#include <boost/numeric/ublas/exception.hpp>#include <iterator>namespace boost { namespace numeric { namespace ublas {  /** \brief Base class of all proxy classes that contain   *       a (redirectable) reference to an immutable object.   *   *       \param C the type of the container referred to   */    template<class C>    class container_const_reference:        private nonassignable {    public:        typedef C container_type;        BOOST_UBLAS_INLINE        container_const_reference ():            c_ (0) {}        BOOST_UBLAS_INLINE        container_const_reference (const container_type &c):            c_ (&c) {}        BOOST_UBLAS_INLINE        const container_type &operator () () const {            return *c_;        }        BOOST_UBLAS_INLINE        container_const_reference &assign (const container_type *c) {            c_ = c;            return *this;        }                // Closure comparison        BOOST_UBLAS_INLINE        bool same_closure (const container_const_reference &cr) const {            return c_ == cr.c_;        }    private:        const container_type *c_;    };  /** \brief Base class of all proxy classes that contain   *         a (redirectable) reference to a mutable object.   *   * \param C the type of the container referred to   */    template<class C>    class container_reference:        private nonassignable {    public:        typedef C container_type;        BOOST_UBLAS_INLINE        container_reference ():            c_ (0) {}        BOOST_UBLAS_INLINE        container_reference (container_type &c):            c_ (&c) {}        BOOST_UBLAS_INLINE        container_type &operator () () const {           return *c_;        }        BOOST_UBLAS_INLINE        container_reference &assign (container_type *c) {            c_ = c;            return *this;        }        // Closure comparison        BOOST_UBLAS_INLINE        bool same_closure (const container_reference &cr) const {            return c_ == cr.c_;        }    private:        container_type *c_;    };  /** \brief Base class of all forward iterators.   *    *  \param IC the iterator category   *  \param I the derived iterator type   *  \param T the value type   *    * The forward iterator can only proceed in one direction   * via the post increment operator.   */    template<class IC, class I, class T>    struct forward_iterator_base:        public std::iterator<IC, T> {        typedef I derived_iterator_type;        typedef T derived_value_type;        // Arithmetic        BOOST_UBLAS_INLINE        derived_iterator_type operator ++ (int) {            derived_iterator_type &d (*static_cast<const derived_iterator_type *> (this));            derived_iterator_type tmp (d);            ++ d;            return tmp;        }        BOOST_UBLAS_INLINE        friend derived_iterator_type operator ++ (derived_iterator_type &d, int) {            derived_iterator_type tmp (d);            ++ d;            return tmp;        }        // Comparison        BOOST_UBLAS_INLINE        bool operator != (const derived_iterator_type &it) const {            const derived_iterator_type *d = static_cast<const derived_iterator_type *> (this);            return ! (*d == it);        }    };  /** \brief Base class of all bidirectional iterators.   *   * \param IC the iterator category   * \param I the derived iterator type   * \param T the value type   *   * The bidirectional iterator can proceed in both directions   * via the post increment and post decrement operator.   */    template<class IC, class I, class T>    struct bidirectional_iterator_base:        public std::iterator<IC, T> {        typedef I derived_iterator_type;        typedef T derived_value_type;        // Arithmetic        BOOST_UBLAS_INLINE        derived_iterator_type operator ++ (int) {            derived_iterator_type &d (*static_cast<const derived_iterator_type *> (this));            derived_iterator_type tmp (d);            ++ d;            return tmp;        }        BOOST_UBLAS_INLINE        friend derived_iterator_type operator ++ (derived_iterator_type &d, int) {            derived_iterator_type tmp (d);            ++ d;            return tmp;        }        BOOST_UBLAS_INLINE        derived_iterator_type operator -- (int) {            derived_iterator_type &d (*static_cast<const derived_iterator_type *> (this));            derived_iterator_type tmp (d);            -- d;            return tmp;        }        BOOST_UBLAS_INLINE        friend derived_iterator_type operator -- (derived_iterator_type &d, int) {            derived_iterator_type tmp (d);            -- d;            return tmp;        }        // Comparison        BOOST_UBLAS_INLINE        bool operator != (const derived_iterator_type &it) const {            const derived_iterator_type *d = static_cast<const derived_iterator_type *> (this);            return ! (*d == it);        }    };  /** \brief Base class of all random access iterators.   *   * \param IC the iterator category   * \param I the derived iterator type   * \param T the value type   * \param D the difference type, default: std::ptrdiff_t   *   * The random access iterator can proceed in both directions   * via the post increment/decrement operator or in larger steps   * via the +, - and +=, -= operators. The random access iterator   * is LessThan Comparable.   */    template<class IC, class I, class T, class D = std::ptrdiff_t>    // ISSUE the default for D seems rather dangerous as it can easily be (silently) incorrect    struct random_access_iterator_base:        public std::iterator<IC, T> {        typedef I derived_iterator_type;        typedef T derived_value_type;        typedef D derived_difference_type;        /* FIXME Need to explicitly pass derived_reference_type as otherwise I undefined type or forward declared        typedef typename derived_iterator_type::reference derived_reference_type;        // Indexed element        BOOST_UBLAS_INLINE        derived_reference_type operator [] (derived_difference_type n) {            return *(*this + n);        }        */        // Arithmetic        BOOST_UBLAS_INLINE        derived_iterator_type operator ++ (int) {            derived_iterator_type &d (*static_cast<derived_iterator_type *> (this));            derived_iterator_type tmp (d);            ++ d;            return tmp;        }        BOOST_UBLAS_INLINE        friend derived_iterator_type operator ++ (derived_iterator_type &d, int) {            derived_iterator_type tmp (d);            ++ d;            return tmp;        }        BOOST_UBLAS_INLINE        derived_iterator_type operator -- (int) {            derived_iterator_type &d (*static_cast<derived_iterator_type *> (this));            derived_iterator_type tmp (d);            -- d;            return tmp;        }        BOOST_UBLAS_INLINE        friend derived_iterator_type operator -- (derived_iterator_type &d, int) {            derived_iterator_type tmp (d);            -- d;            return tmp;        }        BOOST_UBLAS_INLINE        derived_iterator_type operator + (derived_difference_type n) const {            derived_iterator_type tmp (*static_cast<const derived_iterator_type *> (this));            return tmp += n;        }        BOOST_UBLAS_INLINE        friend derived_iterator_type operator + (const derived_iterator_type &d, derived_difference_type n) {            derived_iterator_type tmp (d);            return tmp += n;        }        BOOST_UBLAS_INLINE        friend derived_iterator_type operator + (derived_difference_type n, const derived_iterator_type &d) {            derived_iterator_type tmp (d);            return tmp += n;        }        BOOST_UBLAS_INLINE        derived_iterator_type operator - (derived_difference_type n) const {            derived_iterator_type tmp (*static_cast<const derived_iterator_type *> (this));            return tmp -= n;        }        BOOST_UBLAS_INLINE        friend derived_iterator_type operator - (const derived_iterator_type &d, derived_difference_type n) {            derived_iterator_type tmp (d);            return tmp -= n;        }        // Comparison        BOOST_UBLAS_INLINE        bool operator != (const derived_iterator_type &it) const {            const derived_iterator_type *d = static_cast<const derived_iterator_type *> (this);            return ! (*d == it);        }        BOOST_UBLAS_INLINE        bool operator <= (const derived_iterator_type &it) const {            const derived_iterator_type *d = static_cast<const derived_iterator_type *> (this);            return ! (it < *d);        }        BOOST_UBLAS_INLINE        bool operator >= (const derived_iterator_type &it) const {            const derived_iterator_type *d = static_cast<const derived_iterator_type *> (this);            return ! (*d < it);        }        BOOST_UBLAS_INLINE        bool operator > (const derived_iterator_type &it) const {            const derived_iterator_type *d = static_cast<const derived_iterator_type *> (this);            return it < *d;        }    };  /** \brief Base class of all reverse iterators. (non-MSVC version)   *   * \param I the derived iterator type   * \param T the value type   * \param R the reference type   *   * The reverse iterator implements a bidirectional iterator   * reversing the elements of the underlying iterator. It   * implements most operators of a random access iterator.   *   * uBLAS extension: it.index()   */    // Renamed this class from reverse_iterator to get    // typedef reverse_iterator<...> reverse_iterator    // working. Thanks to Gabriel Dos Reis for explaining this.    template <class I>    class reverse_iterator_base:        public std::reverse_iterator<I> {    public:        typedef typename I::container_type container_type;        typedef typename container_type::size_type size_type;        typedef typename I::difference_type difference_type;        typedef I iterator_type;        // Construction and destruction        BOOST_UBLAS_INLINE        reverse_iterator_base ():            std::reverse_iterator<iterator_type> () {}        BOOST_UBLAS_INLINE        reverse_iterator_base (const iterator_type &it):            std::reverse_iterator<iterator_type> (it) {}        // Arithmetic        BOOST_UBLAS_INLINE        reverse_iterator_base &operator ++ () {            return *this = -- this->base ();        }        BOOST_UBLAS_INLINE        reverse_iterator_base operator ++ (int) {            reverse_iterator_base tmp (*this);            *this = -- this->base ();            return tmp;        }        BOOST_UBLAS_INLINE        reverse_iterator_base &operator -- () {            return *this = ++ this->base ();        }        BOOST_UBLAS_INLINE        reverse_iterator_base operator -- (int) {            reverse_iterator_base tmp (*this);            *this = ++ this->base ();            return tmp;        }        BOOST_UBLAS_INLINE        reverse_iterator_base &operator += (difference_type n) {            return *this = this->base () - n;        }        BOOST_UBLAS_INLINE        reverse_iterator_base &operator -= (difference_type n) {            return *this = this->base () + n;        }        BOOST_UBLAS_INLINE        friend reverse_iterator_base operator + (const reverse_iterator_base &it, difference_type n) {            reverse_iterator_base tmp (it);            return tmp += n;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?