* lib/bitset-int.h, lib/bitset.c, lib/bitset.h, lib/bitsetv.c,

* lib/bitsetv.h, lib/ebitset.c, lib/ebitset.h, lib/lbitset.c,
* lib/lbitset.h, lib/sbitset.c, lib/sbitset.h: New.
* src/closure.c (fderives): Be an array of bitsets.
This commit is contained in:
Akim Demaille
2002-03-04 11:58:52 +00:00
parent 98254360e3
commit 7086e7071e
14 changed files with 4948 additions and 11 deletions

View File

@@ -42,5 +42,10 @@ libbison_a_SOURCES = \
xalloc.h xmalloc.c xstrdup.c xstrndup.c \
readpipe.c
# Implementation of bitsets
libbison_a_SOURCES += \
bitset-int.h bitset.h bitsetv.h ebitset.h lbitset.h sbitset.h \
bitset.c bitsetv.c ebitset.c lbitset.c sbitset.c
libbison_a_LIBADD = @LIBOBJS@ @ALLOCA@
libbison_a_DEPENDENCIES = $(libbison_a_LIBADD)

112
lib/bitset-int.h Normal file
View File

@@ -0,0 +1,112 @@
/* Internal bitset definitions.
Copyright (C) 2002 Free Software Foundation, Inc.
Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ifndef _BITSET_INT_H
#define _BITSET_INT_H
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
/* Currently we support three flavours of bitsets:
BITSET_ARRAY: Array of bits (fixed size, faster).
BITSET_LIST: Linked list of array of bits (variable size, least storage
for large very sparse sets).
BITSET_TABLE: Expandable table of pointers to array of bits
(variable size, less storage for large sparse sets).
*/
enum bitset_type {BITSET_ARRAY, BITSET_LIST, BITSET_TABLE, BITSET_TYPE_NUM};
#define BITSET__TYPE_NAMES {"sbitset", "lbitset", "ebitset"}
/* Non-zero to enable bitset caching. */
#define BITSET_CACHE 1
/* Non-zero to use inline functions instead of macros. */
#define BITSET_INLINE 0
/* Non-zero to enable bitset statistics gathering. */
#define BITSET_STATS 1
/* Non-zero to enable bitset type checking. */
#define BITSET_CHECK 0
typedef unsigned long bitset_word;
#define BITSET_WORD_BITS ((unsigned) CHAR_BIT * sizeof (bitset_word))
/* Bit index. */
typedef unsigned long bitset_bindex;
/* Word index. */
typedef unsigned long bitset_windex;
#define BITSET_INDEX_MAX ((1U << (BITSET_WORD_BITS - 1)))
#define BITSET_MSB (1U << (BITSET_WORD_BITS - 1))
#define BITSET_LIST_SIZE 1024
enum bitset_ops {BITSET_ZERO, BITSET_ONES, BITSET_EMPTY_P,
BITSET_COPY, BITSET_EQUAL_P, BITSET_SUBSET_P, BITSET_NOT,
BITSET_AND, BITSET_OR, BITSET_XOR, BITSET_ANDN, BITSET_ORN,
BITSET_OR_AND, BITSET_AND_OR, BITSET_ANDN_OR};
/* Return size in bits of bitset SRC. */
#define BITSET__SIZE(SRC) (SRC)->ops->size (SRC)
/* Set bit BITNO in bitset DST. */
#define BITSET__SET(DST, BITNO) (DST)->ops->set (DST, BITNO)
/* Reset bit BITNO in bitset DST. */
#define BITSET__RESET(DST, BITNO) (DST)->ops->reset (DST, BITNO)
/* Return non-zero if bit BITNO in bitset SRC is set. */
#define BITSET__TEST(SRC, BITNO) (SRC)->ops->test (SRC, BITNO)
/* Free bitset SRC. */
#define BITSET__FREE(SRC) ((SRC)->ops->free) (SRC)
/* Perform operation OP on DST. */
#define BITSET__OP1(DST, OP) (DST)->ops->op1 (DST, OP)
/* Perform operation OP on SRC and store in DST. */
#define BITSET__OP2(DST, SRC, OP) (DST)->ops->op2 (DST, SRC, OP)
/* DST = SRC1 OP SRC2. */
#define BITSET__OP3(DST, SRC1, SRC2, OP) \
(DST)->ops->op3 (DST, SRC1, SRC2, OP)
/* DST = (SRC1 OP1 SRC2) OP2 SRC3. */
#define BITSET__OP4(DST, SRC1, SRC2, SRC3, OP) \
(DST)->ops->op4 (DST, SRC1, SRC2, SRC3, OP)
/* DST = SRC. */
#define BITSET__COPY(DST, SRC) BITSET__OP2 (DST, SRC, BITSET_COPY)
/* 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)->ops->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)->ops->reverse_list (BSET, LIST, NUM, NEXT)
#endif /* _BITSET_INT_H */

835
lib/bitset.c Normal file
View File

