This commit is contained in:
Simon Cruanes 2021-07-19 23:09:34 -04:00
parent f2b2bbb973
commit 347e098fc2
3 changed files with 29 additions and 4 deletions

View file

@ -42,7 +42,13 @@ module Make(Plugin : PLUGIN)
(* ### types ### *)
(* a boolean variable (positive int) *)
module Var0 : INT_ID = Mk_int_id()
module Var0 : sig
include INT_ID
module Set : Set.S with type elt = t
end = struct
include Mk_int_id()
module Set = Util.Int_set
end
type var = Var0.t
(* a signed atom. +v is (v << 1), -v is (v<<1 | 1) *)

View file

@ -31,6 +31,12 @@ let[@inline] copy t : _ t =
let data = Array.copy t.data in
{t with data}
let resize_ t x size =
let arr' = Array.make size x in
Array.blit t.data 0 arr' 0 t.sz;
t.data <- arr';
()
(* grow the array *)
let[@inline never] grow_to_double_size t x : unit =
if Array.length t.data = Sys.max_array_length then (
@ -39,9 +45,7 @@ let[@inline never] grow_to_double_size t x : unit =
let size =
min Sys.max_array_length (max 4 (2 * Array.length t.data))
in
let arr' = Array.make size x in
Array.blit t.data 0 arr' 0 (Array.length t.data);
t.data <- arr';
resize_ t x size;
assert (Array.length t.data > t.sz);
()
@ -67,6 +71,17 @@ let[@inline] fast_remove t i =
Array.unsafe_set t.data i @@ Array.unsafe_get t.data (t.sz - 1);
t.sz <- t.sz - 1
let prepend v ~into : unit =
if v.sz = 0 then ()
else (
if v.sz + into.sz > Array.length into.data then (
resize_ into v.data.(0) (v.sz + into.sz);
);
Array.blit into.data 0 into.data v.sz into.sz; (* shift elements *)
Array.blit v.data 0 into.data 0 v.sz;
)
let filter_in_place f vec =
let i = ref 0 in
while !i < size vec do

View file

@ -62,6 +62,10 @@ val fast_remove : 'a t -> int -> unit
(** Remove element at index [i] without preserving order
(swap with last element) *)
val prepend : 'a t -> into:'a t -> unit
(** [prepend v ~into] pushes all elements of [v] into [into],
at the beginning. consumes [v]. *)
val filter_in_place : ('a -> bool) -> 'a t -> unit
(** [filter_in_place f v] removes from [v] the elements that do
not satisfy [f] *)