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