bv: refactor for performance and readability

This commit is contained in:
Simon Cruanes 2021-05-17 10:01:35 -04:00
parent 92519b4843
commit 1c8265c3f3
2 changed files with 7 additions and 4 deletions

View file

@ -252,16 +252,18 @@ let iter bv f =
assert (len <= Array.length bv.a);
for n = 0 to len - 2 do
let j = width_ * n in
let word_n = Array.unsafe_get bv.a n in
for i = 0 to width_ - 1 do
f (j+i) (bv.a.(n) land (1 lsl i) <> 0)
f (j+i) ((word_n land (1 lsl i)) <> 0)
done
done;
if bv.size > 0 then (
let j = width_ * (len - 1) in
let r = bv.size mod width_ in
let final_length = if r = 0 then width_ else r in
let final_word = Array.unsafe_get bv.a (len-1) in
for i = 0 to final_length - 1 do
f (j + i) (bv.a.(len - 1) land (1 lsl i) <> 0)
f (j + i) ((final_word land (1 lsl i)) <> 0)
done
)
@ -294,7 +296,7 @@ let iter bv f =
List.length l = n && List.for_all (fun (_,b) -> b) l)
*)
let iter_true bv f =
let[@inline] iter_true bv f =
iter bv (fun i b -> if b then f i else ())
(*$T

View file

@ -79,7 +79,8 @@ val of_list : int list -> t
(** From a list of true bits.
The bits are interpreted as indices into the returned bitvector, so the final
bitvector will have [length t] equal to 1 more than max of list indices. *)
bitvector [bv] will have [length bv] equal to 1 more than max of list indices.
*)
val first : t -> int option
(** First set bit, or return [None].