tvector.h

/*  tvector.h
    May 14 1997
    Chris Lacher

    Definition of the class template TVector<>

    Parametrized vector class
    Safe vectors indexed on size_t (unsigned integers)

    History:

      05/14/97: initial development
      10/08/97: container protocol added
      03/30/98: memory efficiency, capacity decrease capability, PushBack runtime amortized O(1)
      07/30/00: nomenclature changes
      08/26/00: changed <class T> to <typename T> throughout
      12/05/00: subsumption of tvector.cpp
      04/19/01: namespace rcl
      04/19/01: index on size_t
      02/10/02: changed return type of Front() and Back() to reference
      12/09/02: added type conversion operator to const T*
      12/13/02: namespace fsu
      01/01/04: template the iterator "pointer arithmetic" operations
      11/10/04: removed operator const T*
      02/16/06: improved documentation
      01/14/07: style upgrade

    ASSUMPTIONS ON TYPE T:

    TVector<T> assumes that T has overloads of the following operators
    and functions:

       insertion  (output) operator    <<  
       assignment          operator    =
       constructor                     T()
       destructor                      ~T()

    A copy constructor for T is not used.

    Revised 10/8/97 and 3/30/98 as follows: (1) Container class protocol
    supported by adding default constructor and container class protocol;
    (2) made more efficient by separating size from capacity and adding
    capacity_increment = default_capacity.  The client has direct control
    of allocated memory via SetCapacity(), which (re)allocates the
    specified amount of memory (up or down).  The client does not have to
    worry about capacity, however, since SetSize() calls SetCapacity() when
    necessary to expand allocated memory.  Memory is expanded in chunks of
    size default_capacity, a file-scope constant defined in TVector.cpp.
     
    Revised 6/16/98 to include TVector<T>::Iterator and
    TVector<T>::operator +=() .

    Revised 01/14/07 to conform to style

    Efficiency Requirements: 

    1. Each of the container class operations

          PopBack(),
          Empty(), Size(), Clear(), Front(), Back(), and
          operator []()

       has runtime O(1) and runspace O(1)

    2. The operation PushBack() has amortized runtime O(1)
       and amortized runspace O(1), with worst cases Theta(n)

    3. Each Iterator operation has runtime O(1) and runspace O(1)

    Copyright 1997 - 2007, R.C. Lacher
*/

#ifndef _TVECTOR_H
#define _TVECTOR_H

#include <iostream>
#include <stdlib.h>    // EXIT_FAILURE, size_t

namespace fsu
{

// declare classes
template <typename T>
class TVector;

template <typename T>
class TVectorIterator;

// operator overloads (friend status not required)

template < class T >
std::ostream& operator << (std::ostream& os, const TVector<T>& a);

template < class T >
int      operator == (const TVector<T>&, const TVector<T>&); 

template < class T >
int      operator != (const TVector<T>&, const TVector<T>&); 

//----------------------------------
//     TVector<T>
//----------------------------------

template <typename T>
class TVector
{
public:
  // scope TVector<T>:: type definitions
  typedef T ValueType;

  // constructors - specify size and an initial value
  TVector  ();                      // vector of size = 0 and capacity = default_capacity
  explicit TVector (size_t sz);     // vector of size = capacity = sz ...
  TVector  (size_t sz, const T& t); // ... and all elements = t
  TVector  (const TVector<T>&);     // copy constructor
  virtual ~TVector ();              // destructor

  // member operators
  TVector<T>& operator =  (const TVector<T>&); // assignment operator
  TVector<T>& operator += (const TVector<T>&); // expand to append argument
  T&          operator [] (size_t);            // bracket operator
  const T&    operator [] (size_t) const;      // const version

  // other methods
  int      SetSize     (size_t);    // set size as specified, change capacity iff needed
  int      SetSize     (size_t, const T&); // ... and initialize new elements
  int      SetCapacity (size_t);    // force capacity change (up or down)
  size_t   Size        () const;    // return size
  size_t   Capacity    () const;    // return capacity

  // Container class protocol
  int      Empty       () const;    // 1 iff empty
  int      PushBack    (const T&);  // expand by 1 new element appended at end
  int      PopBack     ();          // contract by 1 from end
  void     Clear       ();          // make size = 0
  T&       Front       ();          // return front element (index 0)
  const T& Front       () const;    // cont version
  T&       Back        ();          // return back element (index size - 1)
  const T& Back        () const;    // const version

  // Iterator support
  typedef      TVectorIterator<T> Iterator; 
  friend class TVectorIterator<T>;
  Iterator     Begin       () const;
  Iterator     End         () const;
  Iterator     rBegin      () const;
  Iterator     rEnd        () const;

  // Generic display methods 
  void Display    (std::ostream& os, char ofc = '\0') const;
  void Dump       (std::ostream& os) const;

  // overload of const T* operator, facilitates use of previously defined array functions
  // operator const T* () const; // auto conversion of vector to array
  // removed 11/10/04: new standard does not allow, creates ambiguities

protected:
  // variables
  size_t size_,        // current size of vector, 
         capacity_;    // size of content_ array
  T*     content_;     // pointer to the primative array elements

  // method
  static T*  NewArray (size_t);  // safe space allocator
} ;

//----------------------------------
//     TVectorIterator<T>
//----------------------------------

template <typename T>
class TVectorIterator
{
friend class TVector<T>;

public:
  // terminology support
  typedef T                  ValueType;
  typedef TVectorIterator<T> Iterator;

  // constructors
  TVectorIterator  ();     // default constructor, iterator is not valid
  TVectorIterator  (const TVector<T>& V); // init to V.Begin()
  TVectorIterator  (const Iterator& I);   // copy constructor

  // information/access
  T&        Retrieve    ();       // Return reference to current Tval
  const T&  Retrieve    () const; // const version
  int Valid             () const; // iterator is valid for dereference

  // Initializers
  void  Initialize   (const TVector<T>& V); // set to V.Begin()
  void  rInitialize  (const TVector<T>& V); // set to V.rBegin()

  // various operators
  int       operator == (const Iterator& I2) const;
  int       operator != (const Iterator& I2) const;
  T&        operator *  ();               // Return reference to current Tval
  const T&  operator *  () const;         // const version
  T&        operator [] (size_t i);       // Return reference to Tval at index i
  const T&  operator [] (size_t i) const; // const version
  Iterator& operator =  (const Iterator & I);
  Iterator& operator ++ ();    // prefix increment
  Iterator  operator ++ (int); // postfix
  Iterator& operator -- ();    // prefix decrement
  Iterator  operator -- (int); // postfix

  // "pointer" arithmetic -- uses template parameter for integer type

  long      operator -  (const Iterator & I2) const;

  // these are the new template member operators for pointer arithmetic

  template <typename N>
  Iterator  operator +  (N n) const;

  template <typename N>
  Iterator& operator += (N n);

  template <typename N>
  Iterator& operator -= (N n);

protected:
  T* Tptr_;
} ;

#include <tvector.cpp> // "slave" file, included inside multiple read protection and namespace

}   // namespace fsu
#endif