C Container Collection (CCC)
|
The Bit Set Interface. More...
Go to the source code of this file.
Container Initialization | |
Initialize and create containers with memory and permissions. | |
#define | ccc_bs_blocks(bit_cap) ccc_impl_bs_blocks(bit_cap) |
Get the number of bit blocks needed for the desired bit set capacity. | |
#define | ccc_bs_init(bitblock_ptr, alloc_fn, aux, cap, optional_size...) ccc_impl_bs_init(bitblock_ptr, alloc_fn, aux, cap, optional_size) |
Initialize the bit set with memory and allocation permissions. | |
ccc_result | ccc_bs_copy (ccc_bitset *dst, ccc_bitset const *src, ccc_alloc_fn *fn) |
Copy the bit set at source to destination. | |
Container Types | |
Types available in the container interface. | |
typedef struct ccc_bitset_ | ccc_bitset |
The bit set type that may be stored and initialized on the stack, heap, or data segment at compile time or runtime. | |
typedef ccc_bitblock_ | ccc_bitblock |
The type used to efficiently store bits in the bit set. | |
Bit Membership Interface | |
Test for the presence of bits. | |
ccc_tribool | ccc_bs_test (ccc_bitset const *bs, size_t i) |
Test the bit at index i for boolean status (CCC_TRUE or CCC_FALSE). | |
Bit Modification Interface | |
Set and flip bits in the set. | |
ccc_tribool | ccc_bs_set (ccc_bitset *bs, size_t i, ccc_tribool b) |
Set the bit at valid index i to value b (true or false). | |
ccc_result | ccc_bs_set_all (ccc_bitset *bs, ccc_tribool b) |
Set all the bits to the provided value (CCC_TRUE or CCC_FALSE). | |
ccc_result | ccc_bs_set_range (ccc_bitset *bs, size_t i, size_t count, ccc_tribool b) |
Set all the bits in the specified range starting at the Least Significant Bit of the range and ending at the Most Significant Bit of the range (CCC_TRUE or CCC_FALSE). | |
ccc_tribool | ccc_bs_reset (ccc_bitset *bs, size_t i) |
Set the bit at valid index i to boolean value b (true or false). | |
ccc_result | ccc_bs_reset_all (ccc_bitset *bs) |
Set all the bits to CCC_FALSE. | |
ccc_result | ccc_bs_reset_range (ccc_bitset *bs, size_t i, size_t count) |
Set all the bits in the specified range starting at the Least Significant Bit of the range and ending at the Most Significant Bit of the range to CCC_FALSE. | |
ccc_tribool | ccc_bs_flip (ccc_bitset *bs, size_t i) |
Toggle the bit at index i. | |
ccc_result | ccc_bs_flip_all (ccc_bitset *bs) |
Toggle all of the bits to their opposing boolean value. | |
ccc_result | ccc_bs_flip_range (ccc_bitset *bs, size_t i, size_t count) |
Flip all the bits in the range, starting at the Least Significant Bit in range and ending at the Most Significant Bit, to their opposite value. | |
Bit Scan Interface | |
Find bits with a specific status. | |
ccc_tribool | ccc_bs_any (ccc_bitset const *bs) |
Return true if any bits in set are 1. | |
ccc_tribool | ccc_bs_any_range (ccc_bitset const *bs, size_t i, size_t count) |
Return true if any bits are 1 in the specified range. | |
ccc_tribool | ccc_bs_none (ccc_bitset const *bs) |
Return true if all bits are set to 0. | |
ccc_tribool | ccc_bs_none_range (ccc_bitset const *bs, size_t i, size_t count) |
Return true if all bits are 0 in the specified range. | |
ccc_tribool | ccc_bs_all (ccc_bitset const *bs) |
Return true if all bits in set are 1. | |
ccc_tribool | ccc_bs_all_range (ccc_bitset const *bs, size_t i, size_t count) |
Return true if all bits are set to 1 in the specified range. | |
ccc_ucount | ccc_bs_first_trailing_one (ccc_bitset const *bs) |
Return the index of the first trailing bit set to 1 in the set. | |
ccc_ucount | ccc_bs_first_trailing_one_range (ccc_bitset const *bs, size_t i, size_t count) |
Return the index of the first trailing bit set to 1 in the range, starting from the Least Significant Bit at index 0. | |
ccc_ucount | ccc_bs_first_trailing_ones (ccc_bitset const *bs, size_t num_ones) |
Returns the index of the start of the first trailing num_ones contiguous 1 bits. | |
ccc_ucount | ccc_bs_first_trailing_ones_range (ccc_bitset const *bs, size_t i, size_t count, size_t num_ones) |
Returns the index of the start of the first trailing num_ones contiguous 1 bits in the specified range. | |
ccc_ucount | ccc_bs_first_trailing_zero (ccc_bitset const *bs) |
Return the index of the first bit set to 0 in the set. | |
ccc_ucount | ccc_bs_first_trailing_zero_range (ccc_bitset const *bs, size_t i, size_t count) |
Return the index of the first bit set to 0 in the range. | |
ccc_ucount | ccc_bs_first_trailing_zeros (ccc_bitset const *bs, size_t num_zeros) |
Returns the index of the start of the first trailing num_zeros contiguous 0 bits. | |
ccc_ucount | ccc_bs_first_trailing_zeros_range (ccc_bitset const *bs, size_t i, size_t count, size_t num_zeros) |
Returns the index of the start of the first trailing num_zeros contiguous 0 bits in the specified range. | |
ccc_ucount | ccc_bs_first_leading_one (ccc_bitset const *bs) |
Return the index of the first leading bit set to 1 in the set, starting from the Most Significant Bit at index size - 1. | |
ccc_ucount | ccc_bs_first_leading_one_range (ccc_bitset const *bs, size_t i, size_t count) |
Return the index of the first leading bit set to 1 in the set, starting from the Most Significant Bit at index size - 1. | |
ccc_ucount | ccc_bs_first_leading_ones (ccc_bitset const *bs, size_t num_ones) |
Returns the index of the start of the first leading num_ones contiguous 1 bits. | |
ccc_ucount | ccc_bs_first_leading_ones_range (ccc_bitset const *bs, size_t i, size_t count, size_t num_ones) |
Returns the index of the start of the first leading num_ones contiguous 1 bits in the specified range. | |
ccc_ucount | ccc_bs_first_leading_zero (ccc_bitset const *bs) |
Return the index of the first leading bit set to 0 in the set, starting from the Most Significant Bit at index size - 1. | |
ccc_ucount | ccc_bs_first_leading_zero_range (ccc_bitset const *bs, size_t i, size_t count) |
Return the index of the first leading bit set to 0 in the set, starting from the Most Significant Bit at index size - 1. | |
ccc_ucount | ccc_bs_first_leading_zeros (ccc_bitset const *bs, size_t num_zeros) |
Returns the index of the start of the first leading num_zeros contiguous 0 bits. | |
ccc_ucount | ccc_bs_first_leading_zeros_range (ccc_bitset const *bs, size_t i, size_t count, size_t num_zeros) |
Returns the index of the start of the first leading num_zeros contiguous 0 bits in the specified range. | |
Bit Operations Interface | |
Use standard integer width bit operations on the entire set. | |
ccc_result | ccc_bs_or (ccc_bitset *dst, ccc_bitset const *src) |
Bitwise OR dst set with src set. | |
ccc_result | ccc_bs_and (ccc_bitset *dst, ccc_bitset const *src) |
Bitwise AND dst set with src set. | |
ccc_result | ccc_bs_xor (ccc_bitset *dst, ccc_bitset const *src) |
Bitwise XOR dst set with src set. | |
ccc_result | ccc_bs_shiftl (ccc_bitset *bs, size_t left_shifts) |
Shift the bit set left by left_shifts amount. | |
ccc_result | ccc_bs_shiftr (ccc_bitset *bs, size_t right_shifts) |
Shift the bit set right by right_shifts amount. | |
ccc_tribool | ccc_bs_eq (ccc_bitset const *a, ccc_bitset const *b) |
Checks two bit sets of the same size (not capacity) for equality. | |
Set Operations Interface | |
Perform basic mathematical set operations on the bit set. | |
ccc_tribool | ccc_bs_is_proper_subset (ccc_bitset const *set, ccc_bitset const *subset) |
Return CCC_TRUE if subset is a proper subset of set (subset ⊂ set). | |
ccc_tribool | ccc_bs_is_subset (ccc_bitset const *set, ccc_bitset const *subset) |
Return CCC_TRUE if subset is a subset of set (subset ⊆ set). | |
State Interface | |
Obtain state from the container. | |
ccc_bitblock * | ccc_bs_data (ccc_bitset const *bs) |
Return a reference to the base of the underlying block array. | |
ccc_ucount | ccc_bs_capacity (ccc_bitset const *bs) |
Return total number of bits the capacity of the set can hold. | |
ccc_ucount | ccc_bs_size (ccc_bitset const *bs) |
Return total number of bits actively tracked by the user and set. | |
ccc_ucount | ccc_bs_blocks_capacity (ccc_bitset const *bs) |
Return number of ccc_bitblocks used by bit set for capacity bits. | |
ccc_ucount | ccc_bs_blocks_size (ccc_bitset const *bs) |
Return number of ccc_bitblocks used by the bit set for size bits. | |
ccc_tribool | ccc_bs_empty (ccc_bitset const *bs) |
Return true if no bits are actively tracked by the user and set. | |
ccc_ucount | ccc_bs_popcount (ccc_bitset const *bs) |
Return the number of bits set to CCC_TRUE. O(n). | |
ccc_ucount | ccc_bs_popcount_range (ccc_bitset const *bs, size_t i, size_t count) |
Return the number of bits set to CCC_TRUE in the range. O(n). | |
Destructor Interface | |
Clear the set and manage its memory. | |
ccc_result | ccc_bs_clear (ccc_bitset *bs) |
Clears the bit set by setting the size to 0 and all bits to 0. The underlying memory capacity remains unchanged. | |
ccc_result | ccc_bs_clear_and_free (ccc_bitset *bs) |
Clears the bit set by setting the size to 0 and freeing the underlying memory. Capacity becomes 0 as well. | |
Dynamic Interface | |
Allows adding to and removing from the set. | |
ccc_result | ccc_bs_push_back (ccc_bitset *bs, ccc_tribool b) |
Append a bit value to the set as the new Most Significant Bit. | |
ccc_tribool | ccc_bs_pop_back (ccc_bitset *bs) |
Remove the Most Significant Bit from the set. | |
The Bit Set Interface.
A bit set offers efficient set membership operations when the range of values can be tracked via an index. Both a fixed size and dynamic variant are possible depending on initialization options.
Conceptually, the bit set can be thought of as an arbitrary length integer with index 0 being the Least Significant Bit and index N - 1 being the Most Significant Bit of the integer. Internally, this is implemented by populating each block of the bit set from Least Significant Bit to Most Significant Bit. Therefore, "trailing" means starting from the Least Significant Bit and "leading" means starting from the Most Significant Bit; this is done to stay consistent with upcoming bit operations introduced to the C23 standard.
A bit set can be used for modeling integer operations on integers that exceed the widths available on one's platform. The provided bitwise operation functions are helpful for these types of manipulations.
A bit set can also be used for modeling data that can be abstracted to a position and binary value. For example, disk blocks in a file system, free blocks in a memory allocator, and many other resource abstractions can benefit from a bit set. For these use cases the bit set offers efficient functions to find the first bits set to zero or one from either the trailing or leading direction. A bit set can also efficiently report if contiguous ranges of zeros or ones are available.
To shorten names in the interface, define the following preprocessor directive at the top of your file.
All types and functions can then be written without the ccc_
prefix.
#define ccc_bs_blocks | ( | bit_cap | ) | ccc_impl_bs_blocks(bit_cap) |
Get the number of bit blocks needed for the desired bit set capacity.
[in] | bit_cap | the number of bits representing this bit set. |
This method can be used for compile time initialization of bit sets so the user need to worry about the underlying bit set representation. For example:
The above example also illustrates the benefits of a static compound literal to encapsulate the bits within the bit set array to prevent dangling references. If your compiler has not implemented storage duration of compound literals the more traditional example would look like this.
This macro is required for any initialization where the bit block memory comes from the stack or data segment as determined by the user.
#define ccc_bs_init | ( | bitblock_ptr, | |
alloc_fn, | |||
aux, | |||
cap, | |||
optional_size... | |||
) | ccc_impl_bs_init(bitblock_ptr, alloc_fn, aux, cap, optional_size) |
Initialize the bit set with memory and allocation permissions.
[in] | bitblock_ptr | the pointer to existing blocks or NULL. |
[in] | alloc_fn | the allocation function for a dynamic bit set or NULL. |
[in] | aux | auxiliary data needed for allocation of the bit set. |
[in] | cap | the number of bits that will be stored in this bit set. |
[in] | optional_size | an optional starting size <= capacity. If the bitset is of fixed size with no allocation permission, and dynamic push and pop are not needed, the optional size parameter should be set equivalent to capacity. |
Or, initialize with zero capacity for a dynamic bit set.
See types.h for more on allocation functions.
typedef ccc_bitblock_ ccc_bitblock |
The type used to efficiently store bits in the bit set.
A block is a pre-determined integer width that allows for efficient block sized bit operations for various scanning and setting tasks. The user must participate in the storage of the bit set by using the provided ccc_bs_blocks macro to determine how many blocks are needed for the desired bits in the bit set.
typedef struct ccc_bitset_ ccc_bitset |
The bit set type that may be stored and initialized on the stack, heap, or data segment at compile time or runtime.
A bit set offers fast membership testing and bit range manipulations when the data can be modeled as a 0-indexed key value data set. In the case of a bit set the key is the index in the bit set and the value is 1 or 0 depending on how the bit has been set. Operations on single bits occur in O(1) time. All scanning operations operate in O(N) time.
ccc_tribool ccc_bs_all | ( | ccc_bitset const * | bs | ) |
Return true if all bits in set are 1.
[in] | bs | a pointer to the bit set. |
ccc_tribool ccc_bs_all_range | ( | ccc_bitset const * | bs, |
size_t | i, | ||
size_t | count | ||
) |
Return true if all bits are set to 1 in the specified range.
[in] | bs | a pointer to the bit set. |
[in] | i | the starting position. |
[in] | count | the size of the range to check. |
ccc_result ccc_bs_and | ( | ccc_bitset * | dst, |
ccc_bitset const * | src | ||
) |
Bitwise AND dst set with src set.
[in] | dst | the set to modified with the AND operation. |
[in] | src | the set to be read as the source bits for the AND operation. |
Note that sets are aligned from their Least Significant Bit and a smaller src set is conceptually padded with 0's in its higher order bits to align with the larger dst set (no modifications to the smaller set are performed to achieve this). This is done to stay consistent with integer promotion and widening rules in C.
ccc_tribool ccc_bs_any | ( | ccc_bitset const * | bs | ) |
Return true if any bits in set are 1.
[in] | bs | a pointer to the bit set. |
ccc_tribool ccc_bs_any_range | ( | ccc_bitset const * | bs, |
size_t | i, | ||
size_t | count | ||
) |
Return true if any bits are 1 in the specified range.
[in] | bs | a pointer to the bit set. |
[in] | i | the starting position. |
[in] | count | the size of the range to check. |
ccc_ucount ccc_bs_blocks_capacity | ( | ccc_bitset const * | bs | ) |
Return number of ccc_bitblocks used by bit set for capacity bits.
[in] | bs | a pointer to the bit set. |
Capacity may be greater than or equal to size.
ccc_ucount ccc_bs_blocks_size | ( | ccc_bitset const * | bs | ) |
Return number of ccc_bitblocks used by the bit set for size bits.
[in] | bs | a pointer to the bit set. |
Size may be less than or equal to capacity.
ccc_ucount ccc_bs_capacity | ( | ccc_bitset const * | bs | ) |
Return total number of bits the capacity of the set can hold.
[in] | bs | a pointer to the bit set. |
ccc_result ccc_bs_clear | ( | ccc_bitset * | bs | ) |
Clears the bit set by setting the size to 0 and all bits to 0. The underlying memory capacity remains unchanged.
[in] | bs | a pointer to the bit set. |
ccc_result ccc_bs_clear_and_free | ( | ccc_bitset * | bs | ) |
Clears the bit set by setting the size to 0 and freeing the underlying memory. Capacity becomes 0 as well.
[in] | bs | a pointer to the bit set. |
ccc_result ccc_bs_copy | ( | ccc_bitset * | dst, |
ccc_bitset const * | src, | ||
ccc_alloc_fn * | fn | ||
) |
Copy the bit set at source to destination.
[in] | dst | the initialized destination for the copy of the src set. |
[in] | src | the initialized source of the set. |
[in] | fn | the optional allocation function if resizing is needed. |
Note that there are two ways to copy data from source to destination: provide sufficient memory and pass NULL as fn, or allow the copy function to take care of allocation for the copy.
Manual memory management with no allocation function provided.
The above requires dst capacity be greater than or equal to src capacity. Here is memory management handed over to the copy function.
The above allows dst to have a capacity less than that of the src as long as copy has been provided an allocation function to resize dst. Note that this would still work if copying to a destination that the user wants as a fixed size map.
The above sets up dst with fixed size while src is a dynamic map. Because an allocation function is provided, the dst is resized once for the copy and retains its fixed size after the copy is complete. This would require the user to manually free the underlying buffer at dst eventually if this method is used. Usually it is better to allocate the memory explicitly before the copy if copying between maps without allocation permission.
These options allow users to stay consistent across containers with their memory management strategies.
ccc_bitblock * ccc_bs_data | ( | ccc_bitset const * | bs | ) |
Return a reference to the base of the underlying block array.
[in] | bs | a pointer to the bit set. |
Every block populates bits from Least Significant Bit (LSB) to Most Significant Bit (MSB) so this reference is to the base or LSB of the entire set.
ccc_tribool ccc_bs_empty | ( | ccc_bitset const * | bs | ) |
Return true if no bits are actively tracked by the user and set.
[in] | bs | a pointer to the bit set. |
ccc_tribool ccc_bs_eq | ( | ccc_bitset const * | a, |
ccc_bitset const * | b | ||
) |
Checks two bit sets of the same size (not capacity) for equality.
[in] | a | pointer to a bit set. |
[in] | b | pointer to another bit set of equal size. |
ccc_ucount ccc_bs_first_leading_one | ( | ccc_bitset const * | bs | ) |
Return the index of the first leading bit set to 1 in the set, starting from the Most Significant Bit at index size - 1.
[in] | bs | a pointer to the bit set. |
ccc_ucount ccc_bs_first_leading_one_range | ( | ccc_bitset const * | bs, |
size_t | i, | ||
size_t | count | ||
) |
Return the index of the first leading bit set to 1 in the set, starting from the Most Significant Bit at index size - 1.
[in] | bs | a pointer to the bit set. |
[in] | i | the starting index to search. |
[in] | count | the size of the range to check from i towards index 0. |
ccc_ucount ccc_bs_first_leading_ones | ( | ccc_bitset const * | bs, |
size_t | num_ones | ||
) |
Returns the index of the start of the first leading num_ones contiguous 1 bits.
[in] | bs | a pointer to the bit set. |
[in] | num_ones | the number of leading contiguous 1 bits to find. |
ccc_ucount ccc_bs_first_leading_ones_range | ( | ccc_bitset const * | bs, |
size_t | i, | ||
size_t | count, | ||
size_t | num_ones | ||
) |
Returns the index of the start of the first leading num_ones contiguous 1 bits in the specified range.
[in] | bs | a pointer to the bit set. |
[in] | i | the starting index to search. |
[in] | count | the size of the range to check. |
[in] | num_ones | the number of leading contiguous 1 bits to find. |
ccc_ucount ccc_bs_first_leading_zero | ( | ccc_bitset const * | bs | ) |
Return the index of the first leading bit set to 0 in the set, starting from the Most Significant Bit at index size - 1.
[in] | bs | a pointer to the bit set. |
ccc_ucount ccc_bs_first_leading_zero_range | ( | ccc_bitset const * | bs, |
size_t | i, | ||
size_t | count | ||
) |
Return the index of the first leading bit set to 0 in the set, starting from the Most Significant Bit at index size - 1.
[in] | bs | a pointer to the bit set. |
[in] | i | the starting index to search for a 0 bit. |
[in] | count | size to search from Most Significant Bit to Least in range. |
ccc_ucount ccc_bs_first_leading_zeros | ( | ccc_bitset const * | bs, |
size_t | num_zeros | ||
) |
Returns the index of the start of the first leading num_zeros contiguous 0 bits.
[in] | bs | a pointer to the bit set. |
[in] | num_zeros | the number of leading contiguous 0 bits to find. |
ccc_ucount ccc_bs_first_leading_zeros_range | ( | ccc_bitset const * | bs, |
size_t | i, | ||
size_t | count, | ||
size_t | num_zeros | ||
) |
Returns the index of the start of the first leading num_zeros contiguous 0 bits in the specified range.
[in] | bs | a pointer to the bit set. |
[in] | i | the starting index to search. |
[in] | count | the size of the range to check. |
[in] | num_zeros | the number of leading contiguous 0 bits to find. |
ccc_ucount ccc_bs_first_trailing_one | ( | ccc_bitset const * | bs | ) |
Return the index of the first trailing bit set to 1 in the set.
[in] | bs | a pointer to the bit set. |
ccc_ucount ccc_bs_first_trailing_one_range | ( | ccc_bitset const * | bs, |
size_t | i, | ||
size_t | count | ||
) |
Return the index of the first trailing bit set to 1 in the range, starting from the Least Significant Bit at index 0.
[in] | bs | a pointer to the bit set. |
[in] | i | the starting index to search. |
[in] | count | the size of the range to check. |
ccc_ucount ccc_bs_first_trailing_ones | ( | ccc_bitset const * | bs, |
size_t | num_ones | ||
) |
Returns the index of the start of the first trailing num_ones contiguous 1 bits.
[in] | bs | a pointer to the bit set. |
[in] | num_ones | the number of trailing contiguous 1 bits to find. |
ccc_ucount ccc_bs_first_trailing_ones_range | ( | ccc_bitset const * | bs, |
size_t | i, | ||
size_t | count, | ||
size_t | num_ones | ||
) |
Returns the index of the start of the first trailing num_ones contiguous 1 bits in the specified range.
[in] | bs | a pointer to the bit set. |
[in] | i | the starting index to search. |
[in] | count | the size of the range to check. |
[in] | num_ones | the number of trailing contiguous 1 bits to find. |
ccc_ucount ccc_bs_first_trailing_zero | ( | ccc_bitset const * | bs | ) |
Return the index of the first bit set to 0 in the set.
[in] | bs | a pointer to the bit set. |
ccc_ucount ccc_bs_first_trailing_zero_range | ( | ccc_bitset const * | bs, |
size_t | i, | ||
size_t | count | ||
) |
Return the index of the first bit set to 0 in the range.
[in] | bs | a pointer to the bit set. |
[in] | i | the starting index to search. |
[in] | count | the size of the range to check. |
ccc_ucount ccc_bs_first_trailing_zeros | ( | ccc_bitset const * | bs, |
size_t | num_zeros | ||
) |
Returns the index of the start of the first trailing num_zeros contiguous 0 bits.
[in] | bs | a pointer to the bit set. |
[in] | num_zeros | the number of trailing contiguous 0 bits to find. |
ccc_ucount ccc_bs_first_trailing_zeros_range | ( | ccc_bitset const * | bs, |
size_t | i, | ||
size_t | count, | ||
size_t | num_zeros | ||
) |
Returns the index of the start of the first trailing num_zeros contiguous 0 bits in the specified range.
[in] | bs | a pointer to the bit set. |
[in] | i | the starting index to search. |
[in] | count | the size of the range to check. |
[in] | num_zeros | the number of trailing contiguous 0 bits to find. |
ccc_tribool ccc_bs_flip | ( | ccc_bitset * | bs, |
size_t | i | ||
) |
Toggle the bit at index i.
[in] | bs | a pointer to the bit set. |
[in] | i | the index identifying the bit to toggle |
ccc_result ccc_bs_flip_all | ( | ccc_bitset * | bs | ) |
Toggle all of the bits to their opposing boolean value.
[in] | bs | a pointer to the bit set. |
ccc_result ccc_bs_flip_range | ( | ccc_bitset * | bs, |
size_t | i, | ||
size_t | count | ||
) |
Flip all the bits in the range, starting at the Least Significant Bit in range and ending at the Most Significant Bit, to their opposite value.
[in] | bs | a pointer to the bit set. |
[in] | i | the starting index to reset. |
[in] | count | the count of bits starting at i to reset. |
Note that a range is defined from i to i + count, where i + count is the exclusive end of the range. This is equivalent to moving from Least to Most Significant Bit in an integer.
ccc_tribool ccc_bs_is_proper_subset | ( | ccc_bitset const * | set, |
ccc_bitset const * | subset | ||
) |
Return CCC_TRUE if subset is a proper subset of set (subset ⊂ set).
[set] | the set to check subset against. |
[subset] | the subset to confirm as a proper subset of set. |
If set is of size 0 the function returns CCC_FALSE regardless of the size of subset. If set is not of size 0 and subset is of size 0 the function returns CCC_TRUE.
ccc_tribool ccc_bs_is_subset | ( | ccc_bitset const * | set, |
ccc_bitset const * | subset | ||
) |
Return CCC_TRUE if subset is a subset of set (subset ⊆ set).
[set] | the set to check subset against. |
[subset] | the subset to confirm as a subset of set. |
If set is size zero subset must also be of size 0 to return CCC_TRUE. If subset is size 0 the function returns CCC_TRUE regardless of the size of set.
ccc_tribool ccc_bs_none | ( | ccc_bitset const * | bs | ) |
Return true if all bits are set to 0.
[in] | bs | a pointer to the bit set. |
ccc_tribool ccc_bs_none_range | ( | ccc_bitset const * | bs, |
size_t | i, | ||
size_t | count | ||
) |
Return true if all bits are 0 in the specified range.
[in] | bs | a pointer to the bit set. |
[in] | i | the starting position. |
[in] | count | the size of the range to check. |
ccc_result ccc_bs_or | ( | ccc_bitset * | dst, |
ccc_bitset const * | src | ||
) |
Bitwise OR dst set with src set.
[in] | dst | the set to modified with the OR operation. |
[in] | src | the set to be read as the source bits for the OR operation. |
Note that sets are aligned from their Least Significant Bit and a smaller src set is conceptually padded with 0's in its higher order bits to align with the larger dst set (no modifications to the smaller set are performed to achieve this). This is done to stay consistent with how the operation would work on a smaller integer being stored in a larger integer to align with the larger.
ccc_tribool ccc_bs_pop_back | ( | ccc_bitset * | bs | ) |
Remove the Most Significant Bit from the set.
[in] | bs | a pointer to the bit set. |
ccc_ucount ccc_bs_popcount | ( | ccc_bitset const * | bs | ) |
Return the number of bits set to CCC_TRUE. O(n).
[in] | bs | a pointer to the bit set. |
ccc_ucount ccc_bs_popcount_range | ( | ccc_bitset const * | bs, |
size_t | i, | ||
size_t | count | ||
) |
Return the number of bits set to CCC_TRUE in the range. O(n).
[in] | bs | a pointer to the bit set. |
[in] | i | the starting position. |
[in] | count | the size of the range to check. |
ccc_result ccc_bs_push_back | ( | ccc_bitset * | bs, |
ccc_tribool | b | ||
) |
Append a bit value to the set as the new Most Significant Bit.
[in] | bs | a pointer to the bit set. |
[in] | b | the value to push at the Most Significant Bit CCC_TRUE/CCC_FALSE. |
ccc_tribool ccc_bs_reset | ( | ccc_bitset * | bs, |
size_t | i | ||
) |
Set the bit at valid index i to boolean value b (true or false).
[in] | bs | a pointer to the bit set. |
[in] | i | the valid index identifying the bit to set. |
ccc_result ccc_bs_reset_all | ( | ccc_bitset * | bs | ) |
Set all the bits to CCC_FALSE.
[in] | bs | a pointer to the bit set. |
ccc_result ccc_bs_reset_range | ( | ccc_bitset * | bs, |
size_t | i, | ||
size_t | count | ||
) |
Set all the bits in the specified range starting at the Least Significant Bit of the range and ending at the Most Significant Bit of the range to CCC_FALSE.
[in] | bs | a pointer to the bit set. |
[in] | i | the starting index to reset. |
[in] | count | the count of bits starting at i to reset. |
Note that a range is defined from i to i + count, where i + count is the exclusive end of the range. This is equivalent to moving from Least to Most Significant bit in an integer.
ccc_tribool ccc_bs_set | ( | ccc_bitset * | bs, |
size_t | i, | ||
ccc_tribool | b | ||
) |
Set the bit at valid index i to value b (true or false).
[in] | bs | a pointer to the bit set. |
[in] | i | the valid index identifying the bit to set. |
[in] | b | the value to set at position i (CCC_TRUE or CCC_FALSE). |
ccc_result ccc_bs_set_all | ( | ccc_bitset * | bs, |
ccc_tribool | b | ||
) |
Set all the bits to the provided value (CCC_TRUE or CCC_FALSE).
[in] | bs | a pointer to the bit set. |
[in] | b | the value to set (CCC_TRUE or CCC_FALSE). |
ccc_result ccc_bs_set_range | ( | ccc_bitset * | bs, |
size_t | i, | ||
size_t | count, | ||
ccc_tribool | b | ||
) |
Set all the bits in the specified range starting at the Least Significant Bit of the range and ending at the Most Significant Bit of the range (CCC_TRUE or CCC_FALSE).
[in] | bs | a pointer to the bit set. |
[in] | b | the value to set (CCC_TRUE or CCC_FALSE). |
[in] | i | the starting index to set. |
[in] | count | the count of bits starting at i to set. |
Note that a range is defined from i to i + count, where i + count is the exclusive end of the range. This is equivalent to moving from Least to Most Significant bit in an integer.
ccc_result ccc_bs_shiftl | ( | ccc_bitset * | bs, |
size_t | left_shifts | ||
) |
Shift the bit set left by left_shifts amount.
[in] | bs | a pointer to the bit set. |
[in] | left_shifts | the number of left shifts to perform. |
Note that if the number of shifts is greater than the bit set size the bit set is zeroed out rather than exhibiting undefined behavior as in the equivalent integer operation.
ccc_result ccc_bs_shiftr | ( | ccc_bitset * | bs, |
size_t | right_shifts | ||
) |
Shift the bit set right by right_shifts amount.
[in] | bs | a pointer to the bit set. |
[in] | right_shifts | the number of right shifts to perform. |
Note that if the number of shifts is greater than the bit set size the bit set is zeroed out rather than exhibiting undefined behavior as in the equivalent integer operation.
ccc_ucount ccc_bs_size | ( | ccc_bitset const * | bs | ) |
Return total number of bits actively tracked by the user and set.
[in] | bs | a pointer to the bit set. |
ccc_tribool ccc_bs_test | ( | ccc_bitset const * | bs, |
size_t | i | ||
) |
Test the bit at index i for boolean status (CCC_TRUE or CCC_FALSE).
[in] | bs | a pointer to the bit set. |
[in] | i | the index identifying the bit to set. |
ccc_result ccc_bs_xor | ( | ccc_bitset * | dst, |
ccc_bitset const * | src | ||
) |
Bitwise XOR dst set with src set.
[in] | dst | the set to modified with the XOR operation. |
[in] | src | the set to be read as the source bits for the XOR operation. |
Note that sets are aligned from their Least Significant Bit and a smaller src set is conceptually padded with 0's in its higher order bits to align with the larger dst set (no modifications to the smaller set are performed to achieve this). This is done to stay consistent with how the operation would work on a smaller integer being stored in a larger integer to align with the larger.