1Stdlib.ArrayLabels(3) OCaml library Stdlib.ArrayLabels(3)
2
3
4
6 Stdlib.ArrayLabels - no description
7
9 Module Stdlib.ArrayLabels
10
12 Module ArrayLabels
13 : (module Stdlib__ArrayLabels)
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 ArrayLabels.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 ArrayLabels.create_float .
97
98
99
100 val init : int -> f:(int -> 'a) -> 'a array
101
102
103 init n ~f returns a fresh array of length n , with element number i
104 initialized to the result of f i . In other terms, init n ~f tabulates
105 the 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 : dimx:int -> dimy: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 : dimx:int -> dimy:int -> 'a -> 'a array array
130
131 Deprecated.
132
133 create_matrix is an alias for ArrayLabels.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 ArrayLabels.append , but concatenates a list of arrays.
152
153
154
155 val sub : 'a array -> pos:int -> len:int -> 'a array
156
157
158 sub a ~pos ~len returns a fresh array of length len , containing the
159 elements 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 -> pos:int -> len: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 : src:'a array -> src_pos:int -> dst:'a array -> dst_pos:int
188 -> len:int -> unit
189
190
191 blit ~src ~src_pos ~dst ~dst_pos ~len copies len elements from array
192 src , starting at element number src_pos , to array dst , starting at
193 element number dst_pos . It works correctly even if src and dst are the
194 same array, and the source and destination chunks overlap.
195
196
197 Raises Invalid_argument if src_pos and len do not designate a valid
198 subarray of src , or if dst_pos and len do not designate a valid subar‐
199 ray of dst .
200
201
202
203 val to_list : 'a array -> 'a list
204
205
206 to_list a returns the list of all the elements of a .
207
208
209
210 val of_list : 'a list -> 'a array
211
212
213 of_list l returns a fresh array containing the elements of l .
214
215
216 Raises Invalid_argument if the length of l is greater than Sys.max_ar‐
217 ray_length .
218
219
220
221
222 Iterators
223 val iter : f:('a -> unit) -> 'a array -> unit
224
225
226 iter ~f a applies function f in turn to all the elements of a . It is
227 equivalent to f a.(0); f a.(1); ...; f a.(length a - 1); () .
228
229
230
231 val iteri : f:(int -> 'a -> unit) -> 'a array -> unit
232
233 Same as ArrayLabels.iter , but the function is applied to the index of
234 the element as first argument, and the element itself as second argu‐
235 ment.
236
237
238
239 val map : f:('a -> 'b) -> 'a array -> 'b array
240
241
242 map ~f a applies function f to all the elements of a , and builds an
243 array with the results returned by f : [| f a.(0); f a.(1); ...; f
244 a.(length a - 1) |] .
245
246
247
248 val mapi : f:(int -> 'a -> 'b) -> 'a array -> 'b array
249
250 Same as ArrayLabels.map , but the function is applied to the index of
251 the element as first argument, and the element itself as second argu‐
252 ment.
253
254
255
256 val fold_left : f:('a -> 'b -> 'a) -> init:'a -> 'b array -> 'a
257
258
259 fold_left ~f ~init a computes f (... (f (f init a.(0)) a.(1)) ...)
260 a.(n-1) , where n is the length of the array a .
261
262
263
264 val fold_left_map : f:('a -> 'b -> 'a * 'c) -> init:'a -> 'b array ->
265 'a * 'c array
266
267
268 fold_left_map is a combination of ArrayLabels.fold_left and ArrayLa‐
269 bels.map that threads an accumulator through calls to f .
270
271
272 Since 4.13.0
273
274
275
276 val fold_right : f:('b -> 'a -> 'a) -> 'b array -> init:'a -> 'a
277
278
279 fold_right ~f a ~init computes f a.(0) (f a.(1) ( ... (f a.(n-1) init)
280 ...)) , where n is the length of the array a .
281
282
283
284
285 Iterators on two arrays
286 val iter2 : f:('a -> 'b -> unit) -> 'a array -> 'b array -> unit
287
288
289 iter2 ~f a b applies function f to all the elements of a and b .
290
291
292 Since 4.05.0
293
294
295 Raises Invalid_argument if the arrays are not the same size.
296
297
298
299 val map2 : f:('a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array
300
301
302 map2 ~f a b applies function f to all the elements of a and b , and
303 builds an array with the results returned by f : [| f a.(0) b.(0); ...;
304 f a.(length a - 1) b.(length b - 1)|] .
305
306
307 Since 4.05.0
308
309
310 Raises Invalid_argument if the arrays are not the same size.
311
312
313
314
315 Array scanning
316 val for_all : f:('a -> bool) -> 'a array -> bool
317
318
319 for_all ~f [|a1; ...; an|] checks if all elements of the array satisfy
320 the predicate f . That is, it returns (f a1) && (f a2) && ... && (f an)
321 .
322
323
324 Since 4.03.0
325
326
327
328 val exists : f:('a -> bool) -> 'a array -> bool
329
330
331 exists ~f [|a1; ...; an|] checks if at least one element of the array
332 satisfies the predicate f . That is, it returns (f a1) || (f a2) || ...
333 || (f an) .
334
335
336 Since 4.03.0
337
338
339
340 val for_all2 : f:('a -> 'b -> bool) -> 'a array -> 'b array -> bool
341
342 Same as ArrayLabels.for_all , but for a two-argument predicate.
343
344
345 Since 4.11.0
346
347
348 Raises Invalid_argument if the two arrays have different lengths.
349
350
351
352 val exists2 : f:('a -> 'b -> bool) -> 'a array -> 'b array -> bool
353
354 Same as ArrayLabels.exists , but for a two-argument predicate.
355
356
357 Since 4.11.0
358
359
360 Raises Invalid_argument if the two arrays have different lengths.
361
362
363
364 val mem : 'a -> set:'a array -> bool
365
366
367 mem a ~set is true if and only if a is structurally equal to an element
368 of l (i.e. there is an x in l such that compare a x = 0 ).
369
370
371 Since 4.03.0
372
373
374
375 val memq : 'a -> set:'a array -> bool
376
377 Same as ArrayLabels.mem , but uses physical equality instead of struc‐
378 tural equality to compare list elements.
379
380
381 Since 4.03.0
382
383
384
385 val find_opt : f:('a -> bool) -> 'a array -> 'a option
386
387
388 find_opt ~f a returns the first element of the array a that satisfies
389 the predicate f , or None if there is no value that satisfies f in the
390 array a .
391
392
393 Since 4.13.0
394
395
396
397 val find_map : f:('a -> 'b option) -> 'a array -> 'b option
398
399
400 find_map ~f a applies f to the elements of a in order, and returns the
401 first result of the form Some v , or None if none exist.
402
403
404 Since 4.13.0
405
406
407
408
409 Arrays of pairs
410 val split : ('a * 'b) array -> 'a array * 'b array
411
412
413 split [|(a1,b1); ...; (an,bn)|] is ([|a1; ...; an|], [|b1; ...; bn|]) .
414
415
416 Since 4.13.0
417
418
419
420 val combine : 'a array -> 'b array -> ('a * 'b) array
421
422
423 combine [|a1; ...; an|] [|b1; ...; bn|] is [|(a1,b1); ...; (an,bn)|] .
424 Raise Invalid_argument if the two arrays have different lengths.
425
426
427 Since 4.13.0
428
429
430
431
432 Sorting
433 val sort : cmp:('a -> 'a -> int) -> 'a array -> unit
434
435 Sort an array in increasing order according to a comparison function.
436 The comparison function must return 0 if its arguments compare as
437 equal, a positive integer if the first is greater, and a negative inte‐
438 ger if the first is smaller (see below for a complete specification).
439 For example, compare is a suitable comparison function. After calling
440 sort , the array is sorted in place in increasing order. sort is guar‐
441 anteed to run in constant heap space and (at most) logarithmic stack
442 space.
443
444 The current implementation uses Heap Sort. It runs in constant stack
445 space.
446
447 Specification of the comparison function: Let a be the array and cmp
448 the comparison function. The following must be true for all x , y , z
449 in a :
450
451 - cmp x y > 0 if and only if cmp y x < 0
452
453 - if cmp x y >= 0 and cmp y z >= 0 then cmp x z >= 0
454
455 When sort returns, a contains the same elements as before, reordered in
456 such a way that for all i and j valid indices of a :
457
458 - cmp a.(i) a.(j) >= 0 if and only if i >= j
459
460
461
462
463 val stable_sort : cmp:('a -> 'a -> int) -> 'a array -> unit
464
465 Same as ArrayLabels.sort , but the sorting algorithm is stable (i.e.
466 elements that compare equal are kept in their original order) and not
467 guaranteed to run in constant heap space.
468
469 The current implementation uses Merge Sort. It uses a temporary array
470 of length n/2 , where n is the length of the array. It is usually
471 faster than the current implementation of ArrayLabels.sort .
472
473
474
475 val fast_sort : cmp:('a -> 'a -> int) -> 'a array -> unit
476
477 Same as ArrayLabels.sort or ArrayLabels.stable_sort , whichever is
478 faster on typical input.
479
480
481
482
483 Arrays and Sequences
484 val to_seq : 'a array -> 'a Seq.t
485
486 Iterate on the array, in increasing order. Modifications of the array
487 during iteration will be reflected in the sequence.
488
489
490 Since 4.07
491
492
493
494 val to_seqi : 'a array -> (int * 'a) Seq.t
495
496 Iterate on the array, in increasing order, yielding indices along ele‐
497 ments. Modifications of the array during iteration will be reflected
498 in the sequence.
499
500
501 Since 4.07
502
503
504
505 val of_seq : 'a Seq.t -> 'a array
506
507 Create an array from the generator
508
509
510 Since 4.07
511
512
513
514
515
516OCamldoc 2022-07-22 Stdlib.ArrayLabels(3)