@@ -0,0 +1,835 @@
/* General bitsets.
Copyright (C) 2002 Free Software Foundation, Inc.
Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include "bitset.h"
#include "obstack.h"
static void bitset_print PARAMS ((FILE *, bitset, int));
#if BITSET_CHECK
#define BITSET__CHECK2(DST, SRC) \
if ((DST)->OPS != (SRC)->OPS) abort ();
#define BITSET__CHECK3(DST, SRC1, SRC2) \
if ((DST)->OPS != (SRC1)->OPS || (DST)->OPS != (SRC2)->OPS) abort ();
#define BITSET__CHECK4(DST, SRC1, SRC2) \
if ((DST)->OPS != (SRC1)->OPS || (DST)->OPS != (SRC2)->OPS \
|| (DST)->OPS != (SRC3)->OPS) abort ();
#else
#define BITSET__CHECK2(DST, SRC)
#define BITSET__CHECK3(DST, SRC1, SRC2)
#define BITSET__CHECK4(DST, SRC1, SRC2, SRC3)
#endif
#if BITSET_STATS
#define BITSET_STATS_FILE "bitset.dat"
#define BITSET_LOG_COUNT_BINS 10
#define BITSET_LOG_SIZE_BINS 16
#define BITSET_DENSITY_BINS 20
struct bitset_type_stats_struct
{
unsigned int xmallocs;
unsigned int xfrees;
unsigned int oballocs;
unsigned int obfrees;
unsigned int lists;
unsigned int list_counts[BITSET_LOG_COUNT_BINS];
unsigned int list_sizes[BITSET_LOG_SIZE_BINS];
unsigned int list_density[BITSET_DENSITY_BINS];
};
struct bitset_stats_struct
{
unsigned int runs;
struct bitset_type_stats_struct types[BITSET_TYPE_NUM];
};
struct bitset_stats_struct bitset_stats_data;
struct bitset_stats_struct *bitset_stats;
static void bitset_percent_histogram_print PARAMS ((FILE *, const char *,
const char *,
unsigned int,
unsigned int *));
static void bitset_log_histogram_print PARAMS ((FILE *, const char *,
const char *,
unsigned int, unsigned int *));
static void bitset_stats_print_1 PARAMS ((FILE *, const char *,
struct bitset_type_stats_struct *));
static void bitset_stats_print PARAMS ((FILE *, int));
static void bitset_stats_read PARAMS ((void));
static void bitset_stats_write PARAMS ((void));
#define BITSET_STATS_XMALLOCS_INC(TYPE) \
if (bitset_stats) \
bitset_stats->types[(TYPE)].xmallocs++
#define BITSET_STATS_XFREES_INC(BSET) \
if (bitset_stats) \
bitset_stats->types[(BSET)->ops->type].xfrees++
#define BITSET_STATS_OBALLOCS_INC(TYPE) \
if (bitset_stats) \
bitset_stats->types[(TYPE)].oballocs++
#define BITSET_STATS_OBFREES_INC(BSET) \
if (bitset_stats) \
bitset_stats->types[(BSET)->ops->type].obfrees++
#define BITSET_STATS_LISTS_INC(BSET) \
if (bitset_stats) \
bitset_stats->types[(BSET)->ops->type].lists++
#define BITSET_STATS_LIST_COUNTS_INC(BSET, I) \
if (bitset_stats) \
bitset_stats->types[(BSET)->ops->type].list_counts[(I)]++
#define BITSET_STATS_LIST_SIZES_INC(BSET, I) \
if (bitset_stats) \
bitset_stats->types[(BSET)->ops->type].list_sizes[(I)]++
#define BITSET_STATS_LIST_DENSITY_INC(BSET, I) \
if (bitset_stats) \
bitset_stats->types[(BSET)->ops->type].list_density[(I)]++
#else
#define BITSET_STATS_XMALLOCS_INC(TYPE)
#define BITSET_STATS_XFREES_INC(BSET)
#define BITSET_STATS_OBALLOCS_INC(TYPE)
#define BITSET_STATS_OBFREES_INC(BSET)
#define BITSET_STATS_LISTS_INC(BSET)
#define BITSET_STATS_LIST_COUNTS_INC(BSET, I)
#define BITSET_STATS_LIST_SIZES_INC(BSET, I)
#define BITSET_STATS_LIST_DENSITY_INC(BSET, I)
#endif /* BITSET_STATS */
/* Return number of bytes required to create a N_BIT bitset
of TYPE. The bitset may grow to require more bytes than this. */
int
bitset_bytes (type, n_bits)
enum bitset_type type;
bitset_bindex n_bits;
{
unsigned int bytes;
switch (type)
{
case BITSET_ARRAY:
bytes = sbitset_bytes (n_bits);
break;
case BITSET_LIST:
bytes = lbitset_bytes (n_bits);
break;
case BITSET_TABLE:
bytes = ebitset_bytes (n_bits);
break;
default:
abort ();
}
return bytes;
}
/* Initialise bitset BSET of TYPE for N_BITS. */
bitset
bitset_init (bset, n_bits, type)
bitset bset;
bitset_bindex n_bits;
enum bitset_type type;
{
switch (type)
{
case BITSET_ARRAY:
return sbitset_init (bset, n_bits);
case BITSET_LIST:
return lbitset_init (bset, n_bits);
case BITSET_TABLE:
return ebitset_init (bset, n_bits);
default:
abort ();
}
}
/* Select a bitset type for a set of N_BITS and with attribute hints
specified by ATTR. For variable size bitsets, N_BITS is only a
hint and may be zero. */
enum bitset_type
bitset_type_choose (n_bits, attr)
bitset_bindex n_bits ATTRIBUTE_UNUSED;
unsigned int attr;
{
enum bitset_type type;
#ifdef ENABLE_CHECKING
/* Check attributes. */
if (attr & BITSET_FIXED && attr & BITSET_VARIABLE)
abort ();
if (attr & BITSET_SPARSE && attr & BITSET_DENSE)
abort ();
/* Note that sometimes we will be asked for a zero length
fixed size bitset. */
#endif
/* Choose the type of bitset. */
type = BITSET_ARRAY;
/* Currently, the simple bitsets do not support a variable size. */
if (attr & BITSET_VARIABLE || attr & BITSET_SPARSE)
{
type = BITSET_LIST;
if (attr & BITSET_DENSE || attr & BITSET_GREEDY)
type = BITSET_TABLE;
}
return type;
}
/* Create a bitset of N_BITS of type TYPE. */
bitset
bitset_alloc (n_bits, type)
bitset_bindex n_bits;
enum bitset_type type;
{
unsigned int bytes;
bitset bset;
BITSET_STATS_XMALLOCS_INC (type);
bytes = bitset_bytes (type, n_bits);
bset = (bitset) xmalloc (bytes);
return bitset_init (bset, n_bits, type);
}
/* Create a bitset of N_BITS of type TYPE. */
bitset
bitset_obstack_alloc (bobstack, n_bits, type)
struct obstack *bobstack;
bitset_bindex n_bits;
enum bitset_type type;
{
unsigned int bytes;
BITSET_STATS_OBALLOCS_INC (type);
bytes = bitset_bytes (type, n_bits);
return bitset_init (obstack_alloc (bobstack, bytes), n_bits, type);
}
/* Create a bitset of N_BITS and with attribute hints specified by
ATTR. */
bitset
bitset_create (n_bits, attr)
bitset_bindex n_bits;
unsigned int attr;
{
enum bitset_type type;
type = bitset_type_choose (n_bits, attr);
return bitset_alloc (n_bits, type);
}
/* Free bitset BSET. */
void
bitset_free (bset)
bitset bset;
{
BITSET_STATS_XFREES_INC (bset);
if (bset->ops->free)
BITSET__FREE (bset);
bset->ops = NULL;
free (bset);
}
/* Free bitset BSET allocated on obstack. */
void
bitset_obstack_free (bset)
bitset bset;
{
BITSET_STATS_OBFREES_INC (bset);
if (bset->ops->free)
BITSET__FREE (bset);
bset->ops = NULL;
}
/* Find next bit set in SRC starting from and including BITNO.
Return -1 if SRC empty. */
int
bitset_next (src, bitno)
bitset src;
bitset_bindex bitno;
{
bitset_bindex val;
bitset_bindex next = bitno;
if (!bitset_list (src, &val, 1, &next))
return -1;
return val;
}
/* Find previous bit set in SRC starting from and including BITNO.
Return -1 if SRC empty. */
int
bitset_prev (src, bitno)
bitset src;
bitset_bindex bitno;
{
bitset_bindex val;
bitset_bindex next = bitno;
if (!bitset_reverse_list (src, &val, 1, &next))
return -1;
return val;
}
/* Find first set bit. */
int
bitset_first (src)
bitset src;
{
return bitset_next (src, 0);
}
/* Find last set bit. */
int
bitset_last (src)
bitset src;
{
return bitset_prev (src, 0);
}
static void
bitset_print (file, bset, verbose)
FILE *file;
bitset bset;
int verbose;
{
unsigned int i, pos;
if (verbose)
fprintf (file, "n_bits = %d, set = {", bitset_size (bset));
pos = 30;
BITSET_EXECUTE (bset, 0, i,
{
if (pos > 70)
{
fprintf (file, "\n");
pos = 0;
}
fprintf (file, "%d ", i);
pos += 1 + (i >= 10) + (i >= 100);
});
if (verbose)
fprintf (file, "}\n");
}
int
bitset_copy (dst, src)
bitset dst;
bitset src;
{
unsigned int i;
if (dst->ops == src->ops)
return BITSET__COPY (dst, src);
/* Convert bitset types. We assume that the DST bitset
is large enough to hold the SRC bitset. */
bitset_zero (dst);
BITSET_EXECUTE (src, 0, i,
{
bitset_set (dst, i);
});
return 1;
}
/* Return size in bits of bitset SRC. */
int
bitset_size (src)
bitset src;
{
return BITSET__SIZE (src);
}
/* DST = 0. */
int
bitset_zero (dst)
bitset dst;
{
return BITSET__OP1 (dst, BITSET_ZERO);
}
/* DST = ~0. */
int
bitset_ones (dst)
bitset dst;
{
return BITSET__OP1 (dst, BITSET_ONES);
}
/* Return non-zero if all bits in bitset SRC are reset. */
int
bitset_empty_p (src)
bitset src;
{
return BITSET__OP1 (src, BITSET_EMPTY_P);
}
/* Return DST == DST | SRC. */
int
bitset_subset_p (dst, src)
bitset dst;
bitset src;
{
return BITSET__OP2 (dst, src, BITSET_SUBSET_P);
}
/* Return DST == SRC. */
int
bitset_equal_p (dst, src)
bitset dst;
bitset src;
{
BITSET__CHECK2 (dst, src);
return BITSET__OP2 (dst, src, BITSET_EQUAL_P);
}
/* DST = ~SRC. */
int
bitset_not (dst, src)
bitset dst;
bitset src;
{
BITSET__CHECK2 (dst, src);
return BITSET__OP2 (dst, src, BITSET_NOT);
}
/* DST = SRC1 | SRC2. Return non-zero if DST != SRC1 | SRC2. */
int
bitset_or (dst, src1, src2)
bitset dst;
bitset src1;
bitset src2;
{
BITSET__CHECK3 (dst, src1, src2);
return BITSET__OP3 (dst, src1, src2, BITSET_OR);
}
/* DST = SRC1 & SRC2. Return non-zero if DST != SRC1 & SRC2. */
int
bitset_and (dst, src1, src2)
bitset dst;
bitset src1;
bitset src2;
{
BITSET__CHECK3 (dst, src1, src2);
return BITSET__OP3 (dst, src1, src2, BITSET_AND);
}
/* DST = SRC1 ^ SRC2. Return non-zero if DST != SRC1 ^ SRC2. */
int
bitset_xor (dst, src1, src2)
bitset dst;
bitset src1;
bitset src2;
{
BITSET__CHECK3 (dst, src1, src2);
return BITSET__OP3 (dst, src1, src2, BITSET_XOR);
}
/* DST = SRC1 & ~SRC2. Return non-zero if DST != SRC1 & ~SRC2. */
int
bitset_andn (dst, src1, src2)
bitset dst;
bitset src1;
bitset src2;
{
BITSET__CHECK3 (dst, src1, src2);
return BITSET__OP3 (dst, src1, src2, BITSET_ANDN);
}
/* DST = SRC1 | ~SRC2. Return non-zero if DST != SRC1 | ~SRC2. */
int
bitset_orn (dst, src1, src2)
bitset dst;
bitset src1;
bitset src2;
{
BITSET__CHECK3 (dst, src1, src2);
return BITSET__OP3 (dst, src1, src2, BITSET_ORN);
}
/* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
DST != (SRC1 | SRC2) & SRC3. */
int
bitset_or_and (dst, src1, src2, src3)
bitset dst;
bitset src1;
bitset src2;
bitset src3;
{
BITSET__CHECK4 (dst, src1, src2, src3);
return BITSET__OP4 (dst, src1, src2, src3, BITSET_OR_AND);
}
/* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
DST != (SRC1 & SRC2) | SRC3. */
int
bitset_and_or (dst, src1, src2, src3)
bitset dst;
bitset src1;
bitset src2;
bitset src3;
{
BITSET__CHECK4 (dst, src1, src2, src3);
return BITSET__OP4 (dst, src1, src2, src3, BITSET_AND_OR);
}
/* Dump bitset BSET to FILE. */
void
bitset_dump (file, bset)
FILE *file;
bitset bset;
{
bitset_print (file, bset, 0);
}
/* Function to be called from debugger to print bitset. */
void
debug_bitset (bset)
bitset bset;
{
bitset_print (stderr, bset, 1);
}
/* Release memory associated with bitsets. */
void
bitset_release_memory ()
{
lbitset_release_memory ();
ebitset_release_memory ();
}
#if BITSET_STATS
int
bitset_list (bset, list, num, next)
bitset bset;
bitset_bindex *list;
bitset_bindex num;
bitset_bindex *next;
{
bitset_bindex count;
count = BITSET__LIST (bset, list, num, next);
if (bitset_stats)
{
bitset_bindex tmp;
bitset_bindex size;
bitset_bindex i;
enum bitset_type type;
type = bset->ops->type;
BITSET_STATS_LISTS_INC (bset);
/* Log histogram of number of set bits. */
for (i = 0, tmp = count; tmp; tmp >>= 1, i++)
continue;
if (i >= BITSET_LOG_COUNT_BINS)
i = BITSET_LOG_COUNT_BINS - 1;
BITSET_STATS_LIST_COUNTS_INC (bset, i);
/* Log histogram of number of bits in set. */
size = bitset_size (bset);
for (i = 0, tmp = size; tmp; tmp >>= 1, i++)
continue;
if (i >= BITSET_LOG_SIZE_BINS)
i = BITSET_LOG_SIZE_BINS - 1;
BITSET_STATS_LIST_SIZES_INC (bset, i);
/* Histogram of fraction of bits set. */
i = size ? (count * BITSET_DENSITY_BINS) / size : 0;
if (i >= BITSET_DENSITY_BINS)
i = BITSET_DENSITY_BINS - 1;
BITSET_STATS_LIST_DENSITY_INC (bset, i);
}
return count;
}
/* Print a percentage histogram with message MSG to FILE. */
static void
bitset_percent_histogram_print (file, name, msg, n_bins, bins)
FILE *file;
const char *name;
const char *msg;
unsigned int n_bins;
unsigned int *bins;
{
unsigned int i;
unsigned int total;
total = 0;
for (i = 0; i < n_bins; i++)
total += bins[i];
if (!total)
return;
fprintf (file, "%s %s", name, msg);
for (i = 0; i < n_bins; i++)
fprintf (file, "%.0f-%.0f%%\t%8d (%5.1f%%)\n",
i * 100.0 / n_bins,
(i + 1) * 100.0 / n_bins,
bins[i], (100.0 * bins[i]) / total);
}
/* Print a log histogram with message MSG to FILE. */
static void
bitset_log_histogram_print (file, name, msg, n_bins, bins)
FILE *file;
const char *name;
const char *msg;
unsigned int n_bins;
unsigned int *bins;
{
unsigned int i;
unsigned int total;
unsigned int max_width;
total = 0;
for (i = 0; i < n_bins; i++)
total += bins[i];
if (!total)
return;
/* 2 * ceil (log10(2) * (N - 1)) + 1 */
max_width = 2 * (unsigned int)(0.30103 * (n_bins - 1) + 0.9999) + 1;
fprintf (file, "%s %s", name, msg);
for (i = 0; i < 2; i++)
fprintf (file, "%*d\t%8d (%5.1f%%)\n",
max_width, i, bins[i], 100.0 * bins[i] / total);
/* Perhaps we should bail out once the histogram goes to zero. */
for (; i < n_bins; i++)
fprintf (file, "%*d-%d\t%8d (%5.1f%%)\n",
max_width - ((unsigned int) (0.30103 * (i) + 0.9999) + 1),
1 << (i - 1), (1 << i) - 1, bins[i], (100.0 * bins[i]) / total);
}
/* Print bitset statistics to FILE. */
static void
bitset_stats_print_1 (file, name, stats)
FILE *file;
const char *name;
struct bitset_type_stats_struct *stats;
{
if (!stats)
return;
fprintf (file, "%d %ss xmalloced, %d freed.\n",
stats->xmallocs, name, stats->xfrees);
fprintf (file, "%d %ss oballoced, %d freed.\n",
stats->oballocs, name, stats->obfrees);
fprintf (file, "%d bitset_lists\n", stats->lists);
bitset_log_histogram_print (file, name, "count log histogram\n",
BITSET_LOG_COUNT_BINS, stats->list_counts);
bitset_log_histogram_print (file, name, "size log histogram\n",
BITSET_LOG_SIZE_BINS, stats->list_sizes);
bitset_percent_histogram_print (file, name, "density histogram\n",
BITSET_DENSITY_BINS, stats->list_density);
}
/* Print all bitset statistics to FILE. */
static void
bitset_stats_print (file, verbose)
FILE *file;
int verbose ATTRIBUTE_UNUSED;
{
int i;
static const char *names[] = BITSET__TYPE_NAMES;
if (!bitset_stats)
return;
fprintf (file, "Bitset statistics:\n\n");
if (bitset_stats->runs > 1)
fprintf (file, "Accumulated runs = %d\n", bitset_stats->runs);
for (i = 0; i < BITSET_TYPE_NUM; i++)
bitset_stats_print_1 (file, names[i], &bitset_stats->types[i]);
}
#endif /* BITSET_STATS */
/* Initialise bitset statistics logging. */
void
bitset_stats_init ()
{
#if BITSET_STATS
bitset_stats = &bitset_stats_data;
bitset_stats_read ();
#endif /* BITSET_STATS */
}
/* Read bitset statistics file. */
static void
bitset_stats_read ()
{
FILE *file;
if (!bitset_stats)
return;
file = fopen (BITSET_STATS_FILE, "r");
if (file)
{
if (fread (&bitset_stats_data, sizeof (bitset_stats_data),
1, file) != 1)
{
if (ferror (file))
perror ("Could not read stats file");
else
fprintf (stderr, "Bad stats file size\n");
}
fclose (file);
}
bitset_stats_data.runs++;
}
/* Write bitset statistics file. */
static void
bitset_stats_write ()
{
FILE *file;
if (!bitset_stats)
return;
file = fopen (BITSET_STATS_FILE, "w");
if (file)
{
if (fwrite (&bitset_stats_data, sizeof (bitset_stats_data),
1, file) != 1)
perror ("Could not write stats file");
fclose (file);
}
else
perror ("Could not open stats file for writing");
}
/* Dump bitset statistics to FILE. */
void
bitset_stats_dump (file)
FILE *file;
{
#if BITSET_STATS
bitset_stats_print (file, 0);
bitset_stats_write ();
#endif /* BITSET_STATS */
}
/* Function to be called from debugger to print bitset stats. */
void
debug_bitset_stats (void)
{
#if BITSET_STATS
bitset_stats_print (stderr, 1);
#endif /* BITSET_STATS */
}

