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