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

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