1Stdlib.Array(3) OCaml library Stdlib.Array(3)
2
3
4
6 Stdlib.Array - no description
7
9 Module Stdlib.Array
10
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_left_map : ('a -> 'b -> 'a * 'c) -> 'a -> 'b array -> 'a * 'c
262 array
263
264
265 fold_left_map is a combination of Array.fold_left and Array.map that
266 threads an accumulator through calls to f .
267
268
269 Since 4.13.0
270
271
272
273 val fold_right : ('b -> 'a -> 'a) -> 'b array -> 'a -> 'a
274
275
276 fold_right f a init computes f a.(0) (f a.(1) ( ... (f a.(n-1) init)
277 ...)) , where n is the length of the array a .
278
279
280
281
282 Iterators on two arrays
283 val iter2 : ('a -> 'b -> unit) -> 'a array -> 'b array -> unit
284
285
286 iter2 f a b applies function f to all the elements of a and b .
287
288
289 Since 4.03.0 (4.05.0 in ArrayLabels)
290
291
292 Raises Invalid_argument if the arrays are not the same size.
293
294
295
296 val map2 : ('a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array
297
298
299 map2 f a b applies function f to all the elements of a and b , and
300 builds an array with the results returned by f : [| f a.(0) b.(0); ...;
301 f a.(length a - 1) b.(length b - 1)|] .
302
303
304 Since 4.03.0 (4.05.0 in ArrayLabels)
305
306
307 Raises Invalid_argument if the arrays are not the same size.
308
309
310
311
312 Array scanning
313 val for_all : ('a -> bool) -> 'a array -> bool
314
315
316 for_all f [|a1; ...; an|] checks if all elements of the array satisfy
317 the predicate f . That is, it returns (f a1) && (f a2) && ... && (f an)
318 .
319
320
321 Since 4.03.0
322
323
324
325 val exists : ('a -> bool) -> 'a array -> bool
326
327
328 exists f [|a1; ...; an|] checks if at least one element of the array
329 satisfies the predicate f . That is, it returns (f a1) || (f a2) || ...
330 || (f an) .
331
332
333 Since 4.03.0
334
335
336
337 val for_all2 : ('a -> 'b -> bool) -> 'a array -> 'b array -> bool
338
339 Same as Array.for_all , 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 exists2 : ('a -> 'b -> bool) -> 'a array -> 'b array -> bool
350
351 Same as Array.exists , but for a two-argument predicate.
352
353
354 Since 4.11.0
355
356
357 Raises Invalid_argument if the two arrays have different lengths.
358
359
360
361 val mem : 'a -> 'a array -> bool
362
363
364 mem a set is true if and only if a is structurally equal to an element
365 of l (i.e. there is an x in l such that compare a x = 0 ).
366
367
368 Since 4.03.0
369
370
371
372 val memq : 'a -> 'a array -> bool
373
374 Same as Array.mem , but uses physical equality instead of structural
375 equality to compare list elements.
376
377
378 Since 4.03.0
379
380
381
382 val find_opt : ('a -> bool) -> 'a array -> 'a option
383
384
385 find_opt f a returns the first element of the array a that satisfies
386 the predicate f , or None if there is no value that satisfies f in the
387 array a .
388
389
390 Since 4.13.0
391
392
393
394 val find_map : ('a -> 'b option) -> 'a array -> 'b option
395
396
397 find_map f a applies f to the elements of a in order, and returns the
398 first result of the form Some v , or None if none exist.
399
400
401 Since 4.13.0
402
403
404
405
406 Arrays of pairs
407 val split : ('a * 'b) array -> 'a array * 'b array
408
409
410 split [|(a1,b1); ...; (an,bn)|] is ([|a1; ...; an|], [|b1; ...; bn|]) .
411
412
413 Since 4.13.0
414
415
416
417 val combine : 'a array -> 'b array -> ('a * 'b) array
418
419
420 combine [|a1; ...; an|] [|b1; ...; bn|] is [|(a1,b1); ...; (an,bn)|] .
421 Raise Invalid_argument if the two arrays have different lengths.
422
423
424 Since 4.13.0
425
426
427
428
429 Sorting
430 val sort : ('a -> 'a -> int) -> 'a array -> unit
431
432 Sort an array in increasing order according to a comparison function.
433 The comparison function must return 0 if its arguments compare as
434 equal, a positive integer if the first is greater, and a negative inte‐
435 ger if the first is smaller (see below for a complete specification).
436 For example, compare is a suitable comparison function. After calling
437 sort , the array is sorted in place in increasing order. sort is guar‐
438 anteed to run in constant heap space and (at most) logarithmic stack
439 space.
440
441 The current implementation uses Heap Sort. It runs in constant stack
442 space.
443
444 Specification of the comparison function: Let a be the array and cmp
445 the comparison function. The following must be true for all x , y , z
446 in a :
447
448 - cmp x y > 0 if and only if cmp y x < 0
449
450 - if cmp x y >= 0 and cmp y z >= 0 then cmp x z >= 0
451
452 When sort returns, a contains the same elements as before, reordered in
453 such a way that for all i and j valid indices of a :
454
455 - cmp a.(i) a.(j) >= 0 if and only if i >= j
456
457
458
459
460 val stable_sort : ('a -> 'a -> int) -> 'a array -> unit
461
462 Same as Array.sort , but the sorting algorithm is stable (i.e. ele‐
463 ments that compare equal are kept in their original order) and not
464 guaranteed to run in constant heap space.
465
466 The current implementation uses Merge Sort. It uses a temporary array
467 of length n/2 , where n is the length of the array. It is usually
468 faster than the current implementation of Array.sort .
469
470
471
472 val fast_sort : ('a -> 'a -> int) -> 'a array -> unit
473
474 Same as Array.sort or Array.stable_sort , whichever is faster on typi‐
475 cal input.
476
477
478
479
480 Arrays and Sequences
481 val to_seq : 'a array -> 'a Seq.t
482
483 Iterate on the array, in increasing order. Modifications of the array
484 during iteration will be reflected in the sequence.
485
486
487 Since 4.07
488
489
490
491 val to_seqi : 'a array -> (int * 'a) Seq.t
492
493 Iterate on the array, in increasing order, yielding indices along ele‐
494 ments. Modifications of the array during iteration will be reflected
495 in the sequence.
496
497
498 Since 4.07
499
500
501
502 val of_seq : 'a Seq.t -> 'a array
503
504 Create an array from the generator
505
506
507 Since 4.07
508
509
510
511
512
513OCamldoc 2022-07-22 Stdlib.Array(3)