home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume19
/
wacco
/
part01
/
bitset.h
< prev
next >
Wrap
C/C++ Source or Header
|
1991-05-19
|
3KB
|
79 lines
// Copyright (c) 1991 by Parag Patel. All Rights Reserved.
// $Header: bitset.h,v 1.3 91/02/22 16:04:57 hmgr Exp $
#define __B_S_BIT(n) ((long)(1L << (n & 0x1F)))
#define __B_S_ELT(n) (n >> 5)
// a set of ints (esp. enums) - also an array of bits
class Bitset
{
int num;
long *elts;
friend class Bitsetiter;
void bumpsize(int); // grow the Bitset
// inline for speed
void BUMPSIZE(int largest) { if (num < largest) bumpsize(largest); }
public:
Bitset(int, int,...); // new element list
Bitset(int); // new size
Bitset(Bitset const &); // new Bitset
Bitset() { elts = new long; num = 0; elts[0] = 0L; }
~Bitset() { delete elts; } // destructor
Bitset& operator=(Bitset const &); // dup
Bitset& add(int); // add to
Bitset& remove(int); // delete from
Bitset& operator+=(int e) { return add(e); }
Bitset& operator-=(int e) { return remove(e); }
int size() const; // number of elements in Bitset
int card() const { return size(); } // for clarity?
boolean isin(int bit) const
{ return bit >= 0 && bit <= num &&
(elts[__B_S_ELT(bit)] & __B_S_BIT(bit)); }
void clear(); // clear the set
unsigned long hash() const; // return a hash number for this set
Bitset& operator|=(Bitset const &); // union with
Bitset& operator&=(Bitset const &); // intersect with
Bitset& operator-=(Bitset const &); // difference from
Bitset& operator^=(Bitset const &); // symmetric difference from
Bitset& operator~(); // complement self
int get(int elt) const
{ return (elt >= 0 && elt <= num &&
(elts[__B_S_ELT(elt)] & __B_S_BIT(elt))) ? 1 : 0; }
int operator[](int elt) const { return get(elt); }
// equality testing:
boolean operator==(Bitset const &s) const;
boolean operator!=(Bitset const &s) const { return !(s == *this); }
// subset and superset:
boolean operator<=(Bitset const &s) const;
boolean operator>=(Bitset const &s) const { return s <= *this; }
// proper subset and proper superset:
boolean operator<(Bitset const &s) const
{ return *this != s && *this <= s; }
boolean operator>(Bitset const &s) const { return s < *this; }
};
// iterator over a Bitset
class Bitsetiter
{
int elt, len;
int *arr;
public:
Bitsetiter(Bitset const &); // creator - iterate over a set
~Bitsetiter() { delete arr; }
// get next element in set
int operator()() { return elt >= len ? elt = 0, -1 : arr[elt++]; }
};
#undef __B_S_ELT
#undef __B_S_BIT