home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / OWLSRC.PAK / BITSET.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  3.8 KB  |  223 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.7  $
  6. //
  7. // Implementation of TBitSet, a set of up to 256 bit flags
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_BITSET_H)
  11. # include <owl/bitset.h>
  12. #endif
  13. #if !defined(CLASSLIB_OBJSTRM_H)
  14. # include <classlib/objstrm.h>
  15. #endif
  16.  
  17. OWL_DIAGINFO;
  18.  
  19. uint8 near TBitSet::Masks[8] = {
  20.   0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
  21. };
  22.  
  23. //
  24. // Initialize all the bits to 0.
  25. //
  26. TBitSet::TBitSet()
  27. {
  28.   for (int i = 0; i < 32; i++)
  29.     Bits[i] = 0;
  30. }
  31.  
  32. //
  33. // Make a copy of the bits.
  34. //
  35. TBitSet::TBitSet(const TBitSet& bs)
  36. {
  37.   for (int i = 0; i < 32; i++)
  38.     Bits[i] = bs.Bits[i];
  39. }
  40.  
  41. //
  42. // Return true if a bit is turned on.
  43. //
  44. int
  45. TBitSet::Has(uint8 item) const
  46. {
  47.   return (Bits[Loc(item)] & Mask(item)) != 0;
  48. }
  49.  
  50. //
  51. // Negate a bit.
  52. //
  53. TBitSet
  54. TBitSet::operator ~() const
  55. {
  56.   TBitSet temp;
  57.   for (int i = 0; i < 32; i++)
  58.     temp.Bits[i] = uint8(~Bits[i]);
  59.   return temp;
  60. }
  61.  
  62. //
  63. // Turn off a specific bit.
  64. //
  65. void
  66. TBitSet::DisableItem(uint8 item)
  67. {
  68.   Bits[Loc(item)] &= uint8(~Mask(item));
  69. }
  70.  
  71. //
  72. // Logically OR in the set of bits.
  73. //
  74. void
  75. TBitSet::EnableItem(const TBitSet& bs)
  76. {
  77.   for (int i = 0; i < 32; i++)
  78.     Bits[i] |= bs.Bits[i];
  79. }
  80.  
  81. //
  82. // Logically
  83. //
  84. void
  85. TBitSet::DisableItem(const TBitSet& bs)
  86. {
  87.   for (int i = 0; i < 32; i++)
  88.     Bits[i] &= uint8(~(bs.Bits[i]));
  89. }
  90.  
  91. //
  92. // Turn on a specific bit.
  93. //
  94. void
  95. TBitSet::EnableItem(uint8 item)
  96. {
  97.   Bits[Loc(item)] |= Mask(item);
  98. }
  99.  
  100. //
  101. // Logically AND each individual bits.
  102. //
  103. TBitSet&
  104. TBitSet::operator &=(const TBitSet& bs)
  105. {
  106.   for (int i = 0; i < 32; i++)
  107.     Bits[i] &= bs.Bits[i];
  108.   return *this;
  109. }
  110.  
  111. //
  112. // Return the AND of two bitsets.
  113. //
  114. TBitSet
  115. operator &(const TBitSet& bs1, const TBitSet& bs2)
  116. {
  117.   TBitSet temp(bs1);
  118.   temp &= bs2;
  119.   return temp;
  120. }
  121.  
  122. //
  123. // Return the OR of two bitsets.
  124. //
  125. TBitSet
  126. operator |(const TBitSet& bs1, const TBitSet& bs2)
  127. {
  128.   TBitSet temp(bs1);
  129.   temp |= bs2;
  130.   return temp;
  131. }
  132.  
  133. //
  134. // Return true if all bits are off.
  135. //
  136. int
  137. TBitSet::IsEmpty() const
  138. {
  139.   for (int i = 0; i < 32; i++)
  140.     if (Bits[i] != 0)
  141.       return false;
  142.   return true;
  143. }
  144.  
  145. #if defined(BI_NAMESPACE)
  146. namespace OWL {
  147. #endif
  148.  
  149. //
  150. // Return true if the two bitsets are indentical.
  151. //
  152. int
  153. operator ==(const TBitSet& bs1, const TBitSet& bs2)
  154. {
  155.   for (int i = 0; i < 32; i++)
  156.     if (bs1.Bits[i] != bs2.Bits[i])
  157.       return false;
  158.   return true;
  159. }
  160.  
  161. //
  162. // Insert the bitset into a persistent output stream.
  163. //
  164. opstream& operator <<(opstream& out, const TBitSet& bs)
  165. {
  166.   out.fwriteBytes(bs.Bits, sizeof(bs.Bits));
  167.   return out;
  168. }
  169.  
  170. //
  171. // Extract the bitset from a persistent input stream.
  172. //
  173. ipstream& operator >>(ipstream& in, TBitSet& bs)
  174. {
  175.   in.freadBytes(bs.Bits, sizeof(bs.Bits));
  176.   return in;
  177. }
  178.  
  179. #if defined(BI_NAMESPACE)
  180. } // namespace OWL
  181. #endif
  182.  
  183. //----------------------------------------------------------------------------
  184.  
  185. //
  186. // Construct a default character set.
  187. // All items are set to 0.
  188. //
  189. TCharSet::TCharSet()
  190. :
  191.   TBitSet()
  192. {
  193. }
  194.  
  195. //
  196. // Copy the bitset into this character set.
  197. //
  198. TCharSet::TCharSet(const TBitSet& bs)
  199. :
  200.   TBitSet(bs)
  201. {
  202. }
  203.  
  204. //
  205. // Construct a character set from a string.
  206. // The characters act as the index for the bitset.
  207. //
  208. TCharSet::TCharSet(const char far* str)
  209. :
  210.   TBitSet()
  211. {
  212.   for (const char far* p = str; *p; p++) {
  213.     if (*p == '\\')
  214.       p++;
  215.     else if (*p == '-' && p > str && p[1]) {  // handle "A-Z" type shorthands
  216.       p++;
  217.       for (char c = char(p[-2]+1); c < *p; c++) // replace "-" with "B..Y"
  218.         EnableItem(c);
  219.     }
  220.     EnableItem(*p);
  221.   }
  222. }
  223.