Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Persistent Vector Iterators


January 1999/Persistent Vector Iterators/Listing 1

Listing 1: Definition of pvector template

//////////////////////////////////////////////////////
//  Copyright (c) 1998 Radoslav Getov               //
//                                                  //
//  Permission to use, copy, modify and distribute  //
//  this software is granted without fee, provided  //
//  that the above copyright notice and this        //
//  permission notice appear in the modified source //
//  and the source files that #include it.          //
//                                                  //
////////////////////////////////////////////////////// 
#ifndef PVECTOR_H
#define PVECTOR_H

#include <vector>

// -------------- template pvector<> -----------
template <class T, class A = std::allocator<T> >
class pvector : public std::vector <T, A> 
{
typedef std::vector<T> _BaseType;
typedef pvector<T>     _ThisType;

public:

// -------------- pvector::const_iterator -------
class const_iterator 
   : public std::iterator < 
               std::random_access_iterator_tag, T >
   {
   protected:
   // -------------- data members ---------------
   size_t           _itsInd;  // Index in its vector.
   const _ThisType* _itsVect; // Ptr to its vector.

   const_iterator (const _ThisType * v, size_t index)
      : _itsVect (v), _itsInd (index) {}

   _BaseType::const_iterator _getBaseConstIt() const
      {return((_BaseType*)_itsVect)->begin()+_itsInd;}

   friend pvector <T>;

   public:
   // -------------- ctors ----------------------
   const_iterator ()
      : _itsInd (0), _itsVect (0) {}

   // -------------- dereferencing --------------
   const_reference operator *() const
      { return (*_itsVect)[_itsInd]; }

   const T* operator ->() const
      { return & operator *(); }

   // -------------- iterator arithmetic --------
   #define DEFINE_ARITHMETIC(It)                     \
      /* binary */                                   \
      It operator + (int s) const                    \
         { return It (_itsVect, _itsInd + s); }      \
      It operator - (int s) const                    \
         { return It (_itsVect, _itsInd - s); }      \
      int operator - (It to) const                   \
         { return _itsInd - to._itsInd; }            \
      /* increment */                                \
      It& operator += (int s)                        \
         { _itsInd += s; return *this; }             \
      It& operator ++ ()                             \
         { _itsInd++; return *this; }                \
      It operator ++ (int)                           \
         { It t (*this); _itsInd++; return t; }      \
      /* decrement */                                \
      It& operator -= (int s)                        \
         { _itsInd -= s; return *this; }             \
      It& operator -- ()                             \
         { _itsInd -- ; return *this; }              \
      It  operator -- (int)                          \
         { It t (*this); _itsInd--; return t; }      \


   DEFINE_ARITHMETIC (const_iterator)

   // -------------- iterator comparisons -----------
   bool operator != (const const_iterator & to) const
      { return _itsInd  != to._itsInd 
            || _itsVect != to._itsVect; }

   bool operator == (const const_iterator & to) const
      { return _itsInd == to._itsInd 
            && _itsVect == to._itsVect; } 

   bool operator < (const const_iterator & to) const
      { return _itsInd  < to._itsInd  
            || _itsInd == to._itsInd 
            && _itsVect < to._itsVect; }

   bool operator <= (const const_iterator & to) const
       { return _itsInd  < to._itsInd 
             || _itsInd == to._itsInd 
             && _itsVect <= to._itsVect; }

   bool operator >= (const const_iterator & to) const

      { return !(*this < to); }

   bool operator > (const const_iterator & to) const
      { return (to < *this); }
   }; // -------------- class const_iterator ----

// ------------- class pvector<>::iterator ----------
class iterator : public const_iterator
   {
   // --------------- private ctor --------------
   iterator (const _ThisType* v, size_t index)
      : const_iterator (v, index) {};

   _BaseType::iterator _getBaseIt() const
     {return ((_BaseType*)_itsVect)->begin()+_itsInd;}

   friend pvector <T>;
   
   public:
   // -------------- ctor ----------------------
   iterator () : const_iterator() {}

   // -------------- dereferencing --------------
   reference operator *() const
      { return (*(_BaseType*)_itsVect)[_itsInd]; }
                               
   T* operator->() const      
      { return &this->operator *();}

   // ------------ arithmetic -------------------
   DEFINE_ARITHMETIC (iterator);
   }; // pvector<>::class const_iterator

#undef DEFINE_ARITHMETIC // cleanup 

// -------------- constructors ------------------
explicit pvector (const A& a = A())
   : std::vector <T> (a) {}

explicit pvector (size_type n, 
                  const T& v = T(), 
                  const A& a = A())
   : std::vector <T> (n, v, a) {}

template <class InIt>   
pvector (InIt first, InIt last, const A& a = A())
   : std::vector <T> (a) 
   { for (; first != last; first++) 
        push_back (*first); }

pvector (const pvector& x)
   : std::vector <T> (x) {}

// -------------- iterator returns --------------
iterator begin() 
   { return iterator (this, 0); }
iterator end() 
   { return iterator (this, size());}

const_iterator begin() const
   { return const_iterator (this, 0);  }
const_iterator end() const
   { return const_iterator (this, size()); }

// -------------- reverse iterators -------------
typedef std::reverse_iterator <iterator, T>
   reverse_iterator;
reverse_iterator rbegin() 
   { return (reverse_iterator (end())); }
reverse_iterator rend() 
   { return (reverse_iterator(begin())); }

typedef std::reverse_iterator <const_iterator, T>
   const_reverse_iterator;
const_reverse_iterator rbegin() const
   { return (const_reverse_iterator (end())); }
const_reverse_iterator rend() const
   { return (const_reverse_iterator (begin())); }

// -------------- assign ------------------------
pvector & operator = (const pvector & from) 
   { (_BaseType &) *this = from; return *this; }

// -------------- erases ------------------------
iterator erase (iterator it)
   { 
   _BaseType::erase (it._getBaseIt());
   if (2 * size() < capacity()) 
      {  // deallocate some storage
      pvector copy (*this);
      swap (copy);
      }
   return it; 
   }

void pop_back ()
   { erase (end() - 1); }

iterator erase (iterator from, iterator to)
   { 
   _BaseType::erase (from._getBaseIt(),
                     to.  _getBaseIt());
   if (2 * size() < capacity())
      {
      pvector copy (*this);
      swap (copy);
      }
   return from; 
   }
                 
void clear ()
   { erase (begin(), end()); }

// -------------- inserts -----------------------
iterator insert (iterator it, const T& x = T())
   { _BaseType::insert (it._getBaseIt(), x);
      return it;  }
   
void insert (iterator it, size_type n, const T& x)
   { _BaseType::insert (it._getBaseIt(), n, x); }

template <class InIt>    
void insert (iterator it, InIt first, InIt last)
   { for (; first != last; it++, first++)
        insert (it, *first);  }

}; // ------------ template pvector -------------

#else
#  error Multiple #included file!
#endif
/* End of File */

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.