Category : C++ Source Code
Archive   : WHEATLIB.ZIP
Filename : BITSET.H

 
Output of file : BITSET.H contained in archive : WHEATLIB.ZIP

#ifndef BitSetIncluded
#define BitSetIncluded

// copyright (c) 1992 by Paul Wheaton
// 1916 Brooks #205, Missoula, MT 59801
//
// voice phone: (406)543-7543
// modem phone: (406)543-1144 (2400N81)
// CompuServe: 72707,207
// Internet: [email protected]

#include

// for access by BitSets only
class BitSetRef
{
Byte* P; // pointer to byte containing bit
Byte M; // mask for bit
public:
BitSetRef(Byte* BP, int BitOffset);
#ifdef BorlandFixedByteMath
operator Bool() { return ((*P & M) != 0); }
void operator&=(Bool b) { if (!b) *P &= ~M; }
void operator|=(Bool b) { if (b) *P |= M; }
void operator^=(Bool b) { if (b) *P ^= M; }
#else
operator Bool() { return ((int(*P) & int(M)) != 0); }
void operator&=(Bool b);
void operator|=(Bool b);
void operator^=(Bool b);
#endif
Bool operator=(Bool b);
};

void ClearBitRange(void* P, Long Index1,Long Index2);
void SetBitRange(void* P, Long Index1,Long Index2,Bool X=On);

#define CreateBitSetClass(ClassName,BitQuan) \
class ClassName \
{ \
Byte B[(BitQuan+7)/8]; \
public: \
ClassName(){memset(&B[0],0,(BitQuan+7)/8);} \
ClassName(const ClassName& BS){memcpy(&B[0],&(BS.B[0]),(BitQuan+7)/8);} \
BitSetRef Ref(int Index) {return BitSetRef(&B[0],Index);} \
BitSetRef operator[](int Index){return BitSetRef(&B[0],Index);} \
Bool At(int Index){return ((B[Index/8] & (1<<(Index%8)) )!=0);} \
Bool operator()(int Index) \
{return ((B[Index/8] & (1<<(Index%8)) )!=0);} \
void SetRange(int Index1, int Index2, Bool OnOrOff=On) \
{SetBitRange(&B[0],Index1,Index2,OnOrOff);} \
void ClearRange(int Index1, int Index2) \
{ClearBitRange(&B[0],Index1,Index2);} \
void SetAll(Bool OnOrOff=On) {SetBitRange(&B[0],0,BitQuan-1,OnOrOff);} \
void ClearAll() {ClearBitRange(&B[0],0,BitQuan-1);} \
};

CreateBitSetClass(BitSet8,8)
CreateBitSetClass(BitSet16,16)

#endif