home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Headers / g++ / BitSet.h < prev    next >
C/C++ Source or Header  |  1993-06-29  |  9KB  |  372 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 the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #ifndef _BitSet_h
  20. #ifdef __GNUG__
  21. #pragma interface
  22. #endif
  23.  
  24. #define _BitSet_h 1
  25.  
  26. #include <stream.h>
  27. #include <limits.h>
  28.  
  29. #define BITSETBITS  (sizeof(short) * CHAR_BIT)
  30.  
  31. struct BitSetRep
  32. {
  33.   unsigned short  len;          // number of shorts in s
  34.   unsigned short  sz;           // allocated slots
  35.   unsigned short  virt;         // virtual 0 or 1
  36.   unsigned short  s[1];         // bits start here
  37. };
  38.  
  39. extern BitSetRep*   BitSetalloc(BitSetRep*, const unsigned short*, 
  40.                                 int, int, int);
  41. extern BitSetRep*   BitSetcopy(BitSetRep*, const BitSetRep*);
  42. extern BitSetRep*   BitSetresize(BitSetRep*, int);
  43. extern BitSetRep*   BitSetop(const BitSetRep*, const BitSetRep*, 
  44.                              BitSetRep*, char);
  45. extern BitSetRep*   BitSetcmpl(const BitSetRep*, BitSetRep*);
  46.  
  47.  
  48. extern BitSetRep    _nilBitSetRep;
  49.  
  50. class BitSet;
  51.  
  52. class BitSetBit
  53. {
  54. protected:
  55.   BitSet*            src;
  56.   unsigned long      pos;
  57.  
  58.  public:
  59.                      BitSetBit(BitSet* v, int p);
  60.                      BitSetBit(const BitSetBit& b);
  61.                     ~BitSetBit();
  62.                      operator int();
  63.   int                operator = (int b);
  64.   int                operator == (int b);
  65.   int                operator != (int b);
  66. };
  67.  
  68. class BitSet
  69. {
  70. protected:
  71.   BitSetRep*          rep;
  72.  
  73.   
  74. public:
  75.  
  76. // constructors
  77.                      BitSet();
  78.                      BitSet(const BitSet&);
  79.  
  80.                     ~BitSet();
  81.  
  82.   void               operator =  (const BitSet& y);
  83.  
  84. // equality & subset tests
  85.  
  86.   friend int         operator == (const BitSet& x, const BitSet& y);
  87.   friend int         operator != (const BitSet& x, const BitSet& y);
  88.   friend int         operator <  (const BitSet& x, const BitSet& y);
  89.   friend int         operator <= (const BitSet& x, const BitSet& y);
  90.   friend int         operator >  (const BitSet& x, const BitSet& y);
  91.   friend int         operator >= (const BitSet& x, const BitSet& y);
  92.  
  93.  
  94. // operations on self
  95.  
  96.   void               operator |= (const BitSet& y);
  97.   void               operator &= (const BitSet& y);
  98.   void               operator -= (const BitSet& y);
  99.   void               operator ^= (const BitSet& y);
  100.  
  101.   void               complement();
  102.  
  103. // individual bit manipulation
  104.  
  105.   void               set(int pos);
  106.   void               set(int from, int to);
  107.   void               set(); // set all
  108.  
  109.   void               clear(int pos);
  110.   void               clear(int from, int to);
  111.   void               clear(); // clear all
  112.  
  113.   void               invert(int pos);
  114.   void               invert(int from, int to);
  115.  
  116.   int                test(int pos) const;
  117.   int                test(int from, int to) const;
  118.  
  119.   BitSetBit          operator [] (int i);
  120.   
  121. // iterators
  122.  
  123.   int                first(int b = 1) const;
  124.   int                last(int b = 1) const;
  125.  
  126.   int                next(int pos, int b = 1) const;
  127.   int                previous(int pos, int b = 1) const;
  128.  
  129. // status
  130.  
  131.   int                empty() const;
  132.   int                virtual_bit() const;
  133.   int                count(int b = 1) const;
  134.   
  135. // convertors & IO
  136.  
  137.   friend BitSet      atoBitSet(const char* s, 
  138.                                char f='0', char t='1', char star='*');
  139.   // BitSettoa is deprecated; do not use in new programs.
  140.   friend const char* BitSettoa(const BitSet& x, 
  141.                                char f='0', char t='1', char star='*');
  142.  
  143.   friend BitSet      shorttoBitSet(unsigned short w);
  144.   friend BitSet      longtoBitSet(unsigned long w);
  145.  
  146.   friend ostream&    operator << (ostream& s, const BitSet& x);
  147.   void             printon(ostream& s,
  148.                  char f='0', char t='1', char star='*') const;
  149.  
  150. // procedural versions of operators
  151.  
  152.   friend void        and(const BitSet& x, const BitSet& y, BitSet& r);
  153.   friend void        or(const BitSet& x, const BitSet& y, BitSet& r);
  154.   friend void        xor(const BitSet& x, const BitSet& y, BitSet& r);
  155.   friend void        diff(const BitSet& x, const BitSet& y, BitSet& r);
  156.   friend void        complement(const BitSet& x, BitSet& r);
  157.  
  158. // misc
  159.  
  160.   void      error(const char* msg) const;
  161.   int                OK() const;
  162. };
  163.  
  164.  
  165. typedef BitSet BitSetTmp;
  166.  
  167.  
  168.   BitSet      operator |  (const BitSet& x, const BitSet& y);
  169.   BitSet      operator &  (const BitSet& x, const BitSet& y);
  170.   BitSet      operator -  (const BitSet& x, const BitSet& y);
  171.   BitSet      operator ^  (const BitSet& x, const BitSet& y);
  172.  
  173.   BitSet      operator ~  (const BitSet& x);
  174.  
  175. // These are inlined regardless of optimization
  176.  
  177. inline int BitSet_index(int l)
  178. {
  179.   return (unsigned)(l) / BITSETBITS;
  180. }
  181.  
  182. inline int BitSet_pos(int l)
  183. {
  184.   return l & (BITSETBITS - 1);
  185. }
  186.  
  187.  
  188. inline BitSet::BitSet() : rep(&_nilBitSetRep) {}
  189.  
  190. inline BitSet::BitSet(const BitSet& x) :rep(BitSetcopy(0, x.rep)) {}
  191.  
  192. inline BitSet::~BitSet() { if (rep != &_nilBitSetRep) delete rep; }
  193.  
  194. inline void BitSet::operator =  (const BitSet& y)
  195.   rep = BitSetcopy(rep, y.rep);
  196. }
  197.  
  198. inline int operator != (const BitSet& x, const BitSet& y) { return !(x == y); }
  199.  
  200. inline int operator >  (const BitSet& x, const BitSet& y) { return y < x; }
  201.  
  202. inline int operator >= (const BitSet& x, const BitSet& y) { return y <= x; }
  203.  
  204. inline void and(const BitSet& x, const BitSet& y, BitSet& r)
  205. {
  206.   r.rep =  BitSetop(x.rep, y.rep, r.rep, '&');
  207. }
  208.  
  209. inline void or(const BitSet& x, const BitSet& y, BitSet& r)
  210. {
  211.   r.rep =  BitSetop(x.rep, y.rep, r.rep, '|');
  212. }
  213.  
  214. inline void xor(const BitSet& x, const BitSet& y, BitSet& r)
  215. {
  216.   r.rep =  BitSetop(x.rep, y.rep, r.rep, '^');
  217. }
  218.  
  219. inline void diff(const BitSet& x, const BitSet& y, BitSet& r)
  220. {
  221.   r.rep =  BitSetop(x.rep, y.rep, r.rep, '-');
  222. }
  223.  
  224. inline void complement(const BitSet& x, BitSet& r)
  225. {
  226.   r.rep = BitSetcmpl(x.rep, r.rep);
  227. }
  228.  
  229. #if defined(__GNUG__) && !defined(NO_NRV)
  230.  
  231. inline BitSet operator & (const BitSet& x, const BitSet& y) return r
  232. {
  233.   and(x, y, r);
  234. }
  235.  
  236. inline BitSet operator | (const BitSet& x, const BitSet& y) return r
  237. {
  238.   or(x, y, r);
  239. }
  240.  
  241. inline BitSet operator ^ (const BitSet& x, const BitSet& y) return r
  242. {
  243.   xor(x, y, r);
  244. }
  245.  
  246. inline BitSet operator - (const BitSet& x, const BitSet& y) return r
  247. {
  248.   diff(x, y, r);
  249. }
  250.  
  251. inline BitSet operator ~ (const BitSet& x) return r
  252. {
  253.   ::complement(x, r);
  254. }
  255.  
  256. #else /* NO_NRV */
  257.  
  258. inline BitSet operator & (const BitSet& x, const BitSet& y) 
  259. {
  260.   BitSet r; and(x, y, r); return r;
  261. }
  262.  
  263. inline BitSet operator | (const BitSet& x, const BitSet& y) 
  264. {
  265.   BitSet r; or(x, y, r); return r;
  266. }
  267.  
  268. inline BitSet operator ^ (const BitSet& x, const BitSet& y) 
  269. {
  270.   BitSet r; xor(x, y, r); return r;
  271. }
  272.  
  273. inline BitSet operator - (const BitSet& x, const BitSet& y) 
  274. {
  275.   BitSet r; diff(x, y, r); return r;
  276. }
  277.  
  278. inline BitSet operator ~ (const BitSet& x) 
  279. {
  280.   BitSet r; ::complement(x, r); return r;
  281. }
  282.  
  283. #endif
  284.  
  285. inline void BitSet::operator &= (const BitSet& y)
  286. {
  287.   and(*this, y, *this);
  288. }
  289.  
  290. inline void BitSet::operator |= (const BitSet& y)
  291. {
  292.   or(*this, y, *this);
  293. }
  294.  
  295. inline void BitSet::operator ^= (const BitSet& y)
  296. {
  297.   xor(*this, y, *this);
  298. }
  299.  
  300. inline void BitSet::operator -= (const BitSet& y)
  301. {
  302.   diff(*this, y, *this);
  303. }
  304.  
  305.  
  306. inline void BitSet::complement()
  307. {
  308.   ::complement(*this, *this);
  309. }
  310.  
  311. inline int BitSet::virtual_bit() const
  312. {
  313.   return rep->virt;
  314. }
  315.  
  316. inline int BitSet::first(int b) const
  317. {
  318.   return next(-1, b);
  319. }
  320.  
  321. inline int BitSet::test(int p) const
  322. {
  323.   if (p < 0) error("Illegal bit index");
  324.   int index = BitSet_index(p);
  325.   return (index >= rep->len)? rep->virt : 
  326.          ((rep->s[index] & (1 << BitSet_pos(p))) != 0);
  327. }
  328.  
  329.  
  330. inline void BitSet::set()
  331. {
  332.   rep = BitSetalloc(rep, 0, 0, 1, 0);
  333. }
  334.  
  335. inline BitSetBit::BitSetBit(const BitSetBit& b) :src(b.src), pos(b.pos) {}
  336.  
  337. inline BitSetBit::BitSetBit(BitSet* v, int p)
  338. {
  339.   src = v;  pos = p;
  340. }
  341.  
  342. inline BitSetBit::~BitSetBit() {}
  343.  
  344. inline BitSetBit::operator int()
  345. {
  346.   return src->test(pos);
  347. }
  348.  
  349. inline int BitSetBit::operator = (int b)
  350. {
  351.   if (b) src->set(pos); else src->clear(pos); return b;
  352. }
  353.  
  354. inline int BitSetBit::operator == (int b)
  355. {
  356.   return src->test(pos) == b;
  357. }
  358.  
  359. inline int BitSetBit::operator != (int b)
  360. {
  361.   return src->test(pos) != b;
  362. }
  363.  
  364. inline BitSetBit BitSet::operator [] (int i)
  365. {
  366.   if (i < 0) error("illegal bit index");
  367.   return BitSetBit(this, i);
  368. }
  369.  
  370. #endif
  371.