home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c017 / 7.ddi / GPPLIB.ZIP / BITVEC.CC < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-17  |  8.5 KB  |  389 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. #include <Obstack.h>
  25. #include <BitVec.h>
  26.  
  27.  
  28. // error handling
  29.  
  30. void default_BitVec_error_handler(char* msg)
  31. {
  32.   cerr << "Fatal BitVec error. " << msg << "\n";
  33.   exit(1);
  34. }
  35.  
  36. one_arg_error_handler_t BitVec_error_handler = default_BitVec_error_handler;
  37.  
  38. one_arg_error_handler_t Vec_BitVec_error_handler(one_arg_error_handler_t f)
  39. {
  40.   one_arg_error_handler_t old = BitVec_error_handler;
  41.   BitVec_error_handler = f;
  42.   return old;
  43. }
  44.  
  45. void BitVec::error(char* msg)
  46. {
  47.   (*BitVec_error_handler)(msg);
  48. }
  49.  
  50. // break things up into .s indices and positions
  51.  
  52. inline static int word_length(int l)
  53. {
  54.   return (unsigned)(l) / BITVEC_BPW + ((l & (BITVEC_BPW - 1))? 1 : 0);
  55. }
  56.  
  57. inline static int bit_position(int l)
  58. {
  59.   return l & (BITVEC_BPW - 1);
  60. }
  61.  
  62. void BitVec::resize(int newlen)
  63. {
  64.   unsigned long* olds = s;
  65.   int sz = word_length(len);
  66.   int newsz = word_length(newlen);
  67.   s = new unsigned long[newsz];
  68.   bcopy(olds, s, sz * sizeof(unsigned long));
  69.   if (newsz > sz)
  70.     bzero(&s[sz], newsz - sz);
  71.   delete olds;
  72. }
  73.  
  74. int operator == (BitVec& x, BitVec& y)
  75. {
  76.   return x.len == y.len && 
  77.     bcmp((void*)x.s, (void*)y.s, word_length(x.len) * sizeof(long)) == 0;
  78. }
  79.  
  80. int operator <= (BitVec& x, BitVec& y)
  81. {
  82.   unsigned long xl = x.len;
  83.   unsigned long yl = y.len;
  84.   if (xl > yl)
  85.     return 0;
  86.  
  87.   unsigned long* xs = x.s;
  88.   unsigned long* ys = y.s;
  89.   unsigned long* topx = &(xs[word_length(xl)]);
  90.  
  91.   while (xs < topx)
  92.   {
  93.     unsigned long a = *xs++;
  94.     unsigned long b = *ys++;
  95.     if ((a | b) != b)
  96.       return 0;
  97.   }
  98.   return 1;
  99. }
  100.  
  101. int operator < (BitVec& x, BitVec& y)
  102. {
  103.   unsigned long xl = x.len;
  104.   unsigned long yl = y.len;
  105.   if (xl >= yl)
  106.     return 0;
  107.  
  108.   unsigned long* xs = x.s;
  109.   unsigned long* ys = y.s;
  110.   unsigned long* topx = &(xs[word_length(xl)]);
  111.   unsigned long* topy = &(xs[word_length(yl)]);
  112.   int one_diff = 0;
  113.   while (xs < topx)
  114.   {
  115.     unsigned long a = *xs++;
  116.     unsigned long b = *ys++;
  117.     unsigned long c = a | b;
  118.     if (c != b)
  119.       return 0;
  120.     else if (c != a)
  121.       one_diff = 1;
  122.   }
  123.   if (one_diff)
  124.     return 1;
  125.   else
  126.   {
  127.     while (ys < topy)
  128.       if (*ys++ != 0)
  129.         return 1;
  130.     return 0;
  131.   }
  132. }
  133.  
  134. int BitVec::empty()
  135. {
  136.   int xwds = word_length(len);
  137.   int xlast = bit_position(len);
  138.   int l = 0;
  139.   unsigned long* p = s;
  140.   unsigned long* tops = &(s[xwds]);
  141.   while (p < tops)
  142.     if (*p++ != 0)
  143.       return 0;
  144.   return 1;
  145. }
  146.  
  147.   
  148. int BitVec::count(int b = 1)
  149. {
  150.   int xwds = word_length(len);
  151.   int xlast = bit_position(len);
  152.   int l = 0;
  153.   unsigned long* p = s;
  154.   unsigned long* tops = &(s[xwds - 1]);
  155.   unsigned long a;
  156.   int i;
  157.   if (b != 0)
  158.   {
  159.     while (p < tops)
  160.     {
  161.       a = *p++;
  162.       for (i = 0; i < BITVEC_BPW && a != 0; ++i)
  163.       {
  164.         if (a & 1)
  165.           ++l;
  166.         a >>= 1;
  167.       }
  168.     }
  169.     a = *p;
  170.     for (i = 0; i < xlast && a != 0; ++i)
  171.     {
  172.       if (a & 1)
  173.         ++l;
  174.       a >>= 1;
  175.     }
  176.   }
  177.   else
  178.   {
  179.     unsigned long maxbit = 1 << (BITVEC_BPW - 1);
  180.     while (p < tops)
  181.     {
  182.       a = *p++;
  183.       for (i = 0; i < BITVEC_BPW; ++i)
  184.       {
  185.         if ((a & maxbit) == 0)
  186.           ++l;
  187.         a <<= 1;
  188.       }
  189.     }
  190.     maxbit = 1 << (xlast - 1);
  191.     a = *p;
  192.     for (i = 0; i < xlast; ++i)
  193.     {
  194.       if ((a & maxbit) == 0)
  195.         ++l;
  196.       a <<= 1;
  197.     }
  198.   }
  199.   return l;
  200. }
  201.  
  202. BitVec& BitVec::complement()
  203. {
  204.   unsigned long* rs = s;
  205.   unsigned long* topr = &(rs[word_length(len)]);
  206.   while (rs < topr)
  207.   {
  208.     unsigned long cmp = ~(*rs);
  209.     *rs++ = cmp;
  210.   }
  211.   return *this;
  212. }
  213.  
  214. BitVec BitVec:: operator ~()
  215. {
  216.   unsigned long* r = new unsigned long[word_length(len)];
  217.   unsigned long* rs = r;
  218.   unsigned long* xs = s;
  219.   unsigned long* topr = &(rs[word_length(len)]);
  220.   while (rs < topr) *rs++ = ~(*xs++);
  221.   return BitVec(r, len);
  222. }
  223.  
  224. BitVec operator & (BitVec& a, BitVec& b)
  225. {
  226.   if (a.len != b.len)
  227.     a.error("nonconformant vectors");
  228.   unsigned long* r = new unsigned long[word_length(a.len)];
  229.   unsigned long* rs = r;
  230.   unsigned long* xs = a.s;
  231.   unsigned long* ys = b.s;
  232.   unsigned long* topr = &(rs[word_length(a.len)]);
  233.   while (rs < topr) *rs++ = *xs++ & *ys++;
  234.   return BitVec(r, a.len);
  235. }
  236.  
  237. BitVec operator | (BitVec& a, BitVec& b)
  238. {
  239.   if (a.len != b.len)
  240.     a.error("nonconformant vectors");
  241.   unsigned long* r = new unsigned long[word_length(a.len)];
  242.   unsigned long* rs = r;
  243.   unsigned long* xs = a.s;
  244.   unsigned long* ys = b.s;
  245.   unsigned long* topr = &(rs[word_length(a.len)]);
  246.   while (rs < topr) *rs++ = *xs++ | *ys++;
  247.   return BitVec(r, a.len);
  248. }
  249.  
  250. BitVec operator ^ (BitVec& a, BitVec& b)
  251. {
  252.   if (a.len != b.len)
  253.     a.error("nonconformant vectors");
  254.   unsigned long* r = new unsigned long[word_length(a.len)];
  255.   unsigned long* rs = r;
  256.   unsigned long* xs = a.s;
  257.   unsigned long* ys = b.s;
  258.   unsigned long* topr = &(rs[word_length(a.len)]);
  259.   while (rs < topr) *rs++ = *xs++ ^ *ys++;
  260.   return BitVec(r, a.len);
  261. }
  262.  
  263. BitVec operator - (BitVec& a, BitVec& b)
  264. {
  265.   if (a.len != b.len)
  266.     a.error("nonconformant vectors");
  267.   unsigned long* r = new unsigned long[word_length(a.len)];
  268.   unsigned long* rs = r;
  269.   unsigned long* xs = a.s;
  270.   unsigned long* ys = b.s;
  271.   unsigned long* topr = &(rs[word_length(a.len)]);
  272.   while (rs < topr) *rs++ = *xs++ & ~(*ys++);
  273.   return BitVec(r, a.len);
  274. }
  275.  
  276. BitVec& BitVec::operator &= (BitVec& b)
  277. {
  278.   if (len != b.len)
  279.     error("nonconformant vectors");
  280.   unsigned long* xs = s;
  281.   unsigned long* ys = b.s;
  282.   unsigned long* top = &(s[word_length(len)]);
  283.   while (xs < top) *xs++ &= *ys++;
  284.   return *this;
  285. }
  286.  
  287. BitVec& BitVec::operator |= (BitVec& b)
  288. {
  289.   if (len != b.len)
  290.     error("nonconformant vectors");
  291.   unsigned long* xs = s;
  292.   unsigned long* ys = b.s;
  293.   unsigned long* top = &(s[word_length(len)]);
  294.   while (xs < top) *xs++ |= *ys++;
  295.   return *this;
  296. }
  297.  
  298. BitVec& BitVec::operator ^= (BitVec& b)
  299. {
  300.   if (len != b.len)
  301.     error("nonconformant vectors");
  302.   unsigned long* xs = s;
  303.   unsigned long* ys = b.s;
  304.   unsigned long* top = &(s[word_length(len)]);
  305.   while (xs < top) *xs++ ^= *ys++;
  306.   return *this;
  307. }
  308.  
  309. BitVec& BitVec::operator -= (BitVec& b)
  310. {
  311.   if (len != b.len)
  312.     error("nonconformant vectors");
  313.   unsigned long* xs = s;
  314.   unsigned long* ys = b.s;
  315.   unsigned long* top = &(s[word_length(len)]);
  316.   while (xs < top) *xs++ &= ~(*ys++);
  317.   return *this;
  318. }
  319.  
  320. extern Obstack _libgxx_io_ob;
  321. extern char* _libgxx_io_oblast;
  322.  
  323.  
  324. const char* BitVectoa(BitVec& x, char f = '0', char t = '1')
  325. {
  326.   if (_libgxx_io_oblast)  _libgxx_io_ob.free(_libgxx_io_oblast);
  327.   unsigned long xl = x.len;
  328.   unsigned long* p = x.s;
  329.   unsigned long a;
  330.  
  331.   for (unsigned long i = 0; i < xl; ++i)
  332.   {
  333.     if (i % BITVEC_BPW == 0)
  334.       a = *p++;
  335.     _libgxx_io_ob.grow((a & 1)? t : f);
  336.     a >>= 1;
  337.   }
  338.  
  339.   return _libgxx_io_oblast = (char*)(_libgxx_io_ob.finish(0));
  340. }
  341.  
  342. BitVec atoBitVec(const char* s, char f = '0', char t = '1')
  343. {
  344.   int rl = 0;
  345.   int sl = strlen(s);
  346.   unsigned long* r = new unsigned long[word_length(sl)];
  347.   int i = 0;
  348.   if (sl != 0)
  349.   {
  350.     unsigned long* rs = r;
  351.     unsigned long a = 0;
  352.     unsigned long m = 1;
  353.     for(;;)
  354.     {
  355.       char ch = s[i];
  356.       if (ch != t && ch != f)
  357.       {
  358.         *rs = a;
  359.         break;
  360.       }
  361.       ++rl;
  362.       if (ch == t)
  363.         a |= m;
  364.       if (++i == sl)
  365.       {
  366.         *rs = a;
  367.         break;
  368.       }
  369.       else if (i % BITVEC_BPW == 0)
  370.       {
  371.         *rs++ = a;
  372.         a = 0;
  373.         m = 1;
  374.       }
  375.       else
  376.         m <<= 1;
  377.     }
  378.   }
  379.   return BitVec(r, i);
  380. }
  381.  
  382. ostream& operator << (ostream& s, BitVec& x)
  383. {
  384.   return s << BitVectoa(x);
  385. }
  386.  
  387.  
  388.  
  389.