aboutsummaryrefslogtreecommitdiff
path: root/core/container/bit_array/bit_array.odin
diff options
context:
space:
mode:
authorFeoramund <161657516+Feoramund@users.noreply.github.com>2024-09-03 06:02:18 -0400
committerFeoramund <161657516+Feoramund@users.noreply.github.com>2024-09-03 13:33:15 -0400
commitb8f8cb95823e0e56e0d8fd6ace5b38d9d57d03ff (patch)
tree330fd52d1952f33c8e402f7ba372a33a6fbef549 /core/container/bit_array/bit_array.odin
parent001b2b9d8f021eb88db82d7a4cce47c6cdaf44de (diff)
Add `bit_array.shrink`
Diffstat (limited to 'core/container/bit_array/bit_array.odin')
-rw-r--r--core/container/bit_array/bit_array.odin30
1 files changed, 30 insertions, 0 deletions
diff --git a/core/container/bit_array/bit_array.odin b/core/container/bit_array/bit_array.odin
index 61119ef96..b1149e82e 100644
--- a/core/container/bit_array/bit_array.odin
+++ b/core/container/bit_array/bit_array.odin
@@ -1,5 +1,6 @@
package container_dynamic_bit_array
+import "base:builtin"
import "base:intrinsics"
import "core:mem"
@@ -299,6 +300,35 @@ clear :: proc(ba: ^Bit_Array) {
mem.zero_slice(ba.bits[:])
}
/*
+Shrinks the Bit_Array's backing storage to the smallest possible size.
+
+Inputs:
+- ba: The target Bit_Array
+*/
+shrink :: proc(ba: ^Bit_Array) #no_bounds_check {
+ if ba == nil { return }
+ legs_needed := len(ba.bits)
+ for i := legs_needed - 1; i >= 0; i -= 1 {
+ if ba.bits[i] == 0 {
+ legs_needed -= 1
+ } else {
+ break
+ }
+ }
+ if legs_needed == len(ba.bits) {
+ return
+ }
+ ba.max_index = 0
+ if legs_needed > 0 {
+ if legs_needed > 1 {
+ ba.max_index = (legs_needed - 1) * NUM_BITS
+ }
+ ba.max_index += NUM_BITS - 1 - int(intrinsics.count_leading_zeros(ba.bits[legs_needed - 1]))
+ }
+ resize(&ba.bits, legs_needed)
+ builtin.shrink(&ba.bits)
+}
+/*
Deallocates the Bit_Array and its backing storage
Inputs: