Module CCArray_slice
Array Slice
type 'a klist= unit -> [ `Nil | `Cons of 'a * 'a klist ]type 'a gen= unit -> 'a optiontype 'a equal= 'a -> 'a -> booltype 'a ord= 'a -> 'a -> inttype 'a random_gen= Random.State.t -> 'atype 'a printer= Format.formatter -> 'a -> unittype 'a tThe type for an array slice, containing elements of type
'a
val empty : 'a temptyis the empty array slice.
val equal : 'a equal -> 'a t equalequal eq as1 as2istrueif the lengths ofas1andas2are the same and if the corresponding elements test equal usingeq.
val compare : 'a ord -> 'a t ordcompare cmp as1 as2compares the two slicesas1andas2using the comparison functioncmp, element by element.
val get : 'a t -> int -> 'aget as nreturns the element numbernof 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"ifnis outside the range 0 to(length as - 1).
val get_safe : 'a t -> int -> 'a optionget_safe as ireturnsSome as.(i)ifiis a valid index.- since
- 0.18
val make : 'a array -> int -> len:int -> 'a tmake a i ~lencreates a slice from given offsetiand lengthlenof the given arraya.- raises Invalid_argument
if the slice isn't valid.
val of_slice : ('a array * int * int) -> 'a tof_slice (a, i, len)makes a slice from a triple(a, i, len)whereais the array,ithe offset ina, andlenthe 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 * intto_slice asconverts the sliceasinto a triple(a, i, len)wherelenis the length of the sub-array ofastarting at offseti.
val to_list : 'a t -> 'a listto_list asconverts the sliceasdirectly to a list.- since
- 1.0
val full : 'a array -> 'a tfull acreates a slice that covers the full arraya.
val underlying : 'a t -> 'a arrayunderlying asreturns the underlying array (shared). Modifying this array will modify the sliceas.
val copy : 'a t -> 'a arraycopy ascopies the sliceasinto a new array.
val sub : 'a t -> int -> int -> 'a tsub as i lenbuilds a new sub-slice that contains the given subrange specified by the indexiand the lengthlen.
val set : 'a t -> int -> 'a -> unitset as n xmodifies the sliceasin place, replacing element numbernwithx. You can also writeas.(n) <- xinstead ofset as n x.Raise
Invalid_argument "index out of bounds"ifnis outside the range 0 tolength as - 1.
val length : _ t -> intlength asreturns the length (number of elements) of the given sliceas.
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'afold f acc ascomputesf (... (f (f acc as.(0)) as.(1)) ...) as.(length as - 1).
val foldi : ('a -> int -> 'b -> 'a) -> 'a -> 'b t -> 'afoldi f acc asis just likefoldbut 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 -> 'afold_while f acc asfolds left on sliceasuntil a stop condition via('a, `Stop)is indicated by the accumulator.- since
- 0.8
val iter : ('a -> unit) -> 'a t -> unititer f asapplies functionfin 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 -> unititeri f asis likeiter, but the functionfis 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 -> unitblit as1 o1 as2 o2 lencopieslenelements from sliceas1, starting at element numbero1, to sliceas2, starting at element numbero2. It works correctly even ifas1andas2are the same slice, and the source and destination chunks overlap.Raise
Invalid_argument "CCArray_slice.blit"ifo1andlendo not designate a valid subarray ofas1, or ifo2andlendo not designate a valid subarray ofas2.
val reverse_in_place : 'a t -> unitreverse_in_place asreverses the sliceasin place.
val sorted : ('a -> 'a -> int) -> 'a t -> 'a arraysorted cmp asmakes a copy ofasand sorts it withcmp.- since
- 1.0
val sort_indices : ('a -> 'a -> int) -> 'a t -> int arraysort_indices cmp asreturns a new arrayb, with the same length asas, such thatb.(i)is the index at which thei-th element ofsorted cmp asappears inas.asis not modified.In other words,
map (fun i -> as.(i)) (sort_indices cmp as) = sorted cmp as.sort_indicesyields the inverse permutation ofsort_ranking.- since
- 1.0
val sort_ranking : ('a -> 'a -> int) -> 'a t -> int arraysort_ranking cmp asreturns a new arrayb, with the same length asas, such thatb.(i)is the index at which thei-th element ofasappears insorted cmp as.asis not modified.In other words,
map (fun i -> (sorted cmp as).(i)) (sort_ranking cmp as) = as.sort_rankingyields 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 optionfind f asreturnsSome yif there is an elementxsuch thatf x = Some y. Otherwise returnsNone.
val findi : (int -> 'a -> 'b option) -> 'a t -> 'b optionfindi f asis 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) optionfind_idx p asreturnsSome (i,x)wherexis thei-th element ofas, andp xholds. Otherwise returnsNone.- since
- 0.3.4
val lookup : cmp:'a ord -> 'a -> 'a t -> int optionlookup ~cmp x aslookups the indexiof some keyxin the sliceas, providedasis sorted usingcmp.- returns
Noneif the keyxis not present, orSome i(ithe index of the key) otherwise.
val lookup_exn : cmp:'a ord -> 'a -> 'a t -> intlookup_exn ~cmp x asis likelookup, but- raises Not_found
if the key
xis 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 asfinds the index of the objectxin the sliceas, providedasis 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 iifcmp as.(i) x = 0(for some i).`All_lowerif all elements ofasare lower thanx.`All_biggerif all elements ofasare bigger thanx.`Just_after iifas.(i) < x < as.(i+1).`Emptyif the sliceasis 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 -> boolfor_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 -> boolfor_all2 p [|as1; ...; asn|] [|bs1; ...; bsn|]istrueif each pair of elementsasi bsisatisfies 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 -> boolexists p [|as1; ...; asn|]istrueif 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 -> boolexists2 p [|as1; ...; asn|] [|bs1; ...; bsn|]istrueif any pair of elementsasi bsisatisfies 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 -> 'accfold2 f acc as bsfold on two slicesasandbsstepwise. 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 -> unititer2 f as bsiterates on the two slicesasandbsstepwise. 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 -> unitshuffle asrandomly shuffles the sliceas, in place.
val shuffle_with : Random.State.t -> 'a t -> unitshuffle_with rs asrandomly shuffles the sliceas(likeshuffle) but a specialized random statersis used to control the random numbers being produced during shuffling (for reproducibility).
val random_choose : 'a t -> 'a random_genrandom_choose as rsrandomly chooses an element ofas.- raises Not_found
if the array/slice is empty.
val to_iter : 'a t -> 'a iterto_iter areturns aniterof the elements of a slicea. The input arrayais shared with the sequence and modification of it will result in modification of the iterator.- since
- 2.8
val to_std_seq : 'a t -> 'a Seq.tto_std_seq areturns aSeq.tof the elements of a slicea. The input arrayais shared with the sequence and modification of it will result in modification of the sequence.- since
- 2.8
val to_seq : 'a t -> 'a sequenceval to_gen : 'a t -> 'a gento_gen asreturns agenof the elements of a sliceas.