395
lib/bitset.h Normal file
View File

@@ -0,0 +1,395 @@
/* Generic bitsets.
Copyright (C) 2002 Free Software Foundation, Inc.
Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ifndef _BITSET_H
#define _BITSET_H
#if HAVE_CONFIG_H
# include <config.h>
#endif
#define BITSET_STATS 1
# ifndef PARAMS
# if defined PROTOTYPES || (defined __STDC__ && __STDC__)
# define PARAMS(Args) Args
# else
# define PARAMS(Args) ()
# endif
# endif
# ifndef __attribute__
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
# define __attribute__(x)
# endif
# endif
# ifndef ATTRIBUTE_NORETURN
# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
# endif
# ifndef ATTRIBUTE_UNUSED
# define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
# endif
#include "bitset-int.h"
#include "sbitset.h"
#include "lbitset.h"
#include "ebitset.h"
#include "obstack.h"
#include <stdio.h>
#include "xalloc.h"
enum bitset_attr {BITSET_FIXED = 1, /* Bitset size fixed. */
BITSET_VARIABLE = 2, /* Bitset size variable. */
BITSET_DENSE = 4, /* Bitset dense. */
BITSET_SPARSE = 8, /* Bitset sparse. */
BITSET_FRUGAL = 16, /* Prefer most compact. */
BITSET_GREEDY = 32}; /* Prefer fastest. */
typedef unsigned int bitset_attrs;
/* The elements of these structures should be considered
to be private. */
typedef struct bitset_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. */
union bitset_data
{
struct sbitset_struct s;
struct lbitset_struct l;
struct ebitset_struct e;
} u;
} *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;
};
/* Return bytes required for bitset of desired type and size. */
extern int bitset_bytes PARAMS ((enum bitset_type, bitset_bindex));
/* Initialise a bitset with desired type and size. */
extern bitset bitset_init PARAMS ((bitset, bitset_bindex, enum bitset_type));
extern enum bitset_type bitset_type_choose PARAMS ((bitset_bindex,
bitset_attrs));
/* Create a bitset of desired type and size. */
extern bitset bitset_alloc PARAMS ((bitset_bindex, enum bitset_type));
/* Free bitset. */
extern void bitset_free PARAMS ((bitset));
/* Create a bitset of desired type and size using obstack. */
extern bitset bitset_obstack_alloc PARAMS ((struct obstack *bobstack,
bitset_bindex, enum bitset_type));
/* Free bitset allocated on obstack. */
extern void bitset_obstack_free PARAMS ((bitset));
/* Create a bitset of desired size and attributes. */
extern bitset bitset_create PARAMS ((bitset_bindex, bitset_attrs));
/* Return size in bits of bitset SRC. */
extern int bitset_size PARAMS ((bitset));
#if BITSET_CACHE && BITSET_INLINE
static inline void bitset_set PARAMS ((bitset, bitset_bindex));
static inline void bitset_reset PARAMS ((bitset, bitset_bindex));
static inline int bitset_test PARAMS ((bitset, bitset_bindex));
/* Set bit BITNO in bitset BSET. */
static inline void bitset_set (bset, bitno)
bitset bset;
bitset_bindex bitno;
{
bitset_windex index = bitno / BITSET_WORD_BITS;
bitset_windex offset = index - bset->cindex;
if (offset < bset->csize)
bset->cdata[offset] |= (1 << (bitno % BITSET_WORD_BITS));
else
BITSET__SET (bset, bitno);
}
/* Reset bit BITNO in bitset BSET. */
static inline void bitset_reset (bset, bitno)
bitset bset;
bitset_bindex bitno;
{
bitset_windex index = bitno / BITSET_WORD_BITS;
bitset_windex offset = index - bset->cindex;
if (offset < bset->csize)
bset->cdata[offset] &= ~(1 << (bitno % BITSET_WORD_BITS));
else
BITSET__RESET (bset, bitno);
}
/* Test bit BITNO in bitset BSET. */
static inline int bitset_test (bset, bitno)
bitset bset;
bitset_bindex bitno;
{
bitset_windex index = bitno / BITSET_WORD_BITS;
bitset_windex offset = index - bset->cindex;
if (offset < bset->csize)
return (bset->cdata[offset] >> (bitno % BITSET_WORD_BITS)) & 1;
else
return BITSET__TEST (bset, bitno);
}
#endif
#if BITSET_CACHE && ! BITSET_INLINE
/* Set bit BITNO in bitset BSET. */
#define bitset_set(bset, bitno) \
do \
{ \
bitset_bindex _bitno = (bitno); \
bitset_windex _index = _bitno / BITSET_WORD_BITS; \
bitset_windex _offset = _index - (bset)->cindex; \
\
if (_offset < (bset)->csize) \
(bset)->cdata[_offset] |= (1 << (_bitno % BITSET_WORD_BITS)); \
else \
BITSET__SET ((bset), _bitno); \
} while (0)
/* Reset bit BITNO in bitset BSET. */
#define bitset_reset(bset, bitno) \
do \
{ \
bitset_bindex _bitno = (bitno); \
bitset_windex _index = _bitno / BITSET_WORD_BITS; \
bitset_windex _offset = _index - (bset)->cindex; \
\
if (_offset < (bset)->csize) \
(bset)->cdata[_offset] &= ~(1 << (_bitno % BITSET_WORD_BITS)); \
else \
BITSET__RESET ((bset), _bitno); \
} while (0)
/* Test bit BITNO in bitset BSET. */
#define bitset_test(bset, bitno) \
(((((bitno) / BITSET_WORD_BITS) - (bset)->cindex) < (bset)->csize) \
? ((bset)->cdata[(((bitno) / BITSET_WORD_BITS) - (bset)->cindex)] \
>> ((bitno) % BITSET_WORD_BITS)) & 1 \
: (unsigned int) BITSET__TEST ((bset), (bitno)))
#endif
#if ! BITSET_CACHE
/* Set bit BITNO in bitset SRC. */
#define bitset_set(SRC, BITNO) BITSET__SET (SRC, BITNO)
/* Reset bit BITNO in bitset SRC. */
#define bitset_reset(SRC, BITNO) BITSET__RESET (SRC, BITNO)
/* Return non-zero if bit BITNO in bitset SRC is set. */
#define bitset_test(SRC, BITNO) BITSET__TEST (SRC, BITNO)
#endif
/* DST = 0. */
extern int bitset_zero PARAMS ((bitset));
/* DST = ~0. */
extern int bitset_ones PARAMS ((bitset));
/* Return non-zero if all bits in bitset SRC are reset. */
extern int bitset_empty_p PARAMS ((bitset));
/* Return DST == DST | SRC. */
extern int bitset_subset_p PARAMS ((bitset, bitset));
/* Return DST == SRC. */
extern int bitset_equal_p PARAMS ((bitset, bitset));
/* DST == SRC. */
extern int bitset_copy PARAMS ((bitset, bitset));
/* DST = ~SRC. */
extern int bitset_not PARAMS ((bitset, bitset));
/* DST = SRC1 | SRC2. Return non-zero if DST != SRC1 | SRC2. */
extern int bitset_or PARAMS ((bitset, bitset, bitset));
/* DST = SRC1 & SRC2. Return non-zero if DST != SRC1 & SRC2. */
extern int bitset_and PARAMS ((bitset, bitset, bitset));
/* DST = SRC1 ^ SRC2. Return non-zero if DST != SRC1 ^ SRC2. */
extern int bitset_xor PARAMS ((bitset, bitset, bitset));
/* DST = SRC1 & ~SRC2. Return non-zero if DST != SRC1 & ~SRC2. */
extern int bitset_andn PARAMS ((bitset, bitset, bitset));
/* DST = SRC1 | ~SRC2. Return non-zero if DST != SRC1 | ~SRC2. */
extern int bitset_orn PARAMS ((bitset, bitset, bitset));
/* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
DST != (SRC1 | SRC2) & SRC3. */
extern int bitset_or_and PARAMS ((bitset, bitset, bitset, bitset));
/* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
DST != (SRC1 & SRC2) | SRC3. */
extern int bitset_and_or PARAMS ((bitset, bitset, bitset, bitset));
/* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
DST != (SRC1 & ~SRC2) | SRC3. */
#define bitset_andn_or(DST, SRC1, SRC2, SRC3) \
/* Find next bit set in BSET starting from and including BITNO. */
extern int bitset_next PARAMS ((bitset, bitset_bindex));
/* Find previous bit set in BSET starting from and including BITNO. */
extern int bitset_prev PARAMS ((bitset, bitset_bindex));
/* 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. */
#if BITSET_STATS
extern int bitset_list PARAMS ((bitset, bitset_bindex *, bitset_bindex,
bitset_bindex *));
#else
#define bitset_list(BSET, LIST, NUM, NEXT) \
BITSET__LIST (BSET, LIST, NUM, NEXT)
#endif
/* 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) \
BITSET__REVERSE_LIST (BSET, LIST, NUM, NEXT)
/* Find first set bit. */
extern int bitset_first PARAMS ((bitset));
/* Find last set bit. */
extern int bitset_last PARAMS ((bitset));
/* Dump bitset. */
extern void bitset_dump PARAMS ((FILE *, bitset));
/* Loop over all elements of BSET, starting with MIN, executing CODE. */
#define BITSET_EXECUTE(BSET, MIN, N, CODE) \
do { \
bitset_bindex _list[BITSET_LIST_SIZE]; \
bitset_bindex _next = (MIN); \
int _num; \
\
while ((_num = bitset_list ((BSET), _list, BITSET_LIST_SIZE, &_next)))\
{ \
int _i; \
\
for (_i = 0; _i < _num; _i++) \
{ \
(N) = _list[_i]; \
CODE; \
} \
if (_num < BITSET_LIST_SIZE) \
break; \
} \
} while (0)
/* Loop over all elements of BSET, in reverse order starting with
MIN, executing CODE. */
#define BITSET_REVERSE_EXECUTE(BSET, MIN, N, CODE) \
do { \
bitset_bindex _list[BITSET_LIST_SIZE]; \
bitset_bindex _next = (MIN); \
int _num; \
\
while ((_num = bitset_reverse_list ((BSET), _list, \
BITSET_LIST_SIZE, &_next))) \
{ \
int _i; \
\
for (_i = 0; _i < _num; _i++) \
{ \
(N) = _list[_i]; \
CODE; \
} \
if (_num < BITSET_LIST_SIZE) \
break; \
} \
} while (0)
/* Define set operations in terms of bit operations. */
#define bitset_diff(DST, SRC1, SRC2) bitset_andn (DST, SRC1, SRC2)
#define bitset_intersection(DST, SRC1, SRC2) bitset_and (DST, SRC1, SRC2)
#define bitset_union(DST, SRC1, SRC2) bitset_or (DST, SRC1, SRC2)
#define bitset_diff_union(DST, SRC1, SRC2, SRC3) \
bitset_andn_or (DST, SRC1, SRC2, SRC3)
/* Release any memory tied up with bitsets. */
extern void bitset_release_memory PARAMS ((void));
/* Initialise bitset stats. */
extern void bitset_stats_init PARAMS ((void));
/* Dump bitset stats. */
extern void bitset_stats_dump PARAMS ((FILE *));
/* Function to debug bitset from debugger. */
extern void debug_bitset PARAMS ((bitset));
/* Function to debug bitset stats from debugger. */
extern void debug_bitset_stats PARAMS ((void));
extern bitset sbitset_init PARAMS ((bitset, bitset_bindex));
extern bitset lbitset_init PARAMS ((bitset, bitset_bindex));
extern bitset ebitset_init PARAMS ((bitset, bitset_bindex));
#endif /* _BITSET_H */

