home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c017 / 7.ddi / GPPLIB.ZIP / BITVEC.H < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-17  |  5.5 KB  |  237 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY.  No author or distributor
  10. accepts responsibility to anyone for the consequences of using it
  11. or for whether it serves any particular purpose or works at all,
  12. unless he says so in writing.  Refer to the GNU CC General Public
  13. License for full details.
  14.  
  15. Everyone is granted permission to copy, modify and redistribute
  16. GNU CC, but only under the conditions described in the
  17. GNU CC General Public License.   A copy of this license is
  18. supposed to have been given to you along with GNU CC so you
  19. can know your rights and responsibilities.  It should be in a
  20. file named COPYING.  Among other things, the copyright notice
  21. and this notice must be preserved on all copies.  
  22. */
  23.  
  24. #ifndef _BitVec_h
  25.  
  26. #define _BitVec_h 1
  27.  
  28. #include <stream.h>
  29.  
  30. // Bits per word. Done here to allow inlines to use.
  31. // This will be done in a nicer way when gnu libc .h files available
  32. #define BITVEC_BPW  32
  33.  
  34. class BitVec;
  35.  
  36. class OneBit
  37. {
  38.   friend class     BitVec;
  39.  
  40.   unsigned long*   wd;
  41.   int              pos;
  42.  
  43.  public:
  44.                    OneBit(BitVec& v, int p);
  45.                    OneBit(unsigned long* w, int p);
  46.                    OneBit(OneBit& b);
  47.                    ~OneBit();
  48.  
  49.   int              operator = (int b);
  50.   friend int       operator = (int& a, OneBit& b);
  51.   int              operator int();
  52. };
  53.  
  54.  
  55. class BitVec
  56. {
  57.   friend class       OneBit;
  58.  
  59.   unsigned long*     s;
  60.   int                len;
  61.  
  62.                      BitVec(unsigned long* w, int l);
  63. public:
  64.  
  65. // constructors
  66.                      BitVec(BitVec&);
  67.                      BitVec(int l);
  68.  
  69.                      ~BitVec();
  70.  
  71.   BitVec&            operator =  (BitVec& y);
  72.  
  73. // equality & subset tests
  74.  
  75.   friend int         operator == (BitVec& x, BitVec& y);
  76.   friend int         operator != (BitVec& x, BitVec& y);
  77.   friend int         operator <  (BitVec& x, BitVec& y);
  78.   friend int         operator <= (BitVec& x, BitVec& y);
  79.   friend int         operator >  (BitVec& x, BitVec& y);
  80.   friend int         operator >= (BitVec& x, BitVec& y);
  81.  
  82. // set operators
  83.  
  84.   friend BitVec      operator |  (BitVec& x, BitVec& y);
  85.   friend BitVec      operator &  (BitVec& x, BitVec& y);
  86.   friend BitVec      operator -  (BitVec& x, BitVec& y);
  87.   friend BitVec      operator ^  (BitVec& x, BitVec& y);
  88.  
  89.   BitVec&            operator |= (BitVec& y);
  90.   BitVec&            operator &= (BitVec& y);
  91.   BitVec&            operator -= (BitVec& y);
  92.   BitVec&            operator ^= (BitVec& y);
  93.  
  94.   BitVec             operator ~  ();
  95.   BitVec&            complement();
  96.  
  97. // status
  98.  
  99.   int                empty();
  100.   int                count(int b = 1);
  101.   int                capacity();
  102.   void               resize(int newlen);
  103.  
  104.   OneBit             operator [] (int i);
  105.   OneBit             elem (int i);
  106.  
  107.   friend BitVec      atoBitVec(const char* s, char f='0', char t='1');
  108.   friend const char* BitVectoa(BitVec& x, char f='0', char t='1');
  109.  
  110.   friend ostream&    operator << (ostream& s, BitVec& x);
  111.  
  112.   void               error(char* msg);
  113. };
  114.  
  115. // error handlers
  116.  
  117. extern void default_BitVec_error_handler(char*);
  118. extern one_arg_error_handler_t BitVec_error_handler;
  119.  
  120. extern one_arg_error_handler_t 
  121.         set_BitVec_error_handler(one_arg_error_handler_t f);
  122.  
  123.  
  124. //#ifdef __OPTIMIZE__
  125.  
  126. inline BitVec::BitVec(int l)
  127.   int sz = (unsigned)(len = l) / BITVEC_BPW + ((l & (BITVEC_BPW - 1))? 1 : 0);
  128.   s = new unsigned long[sz];
  129.   bzero(s, sz * sizeof(unsigned long));
  130. }
  131.  
  132. inline BitVec::BitVec(unsigned long* w, int l)
  133.   s = w;
  134.   len = l;
  135. }
  136.  
  137. inline BitVec::BitVec(BitVec& x)
  138.   int sz = (unsigned)(len = x.len) / BITVEC_BPW + ((x.len & (BITVEC_BPW - 1))? 1 : 0);
  139.   s = new unsigned long[sz];
  140.   bcopy(x.s, s, sz * sizeof(unsigned long));
  141. }
  142.  
  143.  
  144. inline BitVec::~BitVec()
  145.   delete s;
  146. }
  147.  
  148. inline BitVec& BitVec::operator =  (BitVec& x)
  149.   if (s != x.s)
  150.   {
  151.     delete s;
  152.     int sz = (unsigned)(len = x.len) / BITVEC_BPW + ((x.len & (BITVEC_BPW - 1))? 1 : 0);
  153.     s = new unsigned long[sz];
  154.     bcopy(x.s, s, sz * sizeof(unsigned long));
  155.   }
  156.   return *this; 
  157. }
  158.  
  159. inline int operator != (BitVec& x, BitVec& y)
  160. {
  161.   return !(x == y);
  162. }
  163.  
  164. inline int operator>(BitVec& x, BitVec& y)
  165. {
  166.   return y < x;
  167. }
  168.  
  169. inline int operator>=(BitVec& x, BitVec& y)
  170. {
  171.   return y <= x;
  172. }
  173.  
  174.  
  175. inline OneBit::OneBit(OneBit& b)
  176. {
  177.   wd = b.wd;
  178.   pos = b.pos;
  179. }
  180.  
  181. inline OneBit::OneBit(BitVec& v, int p)
  182. {
  183.   wd = v.s;
  184.   pos = p;
  185. }
  186.  
  187. inline OneBit::OneBit(unsigned long* w, int p)
  188. {
  189.   wd = w;
  190.   pos = p;
  191. }
  192.  
  193. inline OneBit::~OneBit() {}
  194.  
  195. inline int OneBit::operator int()
  196. {
  197.   return (wd[(unsigned)(pos) / BITVEC_BPW] & (1 << (pos & (BITVEC_BPW - 1)))) != 0;
  198. }
  199.  
  200. inline int operator = (int& a, OneBit& b)
  201. {
  202.   return a = (b.wd[(unsigned)(b.pos) / BITVEC_BPW] & (1 << (b.pos & (BITVEC_BPW - 1)))) != 0;
  203. }
  204.  
  205. inline int OneBit::operator = (int b)
  206. {
  207.   if (b)
  208.     wd[(unsigned)(pos) / BITVEC_BPW] |= (1 << (pos & (BITVEC_BPW - 1)));
  209.   else
  210.     wd[(unsigned)(pos) / BITVEC_BPW] &= ~(1 << (pos & (BITVEC_BPW - 1)));
  211.   return b;
  212. }
  213.  
  214. inline OneBit BitVec::operator [] (int i)
  215. {
  216.   if ((unsigned)(i) >= (unsigned)(len))
  217.     error("index out of range");
  218.   return OneBit(s, i);
  219. }
  220.  
  221. inline OneBit BitVec::elem (int i)
  222. {
  223.   return OneBit(s, i);
  224. }
  225.  
  226.  
  227.  
  228. //#endif
  229.  
  230. #endif
  231.  
  232.