Crater Container Library 0.2.0
|
|
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
Definition in file bitvec.h.
Go to the source code of this file.
Data Structures | |
struct | cr8r_bvec |
A bit vector Take care if manipulating these fields directly, this should only be done if the functions in this file are not sufficient. More... | |
struct | cr8r_bvec_ft |
Function table for bit vector. More... | |
Functions | |
bool | cr8r_bvec_init (cr8r_bvec *, cr8r_bvec_ft *, uint64_t bits) |
Initialize a bit vector with an empty buffer of a given capacity. More... | |
void | cr8r_bvec_delete (cr8r_bvec *, cr8r_bvec_ft *) |
Free the buffer of a bit vector. More... | |
bool | cr8r_bvec_copy (cr8r_bvec *dest, const cr8r_bvec *src, cr8r_bvec_ft *) |
Create a copy of a bit vector. More... | |
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. More... | |
bool | cr8r_bvec_resize (cr8r_bvec *, cr8r_bvec_ft *, uint64_t bits) |
Resize a bit vector's reserved buffer. More... | |
bool | cr8r_bvec_trim (cr8r_bvec *, cr8r_bvec_ft *) |
Resize a bit vector's reserved buffer to its length. More... | |
void | cr8r_bvec_clear (cr8r_bvec *) |
Sets len to 0. | |
void | cr8r_bvec_shuffle (cr8r_bvec *, cr8r_prng *) |
Shuffle the bit vector into a random permutation. More... | |
bool | cr8r_bvec_get (cr8r_bvec *, uint64_t i) |
Get the bit at a given index WITH bounds checking. More... | |
bool | cr8r_bvec_getu (cr8r_bvec *, uint64_t i) |
Get the bit at a given index WITHOUT bounds checking. More... | |
bool | cr8r_bvec_getx (cr8r_bvec *, int64_t i) |
Get the element at a given index, with support for negative indices, WITH bounds checking. More... | |
bool | cr8r_bvec_getux (cr8r_bvec *, int64_t i) |
Get the element at a given index, with support for negative indices, WITHOUT bounds checking. More... | |
bool | cr8r_bvec_set (cr8r_bvec *, uint64_t i, bool b) |
Set the bit at a given index WITH bounds checking. More... | |
void | cr8r_bvec_setu (cr8r_bvec *, uint64_t i, bool b) |
Set the bit at a given index WITHOUT bounds checking. More... | |
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. More... | |
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. More... | |
uint64_t | cr8r_bvec_len (cr8r_bvec *) |
Get the length of a bit vector. More... | |
bool | cr8r_bvec_pushr (cr8r_bvec *, cr8r_bvec_ft *, bool b) |
Add a bit to the right hand end of a bit vector. More... | |
bool | cr8r_bvec_popr (cr8r_bvec *, int *status) |
Remove a bit from the right hand end of a bit vector. More... | |
bool | cr8r_bvec_pushl (cr8r_bvec *, cr8r_bvec_ft *, bool b) |
Add a bit to the left hand end of a bit vector. More... | |
bool | cr8r_bvec_popl (cr8r_bvec *, int *status) |
Remove a bit from the left hand end of a bit vector. More... | |
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. More... | |
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. More... | |
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. More... | |
bool | cr8r_bvec_all (const cr8r_bvec *) |
Test if all bits are set. | |
bool | cr8r_bvec_any (const cr8r_bvec *) |
Test if any bits. | |
uint64_t | cr8r_bvec_popcount (const cr8r_bvec *) |
Count the number of bits set. | |
uint64_t | cr8r_bvec_clz (const cr8r_bvec *) |
Count leading zeros (starting from the largest index self->len - 1) | |
uint64_t | cr8r_bvec_ctz (const cr8r_bvec *) |
Count trailing zeros (starting from the smallest index 0) | |
uint64_t | cr8r_bvec_clo (const cr8r_bvec *) |
Count leading ones (starting from the largest index self->len - 1) | |
uint64_t | cr8r_bvec_cto (const cr8r_bvec *) |
Count trailing ones (starting from the smallest index 0) | |
void | cr8r_bvec_icompl (cr8r_bvec *) |
Bitwise negate (~) a bit vector in place. | |
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, missing bits are treated as zero. | |
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, missing bits are treated as zero. | |
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, missing bits are treated as zero. | |
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. | |
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_set_range (cr8r_bvec *, uint64_t a, uint64_t b, bool v) |
Set all bits in a range WITH bounds checking. More... | |
int | cr8r_bvec_cmp (const cr8r_bvec *a, const cr8r_bvec *b) |
Lexicographically compare two bit vectors. More... | |
Variables | |
cr8r_bvec_ft | cr8r_bvecft |
Function table for bit vectors. More... | |
bool cr8r_bvec_init | ( | cr8r_bvec * | , |
cr8r_bvec_ft * | , | ||
uint64_t | bits | ||
) |
Initialize a bit vector with an empty buffer of a given capacity.
[in] | bits | how many bits to reserve space for initially (will be rounded up to a multiple of 64) |
void cr8r_bvec_delete | ( | cr8r_bvec * | , |
cr8r_bvec_ft * | |||
) |
Free the buffer of a bit vector.
ft->resize is called to "resize" to 0. the fields of the vector are all zeroed out
bool cr8r_bvec_copy | ( | cr8r_bvec * | dest, |
const cr8r_bvec * | src, | ||
cr8r_bvec_ft * | |||
) |
Create a copy of a bit vector.
dest should NOT be initialized or its buffer will be leaked! The copy's capacity is only its length.
[out] | dest | the bit vector to copy TO |
[in] | src | the bit vector to copy FROM |
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.
dest should NOT be initialized or its buffer will be leaked! Copies the range [ a : b ) from src to dest. The copy's capacity is only its length (b - a).
[out] | dest | the bit vector to copy TO |
[in] | src | the bit vector to copy FROM |
[in] | a,b | inclusive start index and exclusive end index |
bool cr8r_bvec_resize | ( | cr8r_bvec * | , |
cr8r_bvec_ft * | , | ||
uint64_t | bits | ||
) |
Resize a bit vector's reserved buffer.
Can extend or shrink the buffer, but cannot make it smaller than its length.
[in] | cap | the target capacity, should not be less than the current length |
bool cr8r_bvec_trim | ( | cr8r_bvec * | , |
cr8r_bvec_ft * | |||
) |
Resize a bit vector's reserved buffer to its length.
Exactly like cr8r_bvec_resize with self->len as the cap parameter
Shuffle the bit vector into a random permutation.
Actually works by counting the number of bits set and redistributing them.
bool cr8r_bvec_get | ( | cr8r_bvec * | , |
uint64_t | i | ||
) |
Get the bit at a given index WITH bounds checking.
[in] | i | the index to get, should be from 0 inclusive to self->len exclusive |
bool cr8r_bvec_getu | ( | cr8r_bvec * | , |
uint64_t | i | ||
) |
Get the bit at a given index WITHOUT bounds checking.
[in] | i | the index to get, should be from 0 inclusive to self->len exclusive |
bool cr8r_bvec_getx | ( | cr8r_bvec * | , |
int64_t | i | ||
) |
Get the element at a given index, with support for negative indices, WITH bounds checking.
Negative indicies work backwards, with -1 referring to the last element and so on.
[in] | i | the index to get, should be from 0 inclusive to self->len exclusive |
bool cr8r_bvec_getux | ( | cr8r_bvec * | , |
int64_t | i | ||
) |
Get the element at a given index, with support for negative indices, WITHOUT bounds checking.
Negative indicies work backwards, with -1 referring to the last element and so on.
[in] | i | the index to get, should be from 0 inclusive to self->len exclusive |
bool cr8r_bvec_set | ( | cr8r_bvec * | , |
uint64_t | i, | ||
bool | b | ||
) |
Set the bit at a given index WITH bounds checking.
[in] | i | the index to get, should be from 0 inclusive to self->len exclusive |
void cr8r_bvec_setu | ( | cr8r_bvec * | , |
uint64_t | i, | ||
bool | b | ||
) |
Set the bit at a given index WITHOUT bounds checking.
[in] | i | the index to get, should be from 0 inclusive to self->len exclusive |
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.
Negative indicies work backwards, with -1 referring to the last element and so on.
[in] | i | the index to get, should be from 0 inclusive to self->len exclusive |
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.
Negative indicies work backwards, with -1 referring to the last element and so on.
[in] | i | the index to get, should be from 0 inclusive to self->len exclusive |
uint64_t cr8r_bvec_len | ( | cr8r_bvec * | ) |
Get the length of a bit vector.
Simply returns self->len
bool cr8r_bvec_pushr | ( | cr8r_bvec * | , |
cr8r_bvec_ft * | , | ||
bool | b | ||
) |
Add a bit to the right hand end of a bit vector.
This is an O(1) operation
[in] | b | the bit to add |
bool cr8r_bvec_popr | ( | cr8r_bvec * | , |
int * | status | ||
) |
Remove a bit from the right hand end of a bit vector.
Vectors are arranged with increasing indicies at increasing memory addresses, so this is an O(1) operation
[out] | status | if not NULL, *status will be set to 1 on success or 0 on failure (if the bit vector was empty) |
bool cr8r_bvec_pushl | ( | cr8r_bvec * | , |
cr8r_bvec_ft * | , | ||
bool | b | ||
) |
Add a bit to the left hand end of a bit vector.
Avoid if possible.
Don't do this. This is an O(n) operation because bit vectors are arranged with increasing indicies at increasing memory addresses.
[in] | b | the bit to add |
bool cr8r_bvec_popl | ( | cr8r_bvec * | , |
int * | status | ||
) |
Remove a bit from the left hand end of a bit vector.
Avoid if possible.
Don't do this. This is an O(n) operation because bit vectors are arranged with increasing indicies at increasing memory addresses.
[out] | status | if not NULL, *status will be set to 1 on success or 0 on failure (if the bit vector was empty) |
int cr8r_bvec_forEachPermutation | ( | cr8r_bvec * | , |
void(*)(const cr8r_bvec *, void *data) | f, | ||
void * | data | ||
) |
Execute a function on every permutation of a bit vector.
The bit vector is actually permuted in place using Heap's algorithm, and ends at the "last" permutation without being restored.
[in] | f | callback function |
[in,out] | data | reentrant data to pass to the callback, can provide input, output, and persistant state without needing to hijack ft->base.data or similar |
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.
dest should NOT be initialized or its buffer will be leaked! First, ensures the dest buffer has enough capacity for both src bit vectors, extending if necessary, then copy the bit vectors one after the other.
[out] | dest | bit vector in which to store result |
[in] | src_a,src_b | bit vectors to copy elements from |
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.
First, extends self->buf if needed, then copy the other bit vector right after the current end.
[in,out] | self | bit vector to extend with a copy of the other bit vector |
[in] | other | bit vector to copy from |
bool cr8r_bvec_set_range | ( | cr8r_bvec * | , |
uint64_t | a, | ||
uint64_t | b, | ||
bool | v | ||
) |
Set all bits in a range WITH bounds checking.
The bounds are [a, b)
Lexicographically compare two bit vectors.
[in] | a,b | vectors to compare |
|
extern |
Function table for bit vectors.
The default allocation scheme cr8r_default_new_size and cr8r_default_resize is used.