128
lib/bitsetv.c Normal file
View File

@@ -0,0 +1,128 @@
/* Bitset vectors.
Copyright (C) 2001 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.
GCC 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 General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include "bitsetv.h"
/* Create a vector of N_VECS bitsets, each of N_BITS, and of
type TYPE. */
bitset *
bitsetv_alloc (n_vecs, n_bits, type)
unsigned int n_vecs;
unsigned int n_bits;
enum bitset_type type;
{
unsigned int vector_bytes;
unsigned int bytes;
bitset *bsetv;
unsigned int i;
/* Determine number of bytes for each set. */
bytes = bitset_bytes (type, n_bits);
/* Allocate vector table at head of bitset array. */
vector_bytes = n_vecs * sizeof (bitset);
bsetv = (bitset *) xmalloc (vector_bytes + bytes * n_vecs);
for (i = 0; i < n_vecs; i++)
{
bsetv[i] = (bitset)((char *)bsetv + vector_bytes + i * bytes);
bitset_init (bsetv[i], n_bits, type);
}
return bsetv;
}
/* Create a vector of N_VECS bitsets, each of N_BITS, and with
attribute hints specified by ATTR. */
bitset *
bitsetv_create (n_vecs, n_bits, attr)
unsigned int n_vecs;
unsigned int n_bits;
unsigned int attr;
{
enum bitset_type type;
type = bitset_type_choose (n_bits, attr);
return bitsetv_alloc (n_vecs, n_bits, type);
}
/* Free bitset vector BSETV. */
void
bitsetv_free (bsetv)
bitset *bsetv;
{
free (bsetv);
}
/* Zero a vector of N_VECS bitsets. */
void
bitsetv_zero (bsetv, n_vecs)
struct bitset_struct **bsetv;
unsigned int n_vecs;
{
unsigned int i;
for (i = 0; i < n_vecs; i++)
bitset_zero (bsetv[i]);
}
/* Set a vector of N_VECS bitsets to ones. */
void
bitsetv_ones (bsetv, n_vecs)
bitset *bsetv;
unsigned int n_vecs;
{
unsigned int i;
for (i = 0; i < n_vecs; i++)
bitset_ones (bsetv[i]);
}
/* Dump the contents of a bitset vector BSETV with N_VECS elements to
FILE. */
void
bitsetv_dump (file, title, subtitle, bsetv, n_vecs)
FILE *file;
const char *title, *subtitle;
bitset *bsetv;
unsigned int n_vecs;
{
unsigned int i;
fprintf (file, "%s\n", title);
for (i = 0; i < n_vecs; i++)
{
fprintf (file, "%s %d\n", subtitle, i);
bitset_dump (file, bsetv[i]);
}
fprintf (file, "\n");
}

