home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tlx501.zip / ITER.H < prev    next >
C/C++ Source or Header  |  1996-01-05  |  14KB  |  462 lines

  1. /****************************************************************************
  2.     $Id: iter.h 501.0 1995/03/07 12:26:44 RON Exp $
  3.  
  4.     Copyright (c) 1994-95 Tarma Software Research. All rights reserved.
  5.  
  6.     Project:    Tarma Library for C++ V5.0
  7.     Author:    Ron van der Wal
  8.  
  9.     Declaration of iterator base classes and templates:
  10.  
  11.     TLPrintFormat    - wrapper for formatting info
  12.     TLIter        - base for all iterators
  13.     TLValueIter<T>    - base for iterators of non-const value containers
  14.     TLValueIterConst<T> - base for iterators of const value containers
  15.     TLPtrIter<T>    - base for iterators of non-const pointer containers
  16.     TLPtrIterConst<T>    - base for iterators of const pointer containers
  17.  
  18.     $Log: iter.h $
  19.     Revision 501.0  1995/03/07 12:26:44  RON
  20.     Updated for TLX 5.01
  21.     Revision 1.7  1995/01/31 16:28:02  RON
  22.     Update for release 012
  23.     Added partial support for SunPro C++ compiler
  24.     Revision 1.6  1994/11/16  15:22:13  ron
  25.     Added 'Const' postfix to some constructors
  26.  
  27.     Revision 1.5  1994/10/06  17:48:05  ron
  28.     Small formatting changes
  29.  
  30.     Revision 1.4  1994/10/05  18:22:34    ron
  31.     Added TLPtrItemIter and TLPtrItemIterConst
  32.  
  33.     Revision 1.3  1994/09/28  14:27:03    ron
  34.     Small formatting changes
  35.  
  36.     Revision 1.2  1994/09/27  20:25:18    ron
  37.     Changed path separator from / to \
  38.  
  39.     Revision 1.1  1994/09/26  15:18:28    ron
  40.     Initial revision
  41.  
  42. ****************************************************************************/
  43.  
  44. #ifndef _TLX_ITER_H
  45. #define _TLX_ITER_H
  46.  
  47. #ifndef _TLX_TLX_H
  48. #include <tlx\501\tlx.h>
  49. #endif
  50.  
  51. /*---------------------------------------------------------------------------
  52.     TLPrintFormat -
  53.  
  54.     Wrapper for information that is used to format print-outs by the
  55.     various iterator classes.
  56. ---------------------------------------------------------------------------*/
  57.  
  58. struct _TLXCLASS TLPrintFormat
  59. {
  60.     const char *    mLeader;    // Leader text
  61.     const char *    mPresep;    // Pre-separation text
  62.     const char *    mPostsep;    // Post-separation text
  63.     const char *    mTrailer;    // Trailer text
  64.  
  65.     TLPrintFormat();
  66.     TLPrintFormat(const char *, const char *, const char *, const char *);
  67. };
  68.  
  69. /*---------------------------------------------------------------------------
  70.     TLIter -
  71.  
  72.     Abstract base class for all iterator types. It defines the iterator
  73.     mechanism that is shared by all iterators.
  74. ---------------------------------------------------------------------------*/
  75.  
  76. class _TLXCLASS TLIter
  77. {
  78.     // Iterators can be in one of several states.
  79.  
  80.     enum State
  81.     {
  82.     stReset,            // Reset, not advanced yet
  83.     stValid,            // Valid position in the collection
  84.     stEnd                // Beyond the end of the collection
  85.     };
  86.     State        mState;     // Current state
  87.  
  88. public:
  89.     virtual ~TLIter();
  90.  
  91.     // Iteration functions. They can be used as follows:
  92.     //
  93.     //    iter.Reset();           *or*
  94.     //    while (iter.Next())        for (iter.Reset(); iter.Next(); )
  95.     //        iter.Peek()...            iter.Peek()...
  96.     //
  97.     // Immediately after construction, an iterator is in the reset state
  98.     // and may be used without intervening Reset() operation.
  99.     //
  100.     // Derived classes must define suitable Peek() functions, for example:
  101.     //
  102.     //      T &Peek() const;
  103.     //
  104.     // This function should check that it is called in a valid state (use
  105.     // IsValid() for the check).
  106.  
  107.     void        Reset();
  108.     bool        Next();
  109.  
  110.     // Functions to interrogate the state of the iterator. In addition,
  111.     // Count() returns the number of elements in the collection that is
  112.     // being iterated.
  113.  
  114.     bool        IsReset() const;
  115.     bool        IsValid() const;
  116.     bool        IsAtEnd() const;
  117.     virtual size_t    Count() const = 0;
  118.  
  119. protected:
  120.     TLIter();        // Constructor resets state
  121.  
  122. private:
  123.     // The following functions are used by the implementation of Next().
  124.     //
  125.     // - FirstPos() is called by Next() when the iterator is currently
  126.     //     in the reset state. It should set up the iterator to point
  127.     //     at the first element of the collection and return true if it
  128.     //     succeeds. If the iterator cannot be started (e.g. collection empty),
  129.     //     the false must be returned.
  130.     //
  131.     // - NextPos() is called by Next() when the iterator is in a valid state
  132.     //     and must be advanced to the next element. It should advance the
  133.     //     iterator position, and return true if it is still valid, else
  134.     //     (end of collection) false.
  135.  
  136.     virtual bool    FirstPos() = 0;
  137.     virtual bool    NextPos() = 0;
  138. };
  139.  
  140. /*---------------------------------------------------------------------------
  141.     TLValueIter -
  142.  
  143.     Abstract base class template for iterators that operate on non-const
  144.     value containers of type T.
  145. ---------------------------------------------------------------------------*/
  146.  
  147. template<class T> class _TLXCLASS TLValueIter: public TLIter
  148. {
  149. public:
  150.     typedef void    (*tApplyFunc)(T &, void *);
  151.     typedef bool    (*tTestFunc)(T &, void *);
  152.  
  153. public:
  154.     virtual T &     Peek() const = 0;
  155.     virtual bool    Contains(T &);
  156.  
  157.     // Functions to apply an operation to all or a selected number of
  158.     // elements.
  159.  
  160.     void        ForAll(tApplyFunc, void *);
  161.     bool        FirstThat(tTestFunc, void *, T *&);
  162.     bool        FirstThatNot(tTestFunc, void *, T *&);
  163.  
  164.     // Output operations
  165.  
  166.     ostream &        PrintAll(ostream &);
  167.     ostream &        PrintFormat(ostream &, const TLPrintFormat &);
  168. };
  169.  
  170. /*---------------------------------------------------------------------------
  171.     TLValueIterConst -
  172.  
  173.     Abstract base class template for all iterators that operate on const
  174.     value containers of type T.
  175. ---------------------------------------------------------------------------*/
  176.  
  177. template<class T> class _TLXCLASS TLValueIterConst: public TLIter
  178. {
  179. public:
  180.     typedef void    (*tApplyFunc)(const T &, void *);
  181.     typedef bool    (*tTestFunc)(const T &, void *);
  182.  
  183. public:
  184.     virtual const T &    Peek() const = 0;
  185.     virtual bool    Contains(const T &);
  186.  
  187.     // Functions to apply an operation to all or a selected number of
  188.     // elements.
  189.  
  190.     void        ForAll(tApplyFunc, void *);
  191.     bool        FirstThat(tTestFunc, void *, const T *&);
  192.     bool        FirstThatNot(tTestFunc, void *, const T *&);
  193.  
  194.     // Output operations
  195.  
  196.     ostream &        PrintAll(ostream &);
  197.     ostream &        PrintFormat(ostream &, const TLPrintFormat &);
  198. };
  199.  
  200. /*---------------------------------------------------------------------------
  201.     TLItemIter -
  202.  
  203.     Iterator template for 'iteration' over non-const items of type T.
  204. ---------------------------------------------------------------------------*/
  205.  
  206. template<class T> class _TLXCLASS TLItemIter: public TLValueIter<T>
  207. {
  208.     T &         mItem;        // Single item
  209.  
  210. public:
  211.     TLItemIter(T &);
  212.  
  213.     // Overridden iterator functions
  214.  
  215.     virtual T &     Peek() const;
  216.     virtual size_t    Count() const;
  217.     virtual bool    Contains(T &);
  218.  
  219. private:
  220.     virtual bool    FirstPos();
  221.     virtual bool    NextPos();
  222. };
  223.  
  224. /*---------------------------------------------------------------------------
  225.     TLItemIterConst -
  226.  
  227.     Iterator template for 'iteration' over const items of type T.
  228. ---------------------------------------------------------------------------*/
  229.  
  230. template<class T> class _TLXCLASS TLItemIterConst: public TLValueIterConst<T>
  231. {
  232.     const T &        mItem;        // Single item
  233.  
  234. public:
  235.     TLItemIterConst(const T &);
  236.  
  237.     // Overridden iterator functions
  238.  
  239.     virtual const T &    Peek() const;
  240.     virtual size_t    Count() const;
  241.     virtual bool    Contains(const T &);
  242.  
  243. private:
  244.     virtual bool    FirstPos();
  245.     virtual bool    NextPos();
  246. };
  247.  
  248. /*---------------------------------------------------------------------------
  249.     TLVectorIter -
  250.  
  251.     Iterator template for non-const C-style vectors of type T elements.
  252. ---------------------------------------------------------------------------*/
  253.  
  254. template<class T> class _TLXCLASS TLVectorIter: public TLValueIter<T>
  255. {
  256.     T *         mVector;    // C-style vector
  257.     size_t        mSize;        // Size of the vector
  258.     size_t        mPos;        // Iterator position
  259.  
  260. public:
  261.     TLVectorIter(T [], size_t);
  262.  
  263.     // Overridden iterator functions
  264.  
  265.     virtual T &     Peek() const;
  266.     virtual size_t    Count() const;
  267.  
  268. private:
  269.     virtual bool    FirstPos();
  270.     virtual bool    NextPos();
  271. };
  272.  
  273. /*---------------------------------------------------------------------------
  274.     TLVectorIterConst -
  275.  
  276.     Iterator template for const C-style vectors of type T elements.
  277. ---------------------------------------------------------------------------*/
  278.  
  279. template<class T> class _TLXCLASS TLVectorIterConst: public TLValueIterConst<T>
  280. {
  281.     const T *        mVector;    // C-style vector
  282.     size_t        mSize;        // Size of the vector
  283.     size_t        mPos;        // Iterator position
  284.  
  285. public:
  286.     TLVectorIterConst(const T [], size_t);
  287.  
  288.     // Overridden iterator functions
  289.  
  290.     virtual const T &    Peek() const;
  291.     virtual size_t    Count() const;
  292.  
  293. private:
  294.     virtual bool    FirstPos();
  295.     virtual bool    NextPos();
  296. };
  297.  
  298. /*---------------------------------------------------------------------------
  299.     TLPtrIter -
  300.  
  301.     Base class template for iterators that operate on non-const containers
  302.     of (T *). It can be used as-is, in which case it acts as an iterator
  303.     on an empty collection.
  304. ---------------------------------------------------------------------------*/
  305.  
  306. template<class T> class _TLXCLASS TLPtrIter: public TLIter
  307. {
  308. public:
  309.     typedef void    (*tApplyFunc)(T *&, void *);
  310.     typedef bool    (*tTestFunc)(T *&, void *);
  311.  
  312. public:
  313.     TLPtrIter();
  314.  
  315.     // Default implementations of iterator access: mimic empty collection
  316.  
  317.     virtual T *&    Peek() const;
  318.     virtual bool    Contains(T *&);
  319.     virtual size_t    Count() const;
  320.  
  321.     // Functions to apply an operation to all or a selected number of
  322.     // elements.
  323.  
  324.     void        ForAll(tApplyFunc, void *);
  325.     bool        FirstThat(tTestFunc, void *, T **&);
  326.     bool        FirstThatNot(tTestFunc, void *, T **&);
  327.  
  328.     // Output operations
  329.  
  330.     ostream &        PrintAll(ostream &);
  331.     ostream &        PrintFormat(ostream &, const TLPrintFormat &);
  332.  
  333. private:
  334.     virtual bool    FirstPos();
  335.     virtual bool    NextPos();
  336. };
  337.  
  338. /*---------------------------------------------------------------------------
  339.     TLPtrIterConst -
  340.  
  341.     Base class template for iterators that operate on const containers
  342.     of (T *). It can be used as-is, in which case it acts as an iterator
  343.     on an empty collection.
  344. ---------------------------------------------------------------------------*/
  345.  
  346. template<class T> class _TLXCLASS TLPtrIterConst: public TLIter
  347. {
  348. public:
  349.     typedef void    (*tApplyFunc)(T *, void *);
  350.     typedef bool    (*tTestFunc)(T *, void *);
  351.  
  352. public:
  353.     TLPtrIterConst();
  354.  
  355.     // Default implementations of iterator access: mimic empty collection
  356.  
  357.     virtual T *     Peek() const;
  358.     virtual size_t    Count() const;
  359.     virtual bool    Contains(T *);
  360.  
  361.     // Functions to apply an operation to all or a selected number of
  362.     // elements.
  363.  
  364.     void        ForAll(tApplyFunc, void *);
  365.     bool        FirstThat(tTestFunc, void *, T *&);
  366.     bool        FirstThatNot(tTestFunc, void *, T *&);
  367.  
  368.     // Output operations
  369.  
  370.     ostream &        PrintAll(ostream &);
  371.     ostream &        PrintFormat(ostream &, const TLPrintFormat &);
  372.  
  373. private:
  374.     virtual bool    FirstPos();
  375.     virtual bool    NextPos();
  376. };
  377.  
  378. /*---------------------------------------------------------------------------
  379.     TLPtrItemIter -
  380.  
  381.     Iterator template for 'iteration' over non-const items of type T *.
  382. ---------------------------------------------------------------------------*/
  383.  
  384. template<class T> class _TLXCLASS TLPtrItemIter: public TLPtrIter<T>
  385. {
  386.     T *&        mItem;        // Single item
  387.  
  388. public:
  389.     TLPtrItemIter(T *&);
  390.  
  391.     // Overridden iterator functions
  392.  
  393.     virtual T *&    Peek() const;
  394.     virtual size_t    Count() const;
  395.     virtual bool    Contains(T *&);
  396.  
  397. private:
  398.     virtual bool    FirstPos();
  399.     virtual bool    NextPos();
  400. };
  401.  
  402. /*---------------------------------------------------------------------------
  403.     TLPtrItemIterConst -
  404.  
  405.     Iterator template for 'iteration' over const items of type T *.
  406. ---------------------------------------------------------------------------*/
  407.  
  408. template<class T> class _TLXCLASS TLPtrItemIterConst: public TLPtrIterConst<T>
  409. {
  410.     T *         mItem;        // Single item
  411.  
  412. public:
  413.     TLPtrItemIterConst(T *);
  414.  
  415.     // Overridden iterator functions
  416.  
  417.     virtual T *     Peek() const;
  418.     virtual size_t    Count() const;
  419.     virtual bool    Contains(T *);
  420.  
  421. private:
  422.     virtual bool    FirstPos();
  423.     virtual bool    NextPos();
  424. };
  425.  
  426. /*---------------------------------------------------------------------------
  427.     Inline functions
  428. ---------------------------------------------------------------------------*/
  429.  
  430. //----- TLIter
  431.  
  432. inline bool TLIter::IsReset() const
  433.     { return mState == stReset; }
  434.  
  435. inline bool TLIter::IsValid() const
  436.     { return mState == stValid; }
  437.  
  438. inline bool TLIter::IsAtEnd() const
  439.     { return mState == stEnd; }
  440.  
  441. //----- TLItemIter<T>
  442.  
  443. template<class T> inline size_t TLItemIter<T>::Count() const
  444.     { return 1; }
  445.  
  446. //----- TLItemIterConst<T>
  447.  
  448. template<class T> inline size_t TLItemIterConst<T>::Count() const
  449.     { return 1; }
  450.  
  451. //----- TLVectorIter<T>
  452.  
  453. template<class T> inline size_t TLVectorIter<T>::Count() const
  454.     { return mSize; }
  455.  
  456. //----- TLVectorIterConst<T>
  457.  
  458. template<class T> inline size_t TLVectorIterConst<T>::Count() const
  459.     { return mSize; }
  460.  
  461. #endif    // _TLX_ITER_H
  462.