home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 5
/
FreshFish_July-August1994.bin
/
bbs
/
gnu
/
libg++-2.5.3-bin.lha
/
lib
/
g++-include
/
BitString.h
< prev
next >
Wrap
C/C++ Source or Header
|
1994-07-11
|
20KB
|
758 lines
// This may look like C code, but it is really -*- C++ -*-
/*
Copyright (C) 1988 Free Software Foundation
written by Doug Lea (dl@rocky.oswego.edu)
This file is part of the GNU C++ Library. This library is free
software; you can redistribute it and/or modify it under the terms of
the GNU Library General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version. This library is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef _BitString_h
#ifdef __GNUG__
#pragma interface
#endif
#define _BitString_h 1
#include <stream.h>
#include <limits.h>
#define BITSTRBITS (sizeof(short) * CHAR_BIT)
struct BitStrRep
{
unsigned int len; // length in bits
unsigned short sz; // allocated slots
unsigned short s[1]; // bits start here
};
extern BitStrRep* BStr_alloc(BitStrRep*, const unsigned short*, int, int,int);
extern BitStrRep* BStr_resize(BitStrRep*, int);
extern BitStrRep* BStr_copy(BitStrRep*, const BitStrRep*);
extern BitStrRep* cmpl(const BitStrRep*, BitStrRep*);
extern BitStrRep* and(const BitStrRep*, const BitStrRep*, BitStrRep*);
extern BitStrRep* or(const BitStrRep*, const BitStrRep*, BitStrRep*);
extern BitStrRep* xor(const BitStrRep*, const BitStrRep*, BitStrRep*);
extern BitStrRep* diff(const BitStrRep*, const BitStrRep*, BitStrRep*);
extern BitStrRep* cat(const BitStrRep*, const BitStrRep*, BitStrRep*);
extern BitStrRep* cat(const BitStrRep*, unsigned int, BitStrRep*);
extern BitStrRep* lshift(const BitStrRep*, int, BitStrRep*);
class BitString;
class BitPattern;
class BitStrBit
{
protected:
BitString& src;
unsigned int pos;
public:
BitStrBit(BitString& v, int p);
BitStrBit(const BitStrBit& b);
~BitStrBit();
operator unsigned int() const;
int operator = (unsigned int b);
};
class BitSubString
{
friend class BitString;
friend class BitPattern;
protected:
BitString& S;
unsigned int pos;
unsigned int len;
BitSubString(BitString& x, int p, int l);
BitSubString(const BitSubString& x);
public:
~BitSubString();
void operator = (const BitString&);
void operator = (const BitSubString&);
int length() const;
int empty() const;
int OK() const;
};
class BitString
{
friend class BitSubString;
friend class BitPattern;
protected:
BitStrRep* rep;
int search(int, int, const unsigned short*, int, int) const;
int match(int, int, int, const unsigned short*,int,int) const;
BitSubString _substr(int first, int l);
public:
// constructors
BitString();
BitString(const BitString&);
BitString(const BitSubString& y);
~BitString();
void operator = (unsigned int bit);
void operator = (const BitString& y);
void operator = (const BitSubString& y);
// equality & subset tests
friend int operator == (const BitString&, const BitString&);
friend int operator != (const BitString&, const BitString&);
friend int operator < (const BitString&, const BitString&);
friend int operator <= (const BitString&, const BitString&);
friend int operator > (const BitString&, const BitString&);
friend int operator >= (const BitString&, const BitString&);
// procedural versions of operators
friend void and(const BitString&, const BitString&, BitString&);
friend void or(const BitString&, const BitString&, BitString&);
friend void xor(const BitString&, const BitString&, BitString&);
friend void diff(const BitString&, const BitString&, BitString&);
friend void cat(const BitString&, const BitString&, BitString&);
friend void cat(const BitString&, unsigned int, BitString&);
friend void lshift(const BitString&, int, BitString&);
friend void rshift(const BitString&, int, BitString&);
friend void complement(const BitString&, BitString&);
friend int lcompare(const BitString&, const BitString&);
// assignment-based operators
// (constuctive versions decalred inline below
void operator |= (const BitString&);
void operator &= (const BitString&);
void operator -= (const BitString&);
void operator ^= (const BitString&);
void operator += (const BitString&);
void operator += (unsigned int b);
void operator <<=(int s);
void operator >>=(int s);
void complement();
// individual bit manipulation
void set(int pos);
void set(int from, int to);
void set();
void clear(int pos);
void clear(int from, int to);
void clear();
void invert(int pos);
void invert(int from, int to);
int test(int pos) const;
int test(int from, int to) const;
void assign(int p, unsigned int bit);
// indexing
BitStrBit operator [] (int pos);
// iterators
int first(unsigned int bit = 1) const;
int last(unsigned int b = 1) const;
int next(int pos, unsigned int b = 1) const;
int prev(int pos, unsigned int b = 1) const;
int previous(int pos, unsigned int b = 1) const
{ return prev(pos, b); } /* Obsolete synonym */
// searching & matching
int index(unsigned int bit, int startpos = 0) const ;
int index(const BitString&, int startpos = 0) const;
int index(const BitSubString&, int startpos = 0) const;
int index(const BitPattern&, int startpos = 0) const;
int contains(const BitString&) const;
int contains(const BitSubString&) const;
int contains(const BitPattern&) const;
int contains(const BitString&, int pos) const;
int contains(const BitSubString&, int pos) const;
int contains(const BitPattern&, int pos) const;
int matches(const BitString&, int pos = 0) const;
int matches(const BitSubString&, int pos = 0) const;
int matches(const BitPattern&, int pos = 0) const;
// BitSubString extraction
BitSubString at(int pos, int len);
BitSubString at(const BitString&, int startpos = 0);
BitSubString at(const BitSubString&, int startpos = 0);
BitSubString at(const BitPattern&, int startpos = 0);
BitSubString before(int pos);
BitSubString before(const BitString&, int startpos = 0);
BitSubString before(const BitSubString&, int startpos = 0);
BitSubString before(const BitPattern&, int startpos = 0);
BitSubString after(int pos);
BitSubString after(const BitString&, int startpos = 0);
BitSubString after(const BitSubString&, int startpos = 0);
BitSubString after(const BitPattern&, int startpos = 0);
// other friends & utilities
friend BitString common_prefix(const BitString&, const BitString&,
int pos = 0);
friend BitString common_suffix(const BitString&, const BitString&,
int pos = -1);
friend BitString reverse(const BitString&);
void right_trim(unsigned