49
lib/bitsetv.h Normal file
View File

@@ -0,0 +1,49 @@
/* Bitset vectors.
Copyright (C) 2002 Free Software Foundation, Inc.
Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ifndef _BITSETV_H
#define _BITSETV_H
#include "bitset.h"
typedef bitset * bitsetv;
/* Create a vector of N_VECS bitsets, each of N_BITS, and of
type TYPE. */
extern bitsetv bitsetv_alloc PARAMS ((unsigned int, unsigned int,
enum bitset_type));
/* Create a vector of N_VECS bitsets, each of N_BITS, and with
attribute hints specified by ATTR. */
extern bitsetv bitsetv_create PARAMS ((unsigned int, unsigned int,
unsigned int));
/* Free vector of bitsets. */
extern void bitsetv_free PARAMS ((bitsetv));
/* Zero vector of bitsets. */
extern void bitsetv_zero PARAMS ((bitsetv, unsigned int));
/* Set vector of bitsets. */
extern void bitsetv_ones PARAMS ((bitsetv, unsigned int));
/* Dump vector of bitsets. */
extern void bitsetv_dump PARAMS ((FILE *, const char *,
const char *, bitsetv,
unsigned int));
#endif /* _BITSETV_H */

1245
lib/ebitset.c Normal file

File diff suppressed because it is too large Load Diff

