mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +00:00
Add bit_array.shrink
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package container_dynamic_bit_array
|
package container_dynamic_bit_array
|
||||||
|
|
||||||
|
import "base:builtin"
|
||||||
import "base:intrinsics"
|
import "base:intrinsics"
|
||||||
import "core:mem"
|
import "core:mem"
|
||||||
|
|
||||||
@@ -299,6 +300,35 @@ clear :: proc(ba: ^Bit_Array) {
|
|||||||
mem.zero_slice(ba.bits[:])
|
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
|
Deallocates the Bit_Array and its backing storage
|
||||||
|
|
||||||
Inputs:
|
Inputs:
|
||||||
|
|||||||
Reference in New Issue
Block a user