The unpac monorepo manager self-hosting as a monorepo using unpac
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Implement `Dynarray.{exists2, for_all2}` (#13885)

authored by

hirrolot and committed by
GitHub
574783a8 979ce410

+92
+3
Changes
··· 150 150 151 151 ### Standard library: 152 152 153 + - #13885: Add Dynarray.{exists2, for_all2}. 154 + (T. Kinsart, review by Daniel Bünzli, Gabriel Scherer, and Nicolás Ojeda Bär) 155 + 153 156 - #13836: Add [Float.]Array.{equal,compare}. 154 157 (Daniel Bünzli, review by Nicolás Ojeda Bär and Gabriel Scherer) 155 158
+43
stdlib/dynarray.ml
··· 409 409 let[@inline never] empty_dynarray f = 410 410 Printf.ksprintf invalid_arg 411 411 "Dynarray.%s: empty array" f 412 + 413 + let[@inline never] different_lengths f ~length1 ~length2 = 414 + Printf.ksprintf invalid_arg 415 + "Dynarray.%s: array length mismatch: %d <> %d" 416 + f length1 length2 412 417 end 413 418 414 419 (* Detecting iterator invalidation. ··· 960 965 in 961 966 let res = loop p arr dummy 0 length in 962 967 check_same_length "for_all" a ~length; 968 + res 969 + 970 + let exists2 p a1 a2 = 971 + let Pack {arr = arr1; length = length1; dummy = dummy1} = a1 in 972 + let Pack {arr = arr2; length = length2; dummy = dummy2} = a2 in 973 + check_valid_length length1 arr1; 974 + check_valid_length length2 arr2; 975 + if length1 <> length2 then 976 + Error.different_lengths "exists2" ~length1 ~length2; 977 + let rec loop p arr1 dummy1 arr2 dummy2 i length = 978 + if i = length then false 979 + else 980 + p (unsafe_get arr1 ~dummy:dummy1 ~i ~length) 981 + (unsafe_get arr2 ~dummy:dummy2 ~i ~length) 982 + || loop p arr1 dummy1 arr2 dummy2 (i + 1) length 983 + in 984 + let res = loop p arr1 dummy1 arr2 dummy2 0 length1 in 985 + check_same_length "exists2" a1 ~length:length1; 986 + check_same_length "exists2" a2 ~length:length2; 987 + res 988 + 989 + let for_all2 p a1 a2 = 990 + let Pack {arr = arr1; length = length1; dummy = dummy1} = a1 in 991 + let Pack {arr = arr2; length = length2; dummy = dummy2} = a2 in 992 + check_valid_length length1 arr1; 993 + check_valid_length length2 arr2; 994 + if length1 <> length2 then 995 + Error.different_lengths "for_all2" ~length1 ~length2; 996 + let rec loop p arr1 dummy1 arr2 dummy2 i length = 997 + if i = length then true 998 + else 999 + p (unsafe_get arr1 ~dummy:dummy1 ~i ~length) 1000 + (unsafe_get arr2 ~dummy:dummy2 ~i ~length) 1001 + && loop p arr1 dummy1 arr2 dummy2 (i + 1) length 1002 + in 1003 + let res = loop p arr1 dummy1 arr2 dummy2 0 length1 in 1004 + check_same_length "for_all2" a1 ~length:length1; 1005 + check_same_length "for_all2" a2 ~length:length2; 963 1006 res 964 1007 965 1008 let filter f a =
+16
stdlib/dynarray.mli
··· 327 327 [for_all f a] is [f x0 && f x1 && f x2]. 328 328 *) 329 329 330 + val exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool 331 + (** Same as {!exists}, but for a two-argument predicate. 332 + 333 + @raise Invalid_argument if the two arrays have different lengths. 334 + 335 + @since 5.4 336 + *) 337 + 338 + val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool 339 + (** Same as {!for_all}, but for a two-argument predicate. 340 + 341 + @raise Invalid_argument if the two arrays have different lengths. 342 + 343 + @since 5.4 344 + *) 345 + 330 346 val mem : 'a -> 'a t -> bool 331 347 (** [mem a set] is true if and only if [a] is structurally equal 332 348 to an element of [set] (i.e. there is an [x] in [set] such that
+30
testsuite/tests/lib-dynarray/test.ml
··· 87 87 let a = A.of_list [1; 2; 3] in 88 88 assert (A.fold_right List.cons a [] = [1; 2; 3]);; 89 89 90 + (* exists2 *) 91 + let () = 92 + let a = A.of_list [1; 2; 3] in 93 + assert (A.exists2 (=) a a); 94 + assert (not (A.exists2 (=) (A.of_list []) (A.of_list []))); 95 + assert (A.exists2 (=) a (A.of_list [1; 4; 3])); 96 + assert (match A.exists2 (=) a (A.of_list [1; 2]) with 97 + | exception (Invalid_argument _) -> true 98 + | _ -> false 99 + ); 100 + assert (match A.exists2 (=) (A.of_list [1; 2]) a with 101 + | exception (Invalid_argument _) -> true 102 + | _ -> false 103 + );; 104 + 105 + (* for_all2 *) 106 + let () = 107 + let a = A.of_list [1; 2; 3] in 108 + assert (A.for_all2 (=) a a); 109 + assert (A.for_all2 (=) (A.of_list []) (A.of_list [])); 110 + assert (not (A.for_all2 (=) a (A.of_list [1; 4; 3]))); 111 + assert (match A.for_all2 (=) a (A.of_list [1; 2]) with 112 + | exception (Invalid_argument _) -> true 113 + | _ -> false 114 + ); 115 + assert (match A.for_all2 (=) (A.of_list [1; 2]) a with 116 + | exception (Invalid_argument _) -> true 117 + | _ -> false 118 + );; 119 + 90 120 (** {1:adding Adding elements} *) 91 121 92 122 (** add_last was tested above *)