mirror of
https://github.com/c-cube/sidekick.git
synced 2025-12-07 11:45:41 -05:00
misc
This commit is contained in:
parent
f2b2bbb973
commit
347e098fc2
3 changed files with 29 additions and 4 deletions
|
|
@ -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) *)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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] *)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue