|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <config.h> |
|
|
|
|
|
#include "bitset/table.h" |
|
|
|
|
|
#include <stdlib.h> |
|
|
#include <string.h> |
|
|
|
|
|
#include "obstack.h" |
|
|
#include "xalloc.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define TBITSET_ELT_WORDS 2 |
|
|
|
|
|
|
|
|
#define TBITSET_ELT_BITS \ |
|
|
((unsigned) (TBITSET_ELT_WORDS * BITSET_WORD_BITS)) |
|
|
|
|
|
|
|
|
typedef struct tbitset_elt_struct |
|
|
{ |
|
|
union |
|
|
{ |
|
|
bitset_word words[TBITSET_ELT_WORDS]; |
|
|
struct tbitset_elt_struct *next; |
|
|
} |
|
|
u; |
|
|
} |
|
|
tbitset_elt; |
|
|
|
|
|
|
|
|
typedef tbitset_elt *tbitset_elts; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef TBITSET_INITIAL_SIZE |
|
|
# define TBITSET_INITIAL_SIZE 2 |
|
|
#endif |
|
|
|
|
|
|
|
|
enum tbitset_find_mode |
|
|
{ TBITSET_FIND, TBITSET_CREATE, TBITSET_SUBST }; |
|
|
|
|
|
static tbitset_elt tbitset_zero_elts[1]; |
|
|
|
|
|
|
|
|
static struct obstack tbitset_obstack; |
|
|
static bool tbitset_obstack_init = false; |
|
|
static tbitset_elt *tbitset_free_list; |
|
|
|
|
|
#define TBITSET_N_ELTS(N) (((N) + TBITSET_ELT_BITS - 1) / TBITSET_ELT_BITS) |
|
|
#define TBITSET_ELTS(BSET) ((BSET)->e.elts) |
|
|
#define TBITSET_SIZE(BSET) TBITSET_N_ELTS (BITSET_NBITS_ (BSET)) |
|
|
#define TBITSET_ASIZE(BSET) ((BSET)->e.size) |
|
|
|
|
|
#define TBITSET_NEXT(ELT) ((ELT)->u.next) |
|
|
#define TBITSET_WORDS(ELT) ((ELT)->u.words) |
|
|
|
|
|
|
|
|
#define TBITSET_ZERO_SET(BSET) ((BSET)->b.cindex = BITSET_WINDEX_MAX, \ |
|
|
(BSET)->b.cdata = NULL) |
|
|
|
|
|
#define TBITSET_CACHE_DISABLE(BSET) ((BSET)->b.cindex = BITSET_WINDEX_MAX) |
|
|
|
|
|
|
|
|
#define TBITSET_NONZERO_SET(BSET) \ |
|
|
(TBITSET_CACHE_DISABLE (BSET), (BSET)->b.cdata = (bitset_word *)~0) |
|
|
|
|
|
|
|
|
|
|
|
#define TBITSET_ZERO_P(BSET) ((BSET)->b.cdata == NULL) |
|
|
|
|
|
|
|
|
|
|
|
#define TBITSET_CACHE_SET(BSET, EINDEX) \ |
|
|
((BSET)->b.cindex = (EINDEX) * TBITSET_ELT_WORDS, \ |
|
|
(BSET)->b.cdata = TBITSET_WORDS (TBITSET_ELTS (BSET) [EINDEX])) |
|
|
|
|
|
#undef min |
|
|
#undef max |
|
|
#define min(a, b) ((a) > (b) ? (b) : (a)) |
|
|
#define max(a, b) ((a) > (b) ? (a) : (b)) |
|
|
|
|
|
static bitset_bindex |
|
|
tbitset_resize (bitset src, bitset_bindex n_bits) |
|
|
{ |
|
|
if (n_bits == BITSET_NBITS_ (src)) |
|
|
return n_bits; |
|
|
|
|
|
bitset_windex oldsize = TBITSET_SIZE (src); |
|
|
bitset_windex newsize = TBITSET_N_ELTS (n_bits); |
|
|
|
|
|
if (oldsize < newsize) |
|
|
{ |
|
|
|
|
|
|
|
|
if (newsize > TBITSET_ASIZE (src)) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bitset_windex size = oldsize == 0 ? newsize : newsize + newsize / 4; |
|
|
TBITSET_ELTS (src) |
|
|
= xrealloc (TBITSET_ELTS (src), size * sizeof (tbitset_elt *)); |
|
|
TBITSET_ASIZE (src) = size; |
|
|
} |
|
|
|
|
|
memset (TBITSET_ELTS (src) + oldsize, 0, |
|
|
(newsize - oldsize) * sizeof (tbitset_elt *)); |
|
|
} |
|
|
else |
|
|
{ |
|
|
|
|
|
|
|
|
if ((oldsize - newsize) >= oldsize / 2) |
|
|
{ |
|
|
void *p |
|
|
= realloc (TBITSET_ELTS (src), newsize * sizeof (tbitset_elt *)); |
|
|
if (p) |
|
|
{ |
|
|
TBITSET_ELTS (src) = p; |
|
|
TBITSET_ASIZE (src) = newsize; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
BITSET_NBITS_ (src) = n_bits; |
|
|
return n_bits; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static inline tbitset_elt * |
|
|
tbitset_elt_alloc (void) |
|
|
{ |
|
|
tbitset_elt *elt; |
|
|
|
|
|
if (tbitset_free_list != NULL) |
|
|
{ |
|
|
elt = tbitset_free_list; |
|
|
tbitset_free_list = TBITSET_NEXT (elt); |
|
|
} |
|
|
else |
|
|
{ |
|
|
if (!tbitset_obstack_init) |
|
|
{ |
|
|
tbitset_obstack_init = true; |
|
|
|
|
|
|
|
|
|
|
|
#ifndef OBSTACK_CHUNK_SIZE |
|
|
# define OBSTACK_CHUNK_SIZE 0 |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#ifndef OBSTACK_CHUNK_ALLOC |
|
|
# define OBSTACK_CHUNK_ALLOC xmalloc |
|
|
#endif |
|
|
|
|
|
#ifndef OBSTACK_CHUNK_FREE |
|
|
# define OBSTACK_CHUNK_FREE free |
|
|
#endif |
|
|
|
|
|
#if !(defined __GNUC__ || defined __clang__) |
|
|
# define __alignof__(type) 0 |
|
|
#endif |
|
|
|
|
|
obstack_specify_allocation (&tbitset_obstack, OBSTACK_CHUNK_SIZE, |
|
|
__alignof__ (tbitset_elt), |
|
|
OBSTACK_CHUNK_ALLOC, |
|
|
OBSTACK_CHUNK_FREE); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
elt = (tbitset_elt *) obstack_alloc (&tbitset_obstack, |
|
|
sizeof (tbitset_elt)); |
|
|
} |
|
|
|
|
|
return elt; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static inline tbitset_elt * |
|
|
tbitset_elt_calloc (void) |
|
|
{ |
|
|
tbitset_elt *elt = tbitset_elt_alloc (); |
|
|
memset (TBITSET_WORDS (elt), 0, sizeof (TBITSET_WORDS (elt))); |
|
|
return elt; |
|
|
} |
|
|
|
|
|
|
|
|
static inline void |
|
|
tbitset_elt_free (tbitset_elt *elt) |
|
|
{ |
|
|
TBITSET_NEXT (elt) = tbitset_free_list; |
|
|
tbitset_free_list = elt; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static inline void |
|
|
tbitset_elt_remove (bitset bset, bitset_windex eindex) |
|
|
{ |
|
|
tbitset_elts *elts = TBITSET_ELTS (bset); |
|
|
tbitset_elt *elt = elts[eindex]; |
|
|
|
|
|
elts[eindex] = NULL; |
|
|
tbitset_elt_free (elt); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static inline void |
|
|
tbitset_elt_add (bitset bset, tbitset_elt *elt, bitset_windex eindex) |
|
|
{ |
|
|
tbitset_elts *elts = TBITSET_ELTS (bset); |
|
|
|
|
|
elts[eindex] = elt; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static inline bool |
|
|
tbitset_elt_zero_p (tbitset_elt *elt) |
|
|
{ |
|
|
for (int i = 0; i < TBITSET_ELT_WORDS; i++) |
|
|
if (TBITSET_WORDS (elt)[i]) |
|
|
return false; |
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
static tbitset_elt * |
|
|
tbitset_elt_find (bitset bset, bitset_bindex bindex, |
|
|
enum tbitset_find_mode mode) |
|
|
{ |
|
|
bitset_windex eindex = bindex / TBITSET_ELT_BITS; |
|
|
|
|
|
tbitset_elts *elts = TBITSET_ELTS (bset); |
|
|
bitset_windex size = TBITSET_SIZE (bset); |
|
|
|
|
|
if (eindex < size) |
|
|
{ |
|
|
tbitset_elt *elt = elts[eindex]; |
|
|
if (elt) |
|
|
{ |
|
|
if (TBITSET_WORDS (elt) != bset->b.cdata) |
|
|
TBITSET_CACHE_SET (bset, eindex); |
|
|
return elt; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
switch (mode) |
|
|
{ |
|
|
default: |
|
|
abort (); |
|
|
|
|
|
case TBITSET_FIND: |
|
|
return NULL; |
|
|
|
|
|
case TBITSET_CREATE: |
|
|
if (eindex >= size) |
|
|
tbitset_resize (bset, bindex); |
|
|
|
|
|
|
|
|
{ |
|
|
tbitset_elt *elt = tbitset_elt_calloc (); |
|
|
tbitset_elt_add (bset, elt, eindex); |
|
|
TBITSET_CACHE_SET (bset, eindex); |
|
|
return elt; |
|
|
} |
|
|
|
|
|
case TBITSET_SUBST: |
|
|
return &tbitset_zero_elts[0]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static inline bitset_windex |
|
|
tbitset_weed (bitset bset) |
|
|
{ |
|
|
if (TBITSET_ZERO_P (bset)) |
|
|
return 0; |
|
|
|
|
|
tbitset_elts *elts = TBITSET_ELTS (bset); |
|
|
bitset_windex count = 0; |
|
|
bitset_windex j; |
|
|
for (j = 0; j < TBITSET_SIZE (bset); j++) |
|
|
{ |
|
|
tbitset_elt *elt = elts[j]; |
|
|
|
|
|
if (elt) |
|
|
{ |
|
|
if (tbitset_elt_zero_p (elt)) |
|
|
{ |
|
|
tbitset_elt_remove (bset, j); |
|
|
count++; |
|
|
} |
|
|
} |
|
|
else |
|
|
count++; |
|
|
} |
|
|
|
|
|
count = j - count; |
|
|
if (!count) |
|
|
{ |
|
|
|
|
|
|
|
|
TBITSET_ZERO_SET (bset); |
|
|
} |
|
|
else |
|
|
TBITSET_NONZERO_SET (bset); |
|
|
|
|
|
return count; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static inline void |
|
|
tbitset_zero (bitset bset) |
|
|
{ |
|
|
if (TBITSET_ZERO_P (bset)) |
|
|
return; |
|
|
|
|
|
tbitset_elts *elts = TBITSET_ELTS (bset); |
|
|
for (bitset_windex j = 0; j < TBITSET_SIZE (bset); j++) |
|
|
{ |
|
|
tbitset_elt *elt = elts[j]; |
|
|
if (elt) |
|
|
tbitset_elt_remove (bset, j); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TBITSET_ZERO_SET (bset); |
|
|
} |
|
|
|
|
|
|
|
|
static inline bool |
|
|
tbitset_equal_p (bitset dst, bitset src) |
|
|
{ |
|
|
if (src == dst) |
|
|
return true; |
|
|
|
|
|
tbitset_weed (dst); |
|
|
tbitset_weed (src); |
|
|
|
|
|
if (TBITSET_SIZE (src) != TBITSET_SIZE (dst)) |
|
|
return false; |
|
|
|
|
|
tbitset_elts *selts = TBITSET_ELTS (src); |
|
|
tbitset_elts *delts = TBITSET_ELTS (dst); |
|
|
|
|
|
for (bitset_windex j = 0; j < TBITSET_SIZE (src); j++) |
|
|
{ |
|
|
tbitset_elt *selt = selts[j]; |
|
|
tbitset_elt *delt = delts[j]; |
|
|
|
|
|
if (!selt && !delt) |
|
|
continue; |
|
|
if ((selt && !delt) || (!selt && delt)) |
|
|
return false; |
|
|
|
|
|
for (unsigned i = 0; i < TBITSET_ELT_WORDS; i++) |
|
|
if (TBITSET_WORDS (selt)[i] != TBITSET_WORDS (delt)[i]) |
|
|
return false; |
|
|
} |
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static inline void |
|
|
tbitset_copy_ (bitset dst, bitset src) |
|
|
{ |
|
|
if (src == dst) |
|
|
return; |
|
|
|
|
|
tbitset_zero (dst); |
|
|
|
|
|
if (BITSET_NBITS_ (dst) != BITSET_NBITS_ (src)) |
|
|
tbitset_resize (dst, BITSET_NBITS_ (src)); |
|
|
|
|
|
tbitset_elts *selts = TBITSET_ELTS (src); |
|
|
tbitset_elts *delts = TBITSET_ELTS (dst); |
|
|
for (bitset_windex j = 0; j < TBITSET_SIZE (src); j++) |
|
|
{ |
|
|
tbitset_elt *selt = selts[j]; |
|
|
if (selt) |
|
|
{ |
|
|
tbitset_elt *tmp = tbitset_elt_alloc (); |
|
|
delts[j] = tmp; |
|
|
memcpy (TBITSET_WORDS (tmp), TBITSET_WORDS (selt), |
|
|
sizeof (TBITSET_WORDS (selt))); |
|
|
} |
|
|
} |
|
|
TBITSET_NONZERO_SET (dst); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static inline bool |
|
|
tbitset_copy_cmp (bitset dst, bitset src) |
|
|
{ |
|
|
if (src == dst) |
|
|
return false; |
|
|
|
|
|
if (TBITSET_ZERO_P (dst)) |
|
|
{ |
|
|
tbitset_copy_ (dst, src); |
|
|
return !TBITSET_ZERO_P (src); |
|
|
} |
|
|
|
|
|
if (tbitset_equal_p (dst, src)) |
|
|
return false; |
|
|
|
|
|
tbitset_copy_ (dst, src); |
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void |
|
|
tbitset_set (bitset dst, bitset_bindex bitno) |
|
|
{ |
|
|
bitset_windex windex = bitno / BITSET_WORD_BITS; |
|
|
|
|
|
tbitset_elt_find (dst, bitno, TBITSET_CREATE); |
|
|
|
|
|
dst->b.cdata[windex - dst->b.cindex] |= |
|
|
(bitset_word) 1 << (bitno % BITSET_WORD_BITS); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void |
|
|
tbitset_reset (bitset dst, bitset_bindex bitno) |
|
|
{ |
|
|
bitset_windex windex = bitno / BITSET_WORD_BITS; |
|
|
|
|
|
if (!tbitset_elt_find (dst, bitno, TBITSET_FIND)) |
|
|
return; |
|
|
|
|
|
dst->b.cdata[windex - dst->b.cindex] &= |
|
|
~((bitset_word) 1 << (bitno % BITSET_WORD_BITS)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static bool |
|
|
tbitset_test (bitset src, bitset_bindex bitno) |
|
|
{ |
|
|
bitset_windex windex = bitno / BITSET_WORD_BITS; |
|
|
|
|
|
return (tbitset_elt_find (src, bitno, TBITSET_FIND) |
|
|
&& ((src->b.cdata[windex - src->b.cindex] |
|
|
>> (bitno % BITSET_WORD_BITS)) |
|
|
& 1)); |
|
|
} |
|
|
|
|
|
|
|
|
static void |
|
|
tbitset_free (bitset bset) |
|
|
{ |
|
|
tbitset_zero (bset); |
|
|
free (TBITSET_ELTS (bset)); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bitset_bindex |
|
|
tbitset_list_reverse (bitset bset, bitset_bindex *list, |
|
|
bitset_bindex num, bitset_bindex *next) |
|
|
{ |
|
|
if (TBITSET_ZERO_P (bset)) |
|
|
return 0; |
|
|
|
|
|
bitset_windex size = TBITSET_SIZE (bset); |
|
|
bitset_bindex n_bits = size * TBITSET_ELT_BITS; |
|
|
bitset_bindex rbitno = *next; |
|
|
|
|
|
if (rbitno >= n_bits) |
|
|
return 0; |
|
|
|
|
|
tbitset_elts *elts = TBITSET_ELTS (bset); |
|
|
|
|
|
bitset_bindex bitno = n_bits - (rbitno + 1); |
|
|
|
|
|
bitset_windex windex = bitno / BITSET_WORD_BITS; |
|
|
bitset_windex eindex = bitno / TBITSET_ELT_BITS; |
|
|
bitset_windex woffset = windex - eindex * TBITSET_ELT_WORDS; |
|
|
|
|
|
|
|
|
|
|
|
bitset_bindex count = 0; |
|
|
unsigned bitcnt = bitno % BITSET_WORD_BITS; |
|
|
bitset_bindex bitoff = windex * BITSET_WORD_BITS; |
|
|
|
|
|
do |
|
|
{ |
|
|
tbitset_elt *elt = elts[eindex]; |
|
|
if (elt) |
|
|
{ |
|
|
bitset_word *srcp = TBITSET_WORDS (elt); |
|
|
|
|
|
do |
|
|
{ |
|
|
bitset_word word = srcp[woffset]; |
|
|
if (bitcnt + 1 < BITSET_WORD_BITS) |
|
|
|
|
|
word &= ((bitset_word) 1 << (bitcnt + 1)) - 1; |
|
|
BITSET_FOR_EACH_BIT_REVERSE(pos, word) |
|
|
{ |
|
|
list[count++] = bitoff + pos; |
|
|
if (count >= num) |
|
|
{ |
|
|
*next = n_bits - (bitoff + pos); |
|
|
return count; |
|
|
} |
|
|
} |
|
|
bitoff -= BITSET_WORD_BITS; |
|
|
bitcnt = BITSET_WORD_BITS - 1; |
|
|
} |
|
|
while (woffset--); |
|
|
} |
|
|
|
|
|
woffset = TBITSET_ELT_WORDS - 1; |
|
|
bitoff = eindex * TBITSET_ELT_BITS - BITSET_WORD_BITS; |
|
|
} |
|
|
while (eindex--); |
|
|
|
|
|
*next = n_bits - (bitoff + 1); |
|
|
return count; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bitset_bindex |
|
|
tbitset_list (bitset bset, bitset_bindex *list, |
|
|
bitset_bindex num, bitset_bindex *next) |
|
|
{ |
|
|
if (TBITSET_ZERO_P (bset)) |
|
|
return 0; |
|
|
|
|
|
bitset_bindex bitno = *next; |
|
|
bitset_bindex count = 0; |
|
|
|
|
|
tbitset_elts *elts = TBITSET_ELTS (bset); |
|
|
bitset_windex size = TBITSET_SIZE (bset); |
|
|
bitset_windex eindex = bitno / TBITSET_ELT_BITS; |
|
|
|
|
|
if (bitno % TBITSET_ELT_BITS) |
|
|
{ |
|
|
|
|
|
tbitset_elt *elt = elts[eindex]; |
|
|
if (elt) |
|
|
{ |
|
|
bitset_word *srcp = TBITSET_WORDS (elt); |
|
|
bitset_windex woffset = eindex * TBITSET_ELT_WORDS; |
|
|
|
|
|
for (bitset_windex windex = bitno / BITSET_WORD_BITS; |
|
|
(windex - woffset) < TBITSET_ELT_WORDS; windex++) |
|
|
{ |
|
|
bitset_word word = srcp[windex - woffset] >> (bitno % BITSET_WORD_BITS); |
|
|
|
|
|
BITSET_FOR_EACH_BIT (pos, word) |
|
|
{ |
|
|
list[count++] = bitno + pos; |
|
|
if (count >= num) |
|
|
{ |
|
|
*next = bitno + pos + 1; |
|
|
return count; |
|
|
} |
|
|
} |
|
|
bitno = (windex + 1) * BITSET_WORD_BITS; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
eindex++; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (; eindex < size; eindex++) |
|
|
{ |
|
|
tbitset_elt *elt = elts[eindex]; |
|
|
if (!elt) |
|
|
continue; |
|
|
|
|
|
bitset_word *srcp = TBITSET_WORDS (elt); |
|
|
bitset_windex windex = eindex * TBITSET_ELT_WORDS; |
|
|
bitno = windex * BITSET_WORD_BITS; |
|
|
|
|
|
|
|
|
if ((count + TBITSET_ELT_BITS) < num) |
|
|
{ |
|
|
|
|
|
|
|
|
#if TBITSET_ELT_WORDS == 2 |
|
|
bitset_word word = srcp[0]; |
|
|
if (word) |
|
|
BITSET_FOR_EACH_BIT (pos, word) |
|
|
list[count++] = bitno + pos; |
|
|
windex++; |
|
|
bitno = windex * BITSET_WORD_BITS; |
|
|
|
|
|
word = srcp[1]; |
|
|
if (word) |
|
|
BITSET_FOR_EACH_BIT (pos, word) |
|
|
list[count++] = bitno + pos; |
|
|
windex++; |
|
|
bitno = windex * BITSET_WORD_BITS; |
|
|
#else |
|
|
for (int i = 0; i < TBITSET_ELT_WORDS; i++, windex++) |
|
|
{ |
|
|
bitset_word word = srcp[i]; |
|
|
if (word) |
|
|
BITSET_FOR_EACH_BIT (pos, word) |
|
|
list[count++] = bitno + pos; |
|
|
bitno = windex * BITSET_WORD_BITS; |
|
|
} |
|
|
#endif |
|
|
} |
|
|
else |
|
|
{ |
|
|
|
|
|
|
|
|
for (int i = 0; i < TBITSET_ELT_WORDS; i++) |
|
|
{ |
|
|
bitset_word word = srcp[i]; |
|
|
if (word) |
|
|
BITSET_FOR_EACH_BIT (pos, word) |
|
|
{ |
|
|
list[count++] = bitno + pos; |
|
|
if (count >= num) |
|
|
{ |
|
|
*next = bitno + pos + 1; |
|
|
return count; |
|
|
} |
|
|
} |
|
|
windex++; |
|
|
bitno = windex * BITSET_WORD_BITS; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
*next = bitno; |
|
|
return count; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static inline void |
|
|
tbitset_unused_clear (bitset dst) |
|
|
{ |
|
|
bitset_bindex n_bits = BITSET_NBITS_ (dst); |
|
|
unsigned last_bit = n_bits % TBITSET_ELT_BITS; |
|
|
|
|
|
if (last_bit) |
|
|
{ |
|
|
tbitset_elts *elts = TBITSET_ELTS (dst); |
|
|
|
|
|
bitset_windex eindex = n_bits / TBITSET_ELT_BITS; |
|
|
|
|
|
tbitset_elt *elt = elts[eindex]; |
|
|
if (elt) |
|
|
{ |
|
|
bitset_word *srcp = TBITSET_WORDS (elt); |
|
|
|
|
|
bitset_windex windex = n_bits / BITSET_WORD_BITS; |
|
|
bitset_windex woffset = eindex * TBITSET_ELT_WORDS; |
|
|
|
|
|
srcp[windex - woffset] |
|
|
&= ((bitset_word) 1 << (last_bit % BITSET_WORD_BITS)) - 1; |
|
|
windex++; |
|
|
for (; (windex - woffset) < TBITSET_ELT_WORDS; windex++) |
|
|
srcp[windex - woffset] = 0; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static void |
|
|
tbitset_ones (bitset dst) |
|
|
{ |
|
|
for (bitset_windex j = 0; j < TBITSET_SIZE (dst); j++) |
|
|
{ |
|
|
|
|
|
|
|
|
tbitset_elt *elt = |
|
|
tbitset_elt_find (dst, j * TBITSET_ELT_BITS, TBITSET_CREATE); |
|
|
memset (TBITSET_WORDS (elt), -1, sizeof (TBITSET_WORDS (elt))); |
|
|
} |
|
|
TBITSET_NONZERO_SET (dst); |
|
|
tbitset_unused_clear (dst); |
|
|
} |
|
|
|
|
|
|
|
|
static bool |
|
|
tbitset_empty_p (bitset dst) |
|
|
{ |
|
|
if (TBITSET_ZERO_P (dst)) |
|
|
return true; |
|
|
|
|
|
tbitset_elts *elts = TBITSET_ELTS (dst); |
|
|
for (bitset_windex j = 0; j < TBITSET_SIZE (dst); j++) |
|
|
{ |
|
|
tbitset_elt *elt = elts[j]; |
|
|
|
|
|
if (elt) |
|
|
{ |
|
|
if (!tbitset_elt_zero_p (elt)) |
|
|
return false; |
|
|
|
|
|
tbitset_elt_remove (dst, j); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TBITSET_ZERO_SET (dst); |
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
static void |
|
|
tbitset_not (bitset dst, bitset src) |
|
|
{ |
|
|
tbitset_resize (dst, BITSET_NBITS_ (src)); |
|
|
|
|
|
for (bitset_windex j = 0; j < TBITSET_SIZE (src); j++) |
|
|
{ |
|
|
|
|
|
|
|
|
tbitset_elt *selt = |
|
|
tbitset_elt_find (src, j * TBITSET_ELT_BITS, TBITSET_SUBST); |
|
|
tbitset_elt *delt = |
|
|
tbitset_elt_find (dst, j * TBITSET_ELT_BITS, TBITSET_CREATE); |
|
|
|
|
|
for (unsigned i = 0; i < TBITSET_ELT_WORDS; i++) |
|
|
TBITSET_WORDS (delt)[i] = ~TBITSET_WORDS (selt)[i]; |
|
|
} |
|
|
TBITSET_NONZERO_SET (dst); |
|
|
tbitset_unused_clear (dst); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static bool |
|
|
tbitset_subset_p (bitset dst, bitset src) |
|
|
{ |
|
|
tbitset_elts *selts = TBITSET_ELTS (src); |
|
|
tbitset_elts *delts = TBITSET_ELTS (dst); |
|
|
|
|
|
bitset_windex ssize = TBITSET_SIZE (src); |
|
|
bitset_windex dsize = TBITSET_SIZE (dst); |
|
|
|
|
|
for (bitset_windex j = 0; j < ssize; j++) |
|
|
{ |
|
|
tbitset_elt *selt = j < ssize ? selts[j] : NULL; |
|
|
tbitset_elt *delt = j < dsize ? delts[j] : NULL; |
|
|
|
|
|
if (!selt && !delt) |
|
|
continue; |
|
|
|
|
|
if (!selt) |
|
|
selt = &tbitset_zero_elts[0]; |
|
|
if (!delt) |
|
|
delt = &tbitset_zero_elts[0]; |
|
|
|
|
|
for (unsigned i = 0; i < TBITSET_ELT_WORDS; i++) |
|
|
if (TBITSET_WORDS (delt)[i] |
|
|
!= (TBITSET_WORDS (selt)[i] | TBITSET_WORDS (delt)[i])) |
|
|
return false; |
|
|
} |
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static bool |
|
|
tbitset_disjoint_p (bitset dst, bitset src) |
|
|
{ |
|
|
tbitset_elts *selts = TBITSET_ELTS (src); |
|
|
tbitset_elts *delts = TBITSET_ELTS (dst); |
|
|
|
|
|
bitset_windex ssize = TBITSET_SIZE (src); |
|
|
bitset_windex dsize = TBITSET_SIZE (dst); |
|
|
|
|
|
for (bitset_windex j = 0; j < ssize; j++) |
|
|
{ |
|
|
tbitset_elt *selt = j < ssize ? selts[j] : NULL; |
|
|
tbitset_elt *delt = j < dsize ? delts[j] : NULL; |
|
|
|
|
|
if (!selt || !delt) |
|
|
continue; |
|
|
|
|
|
for (unsigned i = 0; i < TBITSET_ELT_WORDS; i++) |
|
|
if ((TBITSET_WORDS (selt)[i] & TBITSET_WORDS (delt)[i])) |
|
|
return false; |
|
|
} |
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static bool |
|
|
tbitset_op3_cmp (bitset dst, bitset src1, bitset src2, enum bitset_ops op) |
|
|
{ |
|
|
bool changed = false; |
|
|
|
|
|
tbitset_resize (dst, max (BITSET_NBITS_ (src1), BITSET_NBITS_ (src2))); |
|
|
|
|
|
bitset_windex ssize1 = TBITSET_SIZE (src1); |
|
|
bitset_windex ssize2 = TBITSET_SIZE (src2); |
|
|
bitset_windex dsize = TBITSET_SIZE (dst); |
|
|
bitset_windex size = ssize1; |
|
|
if (size < ssize2) |
|
|
size = ssize2; |
|
|
|
|
|
tbitset_elts *selts1 = TBITSET_ELTS (src1); |
|
|
tbitset_elts *selts2 = TBITSET_ELTS (src2); |
|
|
tbitset_elts *delts = TBITSET_ELTS (dst); |
|
|
|
|
|
bitset_windex j = 0; |
|
|
for (j = 0; j < size; j++) |
|
|
{ |
|
|
tbitset_elt *selt1 = j < ssize1 ? selts1[j] : NULL; |
|
|
tbitset_elt *selt2 = j < ssize2 ? selts2[j] : NULL; |
|
|
tbitset_elt *delt = j < dsize ? delts[j] : NULL; |
|
|
|
|
|
if (!selt1 && !selt2) |
|
|
{ |
|
|
if (delt) |
|
|
{ |
|
|
changed = true; |
|
|
tbitset_elt_remove (dst, j); |
|
|
} |
|
|
continue; |
|
|
} |
|
|
|
|
|
if (!selt1) |
|
|
selt1 = &tbitset_zero_elts[0]; |
|
|
if (!selt2) |
|
|
selt2 = &tbitset_zero_elts[0]; |
|
|
if (!delt) |
|
|
delt = tbitset_elt_calloc (); |
|
|
else |
|
|
delts[j] = NULL; |
|
|
|
|
|
bitset_word *srcp1 = TBITSET_WORDS (selt1); |
|
|
bitset_word *srcp2 = TBITSET_WORDS (selt2); |
|
|
bitset_word *dstp = TBITSET_WORDS (delt); |
|
|
switch (op) |
|
|
{ |
|
|
default: |
|
|
abort (); |
|
|
|
|
|
case BITSET_OP_OR: |
|
|
for (unsigned i = 0; i < TBITSET_ELT_WORDS; i++, dstp++) |
|
|
{ |
|
|
bitset_word tmp = *srcp1++ | *srcp2++; |
|
|
|
|
|
if (*dstp != tmp) |
|
|
{ |
|
|
changed = true; |
|
|
*dstp = tmp; |
|
|
} |
|
|
} |
|
|
break; |
|
|
|
|
|
case BITSET_OP_AND: |
|
|
for (unsigned i = 0; i < TBITSET_ELT_WORDS; i++, dstp++) |
|
|
{ |
|
|
bitset_word tmp = *srcp1++ & *srcp2++; |
|
|
|
|
|
if (*dstp != tmp) |
|
|
{ |
|
|
changed = true; |
|
|
*dstp = tmp; |
|
|
} |
|
|
} |
|
|
break; |
|
|
|
|
|
case BITSET_OP_XOR: |
|
|
for (unsigned i = 0; i < TBITSET_ELT_WORDS; i++, dstp++) |
|
|
{ |
|
|
bitset_word tmp = *srcp1++ ^ *srcp2++; |
|
|
|
|
|
if (*dstp != tmp) |
|
|
{ |
|
|
changed = true; |
|
|
*dstp = tmp; |
|
|
} |
|
|
} |
|
|
break; |
|
|
|
|
|
case BITSET_OP_ANDN: |
|
|
for (unsigned i = 0; i < TBITSET_ELT_WORDS; i++, dstp++) |
|
|
{ |
|
|
bitset_word tmp = *srcp1++ & ~(*srcp2++); |
|
|
|
|
|
if (*dstp != tmp) |
|
|
{ |
|
|
changed = true; |
|
|
*dstp = tmp; |
|
|
} |
|
|
} |
|
|
break; |
|
|
} |
|
|
|
|
|
if (!tbitset_elt_zero_p (delt)) |
|
|
tbitset_elt_add (dst, delt, j); |
|
|
else |
|
|
tbitset_elt_free (delt); |
|
|
} |
|
|
|
|
|
|
|
|
for (; j < dsize; j++) |
|
|
{ |
|
|
changed = true; |
|
|
|
|
|
tbitset_elt *delt = delts[j]; |
|
|
if (delt) |
|
|
tbitset_elt_remove (dst, j); |
|
|
} |
|
|
|
|
|
TBITSET_NONZERO_SET (dst); |
|
|
return changed; |
|
|
} |
|
|
|
|
|
|
|
|
static bool |
|
|
tbitset_and_cmp (bitset dst, bitset src1, bitset src2) |
|
|
{ |
|
|
if (TBITSET_ZERO_P (src2)) |
|
|
{ |
|
|
tbitset_weed (dst); |
|
|
bool changed = TBITSET_ZERO_P (dst); |
|
|
tbitset_zero (dst); |
|
|
return changed; |
|
|
} |
|
|
else if (TBITSET_ZERO_P (src1)) |
|
|
{ |
|
|
tbitset_weed (dst); |
|
|
bool changed = TBITSET_ZERO_P (dst); |
|
|
tbitset_zero (dst); |
|
|
return changed; |
|
|
} |
|
|
else |
|
|
return tbitset_op3_cmp (dst, src1, src2, BITSET_OP_AND); |
|
|
} |
|
|
|
|
|
|
|
|
static void |
|
|
tbitset_and (bitset dst, bitset src1, bitset src2) |
|
|
{ |
|
|
tbitset_and_cmp (dst, src1, src2); |
|
|
} |
|
|
|
|
|
|
|
|
static bool |
|
|
tbitset_andn_cmp (bitset dst, bitset src1, bitset src2) |
|
|
{ |
|
|
if (TBITSET_ZERO_P (src2)) |
|
|
return tbitset_copy_cmp (dst, src1); |
|
|
else if (TBITSET_ZERO_P (src1)) |
|
|
{ |
|
|
tbitset_weed (dst); |
|
|
bool changed = TBITSET_ZERO_P (dst); |
|
|
tbitset_zero (dst); |
|
|
return changed; |
|
|
} |
|
|
else |
|
|
return tbitset_op3_cmp (dst, src1, src2, BITSET_OP_ANDN); |
|
|
} |
|
|
|
|
|
|
|
|
static void |
|
|
tbitset_andn (bitset dst, bitset src1, bitset src2) |
|
|
{ |
|
|
tbitset_andn_cmp (dst, src1, src2); |
|
|
} |
|
|
|
|
|
|
|
|
static bool |
|
|
tbitset_or_cmp (bitset dst, bitset src1, bitset src2) |
|
|
{ |
|
|
if (TBITSET_ZERO_P (src2)) |
|
|
return tbitset_copy_cmp (dst, src1); |
|
|
else if (TBITSET_ZERO_P (src1)) |
|
|
return tbitset_copy_cmp (dst, src2); |
|
|
else |
|
|
return tbitset_op3_cmp (dst, src1, src2, BITSET_OP_OR); |
|
|
} |
|
|
|
|
|
|
|
|
static void |
|
|
tbitset_or (bitset dst, bitset src1, bitset src2) |
|
|
{ |
|
|
tbitset_or_cmp (dst, src1, src2); |
|
|
} |
|
|
|
|
|
|
|
|
static bool |
|
|
tbitset_xor_cmp (bitset dst, bitset src1, bitset src2) |
|
|
{ |
|
|
if (TBITSET_ZERO_P (src2)) |
|
|
return tbitset_copy_cmp (dst, src1); |
|
|
else if (TBITSET_ZERO_P (src1)) |
|
|
return tbitset_copy_cmp (dst, src2); |
|
|
else |
|
|
return tbitset_op3_cmp (dst, src1, src2, BITSET_OP_XOR); |
|
|
} |
|
|
|
|
|
|
|
|
static void |
|
|
tbitset_xor (bitset dst, bitset src1, bitset src2) |
|
|
{ |
|
|
tbitset_xor_cmp (dst, src1, src2); |
|
|
} |
|
|
|
|
|
|
|
|
static void |
|
|
tbitset_copy (bitset dst, bitset src) |
|
|
{ |
|
|
if (BITSET_COMPATIBLE_ (dst, src)) |
|
|
tbitset_copy_ (dst, src); |
|
|
else |
|
|
bitset_copy_ (dst, src); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static struct bitset_vtable tbitset_vtable = { |
|
|
tbitset_set, |
|
|
tbitset_reset, |
|
|
bitset_toggle_, |
|
|
tbitset_test, |
|
|
tbitset_resize, |
|
|
bitset_size_, |
|
|
bitset_count_, |
|
|
tbitset_empty_p, |
|
|
tbitset_ones, |
|
|
tbitset_zero, |
|
|
tbitset_copy, |
|
|
tbitset_disjoint_p, |
|
|
tbitset_equal_p, |
|
|
tbitset_not, |
|
|
tbitset_subset_p, |
|
|
tbitset_and, |
|
|
tbitset_and_cmp, |
|
|
tbitset_andn, |
|
|
tbitset_andn_cmp, |
|
|
tbitset_or, |
|
|
tbitset_or_cmp, |
|
|
tbitset_xor, |
|
|
tbitset_xor_cmp, |
|
|
bitset_and_or_, |
|
|
bitset_and_or_cmp_, |
|
|
bitset_andn_or_, |
|
|
bitset_andn_or_cmp_, |
|
|
bitset_or_and_, |
|
|
bitset_or_and_cmp_, |
|
|
tbitset_list, |
|
|
tbitset_list_reverse, |
|
|
tbitset_free, |
|
|
BITSET_TABLE |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
size_t |
|
|
tbitset_bytes (MAYBE_UNUSED bitset_bindex n_bits) |
|
|
{ |
|
|
return sizeof (struct tbitset_struct); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bitset |
|
|
tbitset_init (bitset bset, bitset_bindex n_bits) |
|
|
{ |
|
|
bset->b.vtable = &tbitset_vtable; |
|
|
|
|
|
bset->b.csize = TBITSET_ELT_WORDS; |
|
|
|
|
|
TBITSET_ZERO_SET (bset); |
|
|
|
|
|
TBITSET_ASIZE (bset) = 0; |
|
|
TBITSET_ELTS (bset) = NULL; |
|
|
tbitset_resize (bset, n_bits); |
|
|
|
|
|
return bset; |
|
|
} |
|
|
|
|
|
|
|
|
void |
|
|
tbitset_release_memory (void) |
|
|
{ |
|
|
tbitset_free_list = NULL; |
|
|
if (tbitset_obstack_init) |
|
|
{ |
|
|
tbitset_obstack_init = false; |
|
|
obstack_free (&tbitset_obstack, NULL); |
|
|
} |
|
|
} |
|
|
|