1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
/*
* bitset.c - Arbitrary-length bit sets
*
* Written 2010 by Werner Almesberger
* Copyright 2010 by Werner Almesberger
*
* 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.
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include "util.h"
#include "bitset.h"
struct bitset {
int v_n;
unsigned *v;
};
#define BITS (sizeof(unsigned)*8)
struct bitset *bitset_new(int n)
{
struct bitset *new;
new = alloc_type(struct bitset);
new->v_n = (n+BITS-1) & ~(BITS-1);
new->v = zalloc_size(sizeof(unsigned)*new->v_n);
return new;
}
struct bitset *bitset_clone(const struct bitset *old)
{
struct bitset *new;
size_t bytes;
new = alloc_type(struct bitset);
bytes = sizeof(unsigned)*old->v_n;
new->v_n = old->v_n;
new->v = alloc_size(bytes);
memcpy(new->v, old->v, bytes);
return new;
}
void bitset_free(struct bitset *set)
{
free(set->v);
free(set);
}
void bitset_set(struct bitset *set, int n)
{
assert(n < set->v_n*BITS);
set->v[n/BITS] |= 1U << (n % BITS);
}
void bitset_clear(struct bitset *set, int n)
{
assert(n < set->v_n*BITS);
set->v[n/BITS] &= ~(1U << (n % BITS));
}
int bitset_pick(const struct bitset *set, int n)
{
assert(n < set->v_n*BITS);
return !!(set->v[n/BITS] & (1U << (n % BITS)));
}
int bitset_is_empty(const struct bitset *set)
{
int i;
for (i = 0; i != set->v_n; i++)
if (set->v[i])
return 0;
return 1;
}
void bitset_zero(struct bitset *a)
{
int i;
for (i = 0; i != a->v_n; i++)
a->v[i] = 0;
}
void bitset_and(struct bitset *a, const struct bitset *b)
{
int i;
assert(a->v_n == b->v_n);
for (i = 0; i != a->v_n; i++)
a->v[i] &= b->v[i];
}
void bitset_or(struct bitset *a, const struct bitset *b)
{
int i;
assert(a->v_n == b->v_n);
for (i = 0; i != a->v_n; i++)
a->v[i] |= b->v[i];
}
int bitset_ge(const struct bitset *a, const struct bitset *b)
{
int i;
assert(a->v_n == b->v_n);
for (i = 0; i != a->v_n; i++)
if (~a->v[i] & b->v[i])
return 0;
return 1;
}
|