home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 6.ddi / MWHC.006 / Q2 < prev    next >
Encoding:
Text File  |  1992-06-07  |  5.2 KB  |  197 lines

  1. #ifndef __RWTVORDVEC_H__
  2. #define __RWTVORDVEC_H__
  3. pragma push_align_members(64);
  4.  
  5. /*
  6.  * Parameterized ordered vector.  Items ordered by order of insertion.
  7.  *
  8.  * $Header:   E:/vcs/rw/tvordvec.h_v   1.3   11 Mar 1992 15:23:40   KEFFER  $
  9.  *
  10.  ****************************************************************************
  11.  *
  12.  * Rogue Wave Software, Inc.
  13.  * P.O. Box 2328
  14.  * Corvallis, OR 97339
  15.  *
  16.  * Copyright (C) 1992. This software is subject to copyright 
  17.  * protection under the laws of the United States and other countries.
  18.  *
  19.  ***************************************************************************
  20.  *
  21.  * Stores a *copy* of the inserted item into the collection.
  22.  *
  23.  * Assumes that T has:
  24.  *   - well-defined copy semantics (T::T(const T&) or equiv.);
  25.  *   - well-defined assignment semantics (T::operator=(const T&) or equiv.);
  26.  *   - well-defined equality semantics (T::operator==(const T&)).
  27.  *
  28.  * Note that while these are automatically defined for builtin types
  29.  * (such as "int", "double", or any pointer), you may need to provide
  30.  * appropriate operators for your own classes, particularly those with
  31.  * constructors and/or pointers to other objects.
  32.  *
  33.  ***************************************************************************
  34.  *
  35.  * $Log:   E:/vcs/rw/tvordvec.h_v  $
  36.  * 
  37.  *    Rev 1.3   11 Mar 1992 15:23:40   KEFFER
  38.  * 
  39.  *    Rev 1.0   02 Mar 1992 16:10:54   KEFFER
  40.  * Initial revision.
  41.  */
  42.  
  43. //$DECLARE_TEMPLATE
  44.  
  45. #include "rw/tvvector.h"
  46.  
  47. template <class T> class RWTValOrderedVector : private RWTValVector<T> {
  48.  
  49.   typedef RWTValVector<T>    BASE;
  50.  
  51. protected:
  52.  
  53.   unsigned        _nitems;    // Number of items in the collection
  54.  
  55. public:
  56.  
  57.   RWTValOrderedVector(unsigned capac=RWDEFAULT_CAPACITY);
  58.  
  59.   BASE::operator[];
  60.   BASE::operator();
  61.  
  62.   // Member functions:
  63.   void            append(T val)        {insertAt(_nitems,val);}
  64.   T&            at(int i)        {return (*this)[i];}
  65.   T            at(int i) const        {return (*this)[i];}
  66.   void            clear()            {_nitems=0; reshape(RWDEFAULT_CAPACITY);}
  67.   RWBoolean        contains(T a) const    {return index(a) != -1;}
  68.   BASE::data;
  69.   unsigned        entries() const        {return _nitems;}
  70.   RWBoolean        find(T a,T& ret) const;    // Find first occurrence
  71.   T            first() const        {return (*this)(0);}
  72.   int            index(T) const;
  73.   void            insert(T val)        {insertAt(_nitems,val);}
  74.   void            insertAt(int, T);
  75.   RWBoolean        isEmpty() const        {return _nitems==0;}
  76.   T            last() const        {return (*this)(_nitems-1);}
  77.   unsigned        length() const        {return _nitems;}
  78.   unsigned        occurrencesOf(T) const;
  79.   void            prepend(T val)        {insertAt(0, val);}
  80.   RWBoolean        remove(T);        // Remove first occurrence
  81.   unsigned        removeAll(T);
  82.   T            removeAt(int);
  83.   T            removeFirst()        {return removeAt(0);}
  84.   T            removeLast()        {return (*this)(--_nitems);}
  85.   void            resize(unsigned);    // Cannot shrink below population
  86.  
  87. };
  88.  
  89. //$IMPLEMENT_TEMPLATE
  90.  
  91. template <class T>
  92. RWTValOrderedVector<T>::RWTValOrderedVector(unsigned capac) :
  93.   _nitems(0),
  94.   RWTValVector<T>(capac)
  95. {
  96. }
  97.  
  98. template <class T> RWBoolean
  99. RWTValOrderedVector<T>::find(T val, T& retVal) const
  100. {
  101.   int idx = index(val);
  102.   return idx == -1 ? FALSE : ( retVal = _array[idx], TRUE);
  103. }
  104.  
  105. template <class T> int
  106. RWTValOrderedVector<T>::index(T val) const
  107. {
  108.   for (register i=0; i<(int)_nitems; i++)
  109.     if ((*this)(i)==val) return i;
  110.   return -1;
  111. }
  112.  
  113. // Insert value at position "ipt"; value formerly at "ipt"
  114. // gets moved to "ipt+1".
  115. template <class T> void
  116. RWTValOrderedVector<T>::insertAt(int ipt, T val)
  117. {
  118.   RWPRECONDITION(("RWTValOrderedVector::insertAfter(int,T): index out of range", ipt>=0 && ipt<=(int)_nitems));
  119.  
  120.   // Check for overflow:
  121.   if(_nitems>=BASE::length())
  122.     reshape(_nitems + RWDEFAULT_RESIZE);
  123.  
  124.   // Slide right (could be very expensive)
  125.   for(register i=(int)_nitems; i>ipt; i--)
  126.     (*this)(i) = (*this)(i-1);
  127.  
  128.   _nitems++;
  129.   (*this)(ipt) = val;
  130. }
  131.  
  132. template <class T> unsigned
  133. RWTValOrderedVector<T>::occurrencesOf(T val) const
  134. {
  135.   unsigned count = 0;
  136.   for (register i=0; i<(int)_nitems; i++)
  137.     if ((*this)(i)==val) ++count;
  138.   return count;
  139. }
  140.  
  141. /*
  142.  * Remove first occurrence of the value.
  143.  */
  144. template <class T> RWBoolean
  145. RWTValOrderedVector<T>::remove(T val)
  146. {
  147.   int idx = index(val);
  148.   if (idx==-1) return FALSE;
  149.   removeAt(idx);
  150.   return TRUE;
  151. }
  152.  
  153. /*
  154.  * Remove all occurrences of the value
  155.  */
  156. template <class T> unsigned
  157. RWTValOrderedVector<T>::removeAll(T val)
  158. {
  159.   int j = 0;
  160.   for (register i=0; i<(int)_nitems; i++) {
  161.     // Are they unequal?  If so, this value should be saved.
  162.     if (!((*this)(i)==val)) {
  163.       // Avoid the copy if possible:
  164.       if (i!=j) (*this)(j) = (*this)(i);
  165.       ++j;
  166.     }
  167.   }
  168.  
  169.   int nremoved = _nitems-j;
  170.   _nitems -= nremoved;
  171.   return nremoved;
  172. }
  173.  
  174. template <class T> T
  175. RWTValOrderedVector<T>::removeAt(int ipt)
  176. {
  177.   RWPRECONDITION(("RWTValOrderedVector::removeAt(int,T): index out of range", ipt>=-1 && ipt<(int)_nitems));
  178.  
  179.   T temp = (*this)(ipt);
  180.  
  181.   // Slide left (could be very expensive):
  182.   for(register i=ipt; i<_nitems-1; i++)
  183.     (*this)(i) = (*this)(i+1);
  184.  
  185.   _nitems--;
  186.   return temp;
  187. }
  188.  
  189. template <class T> void
  190. RWTValOrderedVector<T>::resize(unsigned N)
  191. {
  192.   if (N>=_nitems) reshape(N);
  193. }
  194.  
  195. pragma pop_align_members();
  196. #endif    /* __RWTVORDVEC_H__ */
  197.