aboutsummaryrefslogtreecommitdiff
path: root/core/container/bit_array/bit_array.odin
diff options
context:
space:
mode:
authorJungerBoyo <74600205+JungerBoyo@users.noreply.github.com>2022-06-03 15:53:14 +0200
committerGitHub <noreply@github.com>2022-06-03 15:53:14 +0200
commitad6b3bd95fe1e354e77f76f4694a4e076fd00e61 (patch)
tree9ea7f6ea47198defdd67b9f4db65e2f60a1a0ae5 /core/container/bit_array/bit_array.odin
parentfb49841b1d8e84cb721f5f73200d4c8158cbb4fa (diff)
unset function
unset function, clear single bits
Diffstat (limited to 'core/container/bit_array/bit_array.odin')
-rw-r--r--core/container/bit_array/bit_array.odin27
1 files changed, 27 insertions, 0 deletions
diff --git a/core/container/bit_array/bit_array.odin b/core/container/bit_array/bit_array.odin
index 98ef4b542..763a19f8b 100644
--- a/core/container/bit_array/bit_array.odin
+++ b/core/container/bit_array/bit_array.odin
@@ -186,6 +186,33 @@ set :: proc(ba: ^Bit_Array, #any_int index: uint, allocator := context.allocator
}
/*
+ In:
+ - ba: ^Bit_Array - a pointer to the Bit Array
+ - index: The bit index. Can be an enum member.
+
+ Out:
+ - ok: Whether or not we managed to unset requested bit.
+
+ `unset` automatically resizes the Bit Array to accommodate the requested index if needed.
+*/
+unset :: proc(ba: ^Bit_Array, #any_int index: uint, allocator := context.allocator) -> (ok: bool) {
+
+ idx := int(index) - ba.bias
+
+ if ba == nil || int(index) < ba.bias { return false }
+ context.allocator = allocator
+
+ leg_index := idx >> INDEX_SHIFT
+ bit_index := idx & INDEX_MASK
+
+ resize_if_needed(ba, leg_index) or_return
+
+ ba.max_index = max(idx, ba.max_index)
+ ba.bits[leg_index] &= ~(1 << uint(bit_index))
+ return true
+}
+
+/*
A helper function to create a Bit Array with optional bias, in case your smallest index is non-zero (including negative).
*/
create :: proc(max_index: int, min_index := 0, allocator := context.allocator) -> (res: ^Bit_Array, ok: bool) #optional_ok {