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

  1. #ifndef __RWTVSRTVEC_H__
  2. #define __RWTVSRTVEC_H__
  3. pragma push_align_members(64);
  4.  
  5. /*
  6.  * Parameterized sorted vector.  Inserts values using an insertion sort.
  7.  *
  8.  * $Header:   E:/vcs/rw/tvsrtvec.h_v   1.3   17 Mar 1992 11:30:26   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 according
  22.  * to an ordering determined by the less-than (<) operator.
  23.  *
  24.  * Assumes that T has:
  25.  *   - well-defined copy semantics (T::T(const T&) or equiv.);
  26.  *   - well-defined assignment semantics (T::operator=(const T&) or equiv.);
  27.  *   - well-defined equality semantics (T::operator==(const T&));
  28.  *   - well-defined less-than semantics (T::operator<(const T&)).
  29.  *
  30.  * Note that while these are automatically defined for builtin types
  31.  * (such as "int", "double", or any pointer), you may need to provide
  32.  * appropriate operators for your own classes, particularly those with
  33.  * constructors and/or pointers to other objects.
  34.  *
  35.  * This class uses binary searches for efficient value-based retrievals.
  36.  *
  37.  ***************************************************************************
  38.  *
  39.  * $Log:   E:/vcs/rw/tvsrtvec.h_v  $
  40.  * 
  41.  *    Rev 1.3   17 Mar 1992 11:30:26   KEFFER
  42.  * 
  43.  *    Rev 1.2   11 Mar 1992 15:23:44   KEFFER
  44.  * 
  45.  *    Rev 1.0   02 Mar 1992 16:10:54   KEFFER
  46.  * Initial revision.
  47.  */
  48.  
  49. //$DECLARE_TEMPLATE
  50.  
  51. #include "rw/tvvector.h"
  52. #include "rw/tbsearch.h"
  53.  
  54. template <class T> class RWTValSortedVector : private RWTValVector<T> {
  55.  
  56.   typedef RWTValVector<T>    BASE;
  57.  
  58. protected:
  59.  
  60.   unsigned        _nitems;    // Number of items in the collection
  61.  
  62. public:
  63.  
  64.   RWTValSortedVector(unsigned capac = RWDEFAULT_CAPACITY);
  65.  
  66.   BASE::operator[];
  67.   BASE::operator();
  68.  
  69.   T&            at(int i)        {return (*this)[i];}
  70.   T            at(int i) const        {return (*this)[i];}
  71.   void            clear()            {_nitems=0; reshape(RWDEFAULT_CAPACITY);}
  72.   RWBoolean        contains(T a) const    {return index(a) != -1;}
  73.   BASE::data;
  74.   unsigned        entries() const        {return _nitems;}
  75.   RWBoolean        find(T a,T& ret) const;    // Find first occurrence
  76.   T            first() const        {return (*this)(0);}
  77.   int            index(T) const;
  78.   void            insert(T);
  79.   RWBoolean        isEmpty() const        {return _nitems==0;}
  80.   T            last() const        {return (*this)(_nitems-1);}
  81.   unsigned        length() const        {return _nitems;}
  82.   unsigned        occurrencesOf(T) const;
  83.   RWBoolean        remove(T);
  84.   unsigned        removeAll(T);
  85.   T            removeAt(int);
  86.   T            removeFirst()        {return removeAt(0);}
  87.   T            removeLast()        {return (*this)(--_nitems);}
  88.   void            resize(unsigned newCapacity);
  89. };
  90.  
  91.  
  92.  
  93. //$IMPLEMENT_TEMPLATE
  94.  
  95. template <class T>
  96. RWTValSortedVector<T>::RWTValSortedVector(unsigned capac) :
  97.   _nitems(0),
  98.   RWTValVector<T>(capac)
  99. {
  100. }
  101.  
  102. template <class T> RWBoolean
  103. RWTValSortedVector<T>::find(T a, T& ret) const
  104. {
  105.   int idx = index(a);
  106.   if (idx>=0) {
  107.     ret = (*this)(idx);
  108.     return TRUE;
  109.   }
  110.   return FALSE;
  111. }
  112.  
  113. template <class T> int
  114. RWTValSortedVector<T>::index(T item) const
  115. {
  116.   int idx = RWTbsearch(item, data(), _nitems);
  117.   if (idx>=_nitems || !(item==(*this)(idx))) idx = -1;
  118.   return idx;                                            \
  119. }
  120.  
  121. template <class T> void
  122. RWTValSortedVector<T>::insert(T item)
  123. {
  124.   int idx = RWTbsearch(item, data(), _nitems);
  125.  
  126.   /* If duplicates are present, search for the last one: */
  127.   while (idx<_nitems && item==(*this)(idx)) ++idx;
  128.  
  129.   // Check for overflow:
  130.   if(_nitems>=length())
  131.     reshape(length() + RWDEFAULT_RESIZE);
  132.  
  133.   // Slide right (could be very expensive)
  134.   for(register i=(int)_nitems-1; i>=idx; i--)
  135.     (*this)(i+1) = (*this)(i);
  136.  
  137.   (*this)(idx) = item;
  138.   _nitems++;
  139. }
  140.  
  141. template <class T> unsigned
  142. RWTValSortedVector<T>::occurrencesOf(T val) const
  143. {
  144.   // Do a binary search to find the first match:
  145.   int istart = index(val);
  146.   if (istart == -1) return 0;
  147.  
  148.   // Now do a linear search, looking for the last match:
  149.   int i = istart+1;
  150.   while ( i<_nitems && (*this)(i)==val) ++i;
  151.  
  152.   return i-istart;
  153. }
  154.  
  155. template <class T> RWBoolean
  156. RWTValSortedVector<T>::remove(T val)
  157. {
  158.   int idx = index(val);
  159.   if (idx==-1) return FALSE;
  160.   removeAt(idx);
  161.   return TRUE;
  162. }
  163.  
  164. template <class T> unsigned
  165. RWTValSortedVector<T>::removeAll(T val)
  166. {
  167.   // Do a binary search to find the first match:
  168.   int istart = index(val);
  169.   if (istart == -1) return 0;
  170.  
  171.   // Now do a linear search, looking for the last match:
  172.   int i = istart+1;
  173.   while ( i<_nitems && (*this)(i)==val) ++i;
  174.  
  175.   unsigned nremoved = i-istart;
  176.  
  177.   // Do a "solid body" slide left of the remaining items in the collection:
  178.   while (i<_nitems)
  179.     (*this)(istart++) = (*this)(i++);
  180.  
  181.   _nitems -= nremoved;
  182.   return nremoved;
  183. }
  184.  
  185. template <class T> T
  186. RWTValSortedVector<T>::removeAt(int ipt)
  187. {
  188.   RWPRECONDITION(("RWTValSortedVector::removeAt(int,T): index out of range", ipt>=-1 && ipt<(int)_nitems));
  189.  
  190.   T temp = (*this)(ipt);
  191.  
  192.   // Slide left (could be very expensive):
  193.   for(register i=ipt; i<_nitems-1; i++)
  194.     (*this)(i) = (*this)(i+1);
  195.  
  196.   _nitems--;
  197.   return temp;
  198. }
  199.  
  200. template <class T> void
  201. RWTValSortedVector<T>::resize(unsigned N)
  202. { if(N>_nitems) reshape(N); }
  203.  
  204. pragma pop_align_members();
  205. #endif    /* __RWTVSRTVEC_H__ */
  206.