This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project. See the libstdc++ home page for more information.


[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index] [Subject Index] [Author Index] [Thread Index]

Iterator class for vector and basic_string



I wrote an iterator class for std::vector<> and std::basic_string<>.

If You mean it's OK I will make a patch.


Ryszard Kabatek
--
Martin-Luther University Halle-Wittenberg
Department of Physical Chemistry
Geusaer Str. 88, 06217 Merseburg, Germany  
Tel. +49 3461 46 2487 Fax. +49 3461 46 2129

/*
 * Written by Ryszard Kabatek
 */

# ifndef _ITERATOR_FOR_VECTOR_AND_BASIC_STRING_H
# define _ITERATOR_FOR_VECTOR_AND_BASIC_STRING_H

/*******************************************************************
template<typename _Tp, typename _Dist = ptrdiff_t,
         typename _Ptr = _Tp*, typename _Ref = _Tp&>
class _Iterator;

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
_Iterator<_Tp, _Dist, _Ptr, _Ref>::iterator_category
iterator_category(const _Iterator<_Tp, _Dist, _Ptr, _Ref>&);

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
_Iterator<_Tp, _Dist, _Ptr, _Ref>::value_type*
value_type(const _Iterator<_Tp, _Dist, _Ptr, _Ref>&);
 
template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
_Iterator<_Tp, _Dist, _Ptr, _Ref>::difference_type*
distance_type(const _Iterator<_Tp, _Dist, _Ptr, _Ref>&);
*******************************************************************/

# include <iterator>

template<typename _Tp, typename _Dist = ptrdiff_t,
         typename _Ptr = _Tp*, typename _Ref = _Tp&>
class _Iterator {
  public:
    typedef random_access_iterator_tag                   iterator_category;
    typedef _Tp                                           value_type;
    typedef _Dist                                         difference_type;
    typedef _Ptr                                          pointer;
    typedef _Ref                                          reference;

    typedef _Iterator<_Tp, _Dist, _Tp*, _Tp&>             iterator;
    typedef _Iterator<_Tp, _Dist, const _Tp*, const _Tp&> const_iterator;
    typedef _Iterator<_Tp, _Dist, _Ptr, _Ref>             _Self;

  public:
    _Iterator();

    explicit _Iterator(pointer __p);

    // template copy constructor for conversion
    // iterator <-- const_iterator.
    template<typename _Up>
    _Iterator(const _Iterator<_Tp, _Dist, _Up*, _Up&>& __rhs);

    reference operator*() const;

    pointer   operator->() const;

    difference_type operator-(const _Self& __rhs) const;

    _Self& operator++();

    _Self  operator++(int);

    _Self& operator--();

    _Self  operator--(int);

    _Self& operator+=(difference_type __n);

    _Self  operator+(difference_type __n) const;

    _Self& operator-=(difference_type __n);

    _Self  operator-(difference_type __n) const;

    reference operator[](difference_type __i) const;

    // The comparision operators are implemented as member templates
    // for comparision between iterator and const_iterator.
    template<typename _Up>
    bool operator==(const _Iterator<_Tp, _Dist, _Up*, _Up&>& __rhs) const;

    template<typename _Up>
    bool operator!=(const _Iterator<_Tp, _Dist, _Up*, _Up&>& __rhs) const;

    template<typename _Up>
    bool operator<(const _Iterator<_Tp, _Dist, _Up*, _Up&>& __rhs) const;

    template<typename _Up>
    bool operator<=(const _Iterator<_Tp, _Dist, _Up*, _Up&>& __rhs) const;

    template<typename _Up>
    bool operator>(const _Iterator<_Tp, _Dist, _Up*, _Up&>& __rhs) const;

    template<typename _Up>
    bool operator>=(const _Iterator<_Tp, _Dist, _Up*, _Up&>& __rhs) const;

  private:
    pointer _M_data;
};


