Module CCArray_slice
Array Slice
type 'a sequence
= ('a -> unit) -> unit
type 'a klist
= unit -> [ `Nil | `Cons of 'a * 'a klist ]
type 'a gen
= unit -> 'a option
type 'a equal
= 'a -> 'a -> bool
type 'a ord
= 'a -> 'a -> int
type 'a random_gen
= Stdlib.Random.State.t -> 'a
type 'a printer
= Stdlib.Format.formatter -> 'a -> unit
type 'a t
The type for an array slice, containing elements of type
'a
val empty : 'a t
empty
is the empty array slice.
val equal : 'a equal -> 'a t equal
equal eq as1 as2
istrue
if the lengths ofas1
andas2
are the same and if the corresponding elements test equal usingeq
.
val compare : 'a ord -> 'a t ord
compare cmp as1 as2
compares the two slicesas1
andas2
using the comparison functioncmp
, element by element.
val get : 'a t -> int -> 'a
get as n
returns the element numbern
of sliceas
. The first element has number 0. The last element has numberlength as - 1
. You can also writeas.(n)
instead ofget as n
.Raise
Invalid_argument "index out of bounds"
ifn
is outside the range 0 to(length as - 1)
.
val get_safe : 'a t -> int -> 'a option
get_safe as i
returnsSome as.(i)
ifi
is a valid index.- since
- 0.18
val make : 'a array -> int -> len:int -> 'a t
make a i ~len
creates a slice from given offseti
and lengthlen
of the given arraya
.- raises Invalid_argument
if the slice isn't valid.
val of_slice : ('a array * int * int) -> 'a t
of_slice (a, i, len)
makes a slice from a triple(a, i, len)
wherea
is the array,i
the offset ina
, andlen
the number of elements of the slice.- raises Invalid_argument
if the slice isn't valid (See
make
).
val to_slice : 'a t -> 'a array * int * int
to_slice as
converts the sliceas
into a triple(a, i, len)
wherelen
is the length of the sub-array ofa
starting at offseti
.
val to_list : 'a t -> 'a list
to_list as
converts the sliceas
directly to a list.- since
- 1.0
val full : 'a array -> 'a t
full a
creates a slice that covers the full arraya
.
val underlying : 'a t -> 'a array
underlying as
returns the underlying array (shared). Modifying this array will modify the sliceas
.
val copy : 'a t -> 'a array
copy as
copies the sliceas
into a new array.
val sub : 'a t -> int -> int -> 'a t
sub as i len
builds a new sub-slice that contains the given subrange specified by the indexi
and the lengthlen
.
val set : 'a t -> int -> 'a -> unit
set as n x
modifies the sliceas
in place, replacing element numbern
withx
. You can also writeas.(n) <- x
instead ofset as n x
.Raise
Invalid_argument "index out of bounds"
ifn
is outside the range 0 tolength as - 1
.
val length : _ t -> int
length as
returns the length (number of elements) of the given sliceas
.
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
fold f acc as
computesf (... (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 likefold
but it also passes in the index of each element as the second argument to the folded functionf
.
val fold_while : ('a -> 'b -> 'a * [ `Stop | `Continue ]) -> 'a -> 'b t -> 'a
fold_while f acc as
folds left on sliceas
until a stop condition via('a, `Stop)
is indicated by the accumulator.- since
- 0.8
val iter : ('a -> unit) -> 'a t -> unit
iter f as
applies functionf
in turn to all elements ofas
. It is equivalent tof as.(0); f as.(1); ...; f as.(length as - 1); ()
.
val iteri : (int -> 'a -> unit) -> 'a t -> unit
iteri f as
is likeiter
, but the functionf
is applied with the index of the element as first argument, and the element itself as second argument.
val blit : 'a t -> int -> 'a t -> int -> int -> unit
blit as1 o1 as2 o2 len
copieslen
elements from sliceas1
, starting at element numbero1
, to sliceas2
, starting at element numbero2
. It works correctly even ifas1
andas2
are the same slice, and the source and destination chunks overlap.Raise
Invalid_argument "CCArray_slice.blit"
ifo1
andlen
do not designate a valid subarray ofas1
, or ifo2
andlen
do not designate a valid subarray ofas2
.
val reverse_in_place : 'a t -> unit
reverse_in_place as
reverses the sliceas
in place.
val sorted : ('a -> 'a -> int) -> 'a t -> 'a array
sorted cmp as
makes a copy ofas
and sorts it withcmp
.- since
- 1.0
val sort_indices : ('a -> 'a -> int) -> 'a t -> int array
sort_indices cmp as
returns a new arrayb
, with the same length asas
, such thatb.(i)
is the index at which thei
-th element ofsorted cmp as
appears inas
.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 ofsort_ranking
.- since
- 1.0
val sort_ranking : ('a -> 'a -> int) -> 'a t -> int array
sort_ranking cmp as
returns a new arrayb
, with the same length asas
, such thatb.(i)
is the index at which thei
-th element ofas
appears insorted 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 ofsort_indices
.In the absence of duplicate elements in
as
, we also havelookup_exn as.(i) (sorted as) = (sorted_ranking as).(i)
.- since
- 1.0
val find : ('a -> 'b option) -> 'a t -> 'b option
find f as
returnsSome y
if there is an elementx
such thatf x = Some y
. Otherwise returnsNone
.
val findi : (int -> 'a -> 'b option) -> 'a t -> 'b option
findi f as
is likefind
, but the index of the element is also passed to the predicate functionf
.- since
- 0.3.4
val find_idx : ('a -> bool) -> 'a t -> (int * 'a) option
find_idx p as
returnsSome (i,x)
wherex
is thei
-th element ofas
, andp x
holds. Otherwise returnsNone
.- since
- 0.3.4
val lookup : cmp:'a ord -> 'a -> 'a t -> int option
lookup ~cmp x as
lookups the indexi
of some keyx
in the sliceas
, providedas
is sorted usingcmp
.- returns
None
if the keyx
is not present, orSome i
(i
the index of the key) otherwise.
val lookup_exn : cmp:'a ord -> 'a -> 'a t -> int
lookup_exn ~cmp x as
is likelookup
, but- raises Not_found
if the key
x
is not present.
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 objectx
in the sliceas
, providedas
is sorted usingcmp
. 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 sliceas
(dichotomic search).- returns
`At i
ifcmp as.(i) x = 0
(for some i).`All_lower
if all elements ofas
are lower thanx
.`All_bigger
if all elements ofas
are bigger thanx
.`Just_after i
ifas.(i) < x < as.(i+1)
.`Empty
if the sliceas
is empty.
- raises Invalid_argument
if the slice is found to be unsorted w.r.t
cmp
.
- since
- 0.13
val for_all : ('a -> bool) -> 'a t -> bool
for_all p [|as1; ...; asn|]
checks if all elements of the slice satisfy the predicatep
. That is, it returns(p as1) && (p as2) && ... && (p asn)
.
val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
for_all2 p [|as1; ...; asn|] [|bs1; ...; bsn|]
istrue
if each pair of elementsasi bsi
satisfies the predicatep
. That is, it returns(p as1 bs1) && (p as2 bs2) && ... && (p asn bsn)
.- raises Invalid_argument
if slices have distinct lengths. Allow different types.
- since
- 0.20
val exists : ('a -> bool) -> 'a t -> bool
exists p [|as1; ...; asn|]
istrue
if at least one element of the slice satisfies the predicatep
. That is, it returns(p as1) || (p as2) || ... || (p asn)
.
val exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
exists2 p [|as1; ...; asn|] [|bs1; ...; bsn|]
istrue
if any pair of elementsasi bsi
satisfies the predicatep
. That is, it returns(p as1 bs1) || (p as2 bs2) || ... || (p asn bsn)
.- raises Invalid_argument
if slices have distinct lengths. Allow different types.
- since
- 0.20
val fold2 : ('acc -> 'a -> 'b -> 'acc) -> 'acc -> 'a t -> 'b t -> 'acc
fold2 f acc as bs
fold on two slicesas
andbs
stepwise. It computesf (... (f acc as1 bs1)...) asn bsn
.- raises Invalid_argument
if slices have distinct lengths.
- since
- 0.20
val iter2 : ('a -> 'b -> unit) -> 'a t -> 'b t -> unit
iter2 f as bs
iterates on the two slicesas
andbs
stepwise. It is equivalent tof as0 bs0; ...; f as.(length as - 1) bs.(length bs - 1); ()
.- raises Invalid_argument
if slices have distinct lengths.
- since
- 0.20
val shuffle : 'a t -> unit
shuffle as
randomly shuffles the sliceas
, in place.
val shuffle_with : Stdlib.Random.State.t -> 'a t -> unit
shuffle_with rs as
randomly shuffles the sliceas
(likeshuffle
) but a specialized random staters
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 ofas
.- raises Not_found
if the array/slice is empty.
val to_seq : 'a t -> 'a sequence
to_seq as
returns asequence
of the elements of a sliceas
. The input sliceas
is shared with the sequence and modification of it will result in modification of the sequence.