Copyright (C) 1988 Free Software Foundation
written by Doug Lea (dl@rocky.oswego.edu)
This file is part of GNU CC.
GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY. No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing. Refer to the GNU CC General Public
License for full details.
Everyone is granted permission to copy, modify and redistribute
GNU CC, but only under the conditions described in the
GNU CC General Public License. A copy of this license is
supposed to have been given to you along with GNU CC so you
can know your rights and responsibilities. It should be in a
file named COPYING. Among other things, the copyright notice
and this notice must be preserved on all copies.
BitSet class implementation
volatile void BitSet::error(const char* msg
) const
(*lib_error_handler
)("BitSet", msg
);
BitSetRep _nilBitSetRep
= { 0, 1, 0, {0} }; // nil BitSets point here
#define ONES ((unsigned short)(~0L))
#define MAXBitSetRep_SIZE ((1 << (SHORTBITS - 1)) - 1)
#define MINBitSetRep_SIZE 16
#ifndef MALLOC_MIN_OVERHEAD
#define MALLOC_MIN_OVERHEAD 4
// break things up into .s indices and positions
// mask out bits from left
static inline unsigned short lmask(int p
)
static inline unsigned short rmask(int p
)
return ONES
>> (BITSETBITS
- 1 - p
);
inline static BitSetRep
* BSnew(int newlen
)
unsigned int siz
= sizeof(BitSetRep
) + newlen
* sizeof(short)
unsigned int allocsiz
= MINBitSetRep_SIZE
;;
while (allocsiz
< siz
) allocsiz
<<= 1;
allocsiz
-= MALLOC_MIN_OVERHEAD
;
if (allocsiz
>= MAXBitSetRep_SIZE
* sizeof(short))
(*lib_error_handler
)("BitSet", "Requested length out of range");
BitSetRep
* rep
= (BitSetRep
*) new char[allocsiz
];
rep
->sz
= (allocsiz
- sizeof(BitSetRep
) + sizeof(short)) / sizeof(short);
BitSetRep
* BitSetalloc(BitSetRep
* old
, const unsigned short* src
, int srclen
,
if (old
== &_nilBitSetRep
) old
= 0;
if (old
== 0 || newlen
>= old
->sz
)
if (srclen
!= 0 && src
!= rep
->s
)
bcopy(src
, rep
->s
, srclen
* sizeof(short));
if (old
!= rep
&& old
!= 0) delete old
;
BitSetRep
* BitSetresize(BitSetRep
* old
, int newlen
)
if (old
== 0 || old
== &_nilBitSetRep
)
else if (newlen
>= old
->sz
)
bcopy(old
->s
, rep
->s
, old
->len
* sizeof(short));
// same, for straight copy
BitSetRep
* BitSetcopy(BitSetRep
* old
, const BitSetRep
* src
)
if (old
== &_nilBitSetRep
) old
= 0;
if (src
== 0 || src
== &_nilBitSetRep
)
if (old
== 0 || newlen
> old
->sz
)
if (old
!= 0) delete old
;
bcopy(src
->s
, rep
->s
, newlen
* sizeof(short));
// remove unneeded top bits
inline static void trim(BitSetRep
* rep
)
unsigned short* s
= &(rep
->s
[l
- 1]);
while (l
> 0 && *s
-- == 0) --l
;
while (l
> 0 && *s
-- == ONES
) --l
;
int operator == (const BitSet
& x
, const BitSet
& y
)
return x
.rep
->len
== y
.rep
->len
&& x
.rep
->virt
== y
.rep
->virt
&&
bcmp((void*)x
.rep
->s
, (void*)y
.rep
->s
,
x
.rep
->len
* sizeof(short)) == 0;
int operator <= (const BitSet
& x
, const BitSet
& y
)
if (x
.rep
->virt
> y
.rep
->virt
)
unsigned short* xs
= x
.rep
->s
;
unsigned short* ys
= y
.rep
->s
;
unsigned short* topx
= &(xs
[xl
]);
unsigned short* topy
= &(ys
[yl
]);
while (xs
< topx
&& ys
< topy
)
unsigned short a
= *xs
++;
unsigned short b
= *ys
++;
return x
.rep
->virt
<= y
.rep
->virt
;
int operator < (const BitSet
& x
, const BitSet
& y
)
if (x
.rep
->virt
> y
.rep
->virt
)
unsigned short* xs
= x
.rep
->s
;
unsigned short* ys
= y
.rep
->s
;
unsigned short* topx
= &(xs
[xl
]);
unsigned short* topy
= &(ys
[yl
]);
while (xs
< topx
&& ys
< topy
)
unsigned short a
= *xs
++;
unsigned short b
= *ys
++;
unsigned short c
= a
| b
;
return x
.rep
->virt
< y
.rep
->virt
||
(one_diff
&& x
.rep
->virt
== y
.rep
->virt
);
int BitSet::empty() const
unsigned short* bots
= rep
->s
;
unsigned short* s
= &(bots
[rep
->len
- 1]);
while (s
>= bots
) if (*s
-- != 0) return 0;
int BitSet::count(int b
) const
unsigned short* s
= rep
->s
;
unsigned short* tops
= &(s
[rep
->len
]);
for (int i
= 0; i
< BITSETBITS
&& a
!= 0; ++i
)
unsigned short maxbit
= 1 << (BITSETBITS
- 1);
for (int i
= 0; i
< BITSETBITS
; ++i
)
BitSetRep
* BitSetcmpl(const BitSetRep
* src
, BitSetRep
* r
)
unsigned short* rs
= r
->s
;
unsigned short* topr
= &(rs
[r
->len
]);
unsigned short cmp
= ~(*rs
);
BitSetRep
* BitSetop(const BitSetRep
* x
, const BitSetRep
* y
,
int rl
= (xl
>= yl
)? xl
: yl
;
unsigned short* rs
= r
->s
;
unsigned short* topr
= &(rs
[rl
]);
const unsigned short* as
;
const unsigned short* topa
;
const unsigned short* bs
;
const unsigned short* topb
;
as
= (xrsame
)? r
->s
: x
->s
;
bs
= (yrsame
)? r
->s
: y
->s
;
as
= (yrsame
)? r
->s
: y
->s
;
bs
= (xrsame
)? r
->s
: x
->s
;
if (op
== '-') // reverse sense of difference
while (as
< topa
) *rs
++ = *as
++ & *bs
++;
while (rs
< topr
) *rs
++ = *bs
++;
while (rs
< topr
) *rs
++ = 0;
while (as
< topa
) *rs
++ = *as
++ | *bs
++;
while (rs
< topr
) *rs
++ = ONES
;
while (rs
< topr
) *rs
++ = *bs
++;
while (as
< topa
) *rs
++ = *as
++ ^ *bs
++;
while (rs
< topr
) *rs
++ = ~(*bs
++);
while (rs
< topr
) *rs
++ = *bs
++;
while (as
< topa
) *rs
++ = *as
++ & ~(*bs
++);
while (rs
< topr
) *rs
++ = ~(*bs
++);
while (rs
< topr
) *rs
++ = 0;
while (as
< topa
) *rs
++ = ~(*as
++) & (*bs
++);
while (rs
< topr
) *rs
++ = 0;
while (rs
< topr
) *rs
++ = *bs
++;
if (p
< 0) error("Illegal bit index");
int index
= BitSet_index(p
);
rep
= BitSetresize(rep
, index
+1);
rep
->s
[index
] |= (1 << pos
);
void BitSet::clear(int p
)
if (p
< 0) error("Illegal bit index");
int index
= BitSet_index(p
);
rep
= BitSetresize(rep
, index
+1);
rep
->s
[index
] &= ~(1 << BitSet_pos(p
));
void BitSet::invert(int p
)
if (p
< 0) error("Illegal bit index");
int index
= BitSet_index(p
);
if (index
>= rep
->len
) rep
= BitSetresize(rep
, index
+1);
rep
->s
[index
] ^= (1 << BitSet_pos(p
));
void BitSet::set(int from
, int to
)
if (from
< 0 || from
> to
) error("Illegal bit index");
int index1
= BitSet_index(from
);
int pos1
= BitSet_pos(from
);
if (rep
->virt
&& index1
>= rep
->len
)
int index2
= BitSet_index(to
);
int pos2
= BitSet_pos(to
);
rep
= BitSetresize(rep
, index2
+1);
unsigned short* s
= &(rep
->s
[index1
]);
unsigned short m1
= lmask(pos1
);
unsigned short m2
= rmask(pos2
);
unsigned short* top
= &(rep
->s
[index2
]);
void BitSet::clear(int from
, int to
)
if (from
< 0 || from
> to
) error("Illegal bit index");
int index1
= BitSet_index(from
);
int pos1
= BitSet_pos(from
);
if (!rep
->virt
&& index1
>= rep
->len
)
int index2
= BitSet_index(to
);
int pos2
= BitSet_pos(to
);
rep
= BitSetresize(rep
, index2
+1);
unsigned short* s
= &(rep
->s
[index1
]);
unsigned short m1
= lmask(pos1
);
unsigned short m2
= rmask(pos2
);
unsigned short* top
= &(rep
->s
[index2
]);
void BitSet::invert(int from
, int to
)
if (from
< 0 || from
> to
) error("Illegal bit index");
int index1
= BitSet_index(from
);
int pos1
= BitSet_pos(from
);
int index2
= BitSet_index(to
);
int pos2
= BitSet_pos(to
);
rep
= BitSetresize(rep
, index2
+1);
unsigned short* s
= &(rep
->s
[index1
]);
unsigned short m1
= lmask(pos1
);
unsigned short m2
= rmask(pos2
);
unsigned short* top
= &(rep
->s
[index2
]);
unsigned short cmp
= ~(*s
);
int BitSet::test(int from
, int to
) const
if (from
< 0 || from
> to
) return 0;
int index1
= BitSet_index(from
);
int pos1
= BitSet_pos(from
);
int index2
= BitSet_index(to
);
int pos2
= BitSet_pos(to
);
unsigned short* s
= &(rep
->s
[index1
]);
unsigned short m1
= lmask(pos1
);
unsigned short m2
= rmask(pos2
);
return (*s
& m1
& m2
) != 0;
unsigned short* top
= &(rep
->s
[index2
]);
int BitSet::next(int p
, int b
) const
int index
= BitSet_index(p
);
unsigned short* s
= rep
->s
;
unsigned short a
= s
[j
] >> pos
;
for (; i
< BITSETBITS
&& a
!= 0; ++i
)
return j
* BITSETBITS
+ i
;
for (i
= 0; i
< BITSETBITS
&& a
!= 0; ++i
)
return j
* BITSETBITS
+ i
;
for (; i
< BITSETBITS
; ++i
)
return j
* BITSETBITS
+ i
;
for (i
= 0; i
< BITSETBITS
; ++i
)
return j
* BITSETBITS
+ i
;
int BitSet::previous(int p
, int b
) const
int index
= BitSet_index(p
);
unsigned short* s
= rep
->s
;
unsigned short maxbit
= 1 << pos
;
for (; i
>= 0 && a
!= 0; --i
)
return j
* BITSETBITS
+ i
;
maxbit
= 1 << (BITSETBITS
- 1);
for (i
= BITSETBITS
- 1; i
>= 0 && a
!= 0; --i
)
return j
* BITSETBITS
+ i
;
return j
* BITSETBITS
+ i
;
maxbit
= 1 << (BITSETBITS
- 1);
for (i
= BITSETBITS
- 1; i
>= 0; --i
)
return j
* BITSETBITS
+ i
;
int BitSet::last(int b
) const
return previous((rep
->len
) * BITSETBITS
, b
);
extern AllocRing _libgxx_fmtq
;
const char* BitSettoa(const BitSet
& x
, char f
, char t
, char star
)
int wrksiz
= (x
.rep
->len
+ 1) * BITSETBITS
+ 2;
char* fmtbase
= (char *) _libgxx_fmtq
.alloc(wrksiz
);
const unsigned short* s
= x
.rep
->s
;
const unsigned short* top
= &(s
[x
.rep
->len
- 1]);
for (int j
= 0; j
< BITSETBITS
; ++j
)
for (int j
= 0; j
< BITSETBITS
&& a
!= 0; ++j
)
unsigned short mask
= ONES
;
unsigned short himask
= (1 << (BITSETBITS
- 1)) - 1;
for (int j
= 0; j
< BITSETBITS
&& a
!= mask
; ++j
)
mask
= (mask
>> 1) & himask
;
#if defined(__GNUG__) && !defined(NO_NRV)
BitSet
shorttoBitSet(unsigned short w
) return r
r
.rep
= BitSetalloc(0, &w
, 1, 0, 2); trim(r
.rep
);
BitSet
longtoBitSet(unsigned long w
) return r
;
u
[0] = w
& ((unsigned short)(~(0)));
r
.rep
= BitSetalloc(0, &u
[0], 2, 0, 3);
BitSet
atoBitSet(const char* s
, char f
, char t
, char star
) return r
r
.rep
= BitSetresize(r
.rep
, sl
/ BITSETBITS
+ 1);
unsigned short* rs
= r
.rep
->s
;
if (r
.rep
->virt
= lastch
== t
)
else if (i
% BITSETBITS
== 0)
BitSet
shorttoBitSet(unsigned short w
)
r
.rep
= BitSetalloc(0, &w
, 1, 0, 2); trim(r
.rep
);
BitSet
longtoBitSet(unsigned long w
)
u
[0] = w
& ((unsigned short)(~(0)));
r
.rep
= BitSetalloc(0, &u
[0], 2, 0, 3);
BitSet
atoBitSet(const char* s
, char f
, char t
, char star
)
r
.rep
= BitSetresize(r
.rep
, sl
/ BITSETBITS
+ 1);
unsigned short* rs
= r
.rep
->s
;
if (r
.rep
->virt
= lastch
== t
)
else if (i
% BITSETBITS
== 0)
ostream
& operator << (ostream
& s
, const BitSet
& x
)
return s
<< BitSettoa(x
);
int v
= rep
!= 0; // have a rep
v
&= rep
->len
<= rep
->sz
; // within bounds
v
&= rep
->virt
== 0 || rep
->virt
== 1; // valid virtual bit
if (!v
) error("invariant failure");