package core:container/bit_array
Overview
A dynamically-sized array of bits.
The Bit Array can be used in several ways:
By default you don't need to instantiate a Bit_Array.
Example:
package test
import "core:fmt"
import ba "core:container/bit_array"
main :: proc() {
bits: ba.Bit_Array
// returns `true`
fmt.println(ba.set(&bits, 42))
// returns `false`, `false`, because this Bit Array wasn't created to allow negative indices.
was_set, was_retrieved := ba.get(&bits, -1)
fmt.println(was_set, was_retrieved)
ba.destroy(&bits)
}
A Bit_Array can optionally allow for negative indices, if the minimum value was given during creation.
Example:
package test
import "core:fmt"
import ba "core:container/bit_array"
main :: proc() {
Foo :: enum int {
Negative_Test = -42,
Bar = 420,
Leaves = 69105,
}
bits := ba.create_from_enum(Foo)
defer ba.destroy(bits)
assert(bits.bias == int(Foo.Negative_Test))
assert(bits.length == abs(int(min(Foo))) + int(max(Foo)))
fmt.printfln("Set(Bar): %v", ba.set(bits, Foo.Bar))
fmt.printfln("Get(Bar): %v", ba.get(bits, Foo.Bar))
fmt.printfln("Set(Negative_Test): %v", ba.set(bits, Foo.Negative_Test))
fmt.printfln("Get(Leaves): %v", ba.get(bits, Foo.Leaves))
fmt.printfln("Get(Leaves): %v", ba.unsafe_get(bits, Foo.Leaves))
fmt.printfln("Get(Negative_Test): %v", ba.get(bits, Foo.Negative_Test))
fmt.printfln("Unset(Negative_Test): %v", ba.unset(bits, Foo.Negative_Test))
assert(ba.get(bits, Foo.Negative_Test) == false)
}
Types
Bit_Array_Iterator ¶
Related Procedures With Parameters
Constants
This section is empty.
Variables
This section is empty.
Procedures
create ¶
create :: proc( max_index: int, min_index: int = 0, allocator := context.allocator, loc := #caller_location, ) -> (res: ^Bit_Array, ok: bool) #optional_ok {…}
A helper function to create a Bit_Array with optional bias, in case your smallest index is non-zero (including negative).
The range of bits created by this procedure is min_index..<max_index, and the
array will be able to expand beyond max_index if needed.
Allocates (new(Bit_Array) & make(ba.bits))
Inputs:
- max_index maximum starting index
- min_index minimum starting index (used as a bias)
- allocator (default is context.allocator)
Returns:
- ba Allocates a
Bit_Array, backing data is set tomax-min / 64indices, rounded up (eg 65 - 0 allocates for [2]u64).
create_from_enum ¶
create_from_enum :: proc( $T: typeid, allocator := context.allocator, ) -> (res: ^Bit_Array, ok: bool) #optional_ok {…}
A helper function to create a Bit_Array from an enum E.
The range of bits created by this procedure is min(E)..<max(E), and the
array will be able to expand beyond max(E) if needed.
Allocates (new(Bit_Array) & make(ba.bits))
Inputs:
- e an
enum - min_index minimum starting index (used as a bias)
- allocator (default is context.allocator)
Returns:
- ba Allocates a
Bit_Array, backing data is set tomax-min / 64indices, rounded up (eg 65 - 0 allocates for [2]u64).
init ¶
init :: proc( res: ^Bit_Array, max_index: int, min_index: int = 0, allocator := context.allocator, ) -> (ok: bool) {…}
A helper function to initialize a Bit_Array with optional bias, in case your smallest index is non-zero (including negative).
The range of bits created by this procedure is min_index..<max_index, and the
array will be able to expand beyond max_index if needed.
Allocates (make(ba.bits))
Inputs:
- max_index maximum starting index
- min_index minimum starting index (used as a bias)
- allocator (default is context.allocator)
iterate_by_all ¶
iterate_by_all :: proc(it: ^Bit_Array_Iterator) -> (set: bool, index: int, ok: bool) {…}
Returns the next bit, including its set-state. ok=false once exhausted.
Inputs:
- it The iterator that holds the state.
Returns:
- set
trueif the bit atindexis set. - index The next bit of the
Bit_Arrayreferenced byit. - ok
trueif the iterator can continue,falseif the iterator is done
iterate_by_set ¶
iterate_by_set :: proc(it: ^Bit_Array_Iterator) -> (index: int, ok: bool) {…}
Returns the next set bit, for example if 0b1010, then the iterator will return index={1, 3} over two calls.
Inputs:
- it The iterator that holds the state.
Returns:
- index The next set bit of the
Bit_Arrayreferenced byit. - ok
trueif the iterator can continue,falseif the iterator is done
iterate_by_unset ¶
iterate_by_unset :: proc(it: ^Bit_Array_Iterator) -> (index: int, ok: bool) {…}
Returns the next unset bit, for example if 0b1010, then the iterator will return index={0, 2} over two calls.
Inputs:
- it The iterator that holds the state.
Returns:
- index The next unset bit of the
Bit_Arrayreferenced byit. - ok
trueif the iterator can continue,falseif the iterator is done
make_iterator ¶
make_iterator :: proc(ba: ^Bit_Array) -> (it: Bit_Array_Iterator) {…}
set ¶
set :: proc( ba: ^Bit_Array, #any_int index: int, set_to: bool = true, allocator := context.allocator, ) -> (ok: bool) {…}
Sets the state of a bit in the Bit_Array.
Conditionally Allocates (Resizes backing data when index > len(ba.bits))
Inputs:
- ba Pointer to the
Bit_Array - index Which bit in the array
- set_to
truesets the bit on,falseto turn it off - allocator (default is context.allocator)
Returns:
- ok Whether the set was successful,
falseon allocation failure or bad index
unset ¶
unset :: proc( ba: ^Bit_Array, #any_int index: int, allocator := context.allocator, ) -> (ok: bool) {…}
Unsets the state of a bit in the Bit_Array. (Convienence wrapper for set)
Conditionally Allocates (Resizes backing data when index > len(ba.bits))
Inputs:
- ba Pointer to the
Bit_Array - index Which bit in the array
- allocator (default is context.allocator)
Returns:
- ok Whether the unset was successful,
falseon allocation failure or bad index
Procedure Groups
This section is empty.
Source Files
Generation Information
Generated with odin version dev-2026-10 (vendor "odin") Windows_amd64 @ 2026-10-21 23:51:49.279618400 +0000 UTC