58
lib/ebitset.h Normal file
View File

@@ -0,0 +1,58 @@
/* Functions to support ebitsets.
Copyright (C) 2002 Free Software Foundation, Inc.
Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ifndef _EBITSET_H
#define _EBITSET_H
#include "bitset-int.h"
/* Number of words to use for each element. */
#ifndef EBITSET_ELT_WORDS
#define EBITSET_ELT_WORDS 2
#endif
/* Number of bits stored in each element. */
#define EBITSET_ELT_BITS \
((unsigned) (EBITSET_ELT_WORDS * BITSET_WORD_BITS))
/* Ebitset element. We use an array of bits. */
typedef struct ebitset_elt_struct
{
union
{
bitset_word words[EBITSET_ELT_WORDS]; /* Bits that are set. */
struct ebitset_elt_struct *next;
} u;
} ebitset_elt;
typedef ebitset_elt *ebitset_elts;
/* Head of ebitset linked list. */
typedef struct ebitset_struct
{
unsigned int size; /* Number of elements. */
ebitset_elts *elts; /* Expanding array of pointers to elements. */
} *ebitset;
extern int ebitset_bytes PARAMS ((bitset_bindex));
extern void ebitset_release_memory PARAMS ((void));
#endif

1321
lib/lbitset.c Normal file

File diff suppressed because it is too large Load Diff

62
lib/lbitset.h Normal file
View File

@@ -0,0 +1,62 @@
/* Functions to support lbitsets.
Copyright (C) 2002 Free Software Foundation, Inc.
Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ifndef _LBITSET_H
#define _LBITSET_H
#include "bitset-int.h"
/* Number of words to use for each element. The larger the value the
greater the size of the cache and the shorter the time to find a given bit
but the more memory wasted for sparse bitsets and the longer the time
to search for set bits. */
#ifndef LBITSET_ELT_WORDS
#define LBITSET_ELT_WORDS 2
#endif
typedef bitset_word lbitset_word;
#define LBITSET_WORD_BITS BITSET_WORD_BITS
/* Number of bits stored in each element. */
#define LBITSET_ELT_BITS \
((unsigned) (LBITSET_ELT_WORDS * LBITSET_WORD_BITS))
/* Lbitset element. We use an array of bits for each element.
These are linked together in a doubly-linked list. */
typedef struct lbitset_elt_struct
{
struct lbitset_elt_struct *next; /* Next element. */
struct lbitset_elt_struct *prev; /* Previous element. */
bitset_windex index; /* bitno / BITSET_WORD_BITS. */
bitset_word words[LBITSET_ELT_WORDS]; /* Bits that are set. */
} lbitset_elt;
/* Head of lbitset linked list. */
typedef struct lbitset_struct
{
lbitset_elt *head; /* First element in linked list. */
lbitset_elt *tail; /* Last element in linked list. */
} *lbitset;
extern int lbitset_bytes PARAMS ((bitset_bindex));
extern void lbitset_release_memory PARAMS ((void));
#endif

672
lib/sbitset.c Normal file
View File

