Library
Module
Module type
Parameter
Class
Class type
Array Slice
type 'a klist = unit -> [ `Nil | `Cons of 'a * 'a klist ]
type 'a random_gen = Random.State.t -> 'a
type 'a printer = Format.formatter -> 'a -> unit
val empty : 'a t
empty
is the empty array slice.
equal eq as1 as2
is true
if the lengths of as1
and as2
are the same and if the corresponding elements test equal using eq
.
compare cmp as1 as2
compares the two slices as1
and as2
using the comparison function cmp
, element by element.
val get : 'a t -> int -> 'a
get as n
returns the element number n
of slice as
. The first element has number 0. The last element has number length as - 1
. You can also write as.(n)
instead of get as n
.
Raise Invalid_argument "index out of bounds"
if n
is outside the range 0 to (length as - 1)
.
val get_safe : 'a t -> int -> 'a option
get_safe as i
returns Some as.(i)
if i
is a valid index.
val make : 'a array -> int -> len:int -> 'a t
make a i ~len
creates a slice from given offset i
and length len
of the given array a
.
val of_slice : ('a array * int * int) -> 'a t
of_slice (a, i, len)
makes a slice from a triple (a, i, len)
where a
is the array, i
the offset in a
, and len
the number of elements of the slice.
val to_slice : 'a t -> 'a array * int * int
to_slice as
converts the slice as
into a triple (a, i, len)
where len
is the length of the sub-array of a
starting at offset i
.
val to_list : 'a t -> 'a list
to_list as
converts the slice as
directly to a list.
val full : 'a array -> 'a t
full a
creates a slice that covers the full array a
.
val underlying : 'a t -> 'a array
underlying as
returns the underlying array (shared). Modifying this array will modify the slice as
.
val copy : 'a t -> 'a array
copy as
copies the slice as
into a new array.
sub as i len
builds a new sub-slice that contains the given subrange specified by the index i
and the length len
.
val set : 'a t -> int -> 'a -> unit
set as n x
modifies the slice as
in place, replacing element number n
with x
. You can also write as.(n) <- x
instead of set as n x
.
Raise Invalid_argument "index out of bounds"
if n
is outside the range 0 to length as - 1
.
val length : _ t -> int
length as
returns the length (number of elements) of the given slice as
.
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
fold f acc as
computes f (... (f (f acc as.(0)) as.(1)) ...) as.(length as - 1)
.
val foldi : ('a -> int -> 'b -> 'a) -> 'a -> 'b t -> 'a
foldi f acc as
is just like fold
but it also passes in the index of each element as the second argument to the folded function f
.
val fold_while : ('a -> 'b -> 'a * [ `Stop | `Continue ]) -> 'a -> 'b t -> 'a
fold_while f acc as
folds left on slice as
until a stop condition via ('a, `Stop)
is indicated by the accumulator.
val iter : ('a -> unit) -> 'a t -> unit
iter f as
applies function f
in turn to all elements of as
. It is equivalent to f as.(0); f as.(1); ...; f as.(length as - 1); ()
.
val iteri : (int -> 'a -> unit) -> 'a t -> unit
iteri f as
is like iter
, but the function f
is applied with the index of the element as first argument, and the element itself as second argument.
blit as1 o1 as2 o2 len
copies len
elements from slice as1
, starting at element number o1
, to slice as2
, starting at element number o2
. It works correctly even if as1
and as2
are the same slice, and the source and destination chunks overlap.
Raise Invalid_argument "CCArray_slice.blit"
if o1
and len
do not designate a valid subarray of as1
, or if o2
and len
do not designate a valid subarray of as2
.
val reverse_in_place : 'a t -> unit
reverse_in_place as
reverses the slice as
in place.
val sorted : ('a -> 'a -> int) -> 'a t -> 'a array
sorted cmp as
makes a copy of as
and sorts it with cmp
.
val sort_indices : ('a -> 'a -> int) -> 'a t -> int array
sort_indices cmp as
returns a new array b
, with the same length as as
, such that b.(i)
is the index at which the i
-th element of sorted cmp as
appears in as
. as
is not modified.
In other words, map (fun i -> as.(i)) (sort_indices cmp as) = sorted cmp as
. sort_indices
yields the inverse permutation of sort_ranking
.
val sort_ranking : ('a -> 'a -> int) -> 'a t -> int array
sort_ranking cmp as
returns a new array b
, with the same length as as
, such that b.(i)
is the index at which the i
-th element of as
appears in sorted cmp as
. as
is not modified.
In other words, map (fun i -> (sorted cmp as).(i)) (sort_ranking cmp as) = as
. sort_ranking
yields the inverse permutation of sort_indices
.
In the absence of duplicate elements in as
, we also have lookup_exn as.(i) (sorted as) = (sorted_ranking as).(i)
.
val find : ('a -> 'b option) -> 'a t -> 'b option
find f as
returns Some y
if there is an element x
such that f x = Some y
. Otherwise returns None
.
val findi : (int -> 'a -> 'b option) -> 'a t -> 'b option
findi f as
is like find
, but the index of the element is also passed to the predicate function f
.
val find_idx : ('a -> bool) -> 'a t -> (int * 'a) option
find_idx p as
returns Some (i,x)
where x
is the i
-th element of as
, and p x
holds. Otherwise returns None
.
lookup ~cmp x as
lookups the index i
of some key x
in the slice as
, provided as
is sorted using cmp
.
val bsearch :
cmp:('a -> 'a -> int) ->
'a ->
'a t ->
[ `All_lower | `All_bigger | `Just_after of int | `Empty | `At of int ]
bsearch ~cmp x as
finds the index of the object x
in the slice as
, provided as
is sorted using cmp
. If the slice is not sorted, the result is not specified (may raise Invalid_argument).
Complexity: O(log n)
where n is the length of the slice as
(dichotomic search).
val for_all : ('a -> bool) -> 'a t -> bool
for_all p [|as1; ...; asn|]
checks if all elements of the slice satisfy the predicate p
. That is, it returns (p as1) && (p as2) && ... && (p asn)
.
for_all2 p [|as1; ...; asn|] [|bs1; ...; bsn|]
is true
if each pair of elements asi bsi
satisfies the predicate p
. That is, it returns (p as1 bs1) && (p as2 bs2) && ... && (p asn bsn)
.
val exists : ('a -> bool) -> 'a t -> bool
exists p [|as1; ...; asn|]
is true
if at least one element of the slice satisfies the predicate p
. That is, it returns (p as1) || (p as2) || ... || (p asn)
.
exists2 p [|as1; ...; asn|] [|bs1; ...; bsn|]
is true
if any pair of elements asi bsi
satisfies the predicate p
. That is, it returns (p as1 bs1) || (p as2 bs2) || ... || (p asn bsn)
.
fold2 f acc as bs
fold on two slices as
and bs
stepwise. It computes f (... (f acc as1 bs1)...) asn bsn
.
iter2 f as bs
iterates on the two slices as
and bs
stepwise. It is equivalent to f as0 bs0; ...; f as.(length as - 1) bs.(length bs - 1); ()
.
val shuffle : 'a t -> unit
shuffle as
randomly shuffles the slice as
, in place.
val shuffle_with : Random.State.t -> 'a t -> unit
shuffle_with rs as
randomly shuffles the slice as
(like shuffle
) but a specialized random state rs
is used to control the random numbers being produced during shuffling (for reproducibility).
val random_choose : 'a t -> 'a random_gen
random_choose as rs
randomly chooses an element of as
.
to_seq as
returns a sequence
of the elements of a slice as
. The input slice as
is shared with the sequence and modification of it will result in modification of the sequence.
IO
pp ~sep pp_item ppf as
formats the slice as
on ppf
. Each element is formatted with pp_item
and elements are separated by sep
(defaults to ", ").