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

  1. /****************************************************************************
  2.     $Id: ptrarray.h 501.0 1995/03/07 12:26:44 RON Exp $
  3.  
  4.     Copyright (c) 1993-95 Tarma Software Research. All rights reserved.
  5.  
  6.     Project:    Tarma Library for C++
  7.     Author:    Ron van der Wal
  8.  
  9.     Declarations of classes that implement array-based collections of (T *):
  10.  
  11.     - TLPtrVector<T>
  12.     - TLPtrArray<T>
  13.     - TLPtrSeq<T>
  14.     - TLPtrQueue<T>
  15.     - TLPtrStack<T>
  16.     - TLPtrSet<T>
  17.  
  18.     All templates are type-safe wrappers around the (void *) versions of
  19.     the same collections.
  20.  
  21.     NOTE: By default, all pointed to objects of type 'T' are deleted by
  22.     means of the operator delete. This implies that they should have been
  23.     allocated with the operator new.
  24.  
  25.     $Log: ptrarray.h $
  26.     Revision 501.0  1995/03/07 12:26:44  RON
  27.     Updated for TLX 5.01
  28.     Revision 1.5  1995/01/31 16:32:12  RON
  29.     Update for release 012
  30.     Added partial support for SunPro C++ compiler
  31.     Revision 1.4  1994/10/05  18:23:18  ron
  32.     Added more functions that accept iterators
  33.  
  34.     Revision 1.3  1994/09/27  20:25:26  ron
  35.     Changed path separator from / to \
  36.  
  37.     Revision 1.2  1994/09/26  15:19:39  ron
  38.     Moved inline functions to separate file
  39.     Added iterator classes
  40.  
  41.     Revision 1.1  1994/08/16  18:06:49  ron
  42.     Initial revision
  43.  
  44. ****************************************************************************/
  45.  
  46. #ifndef _TLX_PTRARRAY_H
  47. #define _TLX_PTRARRAY_H
  48.  
  49. #ifndef _TLX_ITER_H
  50. #include <tlx\501\iter.h>
  51. #endif
  52. #ifndef _TLX_VPARRAYS_H
  53. #include <tlx\501\vparrays.h>
  54. #endif
  55.  
  56. /*---------------------------------------------------------------------------
  57.     TLPtrVector<T> -
  58.  
  59.     Class template for vectors of (T *). It is implemented as a type-safe
  60.     wrapper around class TLVPVector.
  61. ---------------------------------------------------------------------------*/
  62.  
  63. template<class T> class TLPtrVector: public TLVPVector
  64. {
  65. public:
  66.     static void        DeleteT(void *);
  67.  
  68. public:
  69.     // Constructors to cater for the most common situations.
  70.  
  71.     TLPtrVector(size_t = 0);        // Creates sized vector; default = 0
  72.     TLPtrVector(T *);            // Creates single element vector
  73.     TLPtrVector(T **, size_t);        // Creates copy of C-style vector
  74.     TLPtrVector(const TLPtrVector<T> &);    // Copies other vector
  75.  
  76.     // Assignment operators for common situations.
  77.  
  78.     TLPtrVector<T> &    operator =(const TLPtrVector<T> &);
  79.     TLPtrVector<T> &    operator =(T *);
  80.  
  81.     // Access to vector elements. We provide both range checked and
  82.     // direct access versions (for speed). The operator [] versions have
  83.     // range checking; the PeekAt() versions don't.
  84.  
  85.     T *&        operator [](index_t);
  86.     T *            operator [](index_t) const;
  87.     T *&        PeekAt(index_t);
  88.     T *            PeekAt(index_t) const;
  89.     index_t        IndexOf(const T *) const;
  90.  
  91.     // The following function gives access to the implementation vector.
  92.     // It should be used with care.
  93.  
  94.     T **        StorageVector() const;
  95. };
  96.  
  97. /*---------------------------------------------------------------------------
  98.     TLPtrArray<T> -
  99.  
  100.     Class template for arrays of (T *) Indexing may start at any
  101.     base; the array is resizable and reindexable. Indexed access is
  102.     range checked by default.
  103. ---------------------------------------------------------------------------*/
  104.  
  105. template<class T> class TLPtrArray: public TLVPArray
  106. {
  107. public:
  108.     static void        DeleteT(void *);
  109.  
  110. public:
  111.     // Constructors to cater for the most common situations.
  112.  
  113.     TLPtrArray(size_t = 0);        // Creates 0-based array; default = 0
  114.     TLPtrArray(index_t, index_t);        // Creates array [from, to]
  115.     TLPtrArray(T *);            // Creates single element array
  116.     TLPtrArray(T **, size_t);        // Creates copy of C-style vector
  117.     TLPtrArray(const TLPtrArray<T> &);    // Copies other array
  118.  
  119.     // Assignment operators for common situations.
  120.  
  121.     TLPtrArray<T> &    operator =(const TLPtrArray<T> &);
  122.     TLPtrArray<T> &    operator =(T *);
  123.  
  124.     // Access to array elements. We provide both range checked and
  125.     // direct access versions (for speed). The operator [] versions have
  126.     // range checking; the PeekAt() versions don't and are implemented inline.
  127.  
  128.     T *&        operator [](index_t);
  129.     T *            operator [](index_t) const;
  130.     T *&        PeekAt(index_t);
  131.     T *            PeekAt(index_t) const;
  132.     index_t        IndexOf(const T *) const;
  133. };
  134.  
  135. /*---------------------------------------------------------------------------
  136.     TLPtrSeq<T> -
  137.  
  138.     Class that implements a resizable sequence of (void *). Elements can be
  139.     added at any place; access is by index (1-based), by position, or by
  140.     value.
  141. ---------------------------------------------------------------------------*/
  142.  
  143. const size_t kDefaultDelta = 100;
  144.  
  145. template<class T> class TLPtrSeq: public TLVPSeq
  146. {
  147. public:
  148.     static void        DeleteT(void *);
  149.  
  150. public:
  151.     TLPtrSeq(size_t = 0, size_t = kDefaultDelta);
  152.     TLPtrSeq(T *);            // Single element sequence
  153.     TLPtrSeq(T **, size_t);        // From C-style vector
  154.     TLPtrSeq(TLPtrIter<T> &);        // From iterator
  155.     TLPtrSeq(const TLPtrSeq<T> &);    // Copy constructor
  156.  
  157.     // Assignment operators.
  158.  
  159.     TLPtrSeq<T> &    operator =(const TLPtrSeq<T> &);
  160.     TLPtrSeq<T> &    operator =(TLPtrIter<T> &);
  161.     TLPtrSeq<T> &    operator =(T *);
  162.     TLPtrSeq<T> &    operator +=(const TLPtrSeq<T> &);
  163.     TLPtrSeq<T> &    operator +=(TLPtrIter<T> &);
  164.     TLPtrSeq<T> &    operator +=(T *);
  165.  
  166.     // Insertion and removal of elements. Append() functions add the elements
  167.     // at the end of the sequence; Prepend() functions doe so at the start;
  168.     // the Insert() versions allow the user to specify the index for insertion.
  169.     // With the Replace() functions it is possible to replace parts of the
  170.     // sequence.
  171.  
  172.     void        Append(T *);
  173.     void        Append(T **, size_t);
  174.     void        Append(TLPtrIter<T> &);
  175.     void        Append(const TLPtrSeq<T> &);
  176.     void        Prepend(T *);
  177.     void        Prepend(T **, size_t);
  178.     void        Prepend(TLPtrIter<T> &);
  179.     void        Prepend(const TLPtrSeq<T> &);
  180.     void        Insert(T *);
  181.     void        InsertAt(index_t, T *);
  182.     void        InsertAt(index_t, T **, size_t);
  183.     void        InsertAt(index_t, TLPtrIter<T> &);
  184.     void        InsertAt(index_t, const TLPtrSeq<T> &);
  185.     void        Replace(index_t, T *);
  186.     void        Replace(index_t, T **, size_t);
  187.     void        Replace(index_t, TLPtrIter<T> &);
  188.     void        Replace(index_t, const TLPtrSeq<T> &);
  189.     T *            Extract(T *);
  190.     T *            Extract(index_t);
  191.     TLPtrSeq<T>        Extract(index_t, size_t);
  192.     T *            ExtractFirst();
  193.     TLPtrSeq<T>        ExtractFirst(size_t);
  194.     T *            ExtractLast();
  195.     TLPtrSeq<T>        ExtractLast(size_t);
  196.  
  197.     // Indexed access to the elements. The operator [] versions are range
  198.     // checked, while the PeekAt() functions are not and are implemented
  199.     // inline for speed.
  200.  
  201.     T *&        operator [](index_t);
  202.     T *            operator [](index_t) const;
  203.     T *&        PeekAt(index_t);
  204.     T *            PeekAt(index_t) const;
  205.     T *&        PeekReverse(index_t);
  206.     T *            PeekReverse(index_t) const;
  207.     T *&        PeekFirst();
  208.     T *            PeekFirst() const;
  209.     T *&        PeekLast();
  210.     T *            PeekLast() const;
  211.  
  212.     bool         Contains(T *) const;
  213.     index_t        IndexOf(const T *) const;
  214.     bool         Search(const T *, index_t &) const;
  215. };
  216.  
  217. //-----    Iterator class for non-const sequences
  218.  
  219. template<class T> class TLPtrSeqIter: public TLPtrIter<T>
  220. {
  221.     TLPtrSeq<T> &    mSeq;
  222.     index_t        mPos;
  223.  
  224. public:
  225.     TLPtrSeqIter(TLPtrSeq<T> &);
  226.  
  227.     // Overridden iterator functions
  228.  
  229.     virtual T *&    Peek() const;
  230.     virtual size_t    Count() const;
  231.  
  232. private:
  233.     virtual bool    FirstPos();
  234.     virtual bool    NextPos();
  235. };
  236.  
  237. //-----    Iterator class for const sets
  238.  
  239. template<class T> class TLPtrSeqIterConst: public TLPtrIterConst<T>
  240. {
  241.     const TLPtrSeq<T> &    mSeq;
  242.     index_t        mPos;
  243.  
  244. public:
  245.     TLPtrSeqIterConst(const TLPtrSeq<T> &);
  246.  
  247.     // Overridden iterator functions
  248.  
  249.     virtual T *        Peek() const;
  250.     virtual size_t    Count() const;
  251.  
  252. private:
  253.     virtual bool    FirstPos();
  254.     virtual bool    NextPos();
  255. };
  256.  
  257. /*---------------------------------------------------------------------------
  258.     TLPtrQueue -
  259.  
  260.     Class that implements a resizable queue of (T *). The queue can be
  261.     used as an ordinary FIFO queue, or as a double-ended one.
  262. ---------------------------------------------------------------------------*/
  263.  
  264. template<class T> class TLPtrQueue: public TLVPQueue
  265. {
  266. public:
  267.     static void        DeleteT(void *);
  268.  
  269. public:
  270.     TLPtrQueue(size_t = 0, size_t = 0);    // Creates initial size & expansion
  271.     TLPtrQueue(T *);            // Single element sequence
  272.     TLPtrQueue(T **, size_t);        // Copies C-style vector
  273.     TLPtrQueue(const TLPtrQueue<T> &);    // Copy constructor
  274.     TLPtrQueue(TLPtrIter<T> &);        // From iterator
  275.     TLPtrQueue(TLPtrIterConst<T> &);    // From iterator
  276.  
  277.     // Assignment operators.
  278.  
  279.     TLPtrQueue<T> &    operator =(const TLPtrQueue<T> &);
  280.     TLPtrQueue<T> &    operator =(T *);
  281.  
  282.     // Insertion and removal of elements. For a double-ended queue:
  283.     //
  284.     // - AddLeft() and AddRight() add elements at either end
  285.     // - ExtractLeft() and ExtractRight() remove and return them
  286.     // - RemoveLeft() and RemoveRight() remove them
  287.     // - PeekLeft() and PeekRight() allow access to those elements
  288.  
  289.     void        AddLeft(T *);
  290.     void        AddRight(T *);
  291.     T *            ExtractLeft();
  292.     T *            ExtractRight();
  293.     T *&        PeekLeft();
  294.     T *            PeekLeft() const;
  295.     T *&        PeekRight();
  296.     T *            PeekRight() const;
  297.  
  298.     // For FIFO queue treatment:
  299.     //
  300.     // - Enqueue() adds elements at the back end
  301.     // - Dequeue() removes and returns elements from the front end
  302.     // - PeekHead() and PeekTail() allow inspection
  303.  
  304.     void        Enqueue(T *);
  305.     void        EnqueueUnique(T *);
  306.     T *            Dequeue();
  307.     T *&        PeekHead();
  308.     T *            PeekHead() const;
  309.     T *&        PeekTail();
  310.     T *            PeekTail() const;
  311.  
  312.     // For additional flexibility, it is also possible to check for
  313.     // the presence of an element and remove it if necessary:
  314.     //
  315.     // - RemoveAll() discards all elements
  316.     // - Contains() checks for presence
  317.     // - Extract() removes and returns an element
  318.     // - Remove() removes an element
  319.  
  320.     bool         Contains(T *) const;
  321.     T *            Extract(T *);
  322.     void        Remove(T *);
  323. };
  324.  
  325. /*---------------------------------------------------------------------------
  326.     TLPtrStack -
  327.  
  328.     Class that implements a resizable stack of (T *).
  329. ---------------------------------------------------------------------------*/
  330.  
  331. template<class T> class TLPtrStack: public TLVPStack
  332. {
  333. public:
  334.     static void        DeleteT(void *);
  335.  
  336. public:
  337.     TLPtrStack(size_t = 0, size_t = 0);    // Creates initial size & expansion
  338.     TLPtrStack(T *);            // Single element sequence
  339.     TLPtrStack(T **, size_t);        // Copies C-style vector
  340.     TLPtrStack(const TLPtrStack<T> &);    // Copy constructor
  341.  
  342.     // Assignment operators.
  343.  
  344.     TLPtrStack<T> &    operator =(const TLPtrStack<T> &);
  345.     TLPtrStack<T> &    operator =(T *);
  346.  
  347.     // Insertion and removal of elements is on a LIFO basis.
  348.  
  349.     void        Push(T *);
  350.     void        Push(T **, size_t);
  351.     T *            Pop();
  352.     T *            Pop(T *);
  353.     void        Remove(T *);
  354.     T *&        PeekTop();
  355.     T *            PeekTop() const;
  356.  
  357.     // A few extra member function for additional flexibility.
  358.  
  359.     bool         Contains(T *);
  360. };
  361.  
  362. /*---------------------------------------------------------------------------
  363.     TLPtrSet<T> -
  364.  
  365.     Class that implements a set of (T *). Elements can be inserted and
  366.     removed.
  367. ---------------------------------------------------------------------------*/
  368.  
  369. template<class T>  class TLPtrSetIter;
  370. template<class T>  class TLPtrSetIterConst;
  371.  
  372. template<class T> class TLPtrSet: public TLVPSet
  373. {
  374.     friend class TLPtrSetIter<T>;
  375.     friend class TLPtrSetIterConst<T>;
  376.  
  377. public:
  378.     static void        DeleteT(void *);
  379.  
  380. public:
  381.     TLPtrSet(size_t = 0, size_t = 0);    // Creates initial size & expansion
  382.     TLPtrSet(T *);            // Single element set
  383.     TLPtrSet(const TLPtrSet<T> &);    // Copy constructor
  384.     TLPtrSet(TLPtrIterConst<T> &);    // From iterator
  385.  
  386.     // Assignment operators.
  387.  
  388.     TLPtrSet<T> &    operator =(T *);
  389.     TLPtrSet<T> &    operator =(const TLPtrSet<T> &);
  390.     TLPtrSet<T> &    operator =(TLPtrIterConst<T> &);
  391.     TLPtrSet<T> &    operator +=(T *);
  392.     TLPtrSet<T> &    operator +=(const TLPtrSet<T> &);
  393.     TLPtrSet<T> &    operator +=(TLPtrIterConst<T> &);
  394.  
  395.     // Insertion and removal of elements. Insert() adds an element; Remove()
  396.     // removes it.
  397.  
  398.     void        Insert(T *);
  399.     void        Insert(TLPtrIterConst<T> &);
  400.     T *            Extract(T *);
  401.     void        Remove(T *);
  402.     bool         Contains(const T *) const;
  403.  
  404. private:
  405.     T *&        PeekAt(index_t);
  406.     T *            PeekAt(index_t) const;
  407. };
  408.  
  409. //-----    Iterator class for non-const sets
  410.  
  411. template<class T> class TLPtrSetIter: public TLPtrIter<T>
  412. {
  413.     TLPtrSet<T> &    mSet;
  414.     index_t        mPos;
  415.  
  416. public:
  417.     TLPtrSetIter(TLPtrSet<T> &);
  418.  
  419.     // Overridden iterator functions
  420.  
  421.     virtual T *&    Peek() const;
  422.     virtual size_t    Count() const;
  423.  
  424. private:
  425.     virtual bool    FirstPos();
  426.     virtual bool    NextPos();
  427. };
  428.  
  429. //-----    Iterator class for const sets
  430.  
  431. template<class T> class TLPtrSetIterConst: public TLPtrIterConst<T>
  432. {
  433.     const TLPtrSet<T> &    mSet;
  434.     index_t        mPos;
  435.  
  436. public:
  437.     TLPtrSetIterConst(const TLPtrSet<T> &);
  438.  
  439.     // Overridden iterator functions
  440.  
  441.     virtual T *        Peek() const;
  442.     virtual size_t    Count() const;
  443.  
  444. private:
  445.     virtual bool    FirstPos();
  446.     virtual bool    NextPos();
  447. };
  448.  
  449. /*---------------------------------------------------------------------------
  450.     Inline functions
  451. ---------------------------------------------------------------------------*/
  452.  
  453. #include <tlx\501\ptrarray.inl>
  454.  
  455. #endif    // _TLX_PTRARRAY_H
  456.  
  457.