diff options
| author | Laytan Laats <laytanlaats@hotmail.com> | 2024-09-29 10:53:31 +0200 |
|---|---|---|
| committer | Laytan Laats <laytanlaats@hotmail.com> | 2024-09-29 10:57:42 +0200 |
| commit | bd45900257065cd2fd6db508c2f96c0200db994a (patch) | |
| tree | d637f45c94c6533c83ee30a7c55c89ea09fd071c /core/container | |
| parent | eccb548935eb83bd3d48aeb06563a88a12b40c0a (diff) | |
container/bit_array: add 'init' procedure
Diffstat (limited to 'core/container')
| -rw-r--r-- | core/container/bit_array/bit_array.odin | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/core/container/bit_array/bit_array.odin b/core/container/bit_array/bit_array.odin index 9a76dc78f..85b941d12 100644 --- a/core/container/bit_array/bit_array.odin +++ b/core/container/bit_array/bit_array.odin @@ -259,6 +259,7 @@ Inputs: unsafe_unset :: proc(b: ^Bit_Array, bit: int) #no_bounds_check { b.bits[bit >> INDEX_SHIFT] &~= 1 << uint(bit & INDEX_MASK) } + /* A helper function to create a Bit Array with optional bias, in case your smallest index is non-zero (including negative). @@ -276,22 +277,50 @@ Returns: - ba: Allocates a bit_Array, backing data is set to `max-min / 64` indices, rounded up (eg 65 - 0 allocates for [2]u64). */ create :: proc(max_index: int, min_index: int = 0, allocator := context.allocator) -> (res: ^Bit_Array, ok: bool) #optional_ok { - context.allocator = allocator size_in_bits := max_index - min_index if size_in_bits < 0 { return {}, false } + res = new(Bit_Array, allocator) + ok = init(res, max_index, min_index, allocator) + res.free_pointer = true + + if !ok { free(res, allocator) } + + return +} + +/* +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) +*/ +init :: proc(res: ^Bit_Array, max_index: int, min_index: int = 0, allocator := context.allocator) -> (ok: bool) { + size_in_bits := max_index - min_index + + if size_in_bits < 0 { return false } + legs := size_in_bits >> INDEX_SHIFT - if size_in_bits & INDEX_MASK > 0 {legs+=1} - bits, err := make([dynamic]u64, legs) - ok = err == mem.Allocator_Error.None - res = new(Bit_Array) + if size_in_bits & INDEX_MASK > 0 { legs += 1 } + + bits, err := make([dynamic]u64, legs, allocator) + ok = err == nil + res.bits = bits res.bias = min_index res.length = max_index - min_index - res.free_pointer = true + res.free_pointer = false return } + /* Sets all values in the Bit_Array to zero. |