mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
add labels in CCArray
This commit is contained in:
parent
f29329fb03
commit
b1837e7d05
2 changed files with 91 additions and 90 deletions
|
|
@ -22,9 +22,9 @@ type 'a t = 'a array
|
|||
|
||||
let empty = [| |]
|
||||
|
||||
let map = Array.map
|
||||
let map ~f a = Array.map f a
|
||||
|
||||
let map2 f a b =
|
||||
let map2 ~f a b =
|
||||
if Array.length a <> Array.length b then invalid_arg "map2";
|
||||
Array.init (Array.length a) (fun i -> f (Array.unsafe_get a i) (Array.unsafe_get b i))
|
||||
|
||||
|
|
@ -49,15 +49,15 @@ let get_safe a i =
|
|||
|
||||
let set = Array.set
|
||||
|
||||
let fold = Array.fold_left
|
||||
let fold ~f ~init a = Array.fold_left f init a
|
||||
|
||||
let foldi f acc a =
|
||||
let foldi ~f ~init a =
|
||||
let rec aux acc i =
|
||||
if i = Array.length a then acc else aux (f acc i a.(i)) (i+1)
|
||||
in
|
||||
aux acc 0
|
||||
aux init 0
|
||||
|
||||
let fold_while f acc a =
|
||||
let fold_while ~f ~init a =
|
||||
let rec fold_while_i f acc i =
|
||||
if i < Array.length a then
|
||||
let acc, cont = f acc a.(i) in
|
||||
|
|
@ -65,15 +65,16 @@ let fold_while f acc a =
|
|||
| `Stop -> acc
|
||||
| `Continue -> fold_while_i f acc (i+1)
|
||||
else acc
|
||||
in fold_while_i f acc 0
|
||||
in fold_while_i f init 0
|
||||
|
||||
(*$T
|
||||
fold_while (fun acc b -> if b then acc+1, `Continue else acc, `Stop) 0 (Array.of_list [true;true;false;true]) = 2
|
||||
fold_while ~f:(fun acc b -> if b then acc+1, `Continue else acc, `Stop) \
|
||||
~init:0 (Array.of_list [true;true;false;true]) = 2
|
||||
*)
|
||||
|
||||
let iter = Array.iter
|
||||
let iter ~f a = Array.iter f a
|
||||
|
||||
let iteri = Array.iteri
|
||||
let iteri ~f a = Array.iteri f a
|
||||
|
||||
let blit = Array.blit
|
||||
|
||||
|
|
@ -98,53 +99,53 @@ let reverse_in_place a =
|
|||
a = [| 6;5;4;3;2;1 |]
|
||||
*)
|
||||
|
||||
let sorted cmp a =
|
||||
let sorted ~f a =
|
||||
let b = Array.copy a in
|
||||
Array.sort cmp b;
|
||||
Array.sort f b;
|
||||
b
|
||||
|
||||
(*$= & ~cmp:(=) ~printer:Q.Print.(array int)
|
||||
[||] (sorted Pervasives.compare [||])
|
||||
[|0;1;2;3;4|] (sorted Pervasives.compare [|3;2;1;4;0|])
|
||||
[||] (sorted ~f:Pervasives.compare [||])
|
||||
[|0;1;2;3;4|] (sorted ~f:Pervasives.compare [|3;2;1;4;0|])
|
||||
*)
|
||||
|
||||
(*$Q
|
||||
Q.(array int) (fun a -> \
|
||||
let b = Array.copy a in \
|
||||
Array.sort Pervasives.compare b; b = sorted Pervasives.compare a)
|
||||
Array.sort Pervasives.compare b; b = sorted ~f:Pervasives.compare a)
|
||||
*)
|
||||
|
||||
let sort_indices cmp a =
|
||||
let sort_indices ~f:cmp a =
|
||||
let len = Array.length a in
|
||||
let b = Array.init len (fun k->k) in
|
||||
Array.sort (fun k1 k2 -> cmp a.(k1) a.(k2)) b;
|
||||
b
|
||||
|
||||
(*$= & ~cmp:(=) ~printer:Q.Print.(array int)
|
||||
[||] (sort_indices Pervasives.compare [||])
|
||||
[|4;2;1;0;3|] (sort_indices Pervasives.compare [|"d";"c";"b";"e";"a"|])
|
||||
[||] (sort_indices ~f:Pervasives.compare [||])
|
||||
[|4;2;1;0;3|] (sort_indices ~f:Pervasives.compare [|"d";"c";"b";"e";"a"|])
|
||||
*)
|
||||
|
||||
(*$Q
|
||||
Q.(array printable_string) (fun a -> \
|
||||
let b = sort_indices String.compare a in \
|
||||
sorted String.compare a = Array.map (Array.get a) b)
|
||||
let b = sort_indices ~f:String.compare a in \
|
||||
sorted ~f:String.compare a = map ~f:(Array.get a) b)
|
||||
*)
|
||||
|
||||
let sort_ranking cmp a =
|
||||
let sort_ranking ~f:cmp a =
|
||||
let cmp_int : int -> int -> int = Pervasives.compare in
|
||||
sort_indices cmp_int (sort_indices cmp a)
|
||||
sort_indices ~f:cmp_int (sort_indices ~f:cmp a)
|
||||
|
||||
(*$= & ~cmp:(=) ~printer:Q.Print.(array int)
|
||||
[||] (sort_ranking Pervasives.compare [||])
|
||||
[|3;2;1;4;0|] (sort_ranking Pervasives.compare [|"d";"c";"b";"e";"a"|])
|
||||
[||] (sort_ranking ~f:Pervasives.compare [||])
|
||||
[|3;2;1;4;0|] (sort_ranking ~f:Pervasives.compare [|"d";"c";"b";"e";"a"|])
|
||||
*)
|
||||
|
||||
(*$Q
|
||||
Q.(array printable_string) (fun a -> \
|
||||
let b = sort_ranking String.compare a in \
|
||||
let a_sorted = sorted String.compare a in \
|
||||
a = Array.map (Array.get a_sorted) b)
|
||||
let b = sort_ranking ~f:String.compare a in \
|
||||
let a_sorted = sorted ~f:String.compare a in \
|
||||
a = map ~f:(Array.get a_sorted) b)
|
||||
*)
|
||||
|
||||
let rev a =
|
||||
|
|
@ -168,16 +169,16 @@ let rec find_aux f a i =
|
|||
| Some _ as res -> res
|
||||
| None -> find_aux f a (i+1)
|
||||
|
||||
let find f a =
|
||||
let find ~f a =
|
||||
find_aux (fun _ -> f ) a 0
|
||||
|
||||
let findi f a =
|
||||
let findi ~f a =
|
||||
find_aux f a 0
|
||||
|
||||
let find_idx p a =
|
||||
find_aux (fun i x -> if p x then Some (i,x) else None) a 0
|
||||
let find_idx ~f a =
|
||||
find_aux (fun i x -> if f x then Some (i,x) else None) a 0
|
||||
|
||||
let filter_map f a =
|
||||
let filter_map ~f a =
|
||||
let rec aux acc i =
|
||||
if i = Array.length a
|
||||
then (
|
||||
|
|
@ -197,8 +198,8 @@ let filter_map f a =
|
|||
= [| "2"; "4"; "6" |]
|
||||
*)
|
||||
|
||||
let filter p a =
|
||||
filter_map (fun x -> if p x then Some x else None) a
|
||||
let filter ~f a =
|
||||
filter_map a ~f:(fun x -> if f x then Some x else None)
|
||||
|
||||
(* append [rev a] in front of [acc] *)
|
||||
let rec __rev_append_list a acc i =
|
||||
|
|
@ -207,7 +208,7 @@ let rec __rev_append_list a acc i =
|
|||
else
|
||||
__rev_append_list a (a.(i) :: acc) (i+1)
|
||||
|
||||
let flat_map f a =
|
||||
let flat_map ~f a =
|
||||
let rec aux acc i =
|
||||
if i = Array.length a
|
||||
then (
|
||||
|
|
@ -251,11 +252,11 @@ let _lookup_exn ~cmp k a i j =
|
|||
| n when n<0 -> _lookup_rec ~cmp k a (i+1) (j-1)
|
||||
| _ -> raise Not_found (* too high *)
|
||||
|
||||
let lookup_exn ?(cmp=Pervasives.compare) k a =
|
||||
_lookup_exn ~cmp k a 0 (Array.length a-1)
|
||||
let lookup_exn ?(cmp=Pervasives.compare) ~key a =
|
||||
_lookup_exn ~cmp key a 0 (Array.length a-1)
|
||||
|
||||
let lookup ?(cmp=Pervasives.compare) k a =
|
||||
try Some (_lookup_exn ~cmp k a 0 (Array.length a-1))
|
||||
let lookup ?(cmp=Pervasives.compare) ~key a =
|
||||
try Some (_lookup_exn ~cmp key a 0 (Array.length a-1))
|
||||
with Not_found -> None
|
||||
|
||||
(*$T
|
||||
|
|
@ -268,20 +269,20 @@ let lookup ?(cmp=Pervasives.compare) k a =
|
|||
lookup 2 [| 1 |] = None
|
||||
*)
|
||||
|
||||
let bsearch ?(cmp=Pervasives.compare) k a =
|
||||
let bsearch ?(cmp=Pervasives.compare) ~key a =
|
||||
let rec aux i j =
|
||||
if i > j
|
||||
then `Just_after j
|
||||
else
|
||||
let middle = i + (j - i) / 2 in (* avoid overflow *)
|
||||
match cmp k a.(middle) with
|
||||
match cmp key a.(middle) with
|
||||
| 0 -> `At middle
|
||||
| n when n<0 -> aux i (middle - 1)
|
||||
| _ -> aux (middle + 1) j
|
||||
in
|
||||
let n = Array.length a in
|
||||
if n=0 then `Empty
|
||||
else match cmp a.(0) k, cmp a.(n-1) k with
|
||||
else match cmp a.(0) key, cmp a.(n-1) key with
|
||||
| c, _ when c>0 -> `All_bigger
|
||||
| _, c when c<0 -> `All_lower
|
||||
| _ -> aux 0 (n-1)
|
||||
|
|
@ -296,37 +297,37 @@ let bsearch ?(cmp=Pervasives.compare) k a =
|
|||
bsearch 3 [| |] = `Empty
|
||||
*)
|
||||
|
||||
let (>>=) a f = flat_map f a
|
||||
let (>>=) a f = flat_map ~f a
|
||||
|
||||
let (>>|) a f = map f a
|
||||
let (>>|) a f = map ~f a
|
||||
|
||||
let (>|=) a f = map f a
|
||||
let (>|=) a f = map ~f a
|
||||
|
||||
let for_all p a =
|
||||
let for_all ~f a =
|
||||
let rec aux i =
|
||||
i = Array.length a || (p a.(i) && aux (i+1))
|
||||
i = Array.length a || (f a.(i) && aux (i+1))
|
||||
in
|
||||
aux 0
|
||||
|
||||
let exists p a =
|
||||
let exists ~f a =
|
||||
let rec aux i =
|
||||
i <> Array.length a && (p a.(i) || aux (i+1))
|
||||
i <> Array.length a && (f a.(i) || aux (i+1))
|
||||
in
|
||||
aux 0
|
||||
|
||||
let rec _for_all2 p a1 a2 i1 i2 ~len =
|
||||
len=0 || (p a1.(i1) a2.(i2) && _for_all2 p a1 a2 (i1+1) (i2+1) ~len:(len-1))
|
||||
|
||||
let for_all2 p a b =
|
||||
let for_all2 ~f a b =
|
||||
Array.length a = Array.length b
|
||||
&&
|
||||
_for_all2 p a b 0 0 ~len:(Array.length a)
|
||||
_for_all2 f a b 0 0 ~len:(Array.length a)
|
||||
|
||||
let rec _exists2 p a1 a2 i1 i2 ~len =
|
||||
len>0 && (p a1.(i1) a2.(i2) || _exists2 p a1 a2 (i1+1) (i2+1) ~len:(len-1))
|
||||
|
||||
let exists2 p a b =
|
||||
_exists2 p a b 0 0 ~len:(min (Array.length a) (Array.length b))
|
||||
let exists2 ~f a b =
|
||||
_exists2 f a b 0 0 ~len:(min (Array.length a) (Array.length b))
|
||||
|
||||
let _iter2 f a b i j ~len =
|
||||
for o = 0 to len-1 do
|
||||
|
|
@ -342,13 +343,13 @@ let _fold2 f acc a b i j ~len =
|
|||
in
|
||||
aux acc 0
|
||||
|
||||
let iter2 f a b =
|
||||
let iter2 ~f a b =
|
||||
if length a <> length b then invalid_arg "iter2";
|
||||
_iter2 f a b 0 0 ~len:(Array.length a)
|
||||
|
||||
let fold2 f acc a b =
|
||||
let fold2 ~f ~init a b =
|
||||
if length a <> length b then invalid_arg "fold2";
|
||||
_fold2 f acc a b 0 0 ~len:(Array.length a)
|
||||
_fold2 f init a b 0 0 ~len:(Array.length a)
|
||||
|
||||
let (--) i j =
|
||||
if i<=j
|
||||
|
|
@ -381,9 +382,9 @@ let (--^) i j =
|
|||
|
||||
(** all the elements of a, but the i-th, into a list *)
|
||||
let except_idx a i =
|
||||
foldi
|
||||
(fun acc j elt -> if i = j then acc else elt::acc)
|
||||
[] a
|
||||
foldi a
|
||||
~f:(fun acc j elt -> if i = j then acc else elt::acc)
|
||||
~init:[]
|
||||
|
||||
let equal eq a b =
|
||||
let rec aux i =
|
||||
|
|
@ -469,7 +470,7 @@ let pp_i ?(sep=", ") pp_item out a =
|
|||
pp_item k out a.(k)
|
||||
done
|
||||
|
||||
let to_seq a k = iter k a
|
||||
let to_seq a yield = iter ~f:yield a
|
||||
|
||||
let to_gen a =
|
||||
let k = ref 0 in
|
||||
|
|
|
|||
|
|
@ -31,19 +31,19 @@ val set : 'a t -> int -> 'a -> unit
|
|||
|
||||
val length : _ t -> int
|
||||
|
||||
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
|
||||
val fold : f:('a -> 'b -> 'a) -> init:'a -> 'b t -> 'a
|
||||
|
||||
val foldi : ('a -> int -> 'b -> 'a) -> 'a -> 'b t -> 'a
|
||||
val foldi : f:('a -> int -> 'b -> 'a) -> init:'a -> 'b t -> 'a
|
||||
(** Fold left on array, with index *)
|
||||
|
||||
val fold_while : ('a -> 'b -> 'a * [`Stop | `Continue]) -> 'a -> 'b t -> 'a
|
||||
val fold_while : f:('a -> 'b -> 'a * [`Stop | `Continue]) -> init:'a -> 'b t -> 'a
|
||||
(** Fold left on array until a stop condition via [('a, `Stop)] is
|
||||
indicated by the accumulator
|
||||
@since 0.8 *)
|
||||
|
||||
val iter : ('a -> unit) -> 'a t -> unit
|
||||
val iter : f:('a -> unit) -> 'a t -> unit
|
||||
|
||||
val iteri : (int -> 'a -> unit) -> 'a t -> unit
|
||||
val iteri : f:(int -> 'a -> unit) -> 'a t -> unit
|
||||
|
||||
val blit : 'a t -> int -> 'a t -> int -> int -> unit
|
||||
(** [blit from i into j len] copies [len] elements from the first array
|
||||
|
|
@ -52,18 +52,18 @@ val blit : 'a t -> int -> 'a t -> int -> int -> unit
|
|||
val reverse_in_place : 'a t -> unit
|
||||
(** Reverse the array in place *)
|
||||
|
||||
val sorted : ('a -> 'a -> int) -> 'a t -> 'a array
|
||||
val sorted : f:('a -> 'a -> int) -> 'a t -> 'a array
|
||||
(** [sorted cmp a] makes a copy of [a] and sorts it with [cmp].
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
val sort_indices : ('a -> 'a -> int) -> 'a t -> int array
|
||||
val sort_indices : f:('a -> 'a -> int) -> 'a t -> int array
|
||||
(** [sort_indices cmp a] returns a new array [b], with the same length as [a],
|
||||
such that [b.(i)] is the index of the [i]-th element of [a] in [sort cmp a].
|
||||
In other words, [map (fun i -> a.(i)) (sort_indices a) = sorted cmp a].
|
||||
[a] is not modified.
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
val sort_ranking : ('a -> 'a -> int) -> 'a t -> int array
|
||||
val sort_ranking : f:('a -> 'a -> int) -> 'a t -> int array
|
||||
(** [sort_ranking cmp a] returns a new array [b], with the same length as [a],
|
||||
such that [b.(i)] is the position in [sorted cmp a] of the [i]-th
|
||||
element of [a].
|
||||
|
|
@ -75,31 +75,31 @@ val sort_ranking : ('a -> 'a -> int) -> 'a t -> int array
|
|||
[lookup_exn a.(i) (sorted a) = (sorted_ranking a).(i)]
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
val find : ('a -> 'b option) -> 'a t -> 'b option
|
||||
val find : f:('a -> 'b option) -> 'a t -> 'b option
|
||||
(** [find f a] returns [Some y] if there is an element [x] such
|
||||
that [f x = Some y], else it returns [None] *)
|
||||
|
||||
val findi : (int -> 'a -> 'b option) -> 'a t -> 'b option
|
||||
val findi : f:(int -> 'a -> 'b option) -> 'a t -> 'b option
|
||||
(** Like {!find}, but also pass the index to the predicate function.
|
||||
@since 0.3.4 *)
|
||||
|
||||
val find_idx : ('a -> bool) -> 'a t -> (int * 'a) option
|
||||
val find_idx : f:('a -> bool) -> 'a t -> (int * 'a) option
|
||||
(** [find_idx p x] returns [Some (i,x)] where [x] is the [i]-th element of [l],
|
||||
and [p x] holds. Otherwise returns [None]
|
||||
@since 0.3.4 *)
|
||||
|
||||
val lookup : ?cmp:'a ord -> 'a -> 'a t -> int option
|
||||
val lookup : ?cmp:'a ord -> key:'a -> 'a t -> int option
|
||||
(** Lookup the index of some value in a sorted array.
|
||||
@return [None] if the key is not present, or
|
||||
[Some i] ([i] the index of the key) otherwise *)
|
||||
|
||||
val lookup_exn : ?cmp:'a ord -> 'a -> 'a t -> int
|
||||
val lookup_exn : ?cmp:'a ord -> key:'a -> 'a t -> int
|
||||
(** Same as {!lookup_exn}, but
|
||||
@raise Not_found if the key is not present *)
|
||||
|
||||
val bsearch : ?cmp:('a -> 'a -> int) -> 'a -> 'a t ->
|
||||
val bsearch : ?cmp:('a -> 'a -> int) -> key:'a -> 'a t ->
|
||||
[ `All_lower | `All_bigger | `Just_after of int | `Empty | `At of int ]
|
||||
(** [bsearch ?cmp x arr] finds the index of the object [x] in the array [arr],
|
||||
(** [bsearch ?cmp key arr] finds the index of the object [key] in the array [arr],
|
||||
provided [arr] is {b sorted} using [cmp]. If the array is not sorted,
|
||||
the result is not specified (may raise Invalid_argument).
|
||||
|
||||
|
|
@ -107,35 +107,35 @@ val bsearch : ?cmp:('a -> 'a -> int) -> 'a -> 'a t ->
|
|||
(dichotomic search).
|
||||
|
||||
@return
|
||||
- [`At i] if [cmp arr.(i) x = 0] (for some i)
|
||||
- [`All_lower] if all elements of [arr] are lower than [x]
|
||||
- [`All_bigger] if all elements of [arr] are bigger than [x]
|
||||
- [`Just_after i] if [arr.(i) < x < arr.(i+1)]
|
||||
- [`At i] if [cmp arr.(i) key = 0] (for some i)
|
||||
- [`All_lower] if all elements of [arr] are lower than [key]
|
||||
- [`All_bigger] if all elements of [arr] are bigger than [key]
|
||||
- [`Just_after i] if [arr.(i) < key < arr.(i+1)]
|
||||
- [`Empty] if the array is empty
|
||||
|
||||
@raise Invalid_argument if the array is found to be unsorted w.r.t [cmp]
|
||||
@since 0.13 *)
|
||||
|
||||
val for_all : ('a -> bool) -> 'a t -> bool
|
||||
val for_all : f:('a -> bool) -> 'a t -> bool
|
||||
|
||||
val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
|
||||
val for_all2 : f:('a -> 'b -> bool) -> 'a t -> 'b t -> bool
|
||||
(** Forall on pairs of arrays.
|
||||
@raise Invalid_argument if they have distinct lengths
|
||||
allow different types @since 0.20 *)
|
||||
|
||||
val exists : ('a -> bool) -> 'a t -> bool
|
||||
val exists : f:('a -> bool) -> 'a t -> bool
|
||||
|
||||
val exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
|
||||
val exists2 : f:('a -> 'b -> bool) -> 'a t -> 'b t -> bool
|
||||
(** Exists on pairs of arrays.
|
||||
@raise Invalid_argument if they have distinct lengths
|
||||
allow different types @since 0.20 *)
|
||||
|
||||
val fold2 : ('acc -> 'a -> 'b -> 'acc) -> 'acc -> 'a t -> 'b t -> 'acc
|
||||
val fold2 : f:('acc -> 'a -> 'b -> 'acc) -> init:'acc -> 'a t -> 'b t -> 'acc
|
||||
(** Fold on two arrays stepwise.
|
||||
@raise Invalid_argument if they have distinct lengths
|
||||
@since 0.20 *)
|
||||
|
||||
val iter2 : ('a -> 'b -> unit) -> 'a t -> 'b t -> unit
|
||||
val iter2 : f:('a -> 'b -> unit) -> 'a t -> 'b t -> unit
|
||||
(** Iterate on two arrays stepwise.
|
||||
@raise Invalid_argument if they have distinct lengths
|
||||
@since 0.20 *)
|
||||
|
|
@ -162,9 +162,9 @@ val pp: ?sep:string -> 'a printer -> 'a t printer
|
|||
val pp_i: ?sep:string -> (int -> 'a printer) -> 'a t printer
|
||||
(** Print an array, giving the printing function both index and item *)
|
||||
|
||||
val map : ('a -> 'b) -> 'a t -> 'b t
|
||||
val map : f:('a -> 'b) -> 'a t -> 'b t
|
||||
|
||||
val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
|
||||
val map2 : f:('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
|
||||
(** Map on two arrays stepwise.
|
||||
@raise Invalid_argument if they have distinct lengths
|
||||
@since 0.20 *)
|
||||
|
|
@ -173,14 +173,14 @@ val rev : 'a t -> 'a t
|
|||
(** Copy + reverse in place
|
||||
@since 0.20 *)
|
||||
|
||||
val filter : ('a -> bool) -> 'a t -> 'a t
|
||||
val filter : f:('a -> bool) -> 'a t -> 'a t
|
||||
(** Filter elements out of the array. Only the elements satisfying
|
||||
the given predicate will be kept. *)
|
||||
|
||||
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
|
||||
val filter_map : f:('a -> 'b option) -> 'a t -> 'b t
|
||||
(** Map each element into another value, or discard it *)
|
||||
|
||||
val flat_map : ('a -> 'b t) -> 'a t -> 'b array
|
||||
val flat_map : f:('a -> 'b t) -> 'a t -> 'b array
|
||||
(** Transform each element into an array, then flatten *)
|
||||
|
||||
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue