From 18ffdd707b2e1e5dfe3407b971a3f783373afe1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8C=97=E9=9C=B2?= <69190413+illusory0x0@users.noreply.github.com> Date: Wed, 29 Jan 2025 11:23:53 +0800 Subject: [PATCH 1/3] add CCArray.of_iter --- src/core/CCArray.ml | 6 ++++++ src/core/CCArray.mli | 5 +++++ src/core/CCArrayLabels.mli | 5 +++++ tests/core/t_array.ml | 3 +++ 4 files changed, 19 insertions(+) diff --git a/src/core/CCArray.ml b/src/core/CCArray.ml index 00c57142d..f89043685 100644 --- a/src/core/CCArray.ml +++ b/src/core/CCArray.ml @@ -466,6 +466,12 @@ let to_seq a = let to_iter a k = iter k a +let of_iter (i : 'a iter) : 'a array = + let open CCVector in + let vec = create () in + i (push vec); + to_array vec + let to_gen a = let k = ref 0 in fun () -> diff --git a/src/core/CCArray.mli b/src/core/CCArray.mli index d19d42523..7f0a1ce91 100644 --- a/src/core/CCArray.mli +++ b/src/core/CCArray.mli @@ -240,6 +240,11 @@ val to_iter : 'a t -> 'a iter in modification of the iterator. @since 2.8 *) +val of_iter : 'a iter -> 'a t +(** [of_iter iter] builds a array from a given [iter]. + In the result, elements appear in the same order as they did in the source [iter]. + @since 3.15 *) + val to_seq : 'a t -> 'a Seq.t (** [to_seq a] returns a [Seq.t] of the elements of an array [a]. The input array [a] is shared with the sequence and modification of it will result diff --git a/src/core/CCArrayLabels.mli b/src/core/CCArrayLabels.mli index 8663c7ddd..f681e4822 100644 --- a/src/core/CCArrayLabels.mli +++ b/src/core/CCArrayLabels.mli @@ -248,6 +248,11 @@ val to_iter : 'a t -> 'a iter in modification of the iterator. @since 2.8 *) +val of_iter : 'a iter -> 'a t +(** [of_iter iter] builds a array from a given [iter]. + In the result, elements appear in the same order as they did in the source [iter]. + @since 3.15 *) + val to_seq : 'a t -> 'a Seq.t (** [to_seq a] returns a [Seq.t] of the elements of an array [a]. The input array [a] is shared with the sequence and modification of it will result diff --git a/tests/core/t_array.ml b/tests/core/t_array.ml index ecc76690c..da836f054 100644 --- a/tests/core/t_array.ml +++ b/tests/core/t_array.ml @@ -309,3 +309,6 @@ q ~count:300 arr_arbitrary (fun a -> Array.sort CCInt.compare a1; sort_generic (module IA) ~cmp:CCInt.compare a2; a1 = a2) +;; + +q Q.(array int) (fun a -> of_iter (to_iter a) = a) From 765a8da8763896be475a2e511b29d13b74398d96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8C=97=E9=9C=B2?= <69190413+illusory0x0@users.noreply.github.com> Date: Thu, 30 Jan 2025 09:37:57 +0800 Subject: [PATCH 2/3] update docs --- src/core/CCArray.mli | 2 +- src/core/CCArrayLabels.mli | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/CCArray.mli b/src/core/CCArray.mli index 7f0a1ce91..e411838c7 100644 --- a/src/core/CCArray.mli +++ b/src/core/CCArray.mli @@ -243,7 +243,7 @@ val to_iter : 'a t -> 'a iter val of_iter : 'a iter -> 'a t (** [of_iter iter] builds a array from a given [iter]. In the result, elements appear in the same order as they did in the source [iter]. - @since 3.15 *) + @since NEXT_RELEASE *) val to_seq : 'a t -> 'a Seq.t (** [to_seq a] returns a [Seq.t] of the elements of an array [a]. diff --git a/src/core/CCArrayLabels.mli b/src/core/CCArrayLabels.mli index f681e4822..fd6552bf4 100644 --- a/src/core/CCArrayLabels.mli +++ b/src/core/CCArrayLabels.mli @@ -251,7 +251,7 @@ val to_iter : 'a t -> 'a iter val of_iter : 'a iter -> 'a t (** [of_iter iter] builds a array from a given [iter]. In the result, elements appear in the same order as they did in the source [iter]. - @since 3.15 *) + @since NEXT_RELEASE *) val to_seq : 'a t -> 'a Seq.t (** [to_seq a] returns a [Seq.t] of the elements of an array [a]. From fd1495324a394a4836c5e1da49981b6a290b7cf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8C=97=E9=9C=B2?= <69190413+illusory0x0@users.noreply.github.com> Date: Thu, 30 Jan 2025 10:00:48 +0800 Subject: [PATCH 3/3] add benchmark --- benchs/run_benchs.ml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/benchs/run_benchs.ml b/benchs/run_benchs.ml index ca50ad18d..306a7eab3 100644 --- a/benchs/run_benchs.ml +++ b/benchs/run_benchs.ml @@ -1153,12 +1153,14 @@ module Iter_ = struct let bench_to_array n = let iter () = Iter.to_array Iter.(1 -- n) and gen () = Gen.to_array Gen.(1 -- n) - and oseq () = OSeq.to_array OSeq.(1 -- n) in + and oseq () = OSeq.to_array OSeq.(1 -- n) + and of_iter () = CCArray.of_iter Iter.(1 -- n) in B.throughputN 3 ~repeat [ "iter.to_array", iter, (); "gen.to_array", gen, (); "oseq.to_array", oseq, (); + "ccarray.of_iter", of_iter, (); ] let bench_cons n =