home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 6.ddi / MWHC.006 / I0 < prev    next >
Encoding:
Text File  |  1992-06-07  |  4.7 KB  |  169 lines

  1. #ifndef    __RWBITVEC_H__
  2. #define    __RWBITVEC_H__
  3. pragma push_align_members(64);
  4.  
  5. /*
  6.  * Ar`bit'rarily long bit vector.
  7.  *
  8.  * $Header:   E:/vcs/rw/bitvec.h_v   1.5   17 Mar 1992 19:21:08   KEFFER  $
  9.  *
  10.  ****************************************************************************
  11.  *
  12.  * Rogue Wave 
  13.  * P.O. Box 2328
  14.  * Corvallis, OR 97339
  15.  * Voice: (503) 754-3010    FAX: (503) 757-6650
  16.  *
  17.  * Copyright (C) 1989, 1990, 1991. This software is subject to copyright 
  18.  * protection under the laws of the United States and other countries.
  19.  *
  20.  ***************************************************************************
  21.  *
  22.  * $Log:   E:/vcs/rw/bitvec.h_v  $
  23.  * 
  24.  *    Rev 1.5   17 Mar 1992 19:21:08   KEFFER
  25.  * Changed BOUNDS_CHECK to RWBOUNDS_CHECK
  26.  * 
  27.  *    Rev 1.4   04 Mar 1992 09:03:02   KEFFER
  28.  * nil changed to rwnil
  29.  * 
  30.  *    Rev 1.3   18 Feb 1992 19:21:42   KEFFER
  31.  * Added m.f. nfull()
  32.  * 
  33.  *    Rev 1.2   28 Oct 1991 09:08:08   keffer
  34.  * Changed inclusions to <rw/xxx.h>
  35.  * 
  36.  *    Rev 1.1   10 Oct 1991 09:35:10   keffer
  37.  * Exported global operators and ::sum().
  38.  * 
  39.  *    Rev 1.0   28 Jul 1991 08:12:16   keffer
  40.  * Tools.h++ V4.0.5 PVCS baseline version
  41.  *
  42.  */
  43.  
  44. /*
  45.  * Defining the preprocessor directive RWBOUNDS_CHECK will
  46.  * cause bounds checking on the subscripting operator.
  47.  */
  48.  
  49. #include "rw/bitref.h"
  50. #include "rw/mempool.h"
  51.  
  52. class RWBitVec : public RWMemoryPool {
  53. private:
  54.   RWByte*        v;        // The bit data, packed within a byte vector
  55.   unsigned        npts;        // Length of vector
  56.   void            indexRangeErr(int) const;
  57.   void            lengthErr(unsigned, unsigned) const;
  58.   unsigned        nbytes() const    { return (npts+7) >> 3; }
  59.   unsigned        nfull()  const  { return npts >> 3; }
  60. public:
  61.   RWBitVec() { v=rwnil; npts=0; }
  62.   RWBitVec(unsigned N);
  63.   RWBitVec(unsigned N, RWBoolean initVal);
  64.   RWBitVec(const RWByte*, unsigned N);
  65.   RWBitVec(const RWBitVec&);
  66.   ~RWBitVec()                { delete v; }
  67.  
  68.   // Assignment operators
  69.   RWBitVec&        operator=(const RWBitVec&);
  70.   RWBitVec&        operator=(RWBoolean);
  71.   RWBitVec&        operator&=(const RWBitVec&);
  72.   RWBitVec&        operator^=(const RWBitVec&);
  73.   RWBitVec&        operator|=(const RWBitVec&);
  74.  
  75.   // Indexing operators
  76.   RWBitRef        operator[](int i);    // Can be used as lvalue
  77.   RWBitRef        operator()(int i);    //       ditto
  78. #ifdef CONST_OVERLOADS
  79.   RWBoolean        operator[](int i) const; // Can't be used as lvalue
  80.   RWBoolean        operator()(int i) const; //       ditto
  81. #endif
  82.  
  83.   // Other operators ---
  84.   RWBoolean        operator==(const RWBitVec& u) const {return  isEqual(u);}
  85.   RWBoolean        operator!=(const RWBitVec& u) const {return !isEqual(u);}
  86.   RWBoolean        operator==(RWBoolean b) const;
  87.   RWBoolean        operator!=(RWBoolean b) const {return !operator==(b);}
  88.  
  89.   friend RWBitVec rwexport    operator!(const RWBitVec&);
  90.   friend RWBitVec rwexport     operator&(const RWBitVec&,const RWBitVec&);
  91.   friend RWBitVec rwexport     operator^(const RWBitVec&,const RWBitVec&);
  92.   friend RWBitVec rwexport     operator|(const RWBitVec&,const RWBitVec&);
  93.   friend ostream&         operator<<(ostream&, const RWBitVec&);
  94.   friend istream&        operator>>(istream&, RWBitVec&);
  95.  
  96.   // Member functions
  97.   void            clearBit(int i);        // Clear bit i
  98.   const RWByte*        data() const    { return v; }
  99.   unsigned        hash() const;
  100.   RWBoolean        isEqual(const RWBitVec&) const;
  101.   unsigned        length() const    {return npts;}
  102.   ostream&        printOn(ostream&) const;
  103.   void            restoreFrom(RWvistream&);
  104.   void            restoreFrom(RWFile&);
  105.   void            resize(unsigned);
  106.   void            saveOn(RWvostream&) const;
  107.   void            saveOn(RWFile&) const;
  108.   istream&        scanFrom(istream&);
  109.   void            setBit(int i);            // Set bit i
  110.   RWBoolean        testBit(int i) const;        // Return value of bit i
  111.  
  112.   // Other friend functions:
  113.   friend int rwexport    sum(const RWBitVec&);        // Total # of bits set
  114. };
  115.  
  116. // This macro isolates bit 'i'
  117. #define RWBIT(i) (*(((i)>>3) + (v)) & (1<<(7&(i))))
  118.  
  119. inline RWBitRef
  120. RWBitVec::operator[](int i)
  121. {
  122.   if ((unsigned)i >= npts) indexRangeErr(i);
  123.   return RWBitRef(v,i);
  124. }
  125.     
  126. inline RWBitRef
  127. RWBitVec::operator()(int i)
  128. {
  129. #ifdef RWBOUNDS_CHECK
  130.   if ((unsigned)i >= npts) indexRangeErr(i);
  131. #endif
  132.   return RWBitRef(v,i);
  133. }
  134.  
  135. #ifdef CONST_OVERLOADS    
  136.     
  137. inline RWBoolean
  138. RWBitVec::operator[](int i) const
  139. {
  140.   if ((unsigned)i >= npts) indexRangeErr(i);
  141.   return RWBIT(i) != 0;
  142. }
  143.     
  144. inline RWBoolean
  145. RWBitVec::operator()(int i) const
  146. {
  147. #ifdef RWBOUNDS_CHECK
  148.   if ((unsigned)i >= npts) indexRangeErr(i);
  149. #endif
  150.   return RWBIT(i) != 0;
  151. }
  152.  
  153. #endif    /* CONST_OVERLOADS */
  154.  
  155. inline void
  156. RWBitVec::clearBit(int i)
  157. { (*(v + (i>>3))) &= ~(1<<(7&i)); }
  158.  
  159. inline void
  160. RWBitVec::setBit(int i)
  161. { (*(v + (i>>3))) |= (1<<(7&i)); }
  162.  
  163. inline RWBoolean
  164. RWBitVec::testBit(int i) const
  165. { return RWBIT(i) != 0; }
  166.  
  167. pragma pop_align_members();
  168. #endif /* __RWBITVEC_H__ */
  169.