home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tlx501.zip / INTSET.H < prev    next >
C/C++ Source or Header  |  1996-07-03  |  4KB  |  126 lines

  1. /****************************************************************************
  2.     $Id: intset.h 501.0 1995/03/07 12:26:44 RON Exp $
  3.  
  4.     Copyright (c) 1991-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 class TLIntSet, representing a set of integers.
  10.  
  11.     $Log: intset.h $
  12.     Revision 501.0  1995/03/07 12:26:44  RON
  13.     Updated for TLX 5.01
  14.     Revision 1.4  1995/01/31 16:29:22  RON
  15.     Update for release 012
  16.     Added partial support for SunPro C++ compiler
  17.     Revision 1.3  1994/10/05  18:21:47  ron
  18.     Added output operator for TLIntSet
  19.  
  20.     Revision 1.2  1994/09/27  20:25:16  ron
  21.     Changed path separator from / to \
  22.  
  23.     Revision 1.1  1994/09/26  15:18:12  ron
  24.     Initial revision
  25.  
  26. ****************************************************************************/
  27.  
  28. #ifndef _TLX_INTSET_H
  29. #define _TLX_INTSET_H
  30.  
  31. #ifndef _TLX_BITVECT_H
  32. #include <tlx\501\bitvect.h>
  33. #endif
  34. #ifndef _TLX_ITER_H
  35. #include <tlx\501\iter.h>
  36. #endif
  37.  
  38. /*---------------------------------------------------------------------------
  39.     TLIntSet -
  40.  
  41.     Set of integers with corresponding operations. The set has a fixed
  42.     capacity, which cannot be expanded after creation.
  43. ---------------------------------------------------------------------------*/
  44.  
  45. class _TLXCLASS TLIntSet
  46. {
  47. public:
  48.     // Iterator class to iterate through all elements of the set
  49.  
  50.     class _TLXCLASS Iter: public TLValueIterConst<int>
  51.     {
  52.         const TLIntSet &        mSet;   // Set being iterated
  53.         int                     mPos;   // Iteration position within set
  54.  
  55.     public:
  56.         Iter(const TLIntSet &);
  57.  
  58.         // Overridden iterator operations
  59.  
  60.         virtual const int &     Peek() const;
  61.         virtual size_t          Count() const;
  62.  
  63.     private:
  64.         virtual bool            FirstPos();
  65.         virtual bool            NextPos();
  66.         bool                    SearchFrom(int);
  67.     };
  68.     friend _TLXCLASS class Iter;
  69.  
  70. private:
  71.     TLBitVector         mSet;           // The set representation
  72.     int                 mBase;          // Base element
  73.  
  74. public:
  75.     TLIntSet(size_t = 0);               // Capacity for [0, size>
  76.     TLIntSet(int, int);                 // Capacity for [lower, upper]
  77.     TLIntSet(int *, size_t);            // Capacity for assorted elements
  78.  
  79.     // General information about the set
  80.  
  81.     size_t              Count() const;
  82.     bool                Contains(int) const;
  83.     bool                IsEmpty() const;
  84.  
  85.     // Limits of the set:
  86.     //
  87.     // - LowerBound() and UpperBound() indicate capacity bounds
  88.     // - First() and Last() return the current smallest and largest
  89.     //   elements, respectively.
  90.  
  91.     int                 LowerBound() const;
  92.     int                 UpperBound() const;
  93.     int                 First() const;
  94.     int                 Last() const;
  95.  
  96.     // Adding elements to the set
  97.  
  98.     void                Insert(int);
  99.     void                Insert(int, int);
  100.     void                Insert(int *, size_t);
  101.     void                InsertAll();
  102.  
  103.     // Removing elements from the set
  104.  
  105.     void                Remove(int);
  106.     void                Remove(int, int);
  107.     void                Remove(int *, size_t);
  108.     void                RemoveAll();
  109.  
  110.     // Output operator
  111.  
  112.     friend ostream &    _TLXFUNC operator <<(ostream &, const TLIntSet &);
  113. };
  114.  
  115. /*---------------------------------------------------------------------------
  116.     Inline functions
  117. ---------------------------------------------------------------------------*/
  118.  
  119. inline size_t TLIntSet::Iter::Count() const
  120.     { return mSet.Count(); }
  121.  
  122. inline bool TLIntSet::IsEmpty() const
  123.     { return Count() == 0; }
  124.  
  125. #endif // _TLX_INTSET_H
  126.