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
19
20
21
22
23
24 val length : 'a array -> int
25
26 Return the length (number of elements) of the given array.
27
28
29
30
31 val get : 'a array -> int -> 'a
32
33
34 Array.get a n returns the element number n of array a . The first ele‐
35 ment has number 0. The last element has number Array.length a - 1 .
36 You can also write a.(n) instead of Array.get a n .
37
38 Raise Invalid_argument index out of bounds if n is outside the range 0
39 to (Array.length a - 1) .
40
41
42
43
44 val set : 'a array -> int -> 'a -> unit
45
46
47 Array.set a n x modifies array a in place, replacing element number n
48 with x . You can also write a.(n) <- x instead of Array.set a n x .
49
50 Raise Invalid_argument index out of bounds if n is outside the range 0
51 to Array.length a - 1 .
52
53
54
55
56 val make : int -> 'a -> 'a array
57
58
59 Array.make n x returns a fresh array of length n , initialized with x .
60 All the elements of this new array are initially physically equal to x
61 (in the sense of the == predicate). Consequently, if x is mutable, it
62 is shared among all elements of the array, and modifying x through one
63 of the array entries will modify all other entries at the same time.
64
65 Raise Invalid_argument if n < 0 or n > Sys.max_array_length . If the
66 value of x is a floating-point number, then the maximum size is only
67 Sys.max_array_length / 2 .
68
69
70
71
72 val create : int -> 'a -> 'a array
73
74 Deprecated.
75
76 Array.create is an alias for ArrayLabels.make .
77
78
79
80
81 val init : int -> f:(int -> 'a) -> 'a array
82
83
84 Array.init n f returns a fresh array of length n , with element number
85 i initialized to the result of f i . In other terms, Array.init n f
86 tabulates the results of f applied to the integers 0 to n-1 .
87
88 Raise Invalid_argument if n < 0 or n > Sys.max_array_length . If the
89 return type of f is float , then the maximum size is only
90 Sys.max_array_length / 2 .
91
92
93
94
95 val make_matrix : dimx:int -> dimy:int -> 'a -> 'a array array
96
97
98 Array.make_matrix dimx dimy e returns a two-dimensional array (an array
99 of arrays) with first dimension dimx and second dimension dimy . All
100 the elements of this new matrix are initially physically equal to e .
101 The element ( x,y ) of a matrix m is accessed with the notation
102 m.(x).(y) .
103
104 Raise Invalid_argument if dimx or dimy is negative or greater than
105 Sys.max_array_length . If the value of e is a floating-point number,
106 then the maximum size is only Sys.max_array_length / 2 .
107
108
109
110
111 val create_matrix : dimx:int -> dimy:int -> 'a -> 'a array array
112
113 Deprecated.
114
115 Array.create_matrix is an alias for ArrayLabels.make_matrix .
116
117
118
119
120 val append : 'a array -> 'a array -> 'a array
121
122
123 Array.append v1 v2 returns a fresh array containing the concatenation
124 of the arrays v1 and v2 .
125
126
127
128
129 val concat : 'a array list -> 'a array
130
131 Same as Array.append , but concatenates a list of arrays.
132
133
134
135
136 val sub : 'a array -> pos:int -> len:int -> 'a array
137
138
139 Array.sub a start len returns a fresh array of length len , containing
140 the elements number start to start + len - 1 of array a .
141
142 Raise Invalid_argument Array.sub if start and len do not designate a
143 valid subarray of a ; that is, if start < 0 , or len < 0 , or start +
144 len > Array.length a .
145
146
147
148
149 val copy : 'a array -> 'a array
150
151
152 Array.copy a returns a copy of a , that is, a fresh array containing
153 the same elements as a .
154
155
156
157
158 val fill : 'a array -> pos:int -> len:int -> 'a -> unit
159
160
161 Array.fill a ofs len x modifies the array a in place, storing x in ele‐
162 ments number ofs to ofs + len - 1 .
163
164 Raise Invalid_argument Array.fill if ofs and len do not designate a
165 valid subarray of a .
166
167
168
169
170 val blit : src:'a array -> src_pos:int -> dst:'a array -> dst_pos:int
171 -> len:int -> unit
172
173
174 Array.blit v1 o1 v2 o2 len copies len elements from array v1 , starting
175 at element number o1 , to array v2 , starting at element number o2 . It
176 works correctly even if v1 and v2 are the same array, and the source
177 and destination chunks overlap.
178
179 Raise Invalid_argument Array.blit if o1 and len do not designate a
180 valid subarray of v1 , or if o2 and len do not designate a valid subar‐
181 ray of v2 .
182
183
184
185
186 val to_list : 'a array -> 'a list
187
188
189 Array.to_list a returns the list of all the elements of a .
190
191
192
193
194 val of_list : 'a list -> 'a array
195
196
197 Array.of_list l returns a fresh array containing the elements of l .
198
199
200
201
202 val iter : f:('a -> unit) -> 'a array -> unit
203
204
205 Array.iter f a applies function f in turn to all the elements of a .
206 It is equivalent to f a.(0); f a.(1); ...; f a.(Array.length a - 1); ()
207 .
208
209
210
211
212 val map : f:('a -> 'b) -> 'a array -> 'b array
213
214
215 Array.map f a applies function f to all the elements of a , and builds
216 an array with the results returned by f : [| f a.(0); f a.(1); ...; f
217 a.(Array.length a - 1) |] .
218
219
220
221
222 val iteri : f:(int -> 'a -> unit) -> 'a array -> unit
223
224 Same as ArrayLabels.iter , but the function is applied to the index of
225 the element as first argument, and the element itself as second argu‐
226 ment.
227
228
229
230
231 val mapi : f:(int -> 'a -> 'b) -> 'a array -> 'b array
232
233 Same as ArrayLabels.map , 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
240 val fold_left : f:('a -> 'b -> 'a) -> init:'a -> 'b array -> 'a
241
242
243 Array.fold_left f x a computes f (... (f (f x a.(0)) a.(1)) ...)
244 a.(n-1) , where n is the length of the array a .
245
246
247
248
249 val fold_right : f:('a -> 'b -> 'b) -> 'a array -> init:'b -> 'b
250
251
252 Array.fold_right f a x computes f a.(0) (f a.(1) ( ... (f a.(n-1) x)
253 ...)) , where n is the length of the array a .
254
255
256
257
258
259 === Sorting ===
260
261
262 val sort : cmp:('a -> 'a -> int) -> 'a array -> unit
263
264 Sort an array in increasing order according to a comparison function.
265 The comparison function must return 0 if its arguments compare as
266 equal, a positive integer if the first is greater, and a negative inte‐
267 ger if the first is smaller (see below for a complete specification).
268 For example, Pervasives.compare is a suitable comparison function, pro‐
269 vided there are no floating-point NaN values in the data. After call‐
270 ing Array.sort , the array is sorted in place in increasing order.
271 Array.sort is guaranteed to run in constant heap space and (at most)
272 logarithmic stack space.
273
274 The current implementation uses Heap Sort. It runs in constant stack
275 space.
276
277 Specification of the comparison function: Let a be the array and cmp
278 the comparison function. The following must be true for all x, y, z in
279 a :
280
281 - cmp x y > 0 if and only if cmp y x < 0
282
283 - if cmp x y >= 0 and cmp y z >= 0 then cmp x z >= 0
284
285 When Array.sort returns, a contains the same elements as before,
286 reordered in such a way that for all i and j valid indices of a :
287
288 - cmp a.(i) a.(j) >= 0 if and only if i >= j
289
290
291
292
293
294 val stable_sort : cmp:('a -> 'a -> int) -> 'a array -> unit
295
296 Same as ArrayLabels.sort , but the sorting algorithm is stable (i.e.
297 elements that compare equal are kept in their original order) and not
298 guaranteed to run in constant heap space.
299
300 The current implementation uses Merge Sort. It uses n/2 words of heap
301 space, where n is the length of the array. It is usually faster than
302 the current implementation of ArrayLabels.sort .
303
304
305
306
307 val fast_sort : cmp:('a -> 'a -> int) -> 'a array -> unit
308
309 Same as Array.sort or Array.stable_sort , whichever is faster on typi‐
310 cal input.
311
312
313
314
315
316
317OCamldoc 2017-03-22 ArrayLabels(3)