home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume19 / wacco / part01 / bitset.h < prev    next >
C/C++ Source or Header  |  1991-05-19  |  3KB  |  79 lines

  1. // Copyright (c) 1991 by Parag Patel.  All Rights Reserved.
  2. // $Header: bitset.h,v 1.3 91/02/22 16:04:57 hmgr Exp $
  3.  
  4. #define __B_S_BIT(n) ((long)(1L << (n & 0x1F)))
  5. #define __B_S_ELT(n) (n >> 5)
  6.  
  7. // a set of ints (esp. enums) - also an array of bits
  8. class Bitset
  9. {
  10.     int num;
  11.     long *elts;
  12.  
  13.     friend class Bitsetiter;
  14.  
  15.     void bumpsize(int);        // grow the Bitset
  16.     // inline for speed
  17.     void BUMPSIZE(int largest) { if (num < largest) bumpsize(largest); }
  18.  
  19. public: 
  20.     Bitset(int, int,...);    // new element list
  21.     Bitset(int);        // new size
  22.     Bitset(Bitset const &);    // new Bitset
  23.     Bitset() { elts = new long; num = 0; elts[0] = 0L; }
  24.     ~Bitset() { delete elts; }    // destructor
  25.  
  26.     Bitset& operator=(Bitset const &);   // dup
  27.  
  28.     Bitset& add(int);        // add to
  29.     Bitset& remove(int);    // delete from
  30.     Bitset& operator+=(int e) { return add(e); }
  31.     Bitset& operator-=(int e) { return remove(e); }
  32.  
  33.     int size() const;            // number of elements in Bitset
  34.     int card() const { return size(); }    // for clarity?
  35.     boolean isin(int bit) const
  36.     { return bit >= 0 && bit <= num &&
  37.         (elts[__B_S_ELT(bit)] & __B_S_BIT(bit)); }
  38.     void clear();        // clear the set
  39.     unsigned long hash() const;    // return a hash number for this set
  40.  
  41.     Bitset& operator|=(Bitset const &);  // union with
  42.     Bitset& operator&=(Bitset const &);  // intersect with
  43.     Bitset& operator-=(Bitset const &);  // difference from
  44.     Bitset& operator^=(Bitset const &);  // symmetric difference from
  45.     Bitset& operator~();    // complement self
  46.  
  47.     int get(int elt) const
  48.         { return (elt >= 0 && elt <= num &&
  49.             (elts[__B_S_ELT(elt)] & __B_S_BIT(elt))) ? 1 : 0; }
  50.     int operator[](int elt) const { return get(elt); }
  51.  
  52.     // equality testing:
  53.     boolean operator==(Bitset const &s) const;
  54.     boolean operator!=(Bitset const &s) const { return !(s == *this); }
  55.     // subset and superset:
  56.     boolean operator<=(Bitset const &s) const;
  57.     boolean operator>=(Bitset const &s) const { return s <= *this; }
  58.     // proper subset and proper superset:
  59.     boolean operator<(Bitset const &s) const 
  60.         { return *this != s && *this <= s; }
  61.     boolean operator>(Bitset const &s) const { return s < *this; }
  62. };
  63.  
  64. // iterator over a Bitset
  65. class Bitsetiter
  66. {
  67.     int elt, len;
  68.     int *arr;
  69.  
  70. public: 
  71.     Bitsetiter(Bitset const &);    // creator - iterate over a set
  72.     ~Bitsetiter() { delete arr; }
  73.     // get next element in set
  74.     int operator()() { return elt >= len ? elt = 0, -1 : arr[elt++]; }
  75. };
  76.  
  77. #undef __B_S_ELT
  78. #undef __B_S_BIT
  79.