home *** CD-ROM | disk | FTP | other *** search
- #ifndef __RWBITVEC_H__
- #define __RWBITVEC_H__
- pragma push_align_members(64);
-
- /*
- * Ar`bit'rarily long bit vector.
- *
- * $Header: E:/vcs/rw/bitvec.h_v 1.5 17 Mar 1992 19:21:08 KEFFER $
- *
- ****************************************************************************
- *
- * Rogue Wave
- * P.O. Box 2328
- * Corvallis, OR 97339
- * Voice: (503) 754-3010 FAX: (503) 757-6650
- *
- * Copyright (C) 1989, 1990, 1991. This software is subject to copyright
- * protection under the laws of the United States and other countries.
- *
- ***************************************************************************
- *
- * $Log: E:/vcs/rw/bitvec.h_v $
- *
- * Rev 1.5 17 Mar 1992 19:21:08 KEFFER
- * Changed BOUNDS_CHECK to RWBOUNDS_CHECK
- *
- * Rev 1.4 04 Mar 1992 09:03:02 KEFFER
- * nil changed to rwnil
- *
- * Rev 1.3 18 Feb 1992 19:21:42 KEFFER
- * Added m.f. nfull()
- *
- * Rev 1.2 28 Oct 1991 09:08:08 keffer
- * Changed inclusions to <rw/xxx.h>
- *
- * Rev 1.1 10 Oct 1991 09:35:10 keffer
- * Exported global operators and ::sum().
- *
- * Rev 1.0 28 Jul 1991 08:12:16 keffer
- * Tools.h++ V4.0.5 PVCS baseline version
- *
- */
-
- /*
- * Defining the preprocessor directive RWBOUNDS_CHECK will
- * cause bounds checking on the subscripting operator.
- */
-
- #include "rw/bitref.h"
- #include "rw/mempool.h"
-
- class RWBitVec : public RWMemoryPool {
- private:
- RWByte* v; // The bit data, packed within a byte vector
- unsigned npts; // Length of vector
- void indexRangeErr(int) const;
- void lengthErr(unsigned, unsigned) const;
- unsigned nbytes() const { return (npts+7) >> 3; }
- unsigned nfull() const { return npts >> 3; }
- public:
- RWBitVec() { v=rwnil; npts=0; }
- RWBitVec(unsigned N);
- RWBitVec(unsigned N, RWBoolean initVal);
- RWBitVec(const RWByte*, unsigned N);
- RWBitVec(const RWBitVec&);
- ~RWBitVec() { delete v; }
-
- // Assignment operators
- RWBitVec& operator=(const RWBitVec&);
- RWBitVec& operator=(RWBoolean);
- RWBitVec& operator&=(const RWBitVec&);
- RWBitVec& operator^=(const RWBitVec&);
- RWBitVec& operator|=(const RWBitVec&);
-
- // Indexing operators
- RWBitRef operator[](int i); // Can be used as lvalue
- RWBitRef operator()(int i); // ditto
- #ifdef CONST_OVERLOADS
- RWBoolean operator[](int i) const; // Can't be used as lvalue
- RWBoolean operator()(int i) const; // ditto
- #endif
-
- // Other operators ---
- RWBoolean operator==(const RWBitVec& u) const {return isEqual(u);}
- RWBoolean operator!=(const RWBitVec& u) const {return !isEqual(u);}
- RWBoolean operator==(RWBoolean b) const;
- RWBoolean operator!=(RWBoolean b) const {return !operator==(b);}
-
- friend RWBitVec rwexport operator!(const RWBitVec&);
- friend RWBitVec rwexport operator&(const RWBitVec&,const RWBitVec&);
- friend RWBitVec rwexport operator^(const RWBitVec&,const RWBitVec&);
- friend RWBitVec rwexport operator|(const RWBitVec&,const RWBitVec&);
- friend ostream& operator<<(ostream&, const RWBitVec&);
- friend istream& operator>>(istream&, RWBitVec&);
-
- // Member functions
- void clearBit(int i); // Clear bit i
- const RWByte* data() const { return v; }
- unsigned hash() const;
- RWBoolean isEqual(const RWBitVec&) const;
- unsigned length() const {return npts;}
- ostream& printOn(ostream&) const;
- void restoreFrom(RWvistream&);
- void restoreFrom(RWFile&);
- void resize(unsigned);
- void saveOn(RWvostream&) const;
- void saveOn(RWFile&) const;
- istream& scanFrom(istream&);
- void setBit(int i); // Set bit i
- RWBoolean testBit(int i) const; // Return value of bit i
-
- // Other friend functions:
- friend int rwexport sum(const RWBitVec&); // Total # of bits set
- };
-
- // This macro isolates bit 'i'
- #define RWBIT(i) (*(((i)>>3) + (v)) & (1<<(7&(i))))
-
- inline RWBitRef
- RWBitVec::operator[](int i)
- {
- if ((unsigned)i >= npts) indexRangeErr(i);
- return RWBitRef(v,i);
- }
-
- inline RWBitRef
- RWBitVec::operator()(int i)
- {
- #ifdef RWBOUNDS_CHECK
- if ((unsigned)i >= npts) indexRangeErr(i);
- #endif
- return RWBitRef(v,i);
- }
-
- #ifdef CONST_OVERLOADS
-
- inline RWBoolean
- RWBitVec::operator[](int i) const
- {
- if ((unsigned)i >= npts) indexRangeErr(i);
- return RWBIT(i) != 0;
- }
-
- inline RWBoolean
- RWBitVec::operator()(int i) const
- {
- #ifdef RWBOUNDS_CHECK
- if ((unsigned)i >= npts) indexRangeErr(i);
- #endif
- return RWBIT(i) != 0;
- }
-
- #endif /* CONST_OVERLOADS */
-
- inline void
- RWBitVec::clearBit(int i)
- { (*(v + (i>>3))) &= ~(1<<(7&i)); }
-
- inline void
- RWBitVec::setBit(int i)
- { (*(v + (i>>3))) |= (1<<(7&i)); }
-
- inline RWBoolean
- RWBitVec::testBit(int i) const
- { return RWBIT(i) != 0; }
-
- pragma pop_align_members();
- #endif /* __RWBITVEC_H__ */
-