@@ -0,0 +1,672 @@
/* Simple bitsets.
Copyright (C) 2002 Free Software Foundation, Inc.
Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "bitset.h"
#include <stdlib.h>
# if WITH_DMALLOC
# define DMALLOC_FUNC_CHECK
# include <dmalloc.h>
# endif /* WITH_DMALLOC */
/* This file implements fixed size bitsets stored as an array
of words. Any unused bits in the last word must be zero. */
static void sbitset_unused_clear PARAMS((bitset));
static int sbitset_small_list PARAMS((bitset, bitset_bindex *, bitset_bindex,
bitset_bindex *));
static void sbitset_set PARAMS((bitset, bitset_bindex));
static void sbitset_reset PARAMS((bitset, bitset_bindex));
static int sbitset_test PARAMS((bitset, bitset_bindex));
static int sbitset_size PARAMS((bitset));
static int sbitset_op1 PARAMS((bitset, enum bitset_ops));
static int sbitset_op2 PARAMS((bitset, bitset, enum bitset_ops));
static int sbitset_op3 PARAMS((bitset, bitset, bitset, enum bitset_ops));
static int sbitset_op4 PARAMS((bitset, bitset, bitset, bitset,
enum bitset_ops));
static int sbitset_list PARAMS((bitset, bitset_bindex *, bitset_bindex,
bitset_bindex *));
static int sbitset_reverse_list PARAMS((bitset, bitset_bindex *, bitset_bindex,
bitset_bindex *));
#define SBITSET_N_WORDS(N) (((N) + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS)
#define SBITSET_WORDS(X) ((X)->u.s.words)
#define SBITSET_N_BITS(X) ((X)->u.s.n_bits)
/* Return size in bits of bitset SRC. */
static int
sbitset_size (src)
bitset src;
{
return SBITSET_N_BITS (src);
}
/* Find list of up to NUM bits set in BSET starting from and including
*NEXT and store in array LIST. Return with actual number of bits
found and with *NEXT indicating where search stopped. */
static int
sbitset_small_list (src, list, num, next)
bitset src;
bitset_bindex *list;
bitset_bindex num;
bitset_bindex *next;
{
bitset_bindex bitno;
bitset_bindex count;
bitset_windex size;
bitset_word word;
word = SBITSET_WORDS (src)[0];
/* Short circuit common case. */
if (!word)
return 0;
size = SBITSET_N_BITS (src);
bitno = *next;
if (bitno >= size)
return 0;
word >>= bitno;
/* If num is 1, we could speed things up with a binary search
of the word of interest. */
if (num >= BITSET_WORD_BITS)
{
for (count = 0; word; bitno++)
{
if (word & 1)
list[count++] = bitno;
word >>= 1;
}
}
else
{
for (count = 0; word; bitno++)
{
if (word & 1)
{
list[count++] = bitno;
if (count >= num)
{
bitno++;
break;
}
}
word >>= 1;
}
}
*next = bitno;
return count;
}
/* Set bit BITNO in bitset DST. */
static void
sbitset_set (dst, bitno)
bitset dst ATTRIBUTE_UNUSED;
bitset_bindex bitno ATTRIBUTE_UNUSED;
{
/* This should never occur for sbitsets. */
abort ();
}
/* Reset bit BITNO in bitset DST. */
static void
sbitset_reset (dst, bitno)
bitset dst ATTRIBUTE_UNUSED;
bitset_bindex bitno ATTRIBUTE_UNUSED;
{
/* This should never occur for sbitsets. */
abort ();
}
/* Test bit BITNO in bitset SRC. */
static int
sbitset_test (src, bitno)
bitset src ATTRIBUTE_UNUSED;
bitset_bindex bitno ATTRIBUTE_UNUSED;
{
/* This should never occur for sbitsets. */
abort ();
return 0;
}
/* Find list of up to NUM bits set in BSET in reverse order, starting
from and including NEXT and store in array LIST. Return with
actual number of bits found and with *NEXT indicating where search
stopped. */
static int
sbitset_reverse_list (src, list, num, next)
bitset src;
bitset_bindex *list;
bitset_bindex num;
bitset_bindex *next;
{
bitset_bindex bitno;
bitset_bindex rbitno;
bitset_bindex count;
bitset_windex index;
unsigned int bitcnt;
bitset_bindex bitoff;
bitset_word word;
bitset_word *srcp = SBITSET_WORDS (src);
bitset_bindex n_bits = SBITSET_N_BITS (src);
rbitno = *next;
/* If num is 1, we could speed things up with a binary search
of the word of interest. */
if (rbitno >= n_bits)
return 0;
count = 0;
bitno = n_bits - (rbitno + 1);
index = bitno / BITSET_WORD_BITS;
bitcnt = bitno % BITSET_WORD_BITS;
bitoff = index * BITSET_WORD_BITS;
for (; index != ~0U; index--, bitoff -= BITSET_WORD_BITS,
bitcnt = BITSET_WORD_BITS - 1)
{
word = srcp[index] << (BITSET_WORD_BITS - 1 - bitcnt);
for (; word; bitcnt--)
{
if (word & BITSET_MSB)
{
list[count++] = bitoff + bitcnt;
if (count >= num)
{
*next = n_bits - (bitoff + bitcnt);
return count;
}
}
word <<= 1;
}
}
*next = n_bits - (bitoff + 1);
return count;
}
/* Find list of up to NUM bits set in BSET starting from and including
*NEXT and store in array LIST. Return with actual number of bits
found and with *NEXT indicating where search stopped. */
static int
sbitset_list (src, list, num, next)
bitset src;
bitset_bindex *list;
bitset_bindex num;
bitset_bindex *next;
{
bitset_bindex bitno;
bitset_bindex count;
bitset_windex index;
bitset_bindex bitoff;
bitset_windex size = src->csize;
bitset_word *srcp = SBITSET_WORDS (src);
bitset_word word;
bitno = *next;
count = 0;
if (! bitno)
{
/* Many bitsets are zero, so make this common case fast. */
for (index = 0; index < size && ! srcp[index]; index++)
continue;
if (index >= size)
return 0;
/* If num is 1, we could speed things up with a binary search
of the current word. */
bitoff = index * BITSET_WORD_BITS;
}
else
{
if (bitno >= SBITSET_N_BITS (src))
return 0;
index = bitno / BITSET_WORD_BITS;
bitno = bitno % BITSET_WORD_BITS;
if (bitno)
{
/* Handle the case where we start within a word.
Most often, this is executed with large bitsets
with many set bits where we filled the array
on the previous call to this function. */
bitoff = index * BITSET_WORD_BITS;
word = srcp[index] >> bitno;
for (bitno = bitoff + bitno; word; bitno++)
{
if (word & 1)
{
list[count++] = bitno;
if (count >= num)
{
*next = bitno + 1;
return count;
}
}
word >>= 1;
}
index++;
}
bitoff = index * BITSET_WORD_BITS;
}
for (; index < size; index++, bitoff += BITSET_WORD_BITS)
{
if (! (word = srcp[index]))
continue;
if ((count + BITSET_WORD_BITS) < num)
{
for (bitno = bitoff; word; bitno++)
{
if (word & 1)
list[count++] = bitno;
word >>= 1;
}
}
else
{
for (bitno = bitoff; word; bitno++)
{
if (word & 1)
{
list[count++] = bitno;
if (count >= num)
{
*next = bitno + 1;
return count;
}
}
word >>= 1;
}
}
}
*next = bitoff;
return count;
}
/* Ensure that any unused bits within the last word are clear. */
static inline void
sbitset_unused_clear (dst)
bitset dst;
{
unsigned int last_bit;
last_bit = SBITSET_N_BITS (dst) % BITSET_WORD_BITS;
if (last_bit)
SBITSET_WORDS (dst)[dst->csize - 1] &= (bitset_word)((1 << last_bit) - 1);
}
static int
sbitset_op1 (dst, op)
bitset dst;
enum bitset_ops op;
{
unsigned int i;
bitset_word *dstp = SBITSET_WORDS (dst);
unsigned int bytes;
bytes = sizeof (bitset_word) * dst->csize;
switch (op)
{
case BITSET_ZERO:
memset (dstp, 0, bytes);
break;
case BITSET_ONES:
memset (dstp, ~0, bytes);
sbitset_unused_clear (dst);
break;
case BITSET_EMPTY_P:
for (i = 0; i < dst->csize; i++)
if (dstp[i])
return 0;
break;
default:
abort ();
}
return 1;
}
static int
sbitset_op2 (dst, src, op)
bitset dst;
bitset src;
enum bitset_ops op;
{
unsigned int i;
bitset_word *srcp = SBITSET_WORDS (src);
bitset_word *dstp = SBITSET_WORDS (dst);
bitset_windex size = dst->csize;
#ifdef ENABLE_CHECKING
/* Check for compatiblity. */
if (src->ops != dst->ops || SBITSET_N_BITS (src) != SBITSET_N_BITS (dst))
abort ();
#endif
switch (op)
{
case BITSET_COPY:
if (srcp == dstp)
break;
memcpy (dstp, srcp, sizeof (bitset_word) * size);
break;
case BITSET_NOT:
for (i = 0; i < size; i++)
*dstp++ = ~(*srcp++);
sbitset_unused_clear (dst);
break;
case BITSET_EQUAL_P:
for (i = 0; i < size; i++)
if (*dstp++ != *srcp++)
return 0;
break;
case BITSET_SUBSET_P:
for (i = 0; i < size; i++, dstp++, srcp++)
{
if (*dstp != (*srcp | *dstp))
return 0;
}
break;
default:
abort ();
}
return 1;
}
static int
sbitset_op3 (dst, src1, src2, op)
bitset dst;
bitset src1;
bitset src2;
enum bitset_ops op;
{
unsigned int i;
int changed = 0;
bitset_word *src1p = SBITSET_WORDS (src1);
bitset_word *src2p = SBITSET_WORDS (src2);
bitset_word *dstp = SBITSET_WORDS (dst);
bitset_windex size = dst->csize;
#ifdef ENABLE_CHECKING
/* Check for compatiblity. */
if (src1->ops != dst->ops || SBITSET_N_BITS (src1) != SBITSET_N_BITS (dst)
|| src2->ops != dst->ops || SBITSET_N_BITS (src2) != SBITSET_N_BITS (dst))
abort ();
#endif
switch (op)
{
case BITSET_OR:
for (i = 0; i < size; i++, dstp++)
{
bitset_word tmp = *src1p++ | *src2p++;
if (*dstp != tmp)
{
changed = 1;
*dstp = tmp;
}
}
break;
case BITSET_AND:
for (i = 0; i < size; i++, dstp++)
{
bitset_word tmp = *src1p++ & *src2p++;
if (*dstp != tmp)
{
changed = 1;
*dstp = tmp;
}
}
break;
case BITSET_XOR:
for (i = 0; i < size; i++, dstp++)
{
bitset_word tmp = *src1p++ ^ *src2p++;
if (*dstp != tmp)
{
changed = 1;
*dstp = tmp;
}
}
break;
case BITSET_ANDN:
for (i = 0; i < size; i++, dstp++)
{
bitset_word tmp = *src1p++ & ~(*src2p++);
if (*dstp != tmp)
{
changed = 1;
*dstp = tmp;
}
}
break;
case BITSET_ORN:
for (i = 0; i < size; i++, dstp++)
{
bitset_word tmp = *src1p++ | ~(*src2p++);
if (*dstp != tmp)
{
changed = 1;
*dstp = tmp;
}
}
sbitset_unused_clear (dst);
break;
default:
abort ();
}
return changed;
}
static int
sbitset_op4 (dst, src1, src2, src3, op)
bitset dst;
bitset src1;
bitset src2;
bitset src3;
enum bitset_ops op;
{
unsigned int i;
int changed = 0;
bitset_word *src1p = SBITSET_WORDS (src1);
bitset_word *src2p = SBITSET_WORDS (src2);
bitset_word *src3p = SBITSET_WORDS (src3);
bitset_word *dstp = SBITSET_WORDS (dst);
bitset_windex size = dst->csize;
#ifdef ENABLE_CHECKING
/* Check for compatiblity. */
if (src1->ops != dst->ops || SBITSET_N_BITS (src1) != SBITSET_N_BITS (dst)
|| src2->ops != dst->ops || SBITSET_N_BITS (src2) != SBITSET_N_BITS (dst)
|| src3->ops != dst->ops || SBITSET_N_BITS (src3) != SBITSET_N_BITS (dst))
abort ();
#endif
switch (op)
{
case BITSET_OR_AND:
for (i = 0; i < size; i++, dstp++)
{
bitset_word tmp = (*src1p++ | *src2p++) & *src3p++;
if (*dstp != tmp)
{
changed = 1;
*dstp = tmp;
}
}
break;
case BITSET_AND_OR:
for (i = 0; i < size; i++, dstp++)
{
bitset_word tmp = (*src1p++ & *src2p++) | *src3p++;
if (*dstp != tmp)
{
changed = 1;
*dstp = tmp;
}
}
break;
case BITSET_ANDN_OR:
for (i = 0; i < size; i++, dstp++)
{
bitset_word tmp = (*src1p++ & ~(*src2p++)) | *src3p++;
if (*dstp != tmp)
{
changed = 1;
*dstp = tmp;
}
}
break;
default:
abort ();
}
return changed;
}
/* Vector of operations for single word bitsets. */
struct bitset_ops_struct sbitset_small_ops =
{
sbitset_set,
sbitset_reset,
sbitset_test,
sbitset_size,
sbitset_op1,
sbitset_op2,
sbitset_op3,
sbitset_op4,
sbitset_small_list,
sbitset_reverse_list,
NULL,
BITSET_ARRAY
};
/* Vector of operations for multiple word bitsets. */
struct bitset_ops_struct sbitset_ops =
{
sbitset_set,
sbitset_reset,
sbitset_test,
sbitset_size,
sbitset_op1,
sbitset_op2,
sbitset_op3,
sbitset_op4,
sbitset_list,
sbitset_reverse_list,
NULL,
BITSET_ARRAY
};
int
sbitset_bytes (n_bits)
bitset_bindex n_bits;
{
unsigned int bytes, size;
size = SBITSET_N_WORDS (n_bits);
bytes = size * sizeof (bitset_word);
return sizeof (struct bitset_struct) + bytes - sizeof (bitset_word);
}
bitset
sbitset_init (bset, n_bits)
bitset bset;
bitset_bindex n_bits;
{
bitset_windex size;
size = SBITSET_N_WORDS (n_bits);
SBITSET_N_BITS (bset) = n_bits;
/* Use optimized routines if bitset fits within a single word.
There is probably little merit if using caching since
the small bitset will always fit in the cache. */
if (size == 1)
bset->ops = &sbitset_small_ops;
else
bset->ops = &sbitset_ops;
bset->cindex = 0;
bset->csize = size;
bset->cdata = SBITSET_WORDS (bset);
return bset;
}

33
lib/sbitset.h Normal file
View File

@@ -0,0 +1,33 @@
/* Functions to support sbitsets.
Copyright (C) 2002 Free Software Foundation, Inc.
Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ifndef _SBITSET_H
#define _SBITSET_H
#include "bitset-int.h"
typedef struct sbitset_struct
{
unsigned int n_bits; /* Number of bits. */
bitset_word words[1]; /* The array of bits. */
} *sbitset;
extern int sbitset_bytes PARAMS ((bitset_bindex));
#endif