diff options
author | Akim Demaille <akim@epita.fr> | 2002-09-30 12:50:49 +0000 |
---|---|---|
committer | Akim Demaille <akim@epita.fr> | 2002-09-30 12:50:49 +0000 |
commit | 6aa452a643c65a8df244013e05d41854531ec762 (patch) | |
tree | f4820d42921caf6f8488fa37f667f59a68e0bac6 /lib/bbitset.h | |
parent | 719010ad2b7323d7840d7c074c02eb8d0bcd5885 (diff) | |
download | bison-6aa452a643c65a8df244013e05d41854531ec762.tar.gz |
* lib/abitset.c, lib/bbitset.h, lib/bitset.c, lib/bitset.h,
* lib/bitset_stats.c, lib/bitsetv.c, lib/ebitset.c, lib/lbitset.c:
Updates from Michael Hayes.
Diffstat (limited to 'lib/bbitset.h')
-rw-r--r-- | lib/bbitset.h | 264 |
1 files changed, 158 insertions, 106 deletions
diff --git a/lib/bbitset.h b/lib/bbitset.h index 1e5710d3..059c3e44 100644 --- a/lib/bbitset.h +++ b/lib/bbitset.h @@ -26,11 +26,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #endif /* Currently we support three flavours of bitsets: - BITSET_ARRAY: Array of bits (fixed size, faster). + BITSET_ARRAY: Array of bits (fixed size, fast for dense bitsets). BITSET_LIST: Linked list of array of bits (variable size, least storage - for large very sparse sets). + for large very sparse sets). BITSET_TABLE: Expandable table of pointers to array of bits - (variable size, less storage for large sparse sets). + (variable size, less storage for large sparse sets). BITSET_STATS: Wrapper bitset for internal use only. */ @@ -38,6 +38,8 @@ enum bitset_type {BITSET_ARRAY, BITSET_LIST, BITSET_TABLE, BITSET_TYPE_NUM, BITSET_STATS}; #define BITSET_TYPE_NAMES {"abitset", "lbitset", "ebitset"} +extern const char * const bitset_type_names[]; + enum bitset_alloc_type {BITSET_MALLOC, BITSET_OBALLOC}; /* Non-zero to use inline functions instead of macros. */ @@ -68,163 +70,213 @@ enum bitset_ops {BITSET_OP_ZERO, BITSET_OP_ONES, BITSET_OP_AND, BITSET_OP_OR, BITSET_OP_XOR, BITSET_OP_ANDN, BITSET_OP_OR_AND, BITSET_OP_AND_OR, BITSET_OP_ANDN_OR}; +struct bbitset_struct +{ + const struct bitset_vtable * vtable; + bitset_windex cindex; /* Cache word index. */ + bitset_windex csize; /* Cache size in words. */ + bitset_word *cdata; /* Cache data pointer. */ + /* Perhaps we could sacrifice another word to indicate + that the bitset is known to be zero, that a bit has been set + in the cache, and that a bit has been cleared in the cache. + This would speed up some of the searches but slightly slow down + bit set/reset operations of cached bits. */ +}; + + +typedef struct bitset_struct *bitset; + + +/* The contents of this structure should be considered private. */ +struct bitset_vtable +{ + void (*set) PARAMS ((struct bitset_struct *, bitset_bindex)); + void (*reset) PARAMS ((struct bitset_struct *, bitset_bindex)); + int (*toggle) PARAMS ((struct bitset_struct *, bitset_bindex)); + int (*test) PARAMS ((struct bitset_struct *, bitset_bindex)); + int (*size) PARAMS ((struct bitset_struct *)); + int (*count) PARAMS ((struct bitset_struct *)); + + int (*empty_p) PARAMS ((struct bitset_struct *)); + void (*ones) PARAMS ((struct bitset_struct *)); + void (*zero) PARAMS ((struct bitset_struct *)); + + void (*copy) PARAMS ((struct bitset_struct *, struct bitset_struct *)); + int (*disjoint_p) PARAMS ((struct bitset_struct *, struct bitset_struct *)); + int (*equal_p) PARAMS ((struct bitset_struct *, struct bitset_struct *)); + void (*not) PARAMS ((struct bitset_struct *, struct bitset_struct *)); + int (*subset_p) PARAMS ((struct bitset_struct *, struct bitset_struct *)); + + void (*and) PARAMS ((struct bitset_struct *, struct bitset_struct *, + struct bitset_struct *)); + int (*and_cmp) PARAMS ((struct bitset_struct *, struct bitset_struct *, + struct bitset_struct *)); + void (*andn) PARAMS ((struct bitset_struct *, struct bitset_struct *, + struct bitset_struct *)); + int (*andn_cmp) PARAMS ((struct bitset_struct *, struct bitset_struct *, + struct bitset_struct *)); + void (*or) PARAMS ((struct bitset_struct *, struct bitset_struct *, + struct bitset_struct *)); + int (*or_cmp) PARAMS ((struct bitset_struct *, struct bitset_struct *, + struct bitset_struct *)); + void (*xor) PARAMS ((struct bitset_struct *, struct bitset_struct *, + struct bitset_struct *)); + int (*xor_cmp) PARAMS ((struct bitset_struct *, struct bitset_struct *, + struct bitset_struct *)); + + void (*and_or) PARAMS ((struct bitset_struct *, struct bitset_struct *, + struct bitset_struct *, struct bitset_struct *)); + int (*and_or_cmp) PARAMS ((struct bitset_struct *, struct bitset_struct *, + struct bitset_struct *, struct bitset_struct *)); + void (*andn_or) PARAMS ((struct bitset_struct *, struct bitset_struct *, + struct bitset_struct *, struct bitset_struct *)); + int (*andn_or_cmp) PARAMS ((struct bitset_struct *, struct bitset_struct *, + struct bitset_struct *, struct bitset_struct *)); + void (*or_and) PARAMS ((struct bitset_struct *, struct bitset_struct *, + struct bitset_struct *, struct bitset_struct *)); + int (*or_and_cmp) PARAMS ((struct bitset_struct *, struct bitset_struct *, + struct bitset_struct *, struct bitset_struct *)); + + int (*list) PARAMS ((struct bitset_struct *, bitset_bindex *, + bitset_bindex, bitset_bindex *)); + int (*list_reverse) PARAMS ((struct bitset_struct *, bitset_bindex *, + bitset_bindex, bitset_bindex *)); + void (*free) PARAMS ((struct bitset_struct *)); + enum bitset_type type; +}; + +#define BITSET_COMPATIBLE_(BSET1, BSET2) ((BSET1)->b.vtable == (BSET2)->b.vtable) + +#define BITSET_CHECK2_(DST, SRC) \ +if (!BITSET_COMPATIBLE_ (DST, SRC)) abort (); + +#define BITSET_CHECK3_(DST, SRC1, SRC2) \ +if (!BITSET_COMPATIBLE_ (DST, SRC1) \ + || !BITSET_COMPATIBLE_ (DST, SRC2)) abort (); + +#define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \ +if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \ + || !BITSET_COMPATIBLE_ (DST, SRC3)) abort (); + + /* Return size in bits of bitset SRC. */ -#define BITSET_SIZE_(SRC) (SRC)->b.ops->size (SRC) +#define BITSET_SIZE_(SRC) (SRC)->b.vtable->size (SRC) + +/* Return number of bits set in bitset SRC. */ +#define BITSET_COUNT_(SRC) (SRC)->b.vtable->count (SRC) /* Return type of bitset SRC. */ -#define BITSET_TYPE_(DST) (DST)->b.ops->type +#define BITSET_TYPE_(DST) (DST)->b.vtable->type /* Set bit BITNO in bitset DST. */ -#define BITSET_SET_(DST, BITNO) (DST)->b.ops->set (DST, BITNO) +#define BITSET_SET_(DST, BITNO) (DST)->b.vtable->set (DST, BITNO) /* Reset bit BITNO in bitset DST. */ -#define BITSET_RESET_(DST, BITNO) (DST)->b.ops->reset (DST, BITNO) +#define BITSET_RESET_(DST, BITNO) (DST)->b.vtable->reset (DST, BITNO) + +/* Toggle bit BITNO in bitset DST. */ +#define BITSET_TOGGLE_(DST, BITNO) (DST)->b.vtable->toggle (DST, BITNO) /* Return non-zero if bit BITNO in bitset SRC is set. */ -#define BITSET_TEST_(SRC, BITNO) (SRC)->b.ops->test (SRC, BITNO) +#define BITSET_TEST_(SRC, BITNO) (SRC)->b.vtable->test (SRC, BITNO) /* Free bitset SRC. */ #define BITSET_FREE_(SRC)\ - ((SRC)->b.ops->free ? (SRC)->b.ops->free (SRC) :(void)0) - -/* Perform operation OP on DST. */ -#define BITSET_OP1_(DST, OP) (DST)->b.ops->op1 (DST, OP) + ((SRC)->b.vtable->free ? (SRC)->b.vtable->free (SRC) :(void)0) -/* Perform operation OP on SRC and store in DST. */ -#define BITSET_OP2_(DST, SRC, OP) (DST)->b.ops->op2 (DST, SRC, OP) -/* DST = SRC1 OP SRC2. */ -#define BITSET_OP3_(DST, SRC1, SRC2, OP) \ -(DST)->b.ops->op3 (DST, SRC1, SRC2, OP) +/* Return SRC == 0. */ +#define BITSET_EMPTY_P_(SRC) (SRC)->b.vtable->empty_p (SRC) -/* DST = (SRC1 OP1 SRC2) OP2 SRC3. */ -#define BITSET_OP4_(DST, SRC1, SRC2, SRC3, OP) \ -(DST)->b.ops->op4 (DST, SRC1, SRC2, SRC3, OP) +/* DST = ~0. */ +#define BITSET_ONES_(DST) (DST)->b.vtable->ones (DST) /* DST = 0. */ -#define BITSET_ZERO_(DST) BITSET_OP1_ (DST, BITSET_OP_ZERO); +#define BITSET_ZERO_(DST) (DST)->b.vtable->zero (DST) -/* DST = ~0. */ -#define BITSET_ONES_(DST) BITSET_OP1_ (DST, BITSET_OP_ONES); -/* Return SRC == 0. */ -#define BITSET_EMPTY_P_(SRC) BITSET_OP1_ (SRC, BITSET_OP_EMPTY_P); + +/* DST = SRC. */ +#define BITSET_COPY_(DST, SRC) (SRC)->b.vtable->copy (DST, SRC) + +/* Return DST & SRC == 0. */ +#define BITSET_DISJOINT_P_(DST, SRC) (SRC)->b.vtable->disjoint_p (DST, SRC) /* Return DST == SRC. */ -#define BITSET_EQUAL_P_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_EQUAL_P) +#define BITSET_EQUAL_P_(DST, SRC) (SRC)->b.vtable->equal_p (DST, SRC) + +/* DST = ~SRC. */ +#define BITSET_NOT_(DST, SRC) (SRC)->b.vtable->not (DST, SRC) /* Return DST == DST | SRC. */ -#define BITSET_SUBSET_P_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_SUBSET_P) +#define BITSET_SUBSET_P_(DST, SRC) (SRC)->b.vtable->subset_p (DST, SRC) -/* Return DST & SRC == 0. */ -#define BITSET_DISJOINT_P_(DST, SRC)\ - BITSET_OP2_ (DST, SRC, BITSET_OP_DISJOINT_P) -/* DST = SRC. */ -#define BITSET_COPY_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_COPY) +/* DST = SRC1 & SRC2. */ +#define BITSET_AND_(DST, SRC1, SRC2) (SRC1)->b.vtable->and (DST, SRC1, SRC2) +#define BITSET_AND_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->and_cmp (DST, SRC1, SRC2) -/* DST = ~SRC. */ -#define BITSET_NOT_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_NOT) +/* DST = SRC1 & ~SRC2. */ +#define BITSET_ANDN_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn (DST, SRC1, SRC2) +#define BITSET_ANDN_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn_cmp (DST, SRC1, SRC2) /* DST = SRC1 | SRC2. */ -#define BITSET_OR_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2,\ - BITSET_OP_OR) +#define BITSET_OR_(DST, SRC1, SRC2) (SRC1)->b.vtable->or (DST, SRC1, SRC2) +#define BITSET_OR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->or_cmp (DST, SRC1, SRC2) /* DST = SRC1 ^ SRC2. */ -#define BITSET_XOR_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2,\ - BITSET_OP_XOR) +#define BITSET_XOR_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor (DST, SRC1, SRC2) +#define BITSET_XOR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor_cmp (DST, SRC1, SRC2) -/* DST = SRC1 & SRC2. */ -#define BITSET_AND_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2, \ - BITSET_OP_AND) -/* DST = SRC1 & ~SRC2. */ -#define BITSET_ANDN_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2, \ - BITSET_OP_ANDN) /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if DST != (SRC1 & SRC2) | SRC3. */ -#define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3)\ - BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_AND_OR) +#define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3) \ + (SRC1)->b.vtable->and_or (DST, SRC1, SRC2, SRC3) +#define BITSET_AND_OR_CMP_(DST, SRC1, SRC2, SRC3) \ + (SRC1)->b.vtable->and_or_cmp (DST, SRC1, SRC2, SRC3) + +/* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if + DST != (SRC1 & ~SRC2) | SRC3. */ +#define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3) \ + (SRC1)->b.vtable->andn_or (DST, SRC1, SRC2, SRC3) +#define BITSET_ANDN_OR_CMP_(DST, SRC1, SRC2, SRC3) \ + (SRC1)->b.vtable->andn_or_cmp (DST, SRC1, SRC2, SRC3) /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if DST != (SRC1 | SRC2) & SRC3. */ -#define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3)\ - BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_OR_AND) +#define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3) \ + (SRC1)->b.vtable->or_and (DST, SRC1, SRC2, SRC3) +#define BITSET_OR_AND_CMP_(DST, SRC1, SRC2, SRC3) \ + (SRC1)->b.vtable->or_and_cmp (DST, SRC1, SRC2, SRC3) -/* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if - DST != (SRC1 & ~SRC2) | SRC3. */ -#define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3)\ - BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_ANDN_OR) /* Find list of up to NUM bits set in BSET starting from and including *NEXT. Return with actual number of bits found and with *NEXT indicating where search stopped. */ #define BITSET_LIST_(BSET, LIST, NUM, NEXT) \ -(BSET)->b.ops->list (BSET, LIST, NUM, NEXT) + (BSET)->b.vtable->list (BSET, LIST, NUM, NEXT) /* Find reverse list of up to NUM bits set in BSET starting from and including NEXT. Return with actual number of bits found and with *NEXT indicating where search stopped. */ -#define BITSET_REVERSE_LIST_(BSET, LIST, NUM, NEXT) \ -(BSET)->b.ops->reverse_list (BSET, LIST, NUM, NEXT) +#define BITSET_LIST_REVERSE_(BSET, LIST, NUM, NEXT) \ + (BSET)->b.vtable->list_reverse (BSET, LIST, NUM, NEXT) -struct bbitset_struct -{ - struct bitset_ops_struct *ops; - bitset_windex cindex; /* Cache word index. */ - bitset_windex csize; /* Cache size in words. */ - bitset_word *cdata; /* Cache data pointer. */ - /* Perhaps we could sacrifice another word to indicate - that the bitset is known to be zero, that a bit has been set - in the cache, and that a bit has been cleared in the cache. - This would speed up some of the searches but slightly slow down - bit set/reset operations of cached bits. */ -}; +/* Private functions for bitset implementations. */ + +extern int bitset_toggle_ PARAMS ((bitset, bitset_bindex)); +extern int bitset_count_ PARAMS ((bitset)); -typedef struct bitset_struct *bitset; - - -/* The contents of this structure should be considered private. */ -struct bitset_ops_struct -{ - void (*set) PARAMS ((struct bitset_struct *, bitset_bindex)); - void (*reset) PARAMS ((struct bitset_struct *, bitset_bindex)); - int (*test) PARAMS ((struct bitset_struct *, bitset_bindex)); - int (*size) PARAMS ((struct bitset_struct *)); - int (*op1) PARAMS ((struct bitset_struct *, enum bitset_ops)); - int (*op2) PARAMS ((struct bitset_struct *, struct bitset_struct *, - enum bitset_ops)); - int (*op3) PARAMS ((struct bitset_struct *, struct bitset_struct *, - struct bitset_struct *, enum bitset_ops)); - int (*op4) PARAMS ((struct bitset_struct *, struct bitset_struct *, - struct bitset_struct *, struct bitset_struct *, - enum bitset_ops)); - int (*list) PARAMS ((struct bitset_struct *, bitset_bindex *, - bitset_bindex, bitset_bindex *)); - int (*reverse_list) PARAMS ((struct bitset_struct *, bitset_bindex *, - bitset_bindex, bitset_bindex *)); - void (*free) PARAMS ((struct bitset_struct *)); - enum bitset_type type; -}; +extern int bitset_copy_ PARAMS ((bitset, bitset)); -#define BITSET_COMPATIBLE_(BSET1, BSET2) ((BSET1)->b.ops == (BSET2)->b.ops) - -#define BITSET_CHECK2_(DST, SRC) \ -if (!BITSET_COMPATIBLE_ (DST, SRC)) abort (); - -#define BITSET_CHECK3_(DST, SRC1, SRC2) \ -if (!BITSET_COMPATIBLE_ (DST, SRC1) \ - || !BITSET_COMPATIBLE_ (DST, SRC2)) abort (); - -#define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \ -if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \ - || !BITSET_COMPATIBLE_ (DST, SRC3)) abort (); +extern int bitset_and_or_cmp_ PARAMS ((bitset, bitset, bitset, bitset)); +extern int bitset_andn_or_cmp_ PARAMS ((bitset, bitset, bitset, bitset)); -extern int bitset_op4 PARAMS ((bitset, bitset, bitset, bitset, - enum bitset_ops op)); +extern int bitset_or_and_cmp_ PARAMS ((bitset, bitset, bitset, bitset)); #endif /* _BBITSET_H */ |