Module Caml.Array
val get : 'a array -> int -> 'aArray.get a nreturns the element numbernof arraya. The first element has number 0. The last element has numberArray.length a - 1. You can also writea.(n)instead ofArray.get a n.Raise
Invalid_argumentifnis outside the range 0 to(Array.length a - 1).
val set : 'a array -> int -> 'a -> unitArray.set a n xmodifies arrayain place, replacing element numbernwithx. You can also writea.(n) <- xinstead ofArray.set a n x.Raise
Invalid_argumentifnis outside the range 0 toArray.length a - 1.
val make : int -> 'a -> 'a arrayArray.make n xreturns a fresh array of lengthn, initialized withx. All the elements of this new array are initially physically equal tox(in the sense of the==predicate). Consequently, ifxis mutable, it is shared among all elements of the array, and modifyingxthrough one of the array entries will modify all other entries at the same time.Raise
Invalid_argumentifn < 0orn > Sys.max_array_length. If the value ofxis a floating-point number, then the maximum size is onlySys.max_array_length / 2.
val create_float : int -> float arrayArray.create_float nreturns a fresh float array of lengthn, with uninitialized data.- since
- 4.03
val init : int -> (int -> 'a) -> 'a arrayArray.init n freturns a fresh array of lengthn, with element numberiinitialized to the result off i. In other terms,Array.init n ftabulates the results offapplied to the integers0ton-1.Raise
Invalid_argumentifn < 0orn > Sys.max_array_length. If the return type offisfloat, then the maximum size is onlySys.max_array_length / 2.
val make_matrix : int -> int -> 'a -> 'a array arrayArray.make_matrix dimx dimy ereturns a two-dimensional array (an array of arrays) with first dimensiondimxand second dimensiondimy. All the elements of this new matrix are initially physically equal toe. The element (x,y) of a matrixmis accessed with the notationm.(x).(y).Raise
Invalid_argumentifdimxordimyis negative or greater thanSys.max_array_length. If the value ofeis a floating-point number, then the maximum size is onlySys.max_array_length / 2.
val create_matrix : int -> int -> 'a -> 'a array array- deprecated
Array.create_matrixis an alias forArray.make_matrix.
val append : 'a array -> 'a array -> 'a arrayArray.append v1 v2returns a fresh array containing the concatenation of the arraysv1andv2.Raise
Invalid_argumentifArray.length v1 + Array.length v2 > Sys.max_array_length.
val concat : 'a array list -> 'a arraySame as
Array.append, but concatenates a list of arrays.
val sub : 'a array -> int -> int -> 'a arrayArray.sub a start lenreturns a fresh array of lengthlen, containing the elements numberstarttostart + len - 1of arraya.Raise
Invalid_argumentifstartandlendo not designate a valid subarray ofa; that is, ifstart < 0, orlen < 0, orstart + len > Array.length a.
val copy : 'a array -> 'a arrayArray.copy areturns a copy ofa, that is, a fresh array containing the same elements asa.
val fill : 'a array -> int -> int -> 'a -> unitArray.fill a ofs len xmodifies the arrayain place, storingxin elements numberofstoofs + len - 1.Raise
Invalid_argumentifofsandlendo not designate a valid subarray ofa.
val blit : 'a array -> int -> 'a array -> int -> int -> unitArray.blit v1 o1 v2 o2 lencopieslenelements from arrayv1, starting at element numbero1, to arrayv2, starting at element numbero2. It works correctly even ifv1andv2are the same array, and the source and destination chunks overlap.Raise
Invalid_argumentifo1andlendo not designate a valid subarray ofv1, or ifo2andlendo not designate a valid subarray ofv2.
val of_list : 'a list -> 'a arrayArray.of_list lreturns a fresh array containing the elements ofl.Raise
Invalid_argumentif the length oflis greater thanSys.max_array_length.
Iterators
val iter : ('a -> unit) -> 'a array -> unitArray.iter f aapplies functionfin turn to all the elements ofa. It is equivalent tof a.(0); f a.(1); ...; f a.(Array.length a - 1); ().
val iteri : (int -> 'a -> unit) -> 'a array -> unitSame as
Array.iter, but the function is applied with the index of the element as first argument, and the element itself as second argument.
val map : ('a -> 'b) -> 'a array -> 'b arrayArray.map f aapplies functionfto all the elements ofa, and builds an array with the results returned byf:[| f a.(0); f a.(1); ...; f a.(Array.length a - 1) |].
val mapi : (int -> 'a -> 'b) -> 'a array -> 'b arraySame as
Array.map, but the function is applied to the index of the element as first argument, and the element itself as second argument.
Iterators on two arrays
val iter2 : ('a -> 'b -> unit) -> 'a array -> 'b array -> unitArray.iter2 f a bapplies functionfto all the elements ofaandb. RaiseInvalid_argumentif the arrays are not the same size.- since
- 4.03.0
val map2 : ('a -> 'b -> 'c) -> 'a array -> 'b array -> 'c arrayArray.map2 f a bapplies functionfto all the elements ofaandb, and builds an array with the results returned byf:[| f a.(0) b.(0); ...; f a.(Array.length a - 1) b.(Array.length b - 1)|]. RaiseInvalid_argumentif the arrays are not the same size.- since
- 4.03.0
Array scanning
val for_all : ('a -> bool) -> 'a array -> boolArray.for_all p [|a1; ...; an|]checks if all elements of the array satisfy the predicatep. That is, it returns(p a1) && (p a2) && ... && (p an).- since
- 4.03.0
val exists : ('a -> bool) -> 'a array -> boolArray.exists p [|a1; ...; an|]checks if at least one element of the array satisfies the predicatep. That is, it returns(p a1) || (p a2) || ... || (p an).- since
- 4.03.0
val mem : 'a -> 'a array -> boolmem a lis true if and only ifais structurally equal to an element ofl(i.e. there is anxinlsuch thatcompare a x = 0).- since
- 4.03.0
val memq : 'a -> 'a array -> boolSame as
Array.mem, but uses physical equality instead of structural equality to compare elements.- since
- 4.03.0
Sorting
val sort : ('a -> 'a -> int) -> 'a array -> unitSort an array in increasing order according to a comparison function. The comparison function must return 0 if its arguments compare as equal, a positive integer if the first is greater, and a negative integer if the first is smaller (see below for a complete specification). For example,
Stdlib.compareis a suitable comparison function. After callingArray.sort, the array is sorted in place in increasing order.Array.sortis guaranteed to run in constant heap space and (at most) logarithmic stack space.The current implementation uses Heap Sort. It runs in constant stack space.
Specification of the comparison function: Let
abe the array andcmpthe comparison function. The following must be true for allx,y,zina:cmp x y> 0 if and only ifcmp y x< 0- if
cmp x y>= 0 andcmp y z>= 0 thencmp x z>= 0
When
Array.sortreturns,acontains the same elements as before, reordered in such a way that for all i and j valid indices ofa:cmp a.(i) a.(j)>= 0 if and only if i >= j
val stable_sort : ('a -> 'a -> int) -> 'a array -> unitSame as
Array.sort, but the sorting algorithm is stable (i.e. elements that compare equal are kept in their original order) and not guaranteed to run in constant heap space.The current implementation uses Merge Sort. It uses a temporary array of length
n/2, wherenis the length of the array. It is usually faster than the current implementation ofArray.sort.
val fast_sort : ('a -> 'a -> int) -> 'a array -> unitSame as
Array.sortorArray.stable_sort, whichever is faster on typical input.
Iterators
val to_seq : 'a array -> 'a Seq.tIterate on the array, in increasing order. Modifications of the array during iteration will be reflected in the iterator.
- since
- 4.07
val to_seqi : 'a array -> (int * 'a) Seq.tIterate on the array, in increasing order, yielding indices along elements. Modifications of the array during iteration will be reflected in the iterator.
- since
- 4.07
val of_seq : 'a Seq.t -> 'a arrayCreate an array from the generator
- since
- 4.07