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