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

  1. #ifndef  _RWSLIST_H__
  2. #define  _RWSLIST_H__
  3. pragma push_align_members(64);
  4.  
  5. /*
  6.  * Declarations for Singly-linked lists.
  7.  *
  8.  * $Header:   E:/vcs/rw/slist.h_v   1.3   04 Mar 1992 09:04:52   KEFFER  $
  9.  *
  10.  ****************************************************************************
  11.  *
  12.  * Rogue Wave 
  13.  * P.O. Box 2328
  14.  * Corvallis, OR 97339
  15.  * Voice: (503) 754-3010    FAX: (503) 757-6650
  16.  *
  17.  * Copyright (C) 1989, 1990, 1991. This software is subject to copyright 
  18.  * protection under the laws of the United States and other countries.
  19.  *
  20.  ***************************************************************************
  21.  *
  22.  * $Log:   E:/vcs/rw/slist.h_v  $
  23.  * 
  24.  *    Rev 1.3   04 Mar 1992 09:04:52   KEFFER
  25.  * nil changed to rwnil
  26.  * 
  27.  *    Rev 1.2   18 Feb 1992 09:54:44   KEFFER
  28.  * 
  29.  *    Rev 1.1   28 Oct 1991 09:08:24   keffer
  30.  * Changed inclusions to <rw/xxx.h>
  31.  * 
  32.  *    Rev 1.0   28 Jul 1991 08:17:00   keffer
  33.  * Tools.h++ V4.0.5 PVCS baseline version
  34.  *
  35.  */
  36.  
  37. /*
  38.  * Singly linked list of pointers to voids.
  39.  *
  40.  * See e.g., B. Stroustrup, The C++ Programming Language, Addison-Wesley,
  41.  *                 Reading, Mass., 1986; 328pp. (ISBN 0-201-12078-X) p.203
  42.  */
  43.  
  44. #include "rw/tooldefs.h"
  45. #include "rw/mempool.h"
  46. STARTWRAP
  47. #include <stddef.h>
  48. ENDWRAP
  49.  
  50. class RWExport RWDlink;
  51.  
  52. // Single link:
  53.  
  54. class RWExport RWSlink : public RWMemoryPool {
  55. friend class RWExport RWSlist;
  56. friend class RWExport RWDlist;
  57. friend class RWExport RWSlistIterator;
  58. friend class RWExport RWDlistIterator;
  59. protected:
  60.   union RWExport {
  61.     RWSlink*        nextSlink;        // Pointer to next link.
  62.     RWDlink*        nextDlink;
  63.   };
  64.   void*         e;            // Pointer to void.
  65.  
  66.   RWSlink(void* a, RWSlink* n) { e=a; nextSlink=n; }    // Private constructor.
  67. };
  68.  
  69. class RWExport RWSlist {
  70. friend class RWExport RWSlistIterator;
  71. friend class RWExport RWDlistIterator;
  72. protected:
  73.   union RWExport {
  74.     RWSlink*        lastSlink;    // lastSlink->nextSlink is head of list
  75.     RWDlink*        lastDlink;
  76.   };
  77. protected:
  78.   void            boundsErr(int) const;
  79.   void            deleteRight(RWSlink*);
  80.   RWSlink*        findLeft(RWSlink*) const;
  81.   RWSlink*        findLink(int) const;
  82.   void            insertAfterLink(RWSlink*, void*);
  83. public:
  84.   RWSlist()        {lastSlink=rwnil;}
  85.   RWSlist(void* a);
  86.   RWSlist(const RWSlist&);
  87.   ~RWSlist()        {clear();} 
  88.   void            operator=(const RWSlist& s);
  89.  
  90.   /********************* Member functions **************************/
  91.   void*            append(void*);        // Add at tail of list.
  92.   void            apply(RWapplyGeneric, void*);
  93.   void*&        at(int);        // Error if out of range.
  94. #ifdef CONST_OVERLOADS
  95.   const void*        at(int) const;        // Returns nil if out of range.
  96. #endif
  97.   void            clear();
  98.   RWBoolean        contains(RWtestGeneric, const void*) const;
  99.   RWBoolean        containsReference(const void*) const;
  100.   unsigned        entries() const;        // Total entries
  101.   void*         find(RWtestGeneric, const void*) const;
  102.   void*         findReference(const void*) const; // First occurrence
  103.   void*             first() const;    // Return value at head of list
  104.   void*             get();        // Return value and delete link at head of list
  105.   int            index(RWtestGeneric, const void*) const;
  106.   void*         insert(void* a) {return RWSlist::append(a);}
  107.   void*            insertAfter(int, void*);
  108.   RWBoolean        isEmpty() const;
  109.   void*             last() const;    // Return value at tail of list
  110.   unsigned        occurrencesOf(RWtestGeneric, const void*) const;
  111.   unsigned        occurrencesOfReference(const void*) const;
  112.   void*            prepend(void*);
  113.   void*         remove(RWtestGeneric, const void*);
  114.   void*         removeReference(const void*);
  115. };
  116.  
  117. // Iterator for singly linked list (moves iterator through the list):
  118. class RWExport RWSlistIterator {
  119. protected:
  120.   union RWExport {
  121.     RWSlist*        slist;
  122.     RWDlist*        dlist;
  123.   };
  124.   union RWExport {
  125.     RWSlink*        shere;        // Iterator position
  126.     RWDlink*        dhere;
  127.   };
  128. protected:
  129.   void            resetHere();
  130.   RWSlink*        currentLink() const {return shere ? shere : slist->lastSlink;}
  131. public:
  132.   // Constructor: starts with iterator positioned at last link.
  133.   RWSlistIterator(RWSlist& s); 
  134.   
  135.   void            operator++();        // Advance iterator one link.
  136.   void            operator+=(int n);    // Advance iterator n links.
  137.  
  138.   RWBoolean        atFirst() const;    // Is iterator at head of list?
  139.   RWBoolean        atLast() const;        // Is iterator at end of list?
  140.   void*             findNext(RWtestGeneric, const void*);
  141.   void*             findNextReference(const void*);
  142.   void*             insertAfterPoint(void*);// Insert item after position.
  143.   void*             key() const;        // Return current item
  144.   void*             operator()();        // Advance iterator
  145.   void*             remove();        // Remove current item
  146.   void*             removeNext(RWtestGeneric, const void*);
  147.   void*             removeNextReference(const void*);
  148.   void            reset();
  149.   void            toFirst();        // Move iterator to start of list.
  150.   void            toLast();        // Move iterator to end of list.
  151. };
  152.  
  153. /************************ RWSlist inlines ******************************/
  154.  
  155. Inline RWBoolean
  156. RWSlist::isEmpty() const {return lastSlink==rwnil;}
  157.  
  158. // Return value at head of list
  159. Inline void* 
  160. RWSlist::first() const
  161. { return isEmpty() ? rwnil : lastSlink->nextSlink->e ; }
  162.  
  163. // Return value at tail of list
  164. Inline void* 
  165. RWSlist::last() const
  166. { return isEmpty() ? rwnil : lastSlink->e ; }
  167.  
  168.  
  169. /*********************** RWSlistIterator inlines *****************************/
  170.  
  171. // Constructor: start with iterator positioned at last link
  172. Inline
  173. RWSlistIterator::RWSlistIterator(RWSlist& s) { slist = &s; reset(); } 
  174.  
  175. Inline void
  176. RWSlistIterator::toLast() {reset();}
  177.  
  178. Inline RWBoolean
  179. RWSlistIterator::atLast() const {return shere==slist->lastSlink;} 
  180.  
  181. Inline RWBoolean
  182. RWSlistIterator::atFirst() const {return shere==slist->lastSlink->nextSlink;}
  183.  
  184. pragma pop_align_members();
  185. #endif  /* __RWSLIST_H__ */
  186.