///////////////////
// inline functions
///////////////////

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline
_Iterator<_Tp, _Dist, _Ptr, _Ref>::_Iterator()
: _M_data(0) 
{
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline
_Iterator<_Tp, _Dist, _Ptr, _Ref>::_Iterator(pointer __p) 
: _M_data(__p) 
{
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
template<typename _Up>
inline
_Iterator<_Tp, _Dist, _Ptr, _Ref>::
_Iterator(const _Iterator<_Tp, _Dist, _Up*, _Up&>& __rhs) 
: _M_data(__rhs.operator->()) 
{
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline _Iterator<_Tp, _Dist, _Ptr, _Ref>::reference
_Iterator<_Tp, _Dist, _Ptr, _Ref>::operator*() const 
{
  return *_M_data;
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline _Iterator<_Tp, _Dist, _Ptr, _Ref>::pointer
_Iterator<_Tp, _Dist, _Ptr, _Ref>::operator->() const 
{
  return _M_data;
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline _Iterator<_Tp, _Dist, _Ptr, _Ref>::difference_type 
_Iterator<_Tp, _Dist, _Ptr, _Ref>::operator-(const _Iterator& __rhs) const 
{
  return _M_data - __rhs._M_data;
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline _Iterator<_Tp, _Dist, _Ptr, _Ref>&
_Iterator<_Tp, _Dist, _Ptr, _Ref>::operator++() 
{
  ++_M_data; 
  return *this;
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline _Iterator<_Tp, _Dist, _Ptr, _Ref>
_Iterator<_Tp, _Dist, _Ptr, _Ref>::operator++(int) 
{
  return _Iterator(_M_data++);
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline _Iterator<_Tp, _Dist, _Ptr, _Ref>&
_Iterator<_Tp, _Dist, _Ptr, _Ref>::operator--() 
{
  --_M_data; 
  return *this;
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline _Iterator<_Tp, _Dist, _Ptr, _Ref>
_Iterator<_Tp, _Dist, _Ptr, _Ref>::operator--(int) 
{
  return _Iterator(_M_data--);
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline _Iterator<_Tp, _Dist, _Ptr, _Ref>&
_Iterator<_Tp, _Dist, _Ptr, _Ref>::operator+=(difference_type __n) 
{
  _M_data += __n; 
  return *this;
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline _Iterator<_Tp, _Dist, _Ptr, _Ref>
_Iterator<_Tp, _Dist, _Ptr, _Ref>::operator+(difference_type __n) const 
{
  return _Iterator(_M_data + __n);
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline _Iterator<_Tp, _Dist, _Ptr, _Ref>&
_Iterator<_Tp, _Dist, _Ptr, _Ref>::operator-=(difference_type __n) 
{
  _M_data -= __n; 
  return *this;
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline _Iterator<_Tp, _Dist, _Ptr, _Ref>
_Iterator<_Tp, _Dist, _Ptr, _Ref>::operator-(difference_type __n) const 
{
  return _Iterator(_M_data - __n);
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline _Iterator<_Tp, _Dist, _Ptr, _Ref>::reference
_Iterator<_Tp, _Dist, _Ptr, _Ref>::operator[](difference_type __i) const 
{
  return *(_M_data + __i);
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
template<typename _Up>
inline bool
_Iterator<_Tp, _Dist, _Ptr, _Ref>::
operator==(const _Iterator<_Tp, _Dist, _Up*, _Up&>& __rhs) const 
{
  return _M_data == __rhs.operator->();
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
template<typename _Up>
inline bool
_Iterator<_Tp, _Dist, _Ptr, _Ref>::
operator!=(const _Iterator<_Tp, _Dist, _Up*, _Up&>& __rhs) const 
{
  return _M_data != __rhs.operator->();
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
template<typename _Up>
inline bool
_Iterator<_Tp, _Dist, _Ptr, _Ref>::
operator<(const _Iterator<_Tp, _Dist, _Up*, _Up&>& __rhs) const 
{
  return _M_data < __rhs.operator->();
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
template<typename _Up>
inline bool
_Iterator<_Tp, _Dist, _Ptr, _Ref>::
operator>(const _Iterator<_Tp, _Dist, _Up*, _Up&>& __rhs) const 
{
  return _M_data > __rhs.operator->();
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
template<typename _Up>
inline bool
_Iterator<_Tp, _Dist, _Ptr, _Ref>::
operator<=(const _Iterator<_Tp, _Dist, _Up*, _Up&>& __rhs) const 
{
  return _M_data <= __rhs.operator->();
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
template<typename _Up>
inline bool
_Iterator<_Tp, _Dist, _Ptr, _Ref>::
operator>=(const _Iterator<_Tp, _Dist, _Up*, _Up&>& __rhs) const 
{
  return _M_data >= __rhs.operator->();
}


template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline _Iterator<_Tp, _Dist, _Ptr, _Ref>::iterator_category
iterator_category(const _Iterator<_Tp, _Dist, _Ptr, _Ref>&)
{
  return _Iterator<_Tp, _Dist, _Ptr, _Ref>::iterator_category();
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline _Iterator<_Tp, _Dist, _Ptr, _Ref>::value_type*
value_type(const _Iterator<_Tp, _Dist, _Ptr, _Ref>&)
{
  return 0;
}

template<typename _Tp, typename _Dist, typename _Ptr, typename _Ref>
inline _Iterator<_Tp, _Dist, _Ptr, _Ref>::difference_type*
distance_type(const _Iterator<_Tp, _Dist, _Ptr, _Ref>&)
{
  return 0;
}

# endif // _ITERATOR_FOR_VECTOR_AND_BASIC_STRING_H