home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pctchnqs / 1991 / number6 / bitvect / bitvect.cpp next >
Text File  |  1991-11-14  |  3KB  |  133 lines

  1. // Listing 2: bitvect.cpp
  2. // Implementation of BitVect (bit vector) class.
  3. // Copyright (C) 1991 by Nicholas Wilt.  All rights reserved.
  4.  
  5. #include <mem.h>
  6. #include "bitvect.h"
  7.  
  8. BitVect::BitVect(const BitVect& x)
  9. {
  10.   numbytes = x.numbytes;
  11.   numbits = x.numbits;
  12.   vect = new unsigned char[numbytes];
  13.   memcpy(vect, x.vect, numbytes);
  14. }
  15.  
  16. BitVect::BitVect(long NumElms): numbits(NumElms)
  17. {
  18.   numbytes = (numbits+7) >> 3;
  19.   vect = new unsigned char[numbytes];
  20. }
  21.  
  22. BitVect::BitVect(long NumElms, int val): numbits(NumElms)
  23. {
  24.   numbytes = (numbits+7) >> 3;
  25.   vect = new unsigned char[numbytes];
  26.   memset(vect, (val) ? 0xff : 0, numbytes);
  27. }
  28.  
  29. BitVect::~BitVect()
  30. {
  31.   delete vect;
  32. }
  33.  
  34. // BitVect x[inx] returns the inx'th element of x (a 0 or 1).
  35. // No range checking is performed.
  36. int BitVect::operator[] (long inx)
  37. {
  38.   return (vect[inx>>3] & (1 << (inx & 7))) != 0;
  39. }
  40.  
  41. // Same as operator[].    May be more intuitive at times.
  42. int BitVect::GetBit(long inx)
  43. {
  44.   return (*this)[inx];
  45. }
  46.  
  47. // Sets the inx'th bit in the vector to 1 or 0.
  48. void BitVect::SetBit(long inx, int on)
  49. {
  50.   if (on)
  51.     vect[inx>>3] |= (1 << (inx & 7));
  52.   else
  53.     vect[inx>>3] &= ~(1 << (inx & 7));
  54. }
  55.  
  56. // Sets the given range of bits to the given value.
  57. void BitVect::SetRange(long min, long max, int on)
  58. {
  59.   long truemin, truemax;
  60.  
  61.   truemin = (min < max) ? min : max;
  62.   truemax = (max > min) ? max : min;
  63.   for (long i = truemin; i <= truemax; i++)
  64.     SetBit(i, on);
  65. }
  66.  
  67. // NOT's the bit vector--all the 0's become 1's and vice versa.
  68. BitVect BitVect::operator~()
  69. {
  70.   BitVect ret(numbits);
  71.   unsigned char *src = vect;
  72.   unsigned char *dst = ret.vect;
  73.   for (int cnt = numbytes; cnt--; src++, dst++)
  74.     *dst = ~*src;
  75.   return ret;
  76. }
  77.  
  78. // OR's the bit vector with another.  They should be the same size,
  79. // but this is not checked for.
  80. BitVect& BitVect::operator|= (BitVect& x)
  81. {
  82.   unsigned char *src = x.vect;
  83.   unsigned char *dst = vect;
  84.   for (int cnt = numbytes; cnt--; src++, dst++)
  85.     *dst |= *src;
  86.   return *this;
  87. }
  88.  
  89. // AND's the bit vector with another.  They should be the same
  90. // size, but this is not checked for.
  91. BitVect& BitVect::operator&= (BitVect& x)
  92. {
  93.   unsigned char *src = x.vect;
  94.   unsigned char *dst = vect;
  95.   for (int cnt = numbytes; cnt--; src++, dst++)
  96.     *dst &= *src;
  97.   return *this;
  98. }
  99.  
  100. // Set difference: if the ith element is in *this, and it's
  101. // also in x, remove it from *this.
  102. BitVect& BitVect::operator-= (BitVect& x)
  103. {
  104.   for (long i = 0; i < numbits; i++)
  105.     if (x[i])
  106.       SetBit(i, 0);
  107.   return *this;
  108. }
  109.  
  110. // OR's two bit vectors together, generating a third.
  111. BitVect operator| (BitVect& x, BitVect& y)
  112. {
  113.   BitVect ret(x);
  114.   ret |= y;
  115.   return ret;
  116. }
  117.  
  118. // AND's two bit vectors another, generating a third.
  119. BitVect operator& (BitVect& x, BitVect& y)
  120. {
  121.   BitVect ret(x);
  122.   ret &= y;
  123.   return ret;
  124. }
  125.  
  126. // Returns set difference x - y.
  127. BitVect operator- (BitVect& x, BitVect& y)
  128. {
  129.   BitVect ret(x);
  130.   ret -= y;
  131.   return ret;
  132. }
  133.