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