1Stdlib.Array(3)                  OCaml library                 Stdlib.Array(3)
2
3
4

NAME

6       Stdlib.Array - no description
7

Module

9       Module   Stdlib.Array
10

Documentation

12       Module Array
13        : (module Stdlib__array)
14
15
16
17
18
19
20
21       type 'a t = 'a array
22
23
24       An alias for the type of arrays.
25
26
27
28       val length : 'a array -> int
29
30       Return the length (number of elements) of the given array.
31
32
33
34       val get : 'a array -> int -> 'a
35
36
37       get  a  n  returns the element number n of array a .  The first element
38       has number 0.  The last element has number length a - 1 .  You can also
39       write a.(n) instead of get a n .
40
41
42       Raises Invalid_argument if n is outside the range 0 to (length a - 1) .
43
44
45
46       val set : 'a array -> int -> 'a -> unit
47
48
49       set  a n x modifies array a in place, replacing element number n with x
50       .  You can also write a.(n) <- x instead of set a n x .
51
52
53       Raises Invalid_argument if n is outside the range 0 to length a - 1 .
54
55
56
57       val make : int -> 'a -> 'a array
58
59
60       make n x returns a fresh array of length n , initialized with x .   All
61       the  elements of this new array are initially physically equal to x (in
62       the sense of the == predicate).  Consequently, if x is mutable,  it  is
63       shared  among all elements of the array, and modifying x through one of
64       the array entries will modify all other entries at the same time.
65
66
67       Raises Invalid_argument if n < 0 or n > Sys.max_array_length .  If  the
68       value  of  x  is a floating-point number, then the maximum size is only
69       Sys.max_array_length / 2 .
70
71
72
73       val create : int -> 'a -> 'a array
74
75       Deprecated.
76
77       create is an alias for Array.make .
78
79
80
81       val create_float : int -> float array
82
83
84       create_float n returns a fresh float array of length n  ,  with  unini‐
85       tialized data.
86
87
88       Since 4.03
89
90
91
92       val make_float : int -> float array
93
94       Deprecated.
95
96       make_float is an alias for Array.create_float .
97
98
99
100       val init : int -> (int -> 'a) -> 'a array
101
102
103       init n f returns a fresh array of length n , with element number i ini‐
104       tialized to the result of f i .  In other terms, init n f tabulates the
105       results of f applied to the integers 0 to n-1 .
106
107
108       Raises  Invalid_argument if n < 0 or n > Sys.max_array_length .  If the
109       return type of f is float , then the maximum size is  only  Sys.max_ar‐
110       ray_length / 2 .
111
112
113
114       val make_matrix : int -> int -> 'a -> 'a array array
115
116
117       make_matrix  dimx  dimy  e returns a two-dimensional array (an array of
118       arrays) with first dimension dimx and second dimension dimy .  All  the
119       elements  of this new matrix are initially physically equal to e .  The
120       element ( x,y ) of a matrix m is accessed with the notation m.(x).(y) .
121
122
123       Raises Invalid_argument if dimx or dimy is  negative  or  greater  than
124       Sys.max_array_length  .   If the value of e is a floating-point number,
125       then the maximum size is only Sys.max_array_length / 2 .
126
127
128
129       val create_matrix : int -> int -> 'a -> 'a array array
130
131       Deprecated.
132
133       create_matrix is an alias for Array.make_matrix .
134
135
136
137       val append : 'a array -> 'a array -> 'a array
138
139
140       append v1 v2 returns a fresh array containing the concatenation of  the
141       arrays v1 and v2 .
142
143
144       Raises Invalid_argument if length v1 + length v2 > Sys.max_array_length
145       .
146
147
148
149       val concat : 'a array list -> 'a array
150
151       Same as Array.append , but concatenates a list of arrays.
152
153
154
155       val sub : 'a array -> int -> int -> 'a array
156
157
158       sub a pos len returns a fresh array of length len , containing the ele‐
159       ments number pos to pos + len - 1 of array a .
160
161
162       Raises  Invalid_argument if pos and len do not designate a valid subar‐
163       ray of a ; that is, if pos < 0 , or len < 0 , or pos + len > length a .
164
165
166
167       val copy : 'a array -> 'a array
168
169
170       copy a returns a copy of a , that is, a fresh array containing the same
171       elements as a .
172
173
174
175       val fill : 'a array -> int -> int -> 'a -> unit
176
177
178       fill  a  pos len x modifies the array a in place, storing x in elements
179       number pos to pos + len - 1 .
180
181
182       Raises Invalid_argument if pos and len do not designate a valid  subar‐
183       ray of a .
184
185
186
187       val blit : 'a array -> int -> 'a array -> int -> int -> unit
188
189
190       blit  src  src_pos dst dst_pos len copies len elements from array src ,
191       starting at element number src_pos , to array dst , starting at element
192       number  dst_pos  .  It works correctly even if src and dst are the same
193       array, and the source and destination chunks overlap.
194
195
196       Raises Invalid_argument if src_pos and len do  not  designate  a  valid
197       subarray of src , or if dst_pos and len do not designate a valid subar‐
198       ray of dst .
199
200
201
202       val to_list : 'a array -> 'a list
203
204
205       to_list a returns the list of all the elements of a .
206
207
208
209       val of_list : 'a list -> 'a array
210
211
212       of_list l returns a fresh array containing the elements of l .
213
214
215       Raises Invalid_argument if the length of l is greater than  Sys.max_ar‐
216       ray_length .
217
218
219
220
221   Iterators
222       val iter : ('a -> unit) -> 'a array -> unit
223
224
225       iter  f  a applies function f in turn to all the elements of a .  It is
226       equivalent to f a.(0); f a.(1); ...; f a.(length a - 1); () .
227
228
229
230       val iteri : (int -> 'a -> unit) -> 'a array -> unit
231
232       Same as Array.iter , but the function is applied to the  index  of  the
233       element as first argument, and the element itself as second argument.
234
235
236
237       val map : ('a -> 'b) -> 'a array -> 'b array
238
239
240       map f a applies function f to all the elements of a , and builds an ar‐
241       ray with the results returned by f :  [|  f  a.(0);  f  a.(1);  ...;  f
242       a.(length a - 1) |] .
243
244
245
246       val mapi : (int -> 'a -> 'b) -> 'a array -> 'b array
247
248       Same as Array.map , but the function is applied to the index of the el‐
249       ement as first argument, and the element itself as second argument.
250
251
252
253       val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b array -> 'a
254
255
256       fold_left f init a computes f  (...  (f  (f  init  a.(0))  a.(1))  ...)
257       a.(n-1) , where n is the length of the array a .
258
259
260
261       val fold_right : ('b -> 'a -> 'a) -> 'b array -> 'a -> 'a
262
263
264       fold_right  f  a  init computes f a.(0) (f a.(1) ( ... (f a.(n-1) init)
265       ...))  , where n is the length of the array a .
266
267
268
269
270   Iterators on two arrays
271       val iter2 : ('a -> 'b -> unit) -> 'a array -> 'b array -> unit
272
273
274       iter2 f a b applies function f to all the elements of a and b .
275
276
277       Since 4.03.0 (4.05.0 in ArrayLabels)
278
279
280       Raises Invalid_argument if the arrays are not the same size.
281
282
283
284       val map2 : ('a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array
285
286
287       map2 f a b applies function f to all the elements of  a  and  b  ,  and
288       builds an array with the results returned by f : [| f a.(0) b.(0); ...;
289       f a.(length a - 1) b.(length b - 1)|] .
290
291
292       Since 4.03.0 (4.05.0 in ArrayLabels)
293
294
295       Raises Invalid_argument if the arrays are not the same size.
296
297
298
299
300   Array scanning
301       val for_all : ('a -> bool) -> 'a array -> bool
302
303
304       for_all f [|a1; ...; an|] checks if all elements of the  array  satisfy
305       the predicate f . That is, it returns (f a1) && (f a2) && ... && (f an)
306       .
307
308
309       Since 4.03.0
310
311
312
313       val exists : ('a -> bool) -> 'a array -> bool
314
315
316       exists f [|a1; ...; an|] checks if at least one element  of  the  array
317       satisfies the predicate f . That is, it returns (f a1) || (f a2) || ...
318       || (f an) .
319
320
321       Since 4.03.0
322
323
324
325       val for_all2 : ('a -> 'b -> bool) -> 'a array -> 'b array -> bool
326
327       Same as Array.for_all , but for a two-argument predicate.
328
329
330       Since 4.11.0
331
332
333       Raises Invalid_argument if the two arrays have different lengths.
334
335
336
337       val exists2 : ('a -> 'b -> bool) -> 'a array -> 'b array -> bool
338
339       Same as Array.exists , but for a two-argument predicate.
340
341
342       Since 4.11.0
343
344
345       Raises Invalid_argument if the two arrays have different lengths.
346
347
348
349       val mem : 'a -> 'a array -> bool
350
351
352       mem a set is true if and only if a is structurally equal to an  element
353       of l (i.e. there is an x in l such that compare a x = 0 ).
354
355
356       Since 4.03.0
357
358
359
360       val memq : 'a -> 'a array -> bool
361
362       Same  as  Array.mem  , but uses physical equality instead of structural
363       equality to compare list elements.
364
365
366       Since 4.03.0
367
368
369
370
371   Sorting
372       val sort : ('a -> 'a -> int) -> 'a array -> unit
373
374       Sort an array in increasing order according to a  comparison  function.
375       The  comparison  function  must  return  0  if its arguments compare as
376       equal, a positive integer if the first is greater, and a negative inte‐
377       ger  if  the first is smaller (see below for a complete specification).
378       For example, compare is a suitable comparison function.  After  calling
379       sort , the array is sorted in place in increasing order.  sort is guar‐
380       anteed to run in constant heap space and (at  most)  logarithmic  stack
381       space.
382
383       The  current  implementation uses Heap Sort.  It runs in constant stack
384       space.
385
386       Specification of the comparison function: Let a be the  array  and  cmp
387       the  comparison function.  The following must be true for all x , y , z
388       in a :
389
390       - cmp x y > 0 if and only if cmp y x < 0
391
392       -  if cmp x y >= 0 and cmp y z >= 0 then cmp x z >= 0
393
394       When sort returns, a contains the same elements as before, reordered in
395       such a way that for all i and j valid indices of a :
396
397       - cmp a.(i) a.(j) >= 0 if and only if i >= j
398
399
400
401
402       val stable_sort : ('a -> 'a -> int) -> 'a array -> unit
403
404       Same  as  Array.sort  , but the sorting algorithm is stable (i.e.  ele‐
405       ments that compare equal are kept in  their  original  order)  and  not
406       guaranteed to run in constant heap space.
407
408       The  current  implementation uses Merge Sort. It uses a temporary array
409       of length n/2 , where n is the length of  the  array.   It  is  usually
410       faster than the current implementation of Array.sort .
411
412
413
414       val fast_sort : ('a -> 'a -> int) -> 'a array -> unit
415
416       Same  as Array.sort or Array.stable_sort , whichever is faster on typi‐
417       cal input.
418
419
420
421
422   Iterators
423       val to_seq : 'a array -> 'a Seq.t
424
425       Iterate on the array, in increasing order. Modifications of  the  array
426       during iteration will be reflected in the iterator.
427
428
429       Since 4.07
430
431
432
433       val to_seqi : 'a array -> (int * 'a) Seq.t
434
435       Iterate  on the array, in increasing order, yielding indices along ele‐
436       ments.  Modifications of the array during iteration will  be  reflected
437       in the iterator.
438
439
440       Since 4.07
441
442
443
444       val of_seq : 'a Seq.t -> 'a array
445
446       Create an array from the generator
447
448
449       Since 4.07
450
451
452
453
454
455OCamldoc                          2021-07-22                   Stdlib.Array(3)
Impressum