bitset: clean up bitset.c

* lib/bitset.c: Reduce scopes, etc.
This commit is contained in:
Akim Demaille
2018-10-27 18:46:13 +02:00
parent 81df326649
commit fad0f9231b

View File

@@ -24,12 +24,14 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "obstack.h"
#include "abitset.h" #include "abitset.h"
#include "lbitset.h" #include "lbitset.h"
#include "ebitset.h" #include "ebitset.h"
#include "vbitset.h" #include "vbitset.h"
#include "bitset_stats.h" #include "bitset_stats.h"
#include "obstack.h"
const char * const bitset_type_names[] = BITSET_TYPE_NAMES; const char * const bitset_type_names[] = BITSET_TYPE_NAMES;
@@ -42,30 +44,23 @@ bitset_bytes (enum bitset_type type, bitset_bindex n_bits)
if (bitset_stats_enabled) if (bitset_stats_enabled)
return bitset_stats_bytes (); return bitset_stats_bytes ();
size_t bytes;
switch (type) switch (type)
{ {
default: default:
abort (); abort ();
case BITSET_ARRAY: case BITSET_ARRAY:
bytes = abitset_bytes (n_bits); return abitset_bytes (n_bits);
break;
case BITSET_LIST: case BITSET_LIST:
bytes = lbitset_bytes (n_bits); return lbitset_bytes (n_bits);
break;
case BITSET_TABLE: case BITSET_TABLE:
bytes = ebitset_bytes (n_bits); return ebitset_bytes (n_bits);
break;
case BITSET_VARRAY: case BITSET_VARRAY:
bytes = vbitset_bytes (n_bits); return vbitset_bytes (n_bits);
break;
} }
return bytes;
} }
@@ -193,7 +188,7 @@ bitset_type_get (bitset bset)
{ {
enum bitset_type type = BITSET_TYPE_ (bset); enum bitset_type type = BITSET_TYPE_ (bset);
if (type != BITSET_STATS) if (type != BITSET_STATS)
return type; return type;
return bitset_stats_type_get (bset); return bitset_stats_type_get (bset);
} }
@@ -214,9 +209,8 @@ bitset_type_name_get (bitset bset)
bitset_bindex bitset_bindex
bitset_next (bitset src, bitset_bindex bitno) bitset_next (bitset src, bitset_bindex bitno)
{ {
bitset_bindex val;
bitset_bindex next = bitno; bitset_bindex next = bitno;
bitset_bindex val;
if (!bitset_list (src, &val, 1, &next)) if (!bitset_list (src, &val, 1, &next))
return BITSET_BINDEX_MAX; return BITSET_BINDEX_MAX;
return val; return val;
@@ -236,9 +230,8 @@ bitset_compatible_p (bitset bset1, bitset bset2)
bitset_bindex bitset_bindex
bitset_prev (bitset src, bitset_bindex bitno) bitset_prev (bitset src, bitset_bindex bitno)
{ {
bitset_bindex val;
bitset_bindex next = bitno; bitset_bindex next = bitno;
bitset_bindex val;
if (!bitset_list_reverse (src, &val, 1, &next)) if (!bitset_list_reverse (src, &val, 1, &next))
return BITSET_BINDEX_MAX; return BITSET_BINDEX_MAX;
return val; return val;
@@ -295,7 +288,7 @@ bitset_print (FILE *file, bitset bset, bool verbose)
fprintf (file, "%lu ", (unsigned long) i); fprintf (file, "%lu ", (unsigned long) i);
pos += 1 + (i >= 10) + (i >= 100); pos += 1 + (i >= 10) + (i >= 100);
}; }
if (verbose) if (verbose)
fprintf (file, "}\n"); fprintf (file, "}\n");
@@ -351,17 +344,18 @@ bitset_bindex
bitset_count_ (bitset src) bitset_count_ (bitset src)
{ {
bitset_bindex list[BITSET_LIST_SIZE]; bitset_bindex list[BITSET_LIST_SIZE];
bitset_bindex num; bitset_bindex count = 0;
bitset_bindex count;
/* This could be greatly sped up by adding a count method for each /* This could be greatly sped up by adding a count method for each
bitset implementation that uses a direct technique (based on bitset implementation that uses a direct technique (based on
masks) for counting the number of bits set in a word. */ masks) for counting the number of bits set in a word. */
bitset_bindex next = 0; {
for (count = 0; (num = bitset_list (src, list, BITSET_LIST_SIZE, &next)); bitset_bindex next = 0;
count += num) bitset_bindex num;
continue; while (num = bitset_list (src, list, BITSET_LIST_SIZE, &next))
count += num;
}
return count; return count;
} }
@@ -382,7 +376,7 @@ bitset_copy_ (bitset dst, bitset src)
BITSET_FOR_EACH (iter, src, i, 0) BITSET_FOR_EACH (iter, src, i, 0)
{ {
bitset_set (dst, i); bitset_set (dst, i);
}; }
return true; return true;
} }