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

  1. #ifndef __RWTISLIST_H__
  2. #define __RWTISLIST_H__
  3. pragma push_align_members(64);
  4.  
  5. /*
  6.  * RWTIsvSlist<T>: Parameterized intrusive list of Ts (which must derive from RWIsvSlink)
  7.  *
  8.  * $Header:   E:/vcs/rw/tislist.h_v   1.1   04 Mar 1992 10:16: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.  * $Log:   E:/vcs/rw/tislist.h_v  $
  22.  * 
  23.  *    Rev 1.1   04 Mar 1992 10:16:40   KEFFER
  24.  *  
  25.  * 
  26.  *    Rev 1.0   02 Mar 1992 16:10:52   KEFFER
  27.  * Initial revision.
  28.  */
  29.  
  30. //$DECLARE_TEMPLATE
  31.  
  32. #include "rw/islist.h"
  33.  
  34.  
  35. /****************************************************************
  36.  *                                *
  37.  *        Declarations for RWTIsvSlist<T>            *
  38.  *                                *
  39.  ****************************************************************/
  40.  
  41. template <class T> class RWExport RWTIsvSlist : private RWIsvSlist {
  42.  
  43.   friend class RWExport RWTIsvSlistIterator<T>;
  44.   typedef RWIsvSlist BASE;
  45.  
  46. protected:
  47.  
  48.   RWIsvSlist::_lastSlink;    // Adjust visibility to allow access by subclasses
  49.   RWIsvSlist::findLeft;
  50.   RWIsvSlist::insertAfterLink;
  51.   RWIsvSlist::removeRight;
  52.  
  53. public:
  54.  
  55.   RWTIsvSlist()                       { }
  56.   RWTIsvSlist(T* a) : RWIsvSlist(a)   { }
  57.  
  58.   /********************* Member functions **************************/
  59.   void        append(T* a)                {BASE::append(a);}
  60.   void        apply(void (*applyFun)(T*, void*), void*);
  61.   T*        at(int i) const                {return (T*)BASE::at(i);}
  62.   RWIsvSlist::clear;
  63.   void        clearAndDestroy();
  64.   RWBoolean    contains(RWBoolean (*testFun)(const T*, void*), void*) const;
  65.   RWBoolean    containsReference(const T* a) const    {return BASE::containsReference(a);}
  66.   RWIsvSlist::entries;
  67.   T*        find(RWBoolean (*testFun)(const T*, void*), void*) const;
  68.   T*        first() const                {return (T*)BASE::first();}
  69.   T*        get()                    {return (T*)BASE::get();}
  70.   int        index(RWBoolean (*testFun)(const T*, void*), void*) const;
  71.   void        insert(T* a)                {BASE::append(a);}
  72.   void        insertAfter(int i, T* a)        {BASE::insertAfter(i,a);}
  73.   void        insertAt(int i, T* a)            {BASE::insertAt(i,a);}
  74.   RWIsvSlist::isEmpty;
  75.   T*        last() const                {return (T*)BASE::last();}
  76.   unsigned    occurrencesOf(RWBoolean (*testFun)(const T*, void*), void*) const;
  77.   unsigned    occurrencesOfReference(const T* a) const{return BASE::occurrencesOfReference(a);}
  78.   void        prepend(T* a)                {BASE::prepend(a);}
  79.   T*        remove(RWBoolean (*testFun)(const T*, void*), void*);
  80.   T*        removeAt(int i)                {return (T*)BASE::removeAt(i);}
  81.   T*         removeFirst()                {return (T*)BASE::removeFirst();}
  82.   T*        removeLast()                {return (T*)BASE::removeLast();}
  83.   T*        removeReference(const T* a)        {return (T*)BASE::removeReference(a);}
  84. };
  85.  
  86. /****************************************************************
  87.  *                                *
  88.  *        Declarations for RWTIsvSlistIterator<T>        *
  89.  *                                *
  90.  ****************************************************************/
  91.  
  92. template <class T> class RWExport RWTIsvSlistIterator: private RWIsvSlistIterator {
  93.  
  94.   typedef RWIsvSlistIterator    BASE;
  95.  
  96.   // Disallow postfix increment.  Unless we hide it, some compilers will
  97.   // substitute the prefix increment operator in its place.
  98.   RWBoolean        operator++(int);
  99.  
  100. protected:
  101.  
  102.   RWIsvSlistIterator::_shere;    // Allow visibility to subclasses
  103.   RWIsvSlistIterator::_slist;
  104.   RWIsvSlistIterator::removeRight;
  105.  
  106. public:
  107.  
  108.   // Constructor: starts with iterator positioned at last link.
  109.   RWTIsvSlistIterator(RWTIsvSlist<T>& s) : RWIsvSlistIterator(s) { }
  110.  
  111.   // Operators:
  112.   RWBoolean    operator++()        {return BASE::operator++();}
  113.   T*        operator()()        {return (T*)BASE::operator()();}
  114.   RWIsvSlistIterator::operator+=;    // Advance iterator n links and test
  115.  
  116.   // Member functions:
  117.   RWTIsvSlist<T>*    container() const {return (RWTIsvSlist<T>*)BASE::container();}
  118.   T*        findNext(RWBoolean (*testFun)(const T*, void*), void*);
  119.   T*        findNextReference(const T* a)    {return (T*)BASE::findNextReference(a);}
  120.   void        insertAfterPoint(T* a)        {BASE::insertAfterPoint(a);}
  121.   T*        key() const            {return (T*)BASE::key();}
  122.   T*         remove()            {return (T*)BASE::remove();}
  123.   T*        removeNext(RWBoolean (*testFun)(const T*, void*), void*);
  124.   T*        removeNextReference(const T* a)    {return (T*)BASE::removeNextReference(a);}
  125.   void        reset()                {BASE::reset();}
  126.   void        reset(RWTIsvSlist<T>& s)    {BASE::reset(s);}
  127. };
  128.  
  129.  
  130. //$IMPLEMENT_TEMPLATE
  131.  
  132. /****************************************************************
  133.  *                                *
  134.  *        Definitions for RWTIsvSlist<T>            *
  135.  *                                *
  136.  ****************************************************************/
  137.  
  138. template <class T> void
  139. RWTIsvSlist<T>::apply(void (*applyFun)(T*, void*), void* p)
  140. {
  141.   RWIsvSlink* link = _lastSlink;
  142.   if (link) {
  143.     do {
  144.       link = link->next();        // Advance to next link
  145.       (*applyFun)((T*)link, p);        // Apply the function
  146.     } while (link != _lastSlink);
  147.   }
  148. }
  149.  
  150. template <class T> void
  151. RWTIsvSlist<T>::clearAndDestroy()
  152. {
  153.   // First delete all links, then call the base class's clear()
  154.  
  155.   RWIsvSlink* link = _lastSlink;
  156.   if (link) {
  157.     do {
  158.       RWIsvSlink* n = link->next();    // Remember next link
  159.       delete (T*)link;            // Delete this link
  160.       link = n;                // Advance to next link
  161.     } while (link != _lastSlink);
  162.   }
  163.   RWIsvSlist::clear();            // Call base class clear();
  164. }
  165.   
  166. template <class T> RWBoolean
  167. RWTIsvSlist<T>::contains(RWBoolean (*testFun)(const T*, void*), void* a) const
  168. {
  169.   // Cast "const" away:
  170.   RWTIsvSlistIterator<T> sc(*(RWTIsvSlist<T>*)this);
  171.   return sc.findNext(testFun, a) != rwnil;
  172. }
  173.  
  174. template <class T> T*
  175. RWTIsvSlist<T>::find(RWBoolean (*testFun)(const T*, void*), void* a) const
  176. {
  177.   RWTIsvSlistIterator<T> sc(*(RWTIsvSlist<T>*)this);
  178.   return (T*)sc.findNext(testFun, a);
  179. }
  180.  
  181. template <class T> int
  182. RWTIsvSlist<T>::index(RWBoolean (*testFun)(const T*, void*), void* a) const
  183. {
  184.   int count = -1;
  185.   if (_lastSlink) {            // List is not empty.
  186.     register const RWIsvSlink* link = _lastSlink;
  187.     do {
  188.       link = link->next();
  189.       count++;
  190.       if( (*testFun)((T*)link, a) ) return count;
  191.     } while ( link!=_lastSlink );
  192.     count = -1;
  193.   }
  194.   return count;
  195. }
  196.  
  197. template <class T> unsigned
  198. RWTIsvSlist<T>::occurrencesOf(RWBoolean (*testFun)(const T*, void*), void* a) const
  199. {
  200.   unsigned count = 0;
  201.   RWTIsvSlistIterator<T> sc(*(RWTIsvSlist<T>*)this);
  202.   while (sc.findNext(testFun, a)) count++;
  203.   return count;
  204. }
  205.  
  206. template <class T> T*
  207. RWTIsvSlist<T>::remove(RWBoolean (*testFun)(const T*, void*), void* a)
  208. {
  209.   RWTIsvSlistIterator<T> sc(*(RWTIsvSlist<T>*)this);
  210.   return sc.removeNext(testFun, a);
  211. }
  212.  
  213. /****************************************************************
  214.  *                                *
  215.  *    Definitions for RWTIsvSlistIterator<T>            *
  216.  *                                *
  217.  ****************************************************************/
  218.  
  219. /*
  220.  * Return first occurrence where the tester returns true.
  221.  */
  222. template <class T> T*
  223. RWTIsvSlistIterator<T>::findNext(RWBoolean (*testFun)(const T*, void*), void* a)
  224. {
  225.   while ( ++(*this) ){
  226.     if ( (*testFun)((const T*)key(), a) ) return key();
  227.   }
  228.   return rwnil;
  229. }
  230.  
  231. /*
  232.  * Remove first occurrence where the tester returns true.
  233.  */
  234. template <class T> T*
  235. RWTIsvSlistIterator<T>::removeNext(RWBoolean (*testFun)(const T*, void*), void* a)
  236. {
  237.   T* cur;
  238.   T* prev = key();            // Remember "previous" link
  239.  
  240.   while ( ++(*this) ) {                // Advance iterator
  241.     cur = key();                // Get current value
  242.     if ( (*testFun)((const T*)cur, a) )        // Is this the victim?
  243.       return (T*)BASE::removeRight(prev);    // If it is, remove it.
  244.     prev = cur;
  245.   }
  246.   return rwnil;
  247. }
  248.  
  249. pragma pop_align_members();
  250. #endif    /* __RWTISLIST_H__ */
  251.