1 | // Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved. |
2 | // Use of this source code is governed by an MIT license |
3 | // that can be found in the LICENSE file. |
4 | module builtin |
5 | |
6 | import strings |
7 | |
8 | // array is a struct, used for denoting all array types in V. |
9 | // `.data` is a void pointer to the backing heap memory block, |
10 | // which avoids using generics and thus without generating extra |
11 | // code for every type. |
12 | pub struct array { |
13 | pub mut: |
14 | data voidptr |
15 | offset int // in bytes (should be `usize`), to avoid copying data while making slices, unless it starts changing |
16 | len int // length of the array in elements. |
17 | cap int // capacity of the array in elements. |
18 | flags ArrayFlags |
19 | pub: |
20 | element_size int // size in bytes of one element in the array. |
21 | } |
22 | |
23 | [flag] |
24 | pub enum ArrayFlags { |
25 | noslices // when <<, `.noslices` will free the old data block immediately (you have to be sure, that there are *no slices* to that specific array). TODO: integrate with reference counting/compiler support for the static cases. |
26 | noshrink // when `.noslices` and `.noshrink` are *both set*, .delete(x) will NOT allocate new memory and free the old. It will just move the elements in place, and adjust .len. |
27 | nogrow // the array will never be allowed to grow past `.cap`. set `.nogrow` and `.noshrink` for a truly fixed heap array |
28 | nofree // `.data` will never be freed |
29 | } |
30 | |
31 | // Internal function, used by V (`nums := []int`) |
32 | fn __new_array(mylen int, cap int, elm_size int) array { |
33 | cap_ := if cap < mylen { mylen } else { cap } |
34 | arr := array{ |
35 | element_size: elm_size |
36 | data: vcalloc(u64(cap_) * u64(elm_size)) |
37 | len: mylen |
38 | cap: cap_ |
39 | } |
40 | return arr |
41 | } |
42 | |
43 | fn __new_array_with_default(mylen int, cap int, elm_size int, val voidptr) array { |
44 | cap_ := if cap < mylen { mylen } else { cap } |
45 | mut arr := array{ |
46 | element_size: elm_size |
47 | len: mylen |
48 | cap: cap_ |
49 | } |
50 | // x := []EmptyStruct{cap:5} ; for clang/gcc with -gc none, |
51 | // -> sizeof(EmptyStruct) == 0 -> elm_size == 0 |
52 | // -> total_size == 0 -> malloc(0) -> panic; |
53 | // to avoid it, just allocate a single byte |
54 | total_size := u64(cap_) * u64(elm_size) |
55 | if cap_ > 0 && mylen == 0 { |
56 | arr.data = unsafe { malloc(__at_least_one(total_size)) } |
57 | } else { |
58 | arr.data = vcalloc(total_size) |
59 | } |
60 | if val != 0 { |
61 | mut eptr := &u8(arr.data) |
62 | unsafe { |
63 | if eptr != nil { |
64 | if arr.element_size == 1 { |
65 | byte_value := *(&u8(val)) |
66 | for i in 0 .. arr.len { |
67 | eptr[i] = byte_value |
68 | } |
69 | } else { |
70 | for _ in 0 .. arr.len { |
71 | vmemcpy(eptr, val, arr.element_size) |
72 | eptr += arr.element_size |
73 | } |
74 | } |
75 | } |
76 | } |
77 | } |
78 | return arr |
79 | } |
80 | |
81 | fn __new_array_with_multi_default(mylen int, cap int, elm_size int, val voidptr) array { |
82 | cap_ := if cap < mylen { mylen } else { cap } |
83 | mut arr := array{ |
84 | element_size: elm_size |
85 | len: mylen |
86 | cap: cap_ |
87 | } |
88 | // x := []EmptyStruct{cap:5} ; for clang/gcc with -gc none, |
89 | // -> sizeof(EmptyStruct) == 0 -> elm_size == 0 |
90 | // -> total_size == 0 -> malloc(0) -> panic; |
91 | // to avoid it, just allocate a single byte |
92 | total_size := u64(cap_) * u64(elm_size) |
93 | arr.data = vcalloc(__at_least_one(total_size)) |
94 | if val != 0 { |
95 | mut eptr := &u8(arr.data) |
96 | unsafe { |
97 | if eptr != nil { |
98 | for i in 0 .. arr.len { |
99 | vmemcpy(eptr, charptr(val) + i * arr.element_size, arr.element_size) |
100 | eptr += arr.element_size |
101 | } |
102 | } |
103 | } |
104 | } |
105 | return arr |
106 | } |
107 | |
108 | fn __new_array_with_array_default(mylen int, cap int, elm_size int, val array, depth int) array { |
109 | cap_ := if cap < mylen { mylen } else { cap } |
110 | mut arr := array{ |
111 | element_size: elm_size |
112 | data: unsafe { malloc(__at_least_one(u64(cap_) * u64(elm_size))) } |
113 | len: mylen |
114 | cap: cap_ |
115 | } |
116 | mut eptr := &u8(arr.data) |
117 | unsafe { |
118 | if eptr != nil { |
119 | for _ in 0 .. arr.len { |
120 | val_clone := val.clone_to_depth(depth) |
121 | vmemcpy(eptr, &val_clone, arr.element_size) |
122 | eptr += arr.element_size |
123 | } |
124 | } |
125 | } |
126 | return arr |
127 | } |
128 | |
129 | fn __new_array_with_map_default(mylen int, cap int, elm_size int, val map) array { |
130 | cap_ := if cap < mylen { mylen } else { cap } |
131 | mut arr := array{ |
132 | element_size: elm_size |
133 | data: unsafe { malloc(__at_least_one(u64(cap_) * u64(elm_size))) } |
134 | len: mylen |
135 | cap: cap_ |
136 | } |
137 | mut eptr := &u8(arr.data) |
138 | unsafe { |
139 | if eptr != nil { |
140 | for _ in 0 .. arr.len { |
141 | val_clone := val.clone() |
142 | vmemcpy(eptr, &val_clone, arr.element_size) |
143 | eptr += arr.element_size |
144 | } |
145 | } |
146 | } |
147 | return arr |
148 | } |
149 | |
150 | // Private function, used by V (`nums := [1, 2, 3]`) |
151 | fn new_array_from_c_array(len int, cap int, elm_size int, c_array voidptr) array { |
152 | cap_ := if cap < len { len } else { cap } |
153 | arr := array{ |
154 | element_size: elm_size |
155 | data: vcalloc(u64(cap_) * u64(elm_size)) |
156 | len: len |
157 | cap: cap_ |
158 | } |
159 | // TODO Write all memory functions (like memcpy) in V |
160 | unsafe { vmemcpy(arr.data, c_array, u64(len) * u64(elm_size)) } |
161 | return arr |
162 | } |
163 | |
164 | // Private function, used by V (`nums := [1, 2, 3] !`) |
165 | fn new_array_from_c_array_no_alloc(len int, cap int, elm_size int, c_array voidptr) array { |
166 | arr := array{ |
167 | element_size: elm_size |
168 | data: c_array |
169 | len: len |
170 | cap: cap |
171 | } |
172 | return arr |
173 | } |
174 | |
175 | // Private function. Increases the `cap` of an array to the |
176 | // required value by copying the data to a new memory location |
177 | // (creating a clone) unless `a.cap` is already large enough. |
178 | fn (mut a array) ensure_cap(required int) { |
179 | if required <= a.cap { |
180 | return |
181 | } |
182 | if a.flags.has(.nogrow) { |
183 | panic('array.ensure_cap: array with the flag `.nogrow` cannot grow in size, array required new size: ${required}') |
184 | } |
185 | mut cap := if a.cap > 0 { a.cap } else { 2 } |
186 | for required > cap { |
187 | cap *= 2 |
188 | } |
189 | new_size := u64(cap) * u64(a.element_size) |
190 | new_data := unsafe { malloc(__at_least_one(new_size)) } |
191 | if a.data != unsafe { nil } { |
192 | unsafe { vmemcpy(new_data, a.data, u64(a.len) * u64(a.element_size)) } |
193 | // TODO: the old data may be leaked when no GC is used (ref-counting?) |
194 | if a.flags.has(.noslices) { |
195 | unsafe { |
196 | free(a.data) |
197 | } |
198 | } |
199 | } |
200 | a.data = new_data |
201 | a.offset = 0 |
202 | a.cap = cap |
203 | } |
204 | |
205 | // repeat returns a new array with the given array elements repeated given times. |
206 | // `cgen` will replace this with an apropriate call to `repeat_to_depth()` |
207 | // |
208 | // This is a dummy placeholder that will be overridden by `cgen` with an appropriate |
209 | // call to `repeat_to_depth()`. However the `checker` needs it here. |
210 | pub fn (a array) repeat(count int) array { |
211 | return unsafe { a.repeat_to_depth(count, 0) } |
212 | } |
213 | |
214 | // repeat_to_depth is an unsafe version of `repeat()` that handles |
215 | // multi-dimensional arrays. |
216 | // |
217 | // It is `unsafe` to call directly because `depth` is not checked |
218 | [direct_array_access; unsafe] |
219 | pub fn (a array) repeat_to_depth(count int, depth int) array { |
220 | if count < 0 { |
221 | panic('array.repeat: count is negative: ${count}') |
222 | } |
223 | mut size := u64(count) * u64(a.len) * u64(a.element_size) |
224 | if size == 0 { |
225 | size = u64(a.element_size) |
226 | } |
227 | arr := array{ |
228 | element_size: a.element_size |
229 | data: vcalloc(size) |
230 | len: count * a.len |
231 | cap: count * a.len |
232 | } |
233 | if a.len > 0 { |
234 | a_total_size := u64(a.len) * u64(a.element_size) |
235 | arr_step_size := u64(a.len) * u64(arr.element_size) |
236 | mut eptr := &u8(arr.data) |
237 | unsafe { |
238 | if eptr != nil { |
239 | for _ in 0 .. count { |
240 | if depth > 0 { |
241 | ary_clone := a.clone_to_depth(depth) |
242 | vmemcpy(eptr, &u8(ary_clone.data), a_total_size) |
243 | } else { |
244 | vmemcpy(eptr, &u8(a.data), a_total_size) |
245 | } |
246 | eptr += arr_step_size |
247 | } |
248 | } |
249 | } |
250 | } |
251 | return arr |
252 | } |
253 | |
254 | // insert inserts a value in the array at index `i` and increases |
255 | // the index of subsequent elements by 1. |
256 | // |
257 | // This function is type-aware and can insert items of the same |
258 | // or lower dimensionality as the original array. That is, if |
259 | // the original array is `[]int`, then the insert `val` may be |
260 | // `int` or `[]int`. If the original array is `[][]int`, then `val` |
261 | // may be `[]int` or `[][]int`. Consider the examples. |
262 | // |
263 | // Example: |
264 | // ```v |
265 | // mut a := [1, 2, 4] |
266 | // a.insert(2, 3) // a now is [1, 2, 3, 4] |
267 | // mut b := [3, 4] |
268 | // b.insert(0, [1, 2]) // b now is [1, 2, 3, 4] |
269 | // mut c := [[3, 4]] |
270 | // c.insert(0, [1, 2]) // c now is [[1, 2], [3, 4]] |
271 | // ``` |
272 | pub fn (mut a array) insert(i int, val voidptr) { |
273 | $if !no_bounds_checking { |
274 | if i < 0 || i > a.len { |
275 | panic('array.insert: index out of range (i == ${i}, a.len == ${a.len})') |
276 | } |
277 | } |
278 | if a.len >= a.cap { |
279 | a.ensure_cap(a.len + 1) |
280 | } |
281 | unsafe { |
282 | vmemmove(a.get_unsafe(i + 1), a.get_unsafe(i), u64((a.len - i)) * u64(a.element_size)) |
283 | a.set_unsafe(i, val) |
284 | } |
285 | a.len++ |
286 | } |
287 | |
288 | // insert_many is used internally to implement inserting many values |
289 | // into an the array beginning at `i`. |
290 | [unsafe] |
291 | fn (mut a array) insert_many(i int, val voidptr, size int) { |
292 | $if !no_bounds_checking { |
293 | if i < 0 || i > a.len { |
294 | panic('array.insert_many: index out of range (i == ${i}, a.len == ${a.len})') |
295 | } |
296 | } |
297 | a.ensure_cap(a.len + size) |
298 | elem_size := a.element_size |
299 | unsafe { |
300 | iptr := a.get_unsafe(i) |
301 | vmemmove(a.get_unsafe(i + size), iptr, u64(a.len - i) * u64(elem_size)) |
302 | vmemcpy(iptr, val, u64(size) * u64(elem_size)) |
303 | } |
304 | a.len += size |
305 | } |
306 | |
307 | // prepend prepends one or more elements to an array. |
308 | // It is shorthand for `.insert(0, val)` |
309 | pub fn (mut a array) prepend(val voidptr) { |
310 | a.insert(0, val) |
311 | } |
312 | |
313 | // prepend_many prepends another array to this array. |
314 | // NOTE: `.prepend` is probably all you need. |
315 | // NOTE: This code is never called in all of vlib |
316 | [unsafe] |
317 | fn (mut a array) prepend_many(val voidptr, size int) { |
318 | unsafe { a.insert_many(0, val, size) } |
319 | } |
320 | |
321 | // delete deletes array element at index `i`. |
322 | // This is exactly the same as calling `.delete_many(i, 1)`. |
323 | // NOTE: This function does NOT operate in-place. Internally, it |
324 | // creates a copy of the array, skipping over the element at `i`, |
325 | // and then points the original variable to the new memory location. |
326 | // |
327 | // Example: |
328 | // ```v |
329 | // mut a := ['0', '1', '2', '3', '4', '5'] |
330 | // a.delete(1) // a is now ['0', '2', '3', '4', '5'] |
331 | // ``` |
332 | pub fn (mut a array) delete(i int) { |
333 | a.delete_many(i, 1) |
334 | } |
335 | |
336 | // delete_many deletes `size` elements beginning with index `i` |
337 | // NOTE: This function does NOT operate in-place. Internally, it |
338 | // creates a copy of the array, skipping over `size` elements |
339 | // starting at `i`, and then points the original variable |
340 | // to the new memory location. |
341 | // |
342 | // Example: |
343 | // ```v |
344 | // mut a := [1, 2, 3, 4, 5, 6, 7, 8, 9] |
345 | // b := a[..9] // creates a `slice` of `a`, not a clone |
346 | // a.delete_many(4, 3) // replaces `a` with a modified clone |
347 | // dump(a) // a: [1, 2, 3, 4, 8, 9] // `a` is now different |
348 | // dump(b) // b: [1, 2, 3, 4, 5, 6, 7, 8, 9] // `b` is still the same |
349 | // ``` |
350 | pub fn (mut a array) delete_many(i int, size int) { |
351 | $if !no_bounds_checking { |
352 | if i < 0 || i + size > a.len { |
353 | endidx := if size > 1 { '..${i + size}' } else { '' } |
354 | panic('array.delete: index out of range (i == ${i}${endidx}, a.len == ${a.len})') |
355 | } |
356 | } |
357 | if a.flags.all(.noshrink | .noslices) { |
358 | unsafe { |
359 | vmemmove(&u8(a.data) + u64(i) * u64(a.element_size), &u8(a.data) + u64(i + |
360 | size) * u64(a.element_size), u64(a.len - i - size) * u64(a.element_size)) |
361 | } |
362 | a.len -= size |
363 | return |
364 | } |
365 | // Note: if a is [12,34], a.len = 2, a.delete(0) |
366 | // should move (2-0-1) elements = 1 element (the 34) forward |
367 | old_data := a.data |
368 | new_size := a.len - size |
369 | new_cap := if new_size == 0 { 1 } else { new_size } |
370 | a.data = vcalloc(u64(new_cap) * u64(a.element_size)) |
371 | unsafe { vmemcpy(a.data, old_data, u64(i) * u64(a.element_size)) } |
372 | unsafe { |
373 | vmemcpy(&u8(a.data) + u64(i) * u64(a.element_size), &u8(old_data) + u64(i + |
374 | size) * u64(a.element_size), u64(a.len - i - size) * u64(a.element_size)) |
375 | } |
376 | if a.flags.has(.noslices) { |
377 | unsafe { |
378 | free(old_data) |
379 | } |
380 | } |
381 | a.len = new_size |
382 | a.cap = new_cap |
383 | } |
384 | |
385 | // clear clears the array without deallocating the allocated data. |
386 | // It does it by setting the array length to `0` |
387 | // Example: a.clear() // `a.len` is now 0 |
388 | pub fn (mut a array) clear() { |
389 | a.len = 0 |
390 | } |
391 | |
392 | // trim trims the array length to `index` without modifying the allocated data. |
393 | // If `index` is greater than `len` nothing will be changed. |
394 | // Example: a.trim(3) // `a.len` is now <= 3 |
395 | pub fn (mut a array) trim(index int) { |
396 | if index < a.len { |
397 | a.len = index |
398 | } |
399 | } |
400 | |
401 | // drop advances the array past the first `num` elements whilst preserving spare capacity. |
402 | // If `num` is greater than `len` the array will be emptied. |
403 | // Example: |
404 | // ```v |
405 | // mut a := [1,2] |
406 | // a << 3 |
407 | // a.drop(2) |
408 | // assert a == [3] |
409 | // assert a.cap > a.len |
410 | // ``` |
411 | pub fn (mut a array) drop(num int) { |
412 | if num <= 0 { |
413 | return |
414 | } |
415 | n := if num <= a.len { num } else { a.len } |
416 | blen := u64(n) * u64(a.element_size) |
417 | a.data = unsafe { &u8(a.data) + blen } |
418 | a.offset += int(blen) // TODO: offset should become 64bit as well |
419 | a.len -= n |
420 | a.cap -= n |
421 | } |
422 | |
423 | // we manually inline this for single operations for performance without -prod |
424 | [inline; unsafe] |
425 | fn (a array) get_unsafe(i int) voidptr { |
426 | unsafe { |
427 | return &u8(a.data) + u64(i) * u64(a.element_size) |
428 | } |
429 | } |
430 | |
431 | // Private function. Used to implement array[] operator. |
432 | fn (a array) get(i int) voidptr { |
433 | $if !no_bounds_checking { |
434 | if i < 0 || i >= a.len { |
435 | panic('array.get: index out of range (i == ${i}, a.len == ${a.len})') |
436 | } |
437 | } |
438 | unsafe { |
439 | return &u8(a.data) + u64(i) * u64(a.element_size) |
440 | } |
441 | } |
442 | |
443 | // Private function. Used to implement x = a[i] or { ... } |
444 | fn (a array) get_with_check(i int) voidptr { |
445 | if i < 0 || i >= a.len { |
446 | return 0 |
447 | } |
448 | unsafe { |
449 | return &u8(a.data) + u64(i) * u64(a.element_size) |
450 | } |
451 | } |
452 | |
453 | // first returns the first element of the `array`. |
454 | // If the `array` is empty, this will panic. |
455 | // However, `a[0]` returns an error object |
456 | // so it can be handled with an `or` block. |
457 | pub fn (a array) first() voidptr { |
458 | $if !no_bounds_checking { |
459 | if a.len == 0 { |
460 | panic('array.first: array is empty') |
461 | } |
462 | } |
463 | return a.data |
464 | } |
465 | |
466 | // last returns the last element of the `array`. |
467 | // If the `array` is empty, this will panic. |
468 | pub fn (a array) last() voidptr { |
469 | $if !no_bounds_checking { |
470 | if a.len == 0 { |
471 | panic('array.last: array is empty') |
472 | } |
473 | } |
474 | unsafe { |
475 | return &u8(a.data) + u64(a.len - 1) * u64(a.element_size) |
476 | } |
477 | } |
478 | |
479 | // pop returns the last element of the array, and removes it. |
480 | // If the `array` is empty, this will panic. |
481 | // NOTE: this function reduces the length of the given array, |
482 | // but arrays sliced from this one will not change. They still |
483 | // retain their "view" of the underlying memory. |
484 | // |
485 | // Example: |
486 | // ```v |
487 | // mut a := [1, 2, 3, 4, 5, 6, 7, 8, 9] |
488 | // b := a[..9] // creates a "view" into the same memory |
489 | // c := a.pop() // c == 9 |
490 | // a[1] = 5 |
491 | // dump(a) // a: [1, 5, 3, 4, 5, 6, 7, 8] |
492 | // dump(b) // b: [1, 5, 3, 4, 5, 6, 7, 8, 9] |
493 | // ``` |
494 | pub fn (mut a array) pop() voidptr { |
495 | // in a sense, this is the opposite of `a << x` |
496 | $if !no_bounds_checking { |
497 | if a.len == 0 { |
498 | panic('array.pop: array is empty') |
499 | } |
500 | } |
501 | new_len := a.len - 1 |
502 | last_elem := unsafe { &u8(a.data) + u64(new_len) * u64(a.element_size) } |
503 | a.len = new_len |
504 | // Note: a.cap is not changed here *on purpose*, so that |
505 | // further << ops on that array will be more efficient. |
506 | return last_elem |
507 | } |
508 | |
509 | // delete_last efficiently deletes the last element of the array. |
510 | // It does it simply by reducing the length of the array by 1. |
511 | // If the array is empty, this will panic. |
512 | // See also: [trim](#array.trim) |
513 | pub fn (mut a array) delete_last() { |
514 | // copy pasting code for performance |
515 | $if !no_bounds_checking { |
516 | if a.len == 0 { |
517 | panic('array.pop: array is empty') |
518 | } |
519 | } |
520 | a.len-- |
521 | } |
522 | |
523 | // slice returns an array using the same buffer as original array |
524 | // but starting from the `start` element and ending with the element before |
525 | // the `end` element of the original array with the length and capacity |
526 | // set to the number of the elements in the slice. |
527 | // It will remain tied to the same memory location until the length increases |
528 | // (copy on grow) or `.clone()` is called on it. |
529 | // If `start` and `end` are invalid this function will panic. |
530 | // Alternative: Slices can also be made with [start..end] notation |
531 | // Alternative: `.slice_ni()` will always return an array. |
532 | fn (a array) slice(start int, _end int) array { |
533 | mut end := _end |
534 | $if !no_bounds_checking { |
535 | if start > end { |
536 | panic('array.slice: invalid slice index (${start} > ${end})') |
537 | } |
538 | if end > a.len { |
539 | panic('array.slice: slice bounds out of range (${end} >= ${a.len})') |
540 | } |
541 | if start < 0 { |
542 | panic('array.slice: slice bounds out of range (${start} < 0)') |
543 | } |
544 | } |
545 | // TODO: integrate reference counting |
546 | // a.flags.clear(.noslices) |
547 | offset := u64(start) * u64(a.element_size) |
548 | data := unsafe { &u8(a.data) + offset } |
549 | l := end - start |
550 | res := array{ |
551 | element_size: a.element_size |
552 | data: data |
553 | offset: a.offset + int(offset) // TODO: offset should become 64bit |
554 | len: l |
555 | cap: l |
556 | } |
557 | return res |
558 | } |
559 | |
560 | // slice_ni returns an array using the same buffer as original array |
561 | // but starting from the `start` element and ending with the element before |
562 | // the `end` element of the original array. |
563 | // This function can use negative indexes `a.slice_ni(-3, a.len)` |
564 | // that get the last 3 elements of the array otherwise it return an empty array. |
565 | // This function always return a valid array. |
566 | fn (a array) slice_ni(_start int, _end int) array { |
567 | // a.flags.clear(.noslices) |
568 | mut end := _end |
569 | mut start := _start |
570 | |
571 | if start < 0 { |
572 | start = a.len + start |
573 | if start < 0 { |
574 | start = 0 |
575 | } |
576 | } |
577 | |
578 | if end < 0 { |
579 | end = a.len + end |
580 | if end < 0 { |
581 | end = 0 |
582 | } |
583 | } |
584 | if end >= a.len { |
585 | end = a.len |
586 | } |
587 | |
588 | if start >= a.len || start > end { |
589 | res := array{ |
590 | element_size: a.element_size |
591 | data: a.data |
592 | offset: 0 |
593 | len: 0 |
594 | cap: 0 |
595 | } |
596 | return res |
597 | } |
598 | |
599 | offset := u64(start) * u64(a.element_size) |
600 | data := unsafe { &u8(a.data) + offset } |
601 | l := end - start |
602 | res := array{ |
603 | element_size: a.element_size |
604 | data: data |
605 | offset: a.offset + int(offset) // TODO: offset should be 64bit |
606 | len: l |
607 | cap: l |
608 | } |
609 | return res |
610 | } |
611 | |
612 | // used internally for [2..4] |
613 | fn (a array) slice2(start int, _end int, end_max bool) array { |
614 | end := if end_max { a.len } else { _end } |
615 | return a.slice(start, end) |
616 | } |
617 | |
618 | // clone_static_to_depth() returns an independent copy of a given array. |
619 | // Unlike `clone_to_depth()` it has a value receiver and is used internally |
620 | // for slice-clone expressions like `a[2..4].clone()` and in -autofree generated code. |
621 | fn (a array) clone_static_to_depth(depth int) array { |
622 | return unsafe { a.clone_to_depth(depth) } |
623 | } |
624 | |
625 | // clone returns an independent copy of a given array. |
626 | // this will be overwritten by `cgen` with an apropriate call to `.clone_to_depth()` |
627 | // However the `checker` needs it here. |
628 | pub fn (a &array) clone() array { |
629 | return unsafe { a.clone_to_depth(0) } |
630 | } |
631 | |
632 | // recursively clone given array - `unsafe` when called directly because depth is not checked |
633 | [unsafe] |
634 | pub fn (a &array) clone_to_depth(depth int) array { |
635 | mut arr := array{ |
636 | element_size: a.element_size |
637 | data: vcalloc(u64(a.cap) * u64(a.element_size)) |
638 | len: a.len |
639 | cap: a.cap |
640 | } |
641 | // Recursively clone-generated elements if array element is array type |
642 | if depth > 0 && a.element_size == sizeof(array) && a.len >= 0 && a.cap >= a.len { |
643 | for i in 0 .. a.len { |
644 | ar := array{} |
645 | unsafe { vmemcpy(&ar, a.get_unsafe(i), int(sizeof(array))) } |
646 | ar_clone := unsafe { ar.clone_to_depth(depth - 1) } |
647 | unsafe { arr.set_unsafe(i, &ar_clone) } |
648 | } |
649 | return arr |
650 | } else { |
651 | if a.data != 0 { |
652 | unsafe { vmemcpy(&u8(arr.data), a.data, u64(a.cap) * u64(a.element_size)) } |
653 | } |
654 | return arr |
655 | } |
656 | } |
657 | |
658 | // we manually inline this for single operations for performance without -prod |
659 | [inline; unsafe] |
660 | fn (mut a array) set_unsafe(i int, val voidptr) { |
661 | unsafe { vmemcpy(&u8(a.data) + u64(a.element_size) * u64(i), val, a.element_size) } |
662 | } |
663 | |
664 | // Private function. Used to implement assignment to the array element. |
665 | fn (mut a array) set(i int, val voidptr) { |
666 | $if !no_bounds_checking { |
667 | if i < 0 || i >= a.len { |
668 | panic('array.set: index out of range (i == ${i}, a.len == ${a.len})') |
669 | } |
670 | } |
671 | unsafe { vmemcpy(&u8(a.data) + u64(a.element_size) * u64(i), val, a.element_size) } |
672 | } |
673 | |
674 | fn (mut a array) push(val voidptr) { |
675 | if a.len >= a.cap { |
676 | a.ensure_cap(a.len + 1) |
677 | } |
678 | unsafe { vmemcpy(&u8(a.data) + u64(a.element_size) * u64(a.len), val, a.element_size) } |
679 | a.len++ |
680 | } |
681 | |
682 | // push_many implements the functionality for pushing another array. |
683 | // `val` is array.data and user facing usage is `a << [1,2,3]` |
684 | [unsafe] |
685 | pub fn (mut a3 array) push_many(val voidptr, size int) { |
686 | if size <= 0 || val == unsafe { nil } { |
687 | return |
688 | } |
689 | a3.ensure_cap(a3.len + size) |
690 | if a3.data == val && a3.data != 0 { |
691 | // handle `arr << arr` |
692 | copy := a3.clone() |
693 | unsafe { |
694 | vmemcpy(&u8(a3.data) + u64(a3.element_size) * u64(a3.len), copy.data, u64(a3.element_size) * u64(size)) |
695 | } |
696 | } else { |
697 | if a3.data != 0 && val != 0 { |
698 | unsafe { vmemcpy(&u8(a3.data) + u64(a3.element_size) * u64(a3.len), val, u64(a3.element_size) * u64(size)) } |
699 | } |
700 | } |
701 | a3.len += size |
702 | } |
703 | |
704 | // reverse_in_place reverses existing array data, modifying original array. |
705 | pub fn (mut a array) reverse_in_place() { |
706 | if a.len < 2 || a.element_size == 0 { |
707 | return |
708 | } |
709 | unsafe { |
710 | mut tmp_value := malloc(a.element_size) |
711 | for i in 0 .. a.len / 2 { |
712 | vmemcpy(tmp_value, &u8(a.data) + u64(i) * u64(a.element_size), a.element_size) |
713 | vmemcpy(&u8(a.data) + u64(i) * u64(a.element_size), &u8(a.data) + |
714 | u64(a.len - 1 - i) * u64(a.element_size), a.element_size) |
715 | vmemcpy(&u8(a.data) + u64(a.len - 1 - i) * u64(a.element_size), tmp_value, |
716 | a.element_size) |
717 | } |
718 | free(tmp_value) |
719 | } |
720 | } |
721 | |
722 | // reverse returns a new array with the elements of the original array in reverse order. |
723 | pub fn (a array) reverse() array { |
724 | if a.len < 2 { |
725 | return a |
726 | } |
727 | mut arr := array{ |
728 | element_size: a.element_size |
729 | data: vcalloc(u64(a.cap) * u64(a.element_size)) |
730 | len: a.len |
731 | cap: a.cap |
732 | } |
733 | for i in 0 .. a.len { |
734 | unsafe { arr.set_unsafe(i, a.get_unsafe(a.len - 1 - i)) } |
735 | } |
736 | return arr |
737 | } |
738 | |
739 | // free frees all memory occupied by the array. |
740 | [unsafe] |
741 | pub fn (a &array) free() { |
742 | $if prealloc { |
743 | return |
744 | } |
745 | // if a.is_slice { |
746 | // return |
747 | // } |
748 | if a.flags.has(.nofree) { |
749 | return |
750 | } |
751 | mblock_ptr := &u8(u64(a.data) - u64(a.offset)) |
752 | unsafe { free(mblock_ptr) } |
753 | unsafe { |
754 | a.data = nil |
755 | } |
756 | } |
757 | |
758 | // Some of the following functions have no implementation in V and exist here |
759 | // to expose them to the array namespace. Their implementation is compiler |
760 | // specific because of their use of `it` and `a < b` expressions. |
761 | // Therefore, the implementation is left to the backend. |
762 | |
763 | // filter creates a new array with all elements that pass the test. |
764 | // Ignore the function signature. `filter` does not take an actual callback. Rather, it |
765 | // takes an `it` expression. |
766 | // |
767 | // Certain array functions (`filter` `any` `all`) support a simplified |
768 | // domain-specific-language by the backend compiler to make these operations |
769 | // more idiomatic to V. These functions are described here, but their implementation |
770 | // is compiler specific. |
771 | // |
772 | // Each function takes a boolean test expression as its single argument. |
773 | // These test expressions may use `it` as a pointer to a single element at a time. |
774 | // |
775 | // Example: array.filter(it < 5) // create an array of elements less than 5 |
776 | // Example: array.filter(it % 2 == 1) // create an array of only odd elements |
777 | // Example: array.filter(it.name[0] == `A`) // create an array of elements whose `name` field starts with 'A' |
778 | pub fn (a array) filter(predicate fn (voidptr) bool) array |
779 | |
780 | // any tests whether at least one element in the array passes the test. |
781 | // Ignore the function signature. `any` does not take an actual callback. Rather, it |
782 | // takes an `it` expression. |
783 | // It returns `true` if it finds an element passing the test. Otherwise, |
784 | // it returns `false`. It doesn't modify the array. |
785 | // |
786 | // Example: array.any(it % 2 == 1) // will return true if any element is odd |
787 | // Example: array.any(it.name == 'Bob') // will yield `true` if any element has `.name == 'Bob'` |
788 | pub fn (a array) any(predicate fn (voidptr) bool) bool |
789 | |
790 | // all tests whether all elements in the array pass the test. |
791 | // Ignore the function signature. `all` does not take an actual callback. Rather, it |
792 | // takes an `it` expression. |
793 | // It returns `false` if any element fails the test. Otherwise, |
794 | // it returns `true`. It doesn't modify the array. |
795 | // |
796 | // Example: array.all(it % 2 == 1) // will return true if every element is odd |
797 | pub fn (a array) all(predicate fn (voidptr) bool) bool |
798 | |
799 | // map creates a new array populated with the results of calling a provided function |
800 | // on every element in the calling array. |
801 | // It also accepts an `it` expression. |
802 | // |
803 | // Example: |
804 | // ```v |
805 | // words := ['hello', 'world'] |
806 | // r1 := words.map(it.to_upper()) |
807 | // assert r1 == ['HELLO', 'WORLD'] |
808 | // |
809 | // // map can also accept anonymous functions |
810 | // r2 := words.map(fn (w string) string { |
811 | // return w.to_upper() |
812 | // }) |
813 | // assert r2 == ['HELLO', 'WORLD'] |
814 | // ``` |
815 | pub fn (a array) map(callback fn (voidptr) voidptr) array |
816 | |
817 | // sort sorts the array in place. |
818 | // Ignore the function signature. Passing a callback to `.sort` is not supported |
819 | // for now. Consider using the `.sort_with_compare` method if you need it. |
820 | // |
821 | // sort can take a boolean test expression as its single argument. |
822 | // The expression uses 2 'magic' variables `a` and `b` as pointers to the two elements |
823 | // being compared. |
824 | // |
825 | // Example: array.sort() // will sort the array in ascending order |
826 | // Example: array.sort(b < a) // will sort the array in decending order |
827 | // Example: array.sort(b.name < a.name) // will sort descending by the .name field |
828 | pub fn (mut a array) sort(callback fn (voidptr, voidptr) int) |
829 | |
830 | // sorted returns a sorted copy of the original array. The original array is *NOT* modified. |
831 | // See also .sort() . |
832 | // Example: assert [9,1,6,3,9].sorted() == [1,3,6,9,9] |
833 | // Example: assert [9,1,6,3,9].sorted(b < a) == [9,9,6,3,1] |
834 | pub fn (a &array) sorted(callback fn (voidptr, voidptr) int) array |
835 | |
836 | // sort_with_compare sorts the array in-place using the results of the |
837 | // given function to determine sort order. |
838 | // |
839 | // The function should return one of three values: |
840 | // - `-1` when `a` should come before `b` ( `a < b` ) |
841 | // - `1` when `b` should come before `a` ( `b < a` ) |
842 | // - `0` when the order cannot be determined ( `a == b` ) |
843 | // |
844 | // Example: |
845 | // ```v |
846 | // fn main() { |
847 | // mut a := ['hi', '1', '5', '3'] |
848 | // a.sort_with_compare(fn (a &string, b &string) int { |
849 | // if a < b { |
850 | // return -1 |
851 | // } |
852 | // if a > b { |
853 | // return 1 |
854 | // } |
855 | // return 0 |
856 | // }) |
857 | // assert a == ['1', '3', '5', 'hi'] |
858 | // } |
859 | // ``` |
860 | pub fn (mut a array) sort_with_compare(callback fn (voidptr, voidptr) int) { |
861 | $if freestanding { |
862 | panic('sort_with_compare does not work with -freestanding') |
863 | } $else { |
864 | unsafe { vqsort(a.data, usize(a.len), usize(a.element_size), callback) } |
865 | } |
866 | } |
867 | |
868 | // sorted_with_compare sorts a clone of the array, using the results of the |
869 | // given function to determine sort order. The original array is not modified. |
870 | // See also .sort_with_compare() |
871 | pub fn (a &array) sorted_with_compare(callback fn (voidptr, voidptr) int) array { |
872 | $if freestanding { |
873 | panic('sorted_with_compare does not work with -freestanding') |
874 | } $else { |
875 | mut r := a.clone() |
876 | unsafe { vqsort(r.data, usize(r.len), usize(r.element_size), callback) } |
877 | return r |
878 | } |
879 | return array{} |
880 | } |
881 | |
882 | // contains determines whether an array includes a certain value among its elements |
883 | // It will return `true` if the array contains an element with this value. |
884 | // It is similar to `.any` but does not take an `it` expression. |
885 | // |
886 | // Example: [1, 2, 3].contains(4) == false |
887 | pub fn (a array) contains(value voidptr) bool |
888 | |
889 | // index returns the first index at which a given element can be found in the array |
890 | // or `-1` if the value is not found. |
891 | pub fn (a array) index(value voidptr) int |
892 | |
893 | [direct_array_access; unsafe] |
894 | pub fn (mut a []string) free() { |
895 | $if prealloc { |
896 | return |
897 | } |
898 | for mut s in a { |
899 | unsafe { s.free() } |
900 | } |
901 | unsafe { (&array(&a)).free() } |
902 | } |
903 | |
904 | // The following functions are type-specific functions that apply |
905 | // to arrays of different types in different ways. |
906 | |
907 | // str returns a string representation of an array of strings |
908 | // Example: ['a', 'b', 'c'].str() // => "['a', 'b', 'c']". |
909 | [direct_array_access; manualfree] |
910 | pub fn (a []string) str() string { |
911 | mut sb_len := 4 // 2x" + 1x, + 1xspace |
912 | if a.len > 0 { |
913 | // assume that most strings will be ~large as the first |
914 | sb_len += a[0].len |
915 | sb_len *= a.len |
916 | } |
917 | sb_len += 2 // 1x[ + 1x] |
918 | mut sb := strings.new_builder(sb_len) |
919 | sb.write_u8(`[`) |
920 | for i in 0 .. a.len { |
921 | val := a[i] |
922 | sb.write_u8(`'`) |
923 | sb.write_string(val) |
924 | sb.write_u8(`'`) |
925 | if i < a.len - 1 { |
926 | sb.write_string(', ') |
927 | } |
928 | } |
929 | sb.write_u8(`]`) |
930 | res := sb.str() |
931 | unsafe { sb.free() } |
932 | return res |
933 | } |
934 | |
935 | // hex returns a string with the hexadecimal representation |
936 | // of the byte elements of the array. |
937 | pub fn (b []u8) hex() string { |
938 | mut hex := unsafe { malloc_noscan(u64(b.len) * 2 + 1) } |
939 | mut dst_i := 0 |
940 | for i in b { |
941 | n0 := i >> 4 |
942 | unsafe { |
943 | hex[dst_i] = if n0 < 10 { n0 + `0` } else { n0 + u8(87) } |
944 | dst_i++ |
945 | } |
946 | n1 := i & 0xF |
947 | unsafe { |
948 | hex[dst_i] = if n1 < 10 { n1 + `0` } else { n1 + u8(87) } |
949 | dst_i++ |
950 | } |
951 | } |
952 | unsafe { |
953 | hex[dst_i] = 0 |
954 | return tos(hex, dst_i) |
955 | } |
956 | } |
957 | |
958 | // copy copies the `src` byte array elements to the `dst` byte array. |
959 | // The number of the elements copied is the minimum of the length of both arrays. |
960 | // Returns the number of elements copied. |
961 | // NOTE: This is not an `array` method. It is a function that takes two arrays of bytes. |
962 | // See also: `arrays.copy`. |
963 | pub fn copy(mut dst []u8, src []u8) int { |
964 | min := if dst.len < src.len { dst.len } else { src.len } |
965 | if min > 0 { |
966 | unsafe { vmemmove(&u8(dst.data), src.data, min) } |
967 | } |
968 | return min |
969 | } |
970 | |
971 | // grow_cap grows the array's capacity by `amount` elements. |
972 | // Internally, it does this by copying the entire array to |
973 | // a new memory location (creating a clone). |
974 | pub fn (mut a array) grow_cap(amount int) { |
975 | a.ensure_cap(a.cap + amount) |
976 | } |
977 | |
978 | // grow_len ensures that an array has a.len + amount of length |
979 | // Internally, it does this by copying the entire array to |
980 | // a new memory location (creating a clone) unless the array.cap |
981 | // is already large enough. |
982 | [unsafe] |
983 | pub fn (mut a array) grow_len(amount int) { |
984 | a.ensure_cap(a.len + amount) |
985 | a.len += amount |
986 | } |
987 | |
988 | // pointers returns a new array, where each element |
989 | // is the address of the corresponding element in the array. |
990 | [unsafe] |
991 | pub fn (a array) pointers() []voidptr { |
992 | mut res := []voidptr{} |
993 | for i in 0 .. a.len { |
994 | unsafe { res << a.get_unsafe(i) } |
995 | } |
996 | return res |
997 | } |
998 | |
999 | // vbytes on`voidptr` makes a V []u8 structure from a C style memory buffer. |
1000 | // NOTE: the data is reused, NOT copied! |
1001 | [unsafe] |
1002 | pub fn (data voidptr) vbytes(len int) []u8 { |
1003 | res := array{ |
1004 | element_size: 1 |
1005 | data: data |
1006 | len: len |
1007 | cap: len |
1008 | } |
1009 | return res |
1010 | } |
1011 | |
1012 | // vbytes on `&u8` makes a V []u8 structure from a C style memory buffer. |
1013 | // NOTE: the data is reused, NOT copied! |
1014 | [unsafe] |
1015 | pub fn (data &u8) vbytes(len int) []u8 { |
1016 | return unsafe { voidptr(data).vbytes(len) } |
1017 | } |