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