cbits 0.2.0
High-performance BitVector C-API & Python binding
Loading...
Searching...
No Matches
bitvector.h
Go to the documentation of this file.
1
13#ifndef CBITS_BITVECTOR_H
14#define CBITS_BITVECTOR_H
15
16#include "compat.h"
17#include <stdbool.h>
18
23#define BV_ALIGN 64
29#define BV_WORDS_SUPER_SHIFT 3
34#define BV_WORDS_SUPER (1u << BV_WORDS_SUPER_SHIFT)
35
41static inline size_t
42bv_word(const size_t pos)
43{
44 return pos >> 6;
45}
51static inline size_t
52bv_bit(const size_t pos)
53{
54 return pos & 63;
55}
56
64typedef struct {
65 uint64_t *data;
66 size_t n_bits;
67 size_t n_words;
68 size_t
70 uint16_t *block_rank;
72} BitVector;
73
79static inline void
81{
82 if (!bv->n_words) {
83 return;
84 }
85 unsigned tail = (unsigned) (bv->n_bits & 63);
86 if (tail) {
87 uint64_t mask = (UINT64_C(1) << tail) - 1;
88 bv->data[bv->n_words - 1] &= mask;
89 }
90}
91
98bv_new(size_t n_bits);
107BitVector *
108bv_copy(const BitVector *src);
113void
114bv_free(BitVector *bv);
121static inline int
122bv_get(const BitVector *bv, const size_t pos)
123{
124 return (bv->data[bv_word(pos)] >> bv_bit(pos)) & 1;
125}
133static inline void
134bv_set(BitVector *bv, const size_t pos)
135{
136 uint64_t mask = 1ULL << bv_bit(pos);
137 bv->data[bv_word(pos)] |= mask;
138 bv->rank_dirty = true;
139}
147static inline void
148bv_clear(BitVector *bv, const size_t pos)
149{
150 uint64_t mask = ~(1ULL << bv_bit(pos));
151 bv->data[bv_word(pos)] &= mask;
152 bv->rank_dirty = true;
153}
161static inline void
162bv_flip(BitVector *bv, const size_t pos)
163{
164 uint64_t mask = 1ULL << bv_bit(pos);
165 bv->data[bv_word(pos)] ^= mask;
166 bv->rank_dirty = true;
167}
177void
178bv_set_range(BitVector *bv, size_t start, size_t len);
188void
189bv_clear_range(BitVector *bv, size_t start, size_t len);
199void
200bv_flip_range(BitVector *bv, size_t start, size_t len);
208void
218size_t
219bv_rank(BitVector *bv, const size_t pos);
228bool
229bv_equal(const BitVector *a, const BitVector *b);
239bool
240bv_contains_subvector(const BitVector *a, const BitVector *b);
241
242#endif /* CBITS_BITVECTOR_H */
void bv_set_range(BitVector *bv, size_t start, size_t len)
Set all bits in the half‑open range [start, start+len).
Definition bitvector.c:94
void bv_free(BitVector *bv)
Free all memory associated with a BitVector.
Definition bitvector.c:85
static void bv_flip(BitVector *bv, const size_t pos)
Toggle (flip) the bit at a given position.
Definition bitvector.h:162
void bv_flip_range(BitVector *bv, size_t start, size_t len)
Toggle (flip) all bits in the half‑open range [start, start+len).
Definition bitvector.c:163
bool bv_contains_subvector(const BitVector *a, const BitVector *b)
Check weather B appears as a contiguous sub-bitvector of A.
Definition bitvector.c:280
static void bv_apply_tail_mask(BitVector *bv)
Mask off any excess bits in the last word of a BitVector.
Definition bitvector.h:80
void bv_build_rank(BitVector *bv)
Build or rebuild the rank tables for a BitVector.
Definition bitvector.c:198
BitVector * bv_new(size_t n_bits)
Allocate a new BitVector with all bits cleared.
Definition bitvector.c:35
void bv_clear_range(BitVector *bv, size_t start, size_t len)
Clear all bits in the half‑open range [start, start+len).
Definition bitvector.c:129
static void bv_set(BitVector *bv, const size_t pos)
Set the bit at a given position (set to 1)
Definition bitvector.h:134
static void bv_clear(BitVector *bv, const size_t pos)
Clear the bit at a given position (set to 0)
Definition bitvector.h:148
static size_t bv_word(const size_t pos)
Compute the word index for a given bit position.
Definition bitvector.h:42
BitVector * bv_copy(const BitVector *src)
Make a copy of an existing BitVector.
Definition bitvector.c:72
static size_t bv_bit(const size_t pos)
Compute the bit offset within its 64-bit word.
Definition bitvector.h:52
bool bv_equal(const BitVector *a, const BitVector *b)
Test equality of two BitVectors.
Definition bitvector.c:265
static int bv_get(const BitVector *bv, const size_t pos)
Get the bit value at a given position.
Definition bitvector.h:122
size_t bv_rank(BitVector *bv, const size_t pos)
Compute the rank (number of set bits) up to a position.
Definition bitvector.c:250
Cross-platform aligned allocators, popcount, prefetch.
Packed array of bits with support for rank/select operations.
Definition bitvector.h:64
uint64_t * data
Definition bitvector.h:65
size_t n_bits
Definition bitvector.h:66
uint16_t * block_rank
Definition bitvector.h:70
size_t * super_rank
Definition bitvector.h:69
size_t n_words
Definition bitvector.h:67
bool rank_dirty
Definition bitvector.h:71