mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
Merge pull request #399 from c-cube/wip-ccvector-resize
change growth strategy for Vector
This commit is contained in:
commit
bc8b7b168b
9 changed files with 26 additions and 53 deletions
|
|
@ -79,9 +79,13 @@ let init n f = {
|
||||||
}
|
}
|
||||||
|
|
||||||
(* is the underlying array empty? *)
|
(* is the underlying array empty? *)
|
||||||
let array_is_empty_ v =
|
let[@inline] array_is_empty_ v =
|
||||||
Array.length v.vec = 0
|
Array.length v.vec = 0
|
||||||
|
|
||||||
|
(* next capacity, if current one is [n] *)
|
||||||
|
let[@inline] next_grow_ n =
|
||||||
|
min Sys.max_array_length (n + n lsr 1 + 2)
|
||||||
|
|
||||||
(* resize the underlying array using x to temporarily fill the array *)
|
(* resize the underlying array using x to temporarily fill the array *)
|
||||||
let resize_ v newcapacity x =
|
let resize_ v newcapacity x =
|
||||||
assert (newcapacity >= v.size);
|
assert (newcapacity >= v.size);
|
||||||
|
|
@ -128,7 +132,7 @@ let grow_with_ v ~filler:x =
|
||||||
fill_with_junk_ v.vec 0 len;
|
fill_with_junk_ v.vec 0 len;
|
||||||
) else (
|
) else (
|
||||||
let n = Array.length v.vec in
|
let n = Array.length v.vec in
|
||||||
let size = min (2 * n + 3) Sys.max_array_length in
|
let size = next_grow_ n in
|
||||||
if size = n then invalid_arg "vec: can't grow any further";
|
if size = n then invalid_arg "vec: can't grow any further";
|
||||||
resize_ v size v.vec.(0)
|
resize_ v size v.vec.(0)
|
||||||
)
|
)
|
||||||
|
|
@ -143,8 +147,8 @@ let ensure_assuming_not_empty_ v ~size =
|
||||||
) else if size < Array.length v.vec then (
|
) else if size < Array.length v.vec then (
|
||||||
() (* nothing to do *)
|
() (* nothing to do *)
|
||||||
) else (
|
) else (
|
||||||
let n = ref (max 8 (Array.length v.vec)) in
|
let n = ref (Array.length v.vec) in
|
||||||
while !n < size do n := min Sys.max_array_length (2* !n) done;
|
while !n < size do n := next_grow_ !n done;
|
||||||
resize_ v !n v.vec.(0)
|
resize_ v !n v.vec.(0)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -161,7 +165,7 @@ let ensure v size =
|
||||||
ensure_assuming_not_empty_ v ~size
|
ensure_assuming_not_empty_ v ~size
|
||||||
)
|
)
|
||||||
|
|
||||||
let clear v =
|
let[@inline] clear v =
|
||||||
v.size <- 0
|
v.size <- 0
|
||||||
|
|
||||||
(*$R
|
(*$R
|
||||||
|
|
@ -188,9 +192,9 @@ let clear_and_reset v =
|
||||||
OUnit.assert_equal None (Weak.get a 0);
|
OUnit.assert_equal None (Weak.get a 0);
|
||||||
*)
|
*)
|
||||||
|
|
||||||
let is_empty v = v.size = 0
|
let[@inline] is_empty v = v.size = 0
|
||||||
|
|
||||||
let push_unsafe_ v x =
|
let[@inline] push_unsafe_ v x =
|
||||||
Array.unsafe_set v.vec v.size x;
|
Array.unsafe_set v.vec v.size x;
|
||||||
v.size <- v.size + 1
|
v.size <- v.size + 1
|
||||||
|
|
||||||
|
|
@ -296,11 +300,11 @@ let append a b =
|
||||||
OUnit.assert_equal (Iter.to_array Iter.(6 -- 10)) (to_array b);
|
OUnit.assert_equal (Iter.to_array Iter.(6 -- 10)) (to_array b);
|
||||||
*)
|
*)
|
||||||
|
|
||||||
let get v i =
|
let[@inline] get v i =
|
||||||
if i < 0 || i >= v.size then invalid_arg "CCVector.get";
|
if i < 0 || i >= v.size then invalid_arg "CCVector.get";
|
||||||
Array.unsafe_get v.vec i
|
Array.unsafe_get v.vec i
|
||||||
|
|
||||||
let set v i x =
|
let[@inline] set v i x =
|
||||||
if i < 0 || i >= v.size then invalid_arg "CCVector.set";
|
if i < 0 || i >= v.size then invalid_arg "CCVector.set";
|
||||||
Array.unsafe_set v.vec i x
|
Array.unsafe_set v.vec i x
|
||||||
|
|
||||||
|
|
@ -341,7 +345,7 @@ let remove_unordered v i =
|
||||||
to_list v1 = (List.sort CCInt.compare (to_list v2)))
|
to_list v1 = (List.sort CCInt.compare (to_list v2)))
|
||||||
*)
|
*)
|
||||||
|
|
||||||
let append_iter a i = i (fun x -> push a x)
|
let[@inline] append_iter a i = i (fun x -> push a x)
|
||||||
|
|
||||||
let append_seq a seq = Seq.iter (fun x -> push a x) seq
|
let append_seq a seq = Seq.iter (fun x -> push a x) seq
|
||||||
|
|
||||||
|
|
@ -477,12 +481,12 @@ let pop v =
|
||||||
try Some (pop_exn v)
|
try Some (pop_exn v)
|
||||||
with Empty -> None
|
with Empty -> None
|
||||||
|
|
||||||
let top v =
|
let[@inline] top v =
|
||||||
if v.size = 0 then None else Some v.vec.(v.size-1)
|
if v.size = 0 then None else Some (Array.unsafe_get v.vec (v.size-1))
|
||||||
|
|
||||||
let top_exn v =
|
let[@inline] top_exn v =
|
||||||
if v.size = 0 then raise Empty;
|
if v.size = 0 then raise Empty;
|
||||||
v.vec.(v.size-1)
|
Array.unsafe_get v.vec (v.size-1)
|
||||||
|
|
||||||
(*$T
|
(*$T
|
||||||
1 -- 10 |> top = Some 10
|
1 -- 10 |> top = Some 10
|
||||||
|
|
@ -490,7 +494,7 @@ let top_exn v =
|
||||||
1 -- 10 |> top_exn = 10
|
1 -- 10 |> top_exn = 10
|
||||||
*)
|
*)
|
||||||
|
|
||||||
let copy v = {
|
let[@inline] copy v = {
|
||||||
size = v.size;
|
size = v.size;
|
||||||
vec = Array.sub v.vec 0 v.size;
|
vec = Array.sub v.vec 0 v.size;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@
|
||||||
(modules :standard \ mkshims)
|
(modules :standard \ mkshims)
|
||||||
(flags :standard -warn-error -a+8 -w -32 -safe-string -strict-sequence -nolabels -open
|
(flags :standard -warn-error -a+8 -w -32 -safe-string -strict-sequence -nolabels -open
|
||||||
CCMonomorphic)
|
CCMonomorphic)
|
||||||
(ocamlopt_flags (:include ../flambda.flags))
|
|
||||||
(libraries seq either containers.monomorphic))
|
(libraries seq either containers.monomorphic))
|
||||||
|
|
||||||
(ocamllex (modules CCSexp_lex))
|
(ocamllex (modules CCSexp_lex))
|
||||||
|
|
|
||||||
|
|
@ -4,5 +4,4 @@
|
||||||
(public_name containers-data)
|
(public_name containers-data)
|
||||||
(wrapped false)
|
(wrapped false)
|
||||||
(flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string -open CCShims_)
|
(flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string -open CCShims_)
|
||||||
(ocamlopt_flags :standard (:include ../flambda.flags))
|
|
||||||
(libraries containers))
|
(libraries containers))
|
||||||
|
|
|
||||||
15
src/dune
15
src/dune
|
|
@ -1,15 +1,10 @@
|
||||||
(executable
|
|
||||||
(name mkflags)
|
|
||||||
(modules mkflags)
|
|
||||||
(libraries dune.configurator))
|
|
||||||
|
|
||||||
(env
|
(env
|
||||||
(_ (flags :standard -warn-error -3)))
|
(_
|
||||||
|
(flags :standard -warn-error -3 -color always)
|
||||||
(rule
|
(ocamlopt_flags :standard -O3 -unbox-closures -unbox-closures-factor 20
|
||||||
(targets flambda.flags)
|
-inline 100)
|
||||||
(mode fallback)
|
))
|
||||||
(action (run ./mkflags.exe)))
|
|
||||||
|
|
||||||
(executable
|
(executable
|
||||||
(name mdx_runner)
|
(name mdx_runner)
|
||||||
|
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
||||||
|
|
||||||
module C = Configurator.V1
|
|
||||||
|
|
||||||
let () =
|
|
||||||
C.main ~name:"mkflags" (fun c ->
|
|
||||||
let version = C.ocaml_config_var_exn c "version" in
|
|
||||||
let major, minor =
|
|
||||||
Scanf.sscanf version "%u.%u"
|
|
||||||
(fun major minor -> major, minor)
|
|
||||||
in
|
|
||||||
let after_4_3 = (major, minor) >= (4, 3) in
|
|
||||||
let sexp =
|
|
||||||
if after_4_3 then (
|
|
||||||
["-O3"; "-unbox-closures"; "-unbox-closures-factor"; "20"; "-color"; "always"]
|
|
||||||
) else (
|
|
||||||
[]
|
|
||||||
) in
|
|
||||||
C.Flags.write_sexp "flambda.flags" sexp
|
|
||||||
)
|
|
||||||
|
|
@ -14,5 +14,4 @@
|
||||||
(public_name containers.monomorphic)
|
(public_name containers.monomorphic)
|
||||||
(modules CCMonomorphic CCMonomorphicShims_)
|
(modules CCMonomorphic CCMonomorphicShims_)
|
||||||
(wrapped false)
|
(wrapped false)
|
||||||
(flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string)
|
(flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string))
|
||||||
(ocamlopt_flags :standard (:include ../flambda.flags)))
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,5 @@
|
||||||
(wrapped false)
|
(wrapped false)
|
||||||
(optional)
|
(optional)
|
||||||
(flags :standard -warn-error -a+8 -w -32 -safe-string -open CCShims_)
|
(flags :standard -warn-error -a+8 -w -32 -safe-string -open CCShims_)
|
||||||
(ocamlopt_flags :standard (:include ../flambda.flags))
|
|
||||||
(libraries containers threads))
|
(libraries containers threads))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,5 +5,4 @@
|
||||||
(public_name containers.top)
|
(public_name containers.top)
|
||||||
(wrapped false)
|
(wrapped false)
|
||||||
(flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string)
|
(flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string)
|
||||||
(ocamlopt_flags :standard (:include ../flambda.flags))
|
|
||||||
(libraries compiler-libs.common containers containers.unix))
|
(libraries compiler-libs.common containers containers.unix))
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,4 @@
|
||||||
(wrapped false)
|
(wrapped false)
|
||||||
(optional)
|
(optional)
|
||||||
(flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string)
|
(flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string)
|
||||||
(ocamlopt_flags :standard (:include ../flambda.flags))
|
(libraries unix))
|
||||||
(libraries unix)
|
|
||||||
)
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue