Module Caml.ArrayLabels
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_argument "index out of bounds"ifnis 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_argument "index out of bounds"ifnis 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 init : int -> f:(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 : dimx:int -> dimy: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 : dimx:int -> dimy: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.
val concat : 'a array list -> 'a arraySame as
Array.append, but concatenates a list of arrays.
val sub : 'a array -> pos:int -> len:int -> 'a arrayArray.sub a start lenreturns a fresh array of lengthlen, containing the elements numberstarttostart + len - 1of arraya.Raise
Invalid_argument "Array.sub"ifstartandlendo 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 -> pos:int -> len:int -> 'a -> unitArray.fill a ofs len xmodifies the arrayain place, storingxin elements numberofstoofs + len - 1.Raise
Invalid_argument "Array.fill"ifofsandlendo not designate a valid subarray ofa.
val blit : src:'a array -> src_pos:int -> dst:'a array -> dst_pos:int -> len: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_argument "Array.blit"ifo1andlendo 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.
val iter : f:('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 map : f:('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 iteri : f:(int -> 'a -> unit) -> 'a array -> unitSame as
Array.iter, but the function is applied to the index of the element as first argument, and the element itself as second argument.
val mapi : f:(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.
val fold_left : f:('a -> 'b -> 'a) -> init:'a -> 'b array -> 'aArray.fold_left f x acomputesf (... (f (f x a.(0)) a.(1)) ...) a.(n-1), wherenis the length of the arraya.
val fold_right : f:('b -> 'a -> 'a) -> 'b array -> init:'a -> 'aArray.fold_right f a xcomputesf a.(0) (f a.(1) ( ... (f a.(n-1) x) ...)), wherenis the length of the arraya.
Iterators on two arrays
val iter2 : f:('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.05.0
val map2 : f:('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.05.0
Array scanning
val exists : f:('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 for_all : f:('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 mem : 'a -> set:'a array -> boolmem x ais true if and only ifxis equal to an element ofa.- since
- 4.03.0
val memq : 'a -> set:'a array -> boolSame as
Array.mem, but uses physical equality instead of structural equality to compare list elements.- since
- 4.03.0
Sorting
val sort : cmp:('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, provided there are no floating-point NaN values in the data. 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 all x, y, z in a :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 : cmp:('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
n/2words of heap space, wherenis the length of the array. It is usually faster than the current implementation ofArray.sort.
val fast_sort : cmp:('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
- since
- 4.07
val to_seqi : 'a array -> (int * 'a) Seq.tIterate on the array, in increasing order, yielding indices along elements
- since
- 4.07
val of_seq : 'a Seq.t -> 'a arrayCreate an array from the generator
- since
- 4.07