cbits 0.3.0
High-performance BitVector C-API & Python binding
 
Loading...
Searching...
No Matches
bitvector.h File Reference

Public C API for the BitVector data structure. More...

#include <stdbool.h>
#include "compat.h"
Include dependency graph for bitvector.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  BitVector
 Packed bit array with rank-support structures. More...
 

Macros

#define BV_ALIGN   64
 Alignment (in bytes) for all BitVector allocations.
 
#define BV_WORDS_SUPER_SHIFT   3
 Log2 of the number of 64‑bit words per superblock.
 
#define BV_WORDS_SUPER   (1u << BV_WORDS_SUPER_SHIFT)
 Number of 64-bit worde in a superblock.
 

Functions

BitVectorbv_new (size_t n_bits)
 Allocate a new BitVector with all bits cleared.
 
BitVectorbv_copy (const BitVector *src)
 Make a copy of an existing BitVector.
 
void bv_free (BitVector *bv)
 Free all memory associated with a BitVector.
 
void bv_set_range (BitVector *bv, size_t start, size_t len)
 Set all bits in the half-open range [start, start+len).
 
void bv_clear_range (BitVector *bv, size_t start, size_t len)
 Clear all bits in the half-open range [start, start+len).
 
void bv_flip_range (BitVector *bv, size_t start, size_t len)
 Toggle (flip) all bits in the half-open range [start, start+len).
 
void bv_build_rank (BitVector *bv)
 Build or rebuild the rank tables for a BitVector.
 
size_t bv_rank (BitVector *bv, const size_t pos)
 Compute the rank (number of set bits) up to a position.
 
bool bv_equal (const BitVector *a, const BitVector *b)
 Test equality of two BitVectors.
 
bool bv_contains_subvector (const BitVector *a, const BitVector *b)
 Check weather B appears as a contiguous sub-bitvector of A.
 
int bv_get (const BitVector *bv, const size_t pos)
 Get the bit value at a given position.
 
void bv_set (BitVector *bv, const size_t pos)
 Set the bit at a given position (set to 1)
 
void bv_clear (BitVector *bv, const size_t pos)
 Clear the bit at a given position (set to 0)
 
void bv_flip (BitVector *bv, const size_t pos)
 Toggle (flip) the bit at a given position.
 
BitVectorbv_concat (const BitVector *a, const BitVector *b)
 Concatenate two BitVectors into a new BitVector.
 
BitVectorbv_repeat (const BitVector *bv, const size_t count)
 Repeat a BitVector n times.
 

Detailed Description

Public C API for the BitVector data structure.

Declares the stable, external-facing API for working with BitVectors:

The public API is intentionally minimal. Internal helpers, inline fast‑paths, and low‑level utilities are defined separately in bitvector_internal.h.

BitVector provides a compact, cache‑friendly bit array with optional auxiliary rank tables enabling O(1) prefix‑popcount queries.

Author
lambdaphoenix
Version
0.3.0

Macro Definition Documentation

◆ BV_ALIGN

#define BV_ALIGN   64

Alignment (in bytes) for all BitVector allocations.

Ensures that the underlying word array is aligned for efficient SIMD and cache‑line access.

◆ BV_WORDS_SUPER_SHIFT

#define BV_WORDS_SUPER_SHIFT   3

Log2 of the number of 64‑bit words per superblock.

Used to compute superblock indices via bit‑shifts rather than division.

Function Documentation

◆ bv_build_rank()

void bv_build_rank ( BitVector bv)

Build or rebuild the rank tables for a BitVector.

This populates super_rank[] and block_rank[] to support O(1) rank queries. After this call, bv->rank_dirty is cleared.

Parameters
bvPointer to the BitVector whose tables to build

◆ bv_clear()

void bv_clear ( BitVector bv,
const size_t  pos 
)

Clear the bit at a given position (set to 0)

Marks the rank table dirty so it will be rebuilt on next rank query.

Parameters
bvPointer to the BitVector
posBit index

◆ bv_clear_range()

void bv_clear_range ( BitVector bv,
size_t  start,
size_t  len 
)

Clear all bits in the half-open range [start, start+len).

Marks the rank table dirty so it will be rebuilt on next rank query.

Parameters
bvPointer to the BitVector
startStart bit index
lenNumber of bits to clear
Since
0.2.0

◆ bv_concat()

BitVector * bv_concat ( const BitVector a,
const BitVector b 
)

Concatenate two BitVectors into a new BitVector.

Produces a new BitVector whose bits are A followed by B. Returns NULL on allocation failure.

Parameters
aLeft operand.
bRight operand.
Return values
objectNew BitVector on success.
NULLon failure.
Since
0.3.0

◆ bv_contains_subvector()

bool bv_contains_subvector ( const BitVector a,
const BitVector b 
)

Check weather B appears as a contiguous sub-bitvector of A.

That is, whether there exists an offset i in A such that A[i..i+|B|-1] == B[0..|B|-1] .

Parameters
aHaystack BitVector
bNeedle BitVector
Returns
true if b is contained in a, false otherwise

◆ bv_copy()

BitVector * bv_copy ( const BitVector src)

Make a copy of an existing BitVector.

The copy shares no memory with the source; all bits and rank tables are reinitialized.

Parameters
srcPointer to the source BitVector
Return values
BitVector*Newly allocated BitVector copy.
NULLFailure.

◆ bv_equal()

bool bv_equal ( const BitVector a,
const BitVector b 
)

Test equality of two BitVectors.

Only vectors with the same length can compare equal.

Parameters
aFirst BitVector
bSecond BitVector
Returns
true if length and all words are identical, false otherwise

◆ bv_flip()

void bv_flip ( BitVector bv,
const size_t  pos 
)

Toggle (flip) the bit at a given position.

Marks the rank table dirty so it will be rebuilt on next rank query.

Parameters
bvPointer to the BitVector
posBit index

◆ bv_flip_range()

void bv_flip_range ( BitVector bv,
size_t  start,
size_t  len 
)

Toggle (flip) all bits in the half-open range [start, start+len).

Marks the rank table dirty so it will be rebuilt on next rank query.

Parameters
bvPointer to the BitVector
startStart bit index
lenNumber of bits to flip
Since
0.2.0

◆ bv_free()

void bv_free ( BitVector bv)

Free all memory associated with a BitVector.

Parameters
bvPointer to the BitVector to free

◆ bv_get()

int bv_get ( const BitVector bv,
const size_t  pos 
)

Get the bit value at a given position.

Parameters
bvPointer to the BitVector
posBit index
Returns
0 or 1 depending on the bit value

◆ bv_new()

BitVector * bv_new ( size_t  n_bits)

Allocate a new BitVector with all bits cleared.

Parameters
n_bitsNumber of bits to allocate.
Return values
BitVector*Newly allocated BitVector.
NULLAllocation failure.

◆ bv_rank()

size_t bv_rank ( BitVector bv,
const size_t  pos 
)

Compute the rank (number of set bits) up to a position.

If the internal rank tables are dirty, they will be rebuilt.

Parameters
bvPointer to the BitVector
posBit index
Returns
Number of bits set in range [0...pos)

◆ bv_repeat()

BitVector * bv_repeat ( const BitVector bv,
const size_t  count 
)

Repeat a BitVector n times.

Produces a new BitVector whose bit pattern is the original repeated count times. Returns NULL on allocation failure.

Parameters
bvBitVector instance.
countRepeat count.
Return values
objectNew BitVector on success.
NULLon error (exception set).
Since
0.3.0

◆ bv_set()

void bv_set ( BitVector bv,
const size_t  pos 
)

Set the bit at a given position (set to 1)

Marks the rank table dirty so it will be rebuilt on next rank query.

Parameters
bvPointer to the BitVector
posBit index

◆ bv_set_range()

void bv_set_range ( BitVector bv,
size_t  start,
size_t  len 
)

Set all bits in the half-open range [start, start+len).

Marks the rank table dirty so it will be rebuilt on next rank query.

Parameters
bvPointer to the BitVector
startStart bit index
lenNumber of bits to set
Since
0.2.0