home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / CTECHAPP.ZIP / SETS.ZIP / BITSET.CPP < prev    next >
C/C++ Source or Header  |  1990-03-25  |  4KB  |  217 lines

  1. //  BitSet
  2. //      2.10    25-Mar-1990
  3. //      C++ 2.0
  4. //
  5. //      Defines a set of bits
  6. //
  7. //      Written by Scott Robert Ladd
  8.  
  9. #include "BitSet.hpp"
  10.  
  11. extern "C"
  12.     {
  13.     #include "stddef.h"
  14.     #include "string.h"
  15.     }
  16.  
  17. // constructors
  18. BitSet::BitSet(unsigned long size)
  19.     {
  20.     unsigned long alloc;
  21.  
  22.     Length = size;
  23.  
  24.     alloc = (size + 7) / 8;
  25.  
  26.     Data = new unsigned char[(unsigned int)alloc];
  27.  
  28.     memset(Data,'\x00',(unsigned int)alloc);
  29.     }
  30.  
  31. BitSet::BitSet(BitSet & bs)
  32.     {
  33.     unsigned long alloc;
  34.  
  35.     Length = bs.Length;
  36.  
  37.     alloc = (bs.Length + 7) / 8;
  38.  
  39.     Data = new unsigned char[(unsigned int)alloc];
  40.  
  41.     memcpy(Data,bs.Data,(unsigned int)alloc);
  42.     }
  43.  
  44. // destructor
  45. BitSet::~BitSet(void)
  46.     {
  47.     if (Data != NULL)
  48.         delete Data;
  49.     }
  50.  
  51. // assignment operator
  52. void BitSet::operator = (BitSet & bs)
  53.     {
  54.     unsigned long alloc;
  55.  
  56.     if (Length != bs.Length)
  57.         {
  58.         Length = bs.Length;
  59.  
  60.         alloc = (bs.Length + 7) / 8;
  61.  
  62.         if (Data != NULL)
  63.             delete Data;
  64.  
  65.         Data = new unsigned char[(unsigned int)alloc];
  66.  
  67.         memcpy(Data,bs.Data,(unsigned int)alloc);
  68.         }
  69.     else
  70.         memcpy(Data,bs.Data,(unsigned int)((Length + 7) / 8));
  71.     }
  72.  
  73. // union operators
  74. BitSet BitSet::operator & (BitSet & bs)
  75.     {
  76.     BitSet result;
  77.  
  78.     unsigned long bit;
  79.  
  80.     if (Length < bs.Length)
  81.         {
  82.         result = bs;
  83.  
  84.         for (bit = 0; bit < Length; ++bit)
  85.             if ((*this)[bit])
  86.                 result.Include(bit);
  87.         }
  88.     else
  89.         {
  90.         result = *this;
  91.  
  92.         for (bit = 0; bit < bs.Length; ++bit)
  93.             if (bs[bit])
  94.                 result.Include(bit);
  95.         }
  96.  
  97.     return result;
  98.     }
  99.  
  100. BitSet BitSet::operator &= (BitSet & bs)
  101.     {
  102.     *this = *this & bs;
  103.  
  104.     return *this;
  105.     }
  106.  
  107. // synonyms for union operators
  108. BitSet BitSet::operator + (BitSet & bs)
  109.     {
  110.     BitSet result = *this & bs;
  111.  
  112.     return result;
  113.     }
  114.  
  115. BitSet BitSet::operator += (BitSet & bs)
  116.     {
  117.     BitSet result = *this &= bs;
  118.  
  119.     return result;
  120.     }
  121.  
  122. // intersection operators
  123. BitSet BitSet::operator | (BitSet & bs)
  124.     {
  125.     BitSet result;
  126.  
  127.     unsigned long max;
  128.  
  129.     if (Length > bs.Length)
  130.         {
  131.         result = BitSet(Length);
  132.         max    = bs.Length;
  133.         }
  134.     else
  135.         {
  136.         result = BitSet(bs.Length);
  137.         max    = Length;
  138.         }
  139.  
  140.     for (unsigned long bit = 0; bit < max; ++bit)
  141.         if ((*this)[bit] & bs[bit])
  142.             result.Include(bit);
  143.  
  144.     return result;
  145.     }
  146.  
  147. BitSet BitSet::operator |= (BitSet & bs)
  148.     {
  149.     *this = *this | bs;
  150.  
  151.     return *this;
  152.     }
  153.  
  154. // difference operators
  155. BitSet BitSet::operator - (BitSet & bs)
  156.     {
  157.     BitSet result = *this;
  158.  
  159.     unsigned long stop = (Length < bs.Length) ? Length : bs.Length;
  160.  
  161.     for (unsigned long bit = 0; bit < stop; ++bit)
  162.         if (bs[bit])
  163.             result.Exclude(bit);
  164.  
  165.     return result;
  166.     }
  167.  
  168. BitSet BitSet::operator -= (BitSet & bs)
  169.     {
  170.     *this = *this - bs;
  171.  
  172.     return *this;
  173.     }
  174.  
  175. // complement operator
  176. BitSet BitSet::operator ~ ()
  177.     {
  178.     BitSet result(Length);
  179.  
  180.     for (unsigned long bit = 0; bit < Length; ++bit)
  181.         if ((*this)[bit])
  182.             result.Exclude(bit);
  183.         else
  184.             result.Include(bit);
  185.  
  186.     return result;
  187.     }
  188.  
  189. // comparison operators
  190. int BitSet::operator == (BitSet & bs)
  191.     {
  192.     if (Length != bs.Length)
  193.         return 0;
  194.  
  195.     for (unsigned long bit = 0; bit < Length; ++bit)
  196.         if ((*this)[bit] != bs[bit])
  197.             return 0;
  198.  
  199.     return 1;
  200.     }
  201.  
  202. int BitSet::operator != (BitSet & bs)
  203.     {
  204.     if (Length != bs.Length)
  205.         return 1;
  206.  
  207.     unsigned long bit = 0;
  208.  
  209.     while (bit < Length)
  210.         if ((*this)[bit] == bs[bit])
  211.             ++bit;
  212.         else
  213.             return 1;
  214.  
  215.     return 0;
  216.     }
  217.