home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / CTECHAPP.ZIP / SETS.ZIP / BITSET.HPP < prev    next >
C/C++ Source or Header  |  1990-03-26  |  2KB  |  107 lines

  1. //  BitSet
  2. //      2.10    25-Mar-1990
  3. //      C++ 2.0
  4. //
  5. //      Defines a set of bits
  6. //
  7. //      Written by Scott Robert Ladd
  8.  
  9. #if !defined(_BitSet_HPP)
  10. #define _BitSet_HPP 1
  11.  
  12. extern "C"
  13.     {
  14.     #include "stddef.h"
  15.     #include "string.h"
  16.     }
  17.  
  18. class BitSet
  19.     {
  20.     protected:
  21.         unsigned long   Length;
  22.         unsigned char * Data;
  23.  
  24.         BitSet()
  25.             {
  26.             Length = 0L;
  27.             Data   = NULL;
  28.             }
  29.  
  30.     public:
  31.         // constructors
  32.         BitSet(unsigned long size);
  33.  
  34.         BitSet(BitSet & bs);
  35.  
  36.         // destructor
  37.         ~BitSet(void);
  38.  
  39.         // assignment operator
  40.         void operator = (BitSet & bs);
  41.  
  42.         // Get number of bits in set
  43.         unsigned long Size()
  44.             {
  45.             return Length;
  46.             }
  47.  
  48.         // operation methods
  49.         void Include(unsigned long bit)
  50.             {
  51.             if (bit < Length)
  52.                 Data[bit / 8] |= (unsigned char)(1 << (bit & 7));
  53.             }
  54.  
  55.         void Exclude(unsigned long bit)
  56.             {
  57.             if (bit < Length)
  58.                 Data[bit / 8] &= ~(unsigned char)(1 << (bit & 7));
  59.             }
  60.  
  61.         // turn all bits in set on
  62.         void AllOn()
  63.             {
  64.             memset(Data,'\xFF',(Length + 7) / 8);
  65.             }
  66.  
  67.         // turn all bits in set off
  68.         void AllOff()
  69.             {
  70.             memset(Data,'\x00',(Length + 7) / 8);
  71.             }
  72.  
  73.         // union operators
  74.         BitSet operator & (BitSet & bs);
  75.         BitSet operator &= (BitSet & bs);
  76.  
  77.         // synonyms for union operators
  78.         BitSet operator +  (BitSet & bs);
  79.         BitSet operator += (BitSet & bs);
  80.  
  81.         // intersection operators
  82.         BitSet operator |  (BitSet & bs);
  83.         BitSet operator |= (BitSet & bs);
  84.  
  85.         // difference operators
  86.         BitSet operator -  (BitSet & bs);
  87.         BitSet operator -= (BitSet & bs);
  88.  
  89.         // complement operator
  90.         BitSet operator ~ ();
  91.  
  92.         // comparison operator
  93.         int operator == (BitSet & bs);
  94.         int operator != (BitSet & bs);
  95.  
  96.         // value retrieval method
  97.         int operator [] (unsigned long bit)
  98.             {
  99.             if (bit < Length)
  100.                 return (Data[bit / 8] & (1 << (bit & 7)));
  101.             else
  102.                 return 0;
  103.             }
  104.     };
  105.  
  106. #endif
  107.