bitvec.h
Go to the documentation of this file.
1 #pragma once
2 
11 
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <stdbool.h>
15 
16 #include <crater/container.h>
17 #include <crater/prand.h>
18 
22 typedef struct{
24  uint64_t *buf;
26  uint64_t len;
28  uint64_t cap;
29 } cr8r_bvec;
30 
35 typedef struct cr8r_bvec_ft cr8r_bvec_ft;
36 struct cr8r_bvec_ft{
45  uint64_t (*new_size)(cr8r_base_ft*, uint64_t cap);
52  void *(*resize)(cr8r_base_ft*, void *p, uint64_t cap);
53 };
54 
55 
60 bool cr8r_bvec_init(cr8r_bvec*, cr8r_bvec_ft*, uint64_t bits);
61 
67 
68 
76 bool cr8r_bvec_copy(cr8r_bvec *dest, const cr8r_bvec *src, cr8r_bvec_ft*);
77 
87 bool cr8r_bvec_sub(cr8r_bvec *dest, const cr8r_bvec *src, cr8r_bvec_ft*, uint64_t a, uint64_t b);
88 
89 
95 bool cr8r_bvec_resize(cr8r_bvec*, cr8r_bvec_ft*, uint64_t bits);
96 
102 
103 
106 
107 
113 
114 
119 bool cr8r_bvec_get(cr8r_bvec*, uint64_t i);
120 
125 bool cr8r_bvec_getu(cr8r_bvec*, uint64_t i);
126 
132 bool cr8r_bvec_getx(cr8r_bvec*, int64_t i);
133 
139 bool cr8r_bvec_getux(cr8r_bvec*, int64_t i);
140 
145 bool cr8r_bvec_set(cr8r_bvec*, uint64_t i, bool b);
146 
150 void cr8r_bvec_setu(cr8r_bvec*, uint64_t i, bool b);
151 
157 bool cr8r_bvec_setx(cr8r_bvec*, int64_t i, bool b);
158 
163 void cr8r_bvec_setux(cr8r_bvec*, int64_t i, bool b);
164 
170 
171 
172 
179 
185 bool cr8r_bvec_popr(cr8r_bvec*, int *status);
186 
193 
199 bool cr8r_bvec_popl(cr8r_bvec*, int *status);
200 
201 
210 int cr8r_bvec_forEachPermutation(cr8r_bvec*, void (*f)(const cr8r_bvec*, void *data), void *data);
211 
212 
221 bool cr8r_bvec_combine(cr8r_bvec *dest, const cr8r_bvec *src_a, const cr8r_bvec *src_b, cr8r_bvec_ft*);
222 
229 bool cr8r_bvec_augment(cr8r_bvec *self, const cr8r_bvec *other, cr8r_bvec_ft*);
230 
231 
234 
237 
239 uint64_t cr8r_bvec_popcount(const cr8r_bvec*);
240 
242 uint64_t cr8r_bvec_clz(const cr8r_bvec*);
243 
245 uint64_t cr8r_bvec_ctz(const cr8r_bvec*);
246 
248 uint64_t cr8r_bvec_clo(const cr8r_bvec*);
249 
251 uint64_t cr8r_bvec_cto(const cr8r_bvec*);
252 
253 // TODO: add <<, >>, rotations, reverse
254 
257 
260 void cr8r_bvec_iand(cr8r_bvec *self, const cr8r_bvec *other);
261 
264 void cr8r_bvec_ior(cr8r_bvec *self, const cr8r_bvec *other);
265 
268 void cr8r_bvec_ixor(cr8r_bvec *self, const cr8r_bvec *other);
269 
272 bool cr8r_bvec_any_range(const cr8r_bvec*, uint64_t a, uint64_t b);
273 
276 bool cr8r_bvec_all_range(const cr8r_bvec*, uint64_t a, uint64_t b);
277 
282 bool cr8r_bvec_set_range(cr8r_bvec*, uint64_t a, uint64_t b, bool v);
283 
288 int cr8r_bvec_cmp(const cr8r_bvec *a, const cr8r_bvec *b);
289 
295 
void cr8r_bvec_ior(cr8r_bvec *self, const cr8r_bvec *other)
Bitwise or (|=) a bit vector with another in place If the other vector is shorter,...
bool cr8r_bvec_set(cr8r_bvec *, uint64_t i, bool b)
Set the bit at a given index WITH bounds checking.
bool cr8r_bvec_trim(cr8r_bvec *, cr8r_bvec_ft *)
Resize a bit vector's reserved buffer to its length.
void cr8r_bvec_clear(cr8r_bvec *)
Sets len to 0.
bool cr8r_bvec_augment(cr8r_bvec *self, const cr8r_bvec *other, cr8r_bvec_ft *)
Add a copy of another bit vector to a given bit vector.
uint64_t cr8r_bvec_cto(const cr8r_bvec *)
Count trailing ones (starting from the smallest index 0)
bool cr8r_bvec_resize(cr8r_bvec *, cr8r_bvec_ft *, uint64_t bits)
Resize a bit vector's reserved buffer.
uint64_t cr8r_bvec_len(cr8r_bvec *)
Get the length of a bit vector.
bool cr8r_bvec_pushl(cr8r_bvec *, cr8r_bvec_ft *, bool b)
Add a bit to the left hand end of a bit vector.
bool cr8r_bvec_set_range(cr8r_bvec *, uint64_t a, uint64_t b, bool v)
Set all bits in a range WITH bounds checking.
bool cr8r_bvec_combine(cr8r_bvec *dest, const cr8r_bvec *src_a, const cr8r_bvec *src_b, cr8r_bvec_ft *)
Create a new bit vector by concatenating copies of two given bit vectors.
bool cr8r_bvec_getux(cr8r_bvec *, int64_t i)
Get the element at a given index, with support for negative indices, WITHOUT bounds checking.
bool cr8r_bvec_popr(cr8r_bvec *, int *status)
Remove a bit from the right hand end of a bit vector.
bool cr8r_bvec_all(const cr8r_bvec *)
Test if all bits are set.
bool cr8r_bvec_copy(cr8r_bvec *dest, const cr8r_bvec *src, cr8r_bvec_ft *)
Create a copy of a bit vector.
int cr8r_bvec_cmp(const cr8r_bvec *a, const cr8r_bvec *b)
Lexicographically compare two bit vectors.
void cr8r_bvec_setu(cr8r_bvec *, uint64_t i, bool b)
Set the bit at a given index WITHOUT bounds checking.
uint64_t cr8r_bvec_popcount(const cr8r_bvec *)
Count the number of bits set.
bool cr8r_bvec_any(const cr8r_bvec *)
Test if any bits.
uint64_t cr8r_bvec_ctz(const cr8r_bvec *)
Count trailing zeros (starting from the smallest index 0)
bool cr8r_bvec_pushr(cr8r_bvec *, cr8r_bvec_ft *, bool b)
Add a bit to the right hand end of a bit vector.
int cr8r_bvec_forEachPermutation(cr8r_bvec *, void(*f)(const cr8r_bvec *, void *data), void *data)
Execute a function on every permutation of a bit vector.
bool cr8r_bvec_getu(cr8r_bvec *, uint64_t i)
Get the bit at a given index WITHOUT bounds checking.
bool cr8r_bvec_any_range(const cr8r_bvec *, uint64_t a, uint64_t b)
Test if any bit in the range [a, b) is set If the range is invalid, 0 is returned.
void cr8r_bvec_icompl(cr8r_bvec *)
Bitwise negate (~) a bit vector in place.
bool cr8r_bvec_sub(cr8r_bvec *dest, const cr8r_bvec *src, cr8r_bvec_ft *, uint64_t a, uint64_t b)
Create a copy of a slice of a bit vector.
bool cr8r_bvec_get(cr8r_bvec *, uint64_t i)
Get the bit at a given index WITH bounds checking.
bool cr8r_bvec_popl(cr8r_bvec *, int *status)
Remove a bit from the left hand end of a bit vector.
bool cr8r_bvec_all_range(const cr8r_bvec *, uint64_t a, uint64_t b)
Test if all bits in the range [a, b) are set If the range is invalid, 0 is returned.
bool cr8r_bvec_getx(cr8r_bvec *, int64_t i)
Get the element at a given index, with support for negative indices, WITH bounds checking.
void cr8r_bvec_shuffle(cr8r_bvec *, cr8r_prng *)
Shuffle the bit vector into a random permutation.
bool cr8r_bvec_init(cr8r_bvec *, cr8r_bvec_ft *, uint64_t bits)
Initialize a bit vector with an empty buffer of a given capacity.
bool cr8r_bvec_setx(cr8r_bvec *, int64_t i, bool b)
Set the element at a given index, with support for negative indices, WITH bounds checking.
void cr8r_bvec_iand(cr8r_bvec *self, const cr8r_bvec *other)
Bitwise and (&=) a bit vector with another in place If the other vector is shorter,...
uint64_t cr8r_bvec_clo(const cr8r_bvec *)
Count leading ones (starting from the largest index self->len - 1)
cr8r_bvec_ft cr8r_bvecft
Function table for bit vectors.
void cr8r_bvec_setux(cr8r_bvec *, int64_t i, bool b)
Set the element at a given index, with support for negative indices, WITHOUT bounds checking.
uint64_t cr8r_bvec_clz(const cr8r_bvec *)
Count leading zeros (starting from the largest index self->len - 1)
void cr8r_bvec_delete(cr8r_bvec *, cr8r_bvec_ft *)
Free the buffer of a bit vector.
void cr8r_bvec_ixor(cr8r_bvec *self, const cr8r_bvec *other)
Bitwise xor (^=) a bit vector with another in place If the other vector is shorter,...
Base function table for all Crater containers.
Definition: container.h:22
Function table for bit vector.
Definition: bitvec.h:36
cr8r_base_ft base
Base function table values (data and size).
Definition: bitvec.h:41
uint64_t(* new_size)(cr8r_base_ft *, uint64_t cap)
determines the capacity to grow to if the bit vector is full but needs to have additional elements ad...
Definition: bitvec.h:45
A bit vector Take care if manipulating these fields directly, this should only be done if the functio...
Definition: bitvec.h:22
uint64_t * buf
underlying storage for vector. Managed by ft->resize.
Definition: bitvec.h:24
uint64_t cap
capacity of buf (in number of bits)
Definition: bitvec.h:28
uint64_t len
number of bits in the vector.
Definition: bitvec.h:26
A PseudoRandom Number Generator.
Definition: prand.h:105