home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Headers / g++ / BitString.h < prev    next >
C/C++ Source or Header  |  1993-06-29  |  20KB  |  768 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 _BitString_h
  20. #ifdef __GNUG__
  21. #pragma interface
  22. #endif
  23.  
  24. #define _BitString_h 1
  25.  
  26. #include <stream.h>
  27. #include <limits.h>
  28.  
  29. #define BITSTRBITS  (sizeof(short) * CHAR_BIT)
  30.  
  31. struct BitStrRep
  32. {
  33.   unsigned int    len;          // length in bits
  34.   unsigned short  sz;           // allocated slots
  35.   unsigned short  s[1];         // bits start here
  36. };
  37.  
  38. extern BitStrRep*  BStr_alloc(BitStrRep*, const unsigned short*, int, int,int);
  39. extern BitStrRep*  BStr_resize(BitStrRep*, int);
  40. extern BitStrRep*  BStr_copy(BitStrRep*, const BitStrRep*);
  41. extern BitStrRep*  cmpl(const BitStrRep*, BitStrRep*);
  42. extern BitStrRep*  and(const BitStrRep*, const BitStrRep*, BitStrRep*);
  43. extern BitStrRep*  or(const BitStrRep*, const BitStrRep*, BitStrRep*);
  44. extern BitStrRep*  xor(const BitStrRep*, const BitStrRep*, BitStrRep*);
  45. extern BitStrRep*  diff(const BitStrRep*, const BitStrRep*, BitStrRep*);
  46. extern BitStrRep*  cat(const BitStrRep*, const BitStrRep*, BitStrRep*);
  47. extern BitStrRep*  cat(const BitStrRep*, unsigned int, BitStrRep*);
  48. extern BitStrRep*  lshift(const BitStrRep*, int, BitStrRep*);
  49.  
  50.  
  51. class BitString;
  52. class BitPattern;
  53.  
  54. class BitStrBit
  55. {
  56. protected:
  57.   BitString&        src;
  58.   unsigned int      pos;
  59.  
  60.  public:
  61.                     BitStrBit(BitString& v, int p);
  62.                     BitStrBit(const BitStrBit& b);
  63.                    ~BitStrBit();
  64.                     operator unsigned int() const;
  65.   int               operator =  (unsigned int b);
  66.   int               operator == (unsigned int b) const ;
  67.   int               operator != (unsigned int b) const ;
  68. };
  69.  
  70. class BitSubString
  71. {
  72.   friend class      BitString;
  73.   friend class      BitPattern;
  74.  
  75. protected:
  76.  
  77.   BitString&        S;
  78.   unsigned int      pos;
  79.   unsigned int      len;
  80.  
  81.                     BitSubString(BitString& x, int p, int l);
  82.                     BitSubString(const BitSubString& x);
  83. public:
  84.                     ~BitSubString();
  85.  
  86.   void              operator =  (const BitString&);
  87.   void              operator =  (const BitSubString&);
  88.  
  89.   int               length() const;
  90.   int               empty() const;
  91.  
  92.   int               OK() const;
  93. };
  94.  
  95. class BitString
  96. {
  97.   friend class       BitSubString;
  98.   friend class       BitPattern;
  99. protected:
  100.   BitStrRep*         rep;
  101.  
  102.   int                search(int, int, const unsigned short*, int, int) const;
  103.   int                match(int, int, int, const unsigned short*,int,int) const;
  104.   BitSubString       _substr(int first, int l);
  105.  
  106. public:
  107.  
  108. // constructors
  109.                      BitString();
  110.                      BitString(const BitString&);
  111.                      BitString(const BitSubString& y);
  112.  
  113.                     ~BitString();
  114.  
  115.   void               operator =  (unsigned int bit);
  116.   void               operator =  (const BitString& y);
  117.   void               operator =  (const BitSubString& y);
  118.  
  119. // equality & subset tests
  120.  
  121.   friend int         operator == (const BitString&, const BitString&);
  122.   friend int         operator != (const BitString&, const BitString&);
  123.   friend int         operator <  (const BitString&, const BitString&);
  124.   friend int         operator <= (const BitString&, const BitString&);
  125.   friend int         operator >  (const BitString&, const BitString&);
  126.   friend int         operator >= (const BitString&, const BitString&);
  127.  
  128. // procedural versions of operators
  129.  
  130.  
  131.   friend void        and(const BitString&, const BitString&, BitString&);
  132.   friend void        or(const BitString&, const BitString&, BitString&);
  133.   friend void        xor(const BitString&, const BitString&, BitString&);
  134.   friend void        diff(const BitString&, const BitString&, BitString&);
  135.   friend void        cat(const BitString&, const BitString&, BitString&);
  136.   friend void        cat(const BitString&, unsigned int, BitString&);
  137.   friend void        lshift(const BitString&, int, BitString&);
  138.   friend void        rshift(const BitString&, int, BitString&);
  139.  
  140.   friend void        complement(const BitString&, BitString&);
  141.  
  142.   friend int         lcompare(const BitString&, const BitString&); 
  143.  
  144. // assignment-based operators
  145. // (constuctive versions decalred inline below
  146.  
  147.   void               operator |= (const BitString&);
  148.   void               operator &= (const BitString&);
  149.   void               operator -= (const BitString&);
  150.   void               operator ^= (const BitString&);
  151.   void               operator += (const BitString&);
  152.   void               operator += (unsigned int b);
  153.   void               operator <<=(int s);
  154.   void               operator >>=(int s);
  155.  
  156.   void               complement();
  157.  
  158. // individual bit manipulation
  159.  
  160.   void               set(int pos);
  161.   void               set(int from, int to);
  162.   void               set();
  163.  
  164.   void               clear(int pos);
  165.   void               clear(int from, int to);
  166.   void               clear(); 
  167.  
  168.   void               invert(int pos);
  169.   void               invert(int from, int to);
  170.  
  171.   int                test(int pos) const;
  172.   int                test(int from, int to) const;
  173.  
  174.   void               assign(int p, unsigned int bit);
  175.  
  176. // indexing
  177.  
  178.   BitStrBit          operator [] (int pos);
  179.  
  180. // iterators
  181.  
  182.   int                first(unsigned int bit = 1) const;
  183.   int                last(unsigned int b = 1) const;
  184.  
  185.   int                next(int pos, unsigned int b = 1) const;
  186.   int                previous(int pos, unsigned int b = 1) const;
  187.  
  188. // searching & matching
  189.  
  190.   int                index(unsigned int bit, int startpos = 0) const ;      
  191.   int                index(const BitString&, int startpos = 0) const;
  192.   int                index(const BitSubString&, int startpos = 0) const;
  193.   int                index(const BitPattern&, int startpos = 0) const;
  194.  
  195.   int                contains(const BitString&) const;
  196.   int                contains(const BitSubString&) const;
  197.   int                contains(const BitPattern&) const;
  198.  
  199.   int                contains(const BitString&, int pos) const;
  200.   int                contains(const BitSubString&, int pos) const;
  201.   int                contains(const BitPattern&, int pos) const;
  202.  
  203.   int                matches(const BitString&, int pos = 0) const;
  204.   int                matches(const BitSubString&, int pos = 0) const;
  205.   int                matches(const BitPattern&, int pos = 0) const;
  206.  
  207. // BitSubString extraction
  208.  
  209.   BitSubString       at(int pos, int len);
  210.   BitSubString       at(const BitString&, int startpos = 0); 
  211.   BitSubString       at(const BitSubString&, int startpos = 0); 
  212.   BitSubString       at(const BitPattern&, int startpos = 0); 
  213.  
  214.   BitSubString       before(int pos);
  215.   BitSubString       before(const BitString&, int startpos = 0);
  216.   BitSubString       before(const BitSubString&, int startpos = 0);
  217.   BitSubString       before(const BitPattern&, int startpos = 0);
  218.  
  219.   BitSubString       after(int pos);
  220.   BitSubString       after(const BitString&, int startpos = 0);
  221.   BitSubString       after(const BitSubString&, int startpos = 0);
  222.   BitSubString       after(const BitPattern&, int startpos = 0);
  223.  
  224. // other friends & utilities
  225.  
  226.   friend BitString   common_prefix(const BitString&, const BitString&, 
  227.                                    int pos = 0);
  228.   friend BitString   common_suffix(const BitString&, const BitString&, 
  229.                                    int pos = -1);
  230.   friend BitString   reverse(const BitString&);
  231.  
  232.   void               right_trim(unsigned int bit);
  233.   void               left_trim(unsigned int bit);
  234.  
  235. // status
  236.  
  237.   int                empty() const ;
  238.   int                count(unsigned int bit = 1) const;
  239.   int                length() const;
  240.  
  241. // convertors & IO
  242.  
  243.   friend BitString   atoBitString(const char* s, char f='0', char t='1');
  244.   // BitStringtoa is deprecated; do not use in new programs!
  245.   friend const char* BitStringtoa(const BitString&, char f='0', char t='1');
  246.   void             printon(ostream&, char f='0', char t='1') const;
  247.  
  248.   friend BitString   shorttoBitString(unsigned short);
  249.   friend BitString   longtoBitString(unsigned long);
  250.  
  251.   friend ostream&    operator << (ostream& s, const BitString&);
  252.  
  253. // misc
  254.  
  255.   void      error(const char* msg) const;
  256.  
  257. // indirect friends
  258.  
  259.   friend BitPattern  atoBitPattern(const char* s,
  260.                                   char f='0',char t='1',char x='X');
  261.   friend const char* BitPatterntoa(const BitPattern& p, 
  262.                                   char f='0',char t='1',char x='X');
  263.   int                OK() const;
  264. };
  265.  
  266.  
  267. class BitPattern
  268. {
  269. public:
  270.   BitString          pattern;
  271.   BitString          mask;
  272.  
  273.                      BitPattern();
  274.                      BitPattern(const BitPattern&);
  275.                      BitPattern(const BitString& p, const BitString& m);
  276.  
  277.                     ~BitPattern();
  278.  
  279.   friend const char* BitPatterntoa(const BitPattern& p, 
  280.                                  char f/*='0'*/,char t/*='1'*/,char x/*='X'*/);
  281.   void             printon(ostream&, char f='0',char t='1',char x='X') const;
  282.   friend BitPattern atoBitPattern(const char* s, char f,char t, char x);
  283.   friend ostream&   operator << (ostream& s, const BitPattern&);
  284.  
  285.   int               search(const unsigned short*, int, int) const;
  286.   int               match(const unsigned short* xs, int, int, int) const;
  287.  
  288.   int               OK() const;
  289. };
  290.  
  291. BitString  operator & (const BitString& x, const BitString& y);
  292. BitString  operator | (const BitString& x, const BitString& y);
  293. BitString  operator ^ (const BitString& x, const BitString& y);
  294. BitString  operator << (const BitString& x, int y);
  295. BitString  operator >> (const BitString& x, int y);
  296. BitString  operator - (const BitString& x, const BitString& y);
  297. BitString  operator + (const BitString& x, const BitString& y);
  298. BitString  operator + (const BitString& x, unsigned int y);
  299. BitString  operator ~ (const BitString& x);
  300. int operator != (const BitString& x, const BitString& y);
  301. int operator>(const BitString& x, const BitString& y);
  302. int operator>=(const BitString& x, const BitString& y);
  303.  
  304. extern BitStrRep    _nilBitStrRep;
  305. extern BitString    _nil_BitString;
  306.  
  307. // primitive bit extraction
  308.  
  309. // These must be inlined regardless of optimization.
  310.  
  311. inline int BitStr_index(int l) { return (unsigned)(l) / BITSTRBITS; }
  312.  
  313. inline int BitStr_pos(int l) { return l & (BITSTRBITS - 1); }
  314.  
  315.  
  316. // constructors & assignment
  317.  
  318. inline BitString::BitString() :rep(&_nilBitStrRep) {}
  319.  
  320. inline BitString::BitString(const BitString& x) :rep(BStr_copy(0, x.rep)) {}
  321.  
  322. inline BitString::BitString(const BitSubString& y) 
  323.    :rep (BStr_alloc(0, y.S.rep->s, y.pos, y.pos+y.len, y.len)) {}
  324.  
  325. inline BitString::~BitString()
  326.   if (rep != &_nilBitStrRep) delete rep;
  327. }
  328.  
  329. #if defined(__GNUG__) && !defined(NO_NRV)
  330.  
  331. inline BitString shorttoBitString(unsigned short w) return r
  332.   r.rep = BStr_alloc(0, &w, 0, BITSTRBITS, BITSTRBITS);
  333. }
  334.  
  335. inline BitString longtoBitString(unsigned long w) return r
  336.   unsigned short u[2];
  337.   u[0] = w & ((unsigned short)(~(0)));
  338.   u[1] = w >> BITSTRBITS;
  339.   r.rep = BStr_alloc(0, &u[0], 0, 2*BITSTRBITS, 2*BITSTRBITS);
  340. }
  341.  
  342. #else
  343.  
  344. inline BitString shorttoBitString(unsigned short w) 
  345.   BitString r; r.rep = BStr_alloc(0, &w, 0, BITSTRBITS, BITSTRBITS); return r;
  346. }
  347.  
  348. inline BitString longtoBitString(unsigned long w) 
  349.   BitString r;
  350.   unsigned short u[2];
  351.   u[0] = w & ((unsigned short)(~(0)));
  352.   u[1] = w >> BITSTRBITS;
  353.   r.rep = BStr_alloc(0, &u[0], 0, 2*BITSTRBITS, 2*BITSTRBITS);
  354.   return r;
  355. }
  356.  
  357. #endif
  358.  
  359. inline void BitString::operator =  (const BitString& y)
  360.   rep = BStr_copy(rep, y.rep);
  361. }
  362.  
  363. inline void BitString::operator = (unsigned int b)
  364.   unsigned short bit = b;
  365.   rep = BStr_alloc(rep, &bit, 0, 1, 1);
  366. }
  367.  
  368. inline void BitString::operator=(const BitSubString&  y)
  369. {
  370.   rep = BStr_alloc(rep, y.S.rep->s, y.pos, y.pos+y.len, y.len);
  371. }
  372.  
  373. inline BitSubString::BitSubString(const BitSubString& x) 
  374.     :S(x.S), pos(x.pos), len(x.len) {}
  375.  
  376. inline BitSubString::BitSubString(BitString& x, int p, int l)
  377.      : S(x), pos(p), len(l) {}
  378.  
  379. inline BitSubString::~BitSubString() {}
  380.  
  381. inline BitPattern::BitPattern(const BitString& p, const BitString& m)
  382.     :pattern(p), mask(m) {}
  383.  
  384. inline BitPattern::BitPattern(const BitPattern& b)
  385.     :pattern(b.pattern), mask(b.mask) {}
  386.  
  387. inline BitPattern::BitPattern() {}
  388. inline BitPattern::~BitPattern() {}
  389.  
  390.  
  391. // procedural versions of operators
  392.  
  393. inline void and(const BitString& x, const BitString& y, BitString& r)
  394. {
  395.   r.rep = and(x.rep, y.rep, r.rep);
  396. }
  397.  
  398. inline void or(const BitString& x, const BitString& y, BitString& r)
  399. {
  400.   r.rep = or(x.rep, y.rep, r.rep);
  401. }
  402.  
  403. inline void xor(const BitString& x, const BitString& y, BitString& r)
  404. {
  405.   r.rep = xor(x.rep, y.rep, r.rep);
  406. }
  407.  
  408. inline void diff(const BitString& x, const BitString& y, BitString& r)
  409. {
  410.   r.rep = diff(x.rep, y.rep, r.rep);
  411. }
  412.  
  413. inline void cat(const BitString& x, const BitString& y, BitString& r)
  414. {
  415.   r.rep = cat(x.rep, y.rep, r.rep);
  416. }
  417.  
  418. inline void cat(const BitString& x, unsigned int y, BitString& r)
  419. {
  420.   r.rep = cat(x.rep, y, r.rep);
  421. }
  422.  
  423. inline void rshift(const BitString& x, int y, BitString& r)
  424. {
  425.   r.rep = lshift(x.rep, -y, r.rep);
  426. }
  427.  
  428. inline void lshift(const BitString& x, int y, BitString& r)
  429. {
  430.   r.rep = lshift(x.rep, y, r.rep);
  431. }
  432.  
  433. inline void complement(const BitString& x, BitString& r)
  434. {
  435.   r.rep = cmpl(x.rep, r.rep);
  436. }
  437.  
  438. // operators
  439.  
  440.  
  441. inline void BitString::operator &= (const BitString& y)
  442. {
  443.   and(*this, y, *this);
  444. }
  445.  
  446.  
  447. inline void BitString::operator |= (const BitString& y)
  448. {
  449.   or(*this, y, *this);
  450. }
  451.  
  452. inline void BitString::operator ^= (const BitString& y)
  453. {
  454.   xor(*this, y, *this);
  455. }
  456.  
  457. inline void BitString::operator <<= (int y)
  458. {
  459.   lshift(*this, y, *this);
  460. }
  461.  
  462. inline void BitString::operator >>= (int y)
  463. {
  464.   rshift(*this, y, *this);
  465. }
  466.  
  467. inline void BitString::operator -= (const BitString& y)
  468. {
  469.   diff(*this, y, *this);
  470. }
  471.  
  472. inline void BitString::operator += (const BitString& y)
  473. {
  474.   cat(*this, y, *this);
  475. }
  476.  
  477. inline void BitString::operator += (unsigned int y)
  478. {
  479.   cat(*this, y, *this);
  480. }
  481.  
  482. inline void BitString::complement()
  483. {
  484.   ::complement(*this, *this);
  485. }
  486.  
  487. #if defined(__GNUG__) && !defined(NO_NRV)
  488.  
  489. inline BitString  operator & (const BitString& x, const BitString& y) return r
  490. {
  491.   and(x, y, r);
  492. }
  493.  
  494. inline BitString  operator | (const BitString& x, const BitString& y) return r
  495. {
  496.   or(x, y, r);
  497. }
  498.  
  499. inline BitString  operator ^ (const BitString& x, const BitString& y) return r
  500. {
  501.   xor(x, y, r);
  502. }
  503.  
  504. inline BitString  operator << (const BitString& x, int y) return r
  505. {
  506.   lshift(x, y, r);
  507. }
  508.  
  509. inline BitString  operator >> (const BitString& x, int y) return r
  510. {
  511.   rshift(x, y, r);
  512. }
  513.  
  514. inline BitString  operator - (const BitString& x, const BitString& y) return r
  515. {
  516.   diff(x, y, r);
  517. }
  518.  
  519. inline BitString  operator + (const BitString& x, const BitString& y) return r
  520. {
  521.   cat(x, y, r);
  522. }
  523.  
  524. inline BitString  operator + (const BitString& x, unsigned int y) return r
  525. {
  526.   cat(x, y, r);
  527. }
  528.  
  529. inline BitString  operator ~ (const BitString& x) return r
  530. {
  531.   complement(x, r);
  532. }
  533.  
  534. #else /* NO_NRV */
  535.  
  536. inline BitString  operator & (const BitString& x, const BitString& y) 
  537. {
  538.   BitString r; and(x, y, r); return r;
  539. }
  540.  
  541. inline BitString  operator | (const BitString& x, const BitString& y) 
  542. {
  543.   BitString r; or(x, y, r); return r;
  544. }
  545.  
  546. inline BitString  operator ^ (const BitString& x, const BitString& y) 
  547. {
  548.   BitString r; xor(x, y, r); return r;
  549. }
  550.  
  551. inline BitString  operator << (const BitString& x, int y) 
  552. {
  553.   BitString r; lshift(x, y, r); return r;
  554. }
  555.  
  556. inline BitString  operator >> (const BitString& x, int y) 
  557. {
  558.   BitString r; rshift(x, y, r); return r;
  559. }
  560.  
  561. inline BitString  operator - (const BitString& x, const BitString& y) 
  562. {
  563.   BitString r; diff(x, y, r); return r;
  564. }
  565.  
  566. inline BitString  operator + (const BitString& x, const BitString& y) 
  567. {
  568.   BitString r; cat(x, y, r); return r;
  569. }
  570.  
  571. inline BitString  operator + (const BitString& x, unsigned int y) 
  572. {
  573.   BitString r; cat(x, y, r); return r;
  574. }
  575.  
  576. inline BitString  operator ~ (const BitString& x) 
  577. {
  578.   BitString r; complement(x, r); return r;
  579. }
  580.  
  581. #endif
  582.  
  583. // status, matching
  584.  
  585. inline int BitString::length() const
  586.   return rep->len;
  587. }
  588.  
  589. inline int BitString::empty() const
  590.   return rep->len == 0;
  591. }
  592.  
  593. inline int BitString::index(const BitString& y, int startpos) const
  594. {   
  595.   return search(startpos, rep->len, y.rep->s, 0, y.rep->len);
  596. }
  597.  
  598. inline int BitString::index(const BitSubString& y, int startpos) const
  599. {   
  600.   return search(startpos, rep->len, y.S.rep->s, y.pos, y.pos+y.len);
  601. }
  602.  
  603. inline int BitString::contains(const BitString& y) const
  604. {   
  605.   return search(0, rep->len, y.rep->s, 0, y.rep->len) >= 0;
  606. }
  607.  
  608. inline int BitString::contains(const BitSubString& y) const
  609. {   
  610.   return search(0, rep->len, y.S.rep->s, y.pos, y.pos+y.len) >= 0;
  611. }
  612.  
  613. inline int BitString::contains(const BitString& y, int p) const
  614. {
  615.   return match(p, rep->len, 0, y.rep->s, 0, y.rep->len);
  616. }
  617.  
  618. inline int BitString::matches(const BitString& y, int p) const
  619. {
  620.   return match(p, rep->len, 1, y.rep->s, 0, y.rep->len);
  621. }
  622.  
  623. inline int BitString::contains(const BitSubString& y, int p) const
  624. {
  625.   return match(p, rep->len, 0, y.S.rep->s, y.pos, y.pos+y.len);
  626. }
  627.  
  628. inline int BitString::matches(const BitSubString& y, int p) const
  629. {
  630.   return match(p, rep->len, 1, y.S.rep->s, y.pos, y.pos+y.len);
  631. }
  632.  
  633. inline int BitString::contains(const BitPattern& r) const
  634. {
  635.   return r.search(rep->s, 0, rep->len) >= 0;
  636. }
  637.  
  638. inline int BitString::contains(const BitPattern& r, int p) const
  639. {
  640.   return r.match(rep->s, p, rep->len, 0);
  641. }
  642.  
  643. inline int BitString::matches(const BitPattern& r, int p) const
  644. {
  645.   return r.match(rep->s, p, rep->len, 1);
  646. }
  647.  
  648. inline int BitString::index(const BitPattern& r, int startpos) const
  649. {
  650.   return r.search(rep->s, startpos, rep->len);
  651. }
  652.  
  653. inline  int BitSubString::length() const
  654.   return len;
  655. }
  656.  
  657. inline  int BitSubString::empty() const
  658.   return len == 0;
  659. }
  660.  
  661. inline int operator != (const BitString& x, const BitString& y)
  662. {
  663.   return !(x == y);
  664. }
  665.  
  666. inline int operator>(const BitString& x, const BitString& y)
  667. {
  668.   return y < x;
  669. }
  670.  
  671. inline int operator>=(const BitString& x, const BitString& y)
  672. {
  673.   return y <= x;
  674. }
  675.  
  676. inline int BitString::first(unsigned int b) const
  677. {
  678.   return next(-1, b);
  679. }
  680.  
  681. inline int BitString::last(unsigned int b) const
  682. {
  683.   return previous(rep->len, b);
  684. }
  685.  
  686. inline int BitString::index(unsigned int bit, int startpos) const
  687. {
  688.   if (startpos >= 0)
  689.     return next(startpos - 1, bit);
  690.   else
  691.     return previous(rep->len + startpos + 1, bit);
  692. }
  693.  
  694. inline void BitString::right_trim(unsigned int b) 
  695. {
  696.   int nb = (b == 0)? 1 : 0;
  697.   rep = BStr_resize(rep, previous(rep->len, nb) + 1);
  698. }
  699.  
  700. inline void BitString::left_trim(unsigned int b)
  701. {
  702.   int nb = (b == 0)? 1 : 0;
  703.   int p = next(-1, nb);
  704.   rep = BStr_alloc(rep, rep->s, p, rep->len, rep->len - p);
  705. }
  706.  
  707. inline int BitString::test(int i) const
  708. {
  709.   return ((unsigned)(i) >= rep->len)? 0 : 
  710.          ((rep->s[BitStr_index(i)] & (1 << (BitStr_pos(i)))) != 0);
  711. }
  712.  
  713.  
  714. // subscripting
  715.  
  716. inline BitStrBit::BitStrBit(const BitStrBit& b) :src(b.src), pos(b.pos) {}
  717.  
  718. inline BitStrBit::BitStrBit(BitString& v, int p) :src(v), pos(p) {}
  719.  
  720. inline BitStrBit::~BitStrBit() {}
  721.  
  722. inline BitStrBit::operator unsigned int() const
  723. {
  724.   return src.test(pos);
  725. }
  726.  
  727. inline int BitStrBit::operator = (unsigned int b)
  728. {
  729.   src.assign(pos, b); return b;
  730. }
  731.  
  732. inline int BitStrBit::operator == (unsigned int b) const
  733. {
  734.   return src.test(pos) == b;
  735. }
  736.  
  737. inline int BitStrBit::operator != (unsigned int b) const
  738. {
  739.   return src.test(pos) != b;
  740. }
  741.  
  742. inline BitStrBit BitString::operator [] (int i)
  743. {
  744.   if ((unsigned)(i) >= rep->len) error("illegal bit index");
  745.   return BitStrBit(*this, i);
  746. }
  747.  
  748. inline BitSubString BitString::_substr(int first, int l)
  749. {
  750.   if (first < 0 || l <= 0 || (unsigned)(first + l) > rep->len)
  751.     return BitSubString(_nil_BitString, 0, 0) ;
  752.   else 
  753.     return BitSubString(*this, first, l);
  754. }
  755.  
  756. #endif
  757.