| 1 | module arrays |
| 2 | |
| 3 | import strings |
| 4 | |
| 5 | // Common arrays functions: |
| 6 | // - min / max - return the value of the minimum / maximum |
| 7 | // - idx_min / idx_max - return the index of the first minimum / maximum |
| 8 | // - merge - combine two sorted arrays and maintain sorted order |
| 9 | // - append - combine two arrays, by appending the second array to the first |
| 10 | // - chunk - chunk array to arrays with n elements |
| 11 | // - window - get snapshots of the window of the given size sliding along array with the given step, where each snapshot is an array |
| 12 | // - group - merge two arrays by interleaving e.g. arrays.group([1,3,5], [2,4,6]) => [[1,2],[3,4],[5,6]] |
| 13 | // - flatten - reduce dimensionality of array by one. e.g. arrays.flatten([[1,2],[3,4],[5,6]]) => [1,2,3,4,5,6] |
| 14 | // - each - call a callback fn, for each element of the array, similar to a.map(), but unlike it, the callback should not return anything |
| 15 | |
| 16 | // min returns the minimum value in the array. |
| 17 | // Example: arrays.min([1, 2, 3, 0, 9])! // => 0 |
| 18 | pub fn min[T](array []T) !T { |
| 19 | if array.len == 0 { |
| 20 | return error('.min called on an empty array') |
| 21 | } |
| 22 | mut val := array[0] |
| 23 | for e in array { |
| 24 | if e < val { |
| 25 | val = e |
| 26 | } |
| 27 | } |
| 28 | return val |
| 29 | } |
| 30 | |
| 31 | // max returns the maximum value in the array. |
| 32 | // Example: arrays.max([1, 2, 3, 0, 9])! // => 9 |
| 33 | pub fn max[T](array []T) !T { |
| 34 | if array.len == 0 { |
| 35 | return error('.max called on an empty array') |
| 36 | } |
| 37 | mut val := array[0] |
| 38 | for e in array { |
| 39 | if e > val { |
| 40 | val = e |
| 41 | } |
| 42 | } |
| 43 | return val |
| 44 | } |
| 45 | |
| 46 | // idx_min returns the index of the minimum value in the array. |
| 47 | // Example: arrays.idx_min([1, 2, 3, 0, 9])! // => 3 |
| 48 | pub fn idx_min[T](array []T) !int { |
| 49 | if array.len == 0 { |
| 50 | return error('.idx_min called on an empty array') |
| 51 | } |
| 52 | mut idx := 0 |
| 53 | mut val := array[0] |
| 54 | for i, e in array { |
| 55 | if e < val { |
| 56 | val = e |
| 57 | idx = i |
| 58 | } |
| 59 | } |
| 60 | return idx |
| 61 | } |
| 62 | |
| 63 | // idx_max returns the index of the maximum value in the array. |
| 64 | // Example: arrays.idx_max([1, 2, 3, 0, 9])! // => 4 |
| 65 | pub fn idx_max[T](array []T) !int { |
| 66 | if array.len == 0 { |
| 67 | return error('.idx_max called on an empty array') |
| 68 | } |
| 69 | mut idx := 0 |
| 70 | mut val := array[0] |
| 71 | for i, e in array { |
| 72 | if e > val { |
| 73 | val = e |
| 74 | idx = i |
| 75 | } |
| 76 | } |
| 77 | return idx |
| 78 | } |
| 79 | |
| 80 | // merge two sorted arrays (ascending) and maintain sorted order. |
| 81 | // Example: arrays.merge([1, 3, 5, 7], [2, 4, 6, 8]) // => [1, 2, 3, 4, 5, 6, 7, 8] |
| 82 | @[direct_array_access] |
| 83 | pub fn merge[T](a []T, b []T) []T { |
| 84 | mut m := []T{len: a.len + b.len} |
| 85 | mut ia := 0 |
| 86 | mut ib := 0 |
| 87 | mut j := 0 |
| 88 | // TODO: efficient approach to merge_desc where: a[ia] >= b[ib] |
| 89 | for ia < a.len && ib < b.len { |
| 90 | if a[ia] <= b[ib] { |
| 91 | m[j] = a[ia] |
| 92 | ia++ |
| 93 | } else { |
| 94 | m[j] = b[ib] |
| 95 | ib++ |
| 96 | } |
| 97 | j++ |
| 98 | } |
| 99 | // a leftovers |
| 100 | for ia < a.len { |
| 101 | m[j] = a[ia] |
| 102 | ia++ |
| 103 | j++ |
| 104 | } |
| 105 | // b leftovers |
| 106 | for ib < b.len { |
| 107 | m[j] = b[ib] |
| 108 | ib++ |
| 109 | j++ |
| 110 | } |
| 111 | return m |
| 112 | } |
| 113 | |
| 114 | // append the second array `b` to the first array `a`, and return the result. |
| 115 | // Note, that unlike arrays.concat, arrays.append is less flexible, but more efficient, |
| 116 | // since it does not require you to use ...a for the second parameter. |
| 117 | // Example: arrays.append([1, 3, 5, 7], [2, 4, 6, 8]) // => [1, 3, 5, 7, 2, 4, 6, 8] |
| 118 | pub fn append[T](a []T, b []T) []T { |
| 119 | mut m := []T{cap: a.len + b.len} |
| 120 | m << a |
| 121 | m << b |
| 122 | return m |
| 123 | } |
| 124 | |
| 125 | // group n arrays into a single array of arrays with n elements. |
| 126 | // This function is analogous to the "zip" function of other languages. |
| 127 | // To fully interleave two arrays, follow this function with a call to `flatten`. |
| 128 | // NOTE: An error will be generated if the type annotation is omitted. |
| 129 | // Example: arrays.group[int]([1, 2, 3], [4, 5, 6]) // => [[1, 4], [2, 5], [3, 6]] |
| 130 | pub fn group[T](arrs ...[]T) [][]T { |
| 131 | mut length := if arrs.len > 0 { arrs[0].len } else { 0 } |
| 132 | // calculate length of output by finding shortest input array |
| 133 | for ndx in 1 .. arrs.len { |
| 134 | if arrs[ndx].len < length { |
| 135 | length = arrs[ndx].len |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if length > 0 { |
| 140 | mut arr := [][]T{cap: length} |
| 141 | // append all combined arrays into the resultant array |
| 142 | for ndx in 0 .. length { |
| 143 | mut grouped := []T{cap: arrs.len} |
| 144 | // combine each list item for the ndx position into one array |
| 145 | for arr_ndx in 0 .. arrs.len { |
| 146 | grouped << arrs[arr_ndx][ndx] |
| 147 | } |
| 148 | arr << grouped |
| 149 | } |
| 150 | return arr |
| 151 | } |
| 152 | |
| 153 | return [][]T{} |
| 154 | } |
| 155 | |
| 156 | // chunk array into a single array of arrays where each element is the next `size` elements of the original. |
| 157 | // Example: arrays.chunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 2) // => [[1, 2], [3, 4], [5, 6], [7, 8], [9]] |
| 158 | pub fn chunk[T](array []T, size int) [][]T { |
| 159 | // allocate chunk array |
| 160 | mut chunks := [][]T{cap: array.len / size + if array.len % size == 0 { 0 } else { 1 }} |
| 161 | |
| 162 | for i := 0; true; { |
| 163 | // check chunk size is greater than remaining element size |
| 164 | if array.len < i + size { |
| 165 | // check if there's no more element to chunk |
| 166 | if array.len <= i { |
| 167 | break |
| 168 | } |
| 169 | |
| 170 | chunks << array[i..] |
| 171 | |
| 172 | break |
| 173 | } |
| 174 | |
| 175 | chunks << array[i..i + size] |
| 176 | i += size |
| 177 | } |
| 178 | |
| 179 | return chunks |
| 180 | } |
| 181 | |
| 182 | // chunk_while splits the input array `a` into chunks of varying length, using the `predicate`, passing to it pairs of adjacent elements `before` and `after`. |
| 183 | // Each chunk, will contain all ajdacent elements, for which the `predicate` returned true. |
| 184 | // The chunks are split *between* the `before` and `after` elements, for which the `predicate` returned false. |
| 185 | // Example: assert arrays.chunk_while([0,9,2,2,3,2,7,5,9,5],fn(x int,y int)bool{return x<=y})==[[0,9],[2,2,3],[2,7],[5,9],[5]] |
| 186 | // Example: assert arrays.chunk_while('aaaabbbcca'.runes(),fn(x rune,y rune)bool{return x==y})==[[`a`,`a`,`a`,`a`],[`b`,`b`,`b`],[`c`,`c`],[`a`]] |
| 187 | // Example: assert arrays.chunk_while('aaaabbbcca'.runes(),fn(x rune,y rune)bool{return x==y}).map({it[0]:it.len})==[{`a`:4},{`b`:3},{`c`:2},{`a`:1}] |
| 188 | pub fn chunk_while[T](a []T, predicate fn (before T, after T) bool) [][]T { |
| 189 | if a.len == 0 { |
| 190 | return [] |
| 191 | } |
| 192 | mut chunks := [][]T{} |
| 193 | mut chunk := [a[0]] |
| 194 | mut i := 0 |
| 195 | for i = 1; i < a.len; i++ { |
| 196 | // eprintln('> i: ${i} | a[i]: ${a[i]} | predicate: ${predicate(a[i-1], a[i]):10} | chunk: ${chunk}') |
| 197 | if predicate(a[i - 1], a[i]) { |
| 198 | chunk << a[i] |
| 199 | continue |
| 200 | } |
| 201 | if chunk.len > 0 { |
| 202 | chunks << chunk |
| 203 | } |
| 204 | chunk = [a[i]] |
| 205 | } |
| 206 | if chunk.len > 0 { |
| 207 | chunks << chunk |
| 208 | } |
| 209 | return chunks |
| 210 | } |
| 211 | |
| 212 | pub struct WindowAttribute { |
| 213 | pub: |
| 214 | size int |
| 215 | step int = 1 |
| 216 | } |
| 217 | |
| 218 | // get snapshots of the window of the given size sliding along array with the given step, where each snapshot is an array. |
| 219 | // - `size` - snapshot size |
| 220 | // - `step` - gap size between each snapshot, default is 1. |
| 221 | // |
| 222 | // Example: arrays.window([1, 2, 3, 4], size: 2) // => [[1, 2], [2, 3], [3, 4]] |
| 223 | // Example: arrays.window([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], size: 3, step: 2) // => [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]] |
| 224 | pub fn window[T](array []T, attr WindowAttribute) [][]T { |
| 225 | if array.len == 0 { |
| 226 | return [][]T{} |
| 227 | } |
| 228 | // allocate snapshot array |
| 229 | mut windows := [][]T{cap: array.len - attr.size + 1} |
| 230 | |
| 231 | for i := 0; true; { |
| 232 | // check remaining elements size is less than snapshot size |
| 233 | if array.len < i + attr.size { |
| 234 | break |
| 235 | } |
| 236 | |
| 237 | windows << array[i..i + attr.size] |
| 238 | i += attr.step |
| 239 | } |
| 240 | |
| 241 | return windows |
| 242 | } |
| 243 | |
| 244 | // sum up array, return an error, when the array has no elements. |
| 245 | // Example: arrays.sum([1, 2, 3, 4, 5])! // => 15 |
| 246 | pub fn sum[T](array []T) !T { |
| 247 | if array.len == 0 { |
| 248 | return error('Cannot sum up array of nothing.') |
| 249 | } else { |
| 250 | mut head := array[0] |
| 251 | |
| 252 | for e in array[1..] { |
| 253 | head += e |
| 254 | } |
| 255 | |
| 256 | return head |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // reduce sets `acc = array[0]`, then successively calls `acc = reduce_op(acc, elem)` for each remaining element in `array`. |
| 261 | // returns the accumulated value in `acc`. |
| 262 | // returns an error if the array is empty. |
| 263 | // See also: [fold](#fold). |
| 264 | // Example: arrays.reduce([1, 2, 3, 4, 5], fn (t1 int, t2 int) int { return t1 * t2 })! // => 120 |
| 265 | pub fn reduce[T](array []T, reduce_op fn (acc T, elem T) T) !T { |
| 266 | if array.len == 0 { |
| 267 | return error('Cannot reduce array of nothing.') |
| 268 | } else { |
| 269 | mut value := array[0] |
| 270 | |
| 271 | for e in array[1..] { |
| 272 | value = reduce_op(value, e) |
| 273 | } |
| 274 | |
| 275 | return value |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // reduce_indexed sets `acc = array[0]`, then successively calls `acc = reduce_op(idx, acc, elem)` for each remaining element in `array`. |
| 280 | // returns the accumulated value in `acc`. |
| 281 | // returns an error if the array is empty. |
| 282 | // See also: [fold_indexed](#fold_indexed). |
| 283 | pub fn reduce_indexed[T](array []T, reduce_op fn (idx int, acc T, elem T) T) !T { |
| 284 | if array.len == 0 { |
| 285 | return error('Cannot reduce array of nothing.') |
| 286 | } else { |
| 287 | mut value := array[0] |
| 288 | |
| 289 | for i, e in array { |
| 290 | if i == 0 { |
| 291 | continue |
| 292 | } else { |
| 293 | value = reduce_op(i, value, e) |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return value |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // filter_indexed filters elements based on `predicate` function being invoked on each element with its index in the original array. |
| 302 | pub fn filter_indexed[T](array []T, predicate fn (idx int, elem T) bool) []T { |
| 303 | mut result := []T{cap: array.len} |
| 304 | |
| 305 | for i, e in array { |
| 306 | if predicate(i, e) { |
| 307 | result << e |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | return result |
| 312 | } |
| 313 | |
| 314 | // fold sets `acc = init`, then successively calls `acc = fold_op(acc, elem)` for each element in `array`. |
| 315 | // returns `acc`. |
| 316 | // Example: |
| 317 | // ```v |
| 318 | // // Sum the length of each string in an array |
| 319 | // a := ['Hi', 'all'] |
| 320 | // r := arrays.fold[string, int](a, 0, |
| 321 | // fn (r int, t string) int { return r + t.len }) |
| 322 | // assert r == 5 |
| 323 | // ``` |
| 324 | pub fn fold[T, R](array []T, init R, fold_op fn (acc R, elem T) R) R { |
| 325 | $if R is $array { |
| 326 | mut value := init.clone() |
| 327 | for e in array { |
| 328 | value = fold_op(value, e) |
| 329 | } |
| 330 | return value |
| 331 | } $else { |
| 332 | mut value := init |
| 333 | for e in array { |
| 334 | value = fold_op(value, e) |
| 335 | } |
| 336 | return value |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // fold_indexed sets `acc = init`, then successively calls `acc = fold_op(idx, acc, elem)` for each element in `array`. |
| 341 | // returns `acc`. |
| 342 | pub fn fold_indexed[T, R](array []T, init R, fold_op fn (idx int, acc R, elem T) R) R { |
| 343 | mut value := init |
| 344 | |
| 345 | for i, e in array { |
| 346 | value = fold_op(i, value, e) |
| 347 | } |
| 348 | |
| 349 | return value |
| 350 | } |
| 351 | |
| 352 | // flatten flattens n + 1 dimensional array into n dimensional array. |
| 353 | // Example: arrays.flatten[int]([[1, 2, 3], [4, 5]]) // => [1, 2, 3, 4, 5] |
| 354 | pub fn flatten[T](array [][]T) []T { |
| 355 | // calculate required capacity |
| 356 | mut required_size := 0 |
| 357 | |
| 358 | for e1 in array { |
| 359 | for _ in e1 { |
| 360 | required_size += 1 |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // allocate flattened array |
| 365 | mut result := []T{cap: required_size} |
| 366 | |
| 367 | for e1 in array { |
| 368 | for e2 in e1 { |
| 369 | result << e2 |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | return result |
| 374 | } |
| 375 | |
| 376 | // flat_map creates a new array populated with the flattened result of calling transform function being invoked on each element of `list`. |
| 377 | pub fn flat_map[T, R](array []T, transform fn (elem T) []R) []R { |
| 378 | mut result := [][]R{cap: array.len} |
| 379 | |
| 380 | for v in array { |
| 381 | result << transform(v) |
| 382 | } |
| 383 | |
| 384 | return flatten(result) |
| 385 | } |
| 386 | |
| 387 | // flat_map_indexed creates a new array with the flattened result of calling the `transform` fn, invoked on each idx,elem pair from the original. |
| 388 | pub fn flat_map_indexed[T, R](array []T, transform fn (idx int, elem T) []R) []R { |
| 389 | mut result := [][]R{cap: array.len} |
| 390 | |
| 391 | for i, v in array { |
| 392 | result << transform(i, v) |
| 393 | } |
| 394 | |
| 395 | return flatten(result) |
| 396 | } |
| 397 | |
| 398 | // map_indexed creates a new array with the result of calling the `transform` fn, invoked on each idx,elem pair from the original. |
| 399 | pub fn map_indexed[T, R](array []T, transform fn (idx int, elem T) R) []R { |
| 400 | mut result := []R{cap: array.len} |
| 401 | |
| 402 | for i, v in array { |
| 403 | result << transform(i, v) |
| 404 | } |
| 405 | |
| 406 | return result |
| 407 | } |
| 408 | |
| 409 | // group_by groups together elements, for which the `grouping_op` callback produced the same result. |
| 410 | // Example: arrays.group_by[int, string](['H', 'el', 'lo'], fn (v string) int { return v.len }) // => {1: ['H'], 2: ['el', 'lo']} |
| 411 | pub fn group_by[K, V](array []V, grouping_op fn (val V) K) map[K][]V { |
| 412 | mut result := map[K][]V{} |
| 413 | |
| 414 | for v in array { |
| 415 | key := grouping_op(v) |
| 416 | |
| 417 | // check if key exists, if not, then create a new array with matched value, otherwise append. |
| 418 | if key in result { |
| 419 | result[key] << v |
| 420 | } else { |
| 421 | result[key] = [v] |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | return result |
| 426 | } |
| 427 | |
| 428 | // concatenate an array with an arbitrary number of additional values. |
| 429 | // NOTE: if you have two arrays, you should simply use the `<<` operator directly. |
| 430 | // Example: assert arrays.concat([1, 2, 3], 4, 5, 6) == [1, 2, 3, 4, 5, 6] |
| 431 | // Example: assert arrays.concat([1, 2, 3], ...[4, 5, 6]) == [1, 2, 3, 4, 5, 6] |
| 432 | // Example: mut arr := arrays.concat([1, 2, 3], 4); arr << [10,20]; assert arr == [1,2,3,4,10,20] // note: arr is mutable |
| 433 | pub fn concat[T](a []T, b ...T) []T { |
| 434 | mut m := []T{cap: a.len + b.len} |
| 435 | |
| 436 | m << a |
| 437 | m << b |
| 438 | |
| 439 | return m |
| 440 | } |
| 441 | |
| 442 | // returns the smallest element >= val, requires `array` to be sorted. |
| 443 | // Example: arrays.lower_bound([2, 4, 6, 8], 3)! // => 4 |
| 444 | pub fn lower_bound[T](array []T, val T) !T { |
| 445 | if array.len == 0 { |
| 446 | return error('.lower_bound called on an empty array') |
| 447 | } |
| 448 | mut left, mut right := 0, array.len - 1 |
| 449 | for ; left <= right; { |
| 450 | idx := (left + right) / 2 |
| 451 | elem := array[idx] |
| 452 | if elem < val { |
| 453 | left = idx + 1 |
| 454 | } else { |
| 455 | right = idx - 1 |
| 456 | } |
| 457 | } |
| 458 | if left >= array.len { |
| 459 | return error('') |
| 460 | } else { |
| 461 | return array[left] |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | // returns the largest element <= val, requires `array` to be sorted. |
| 466 | // Example: arrays.upper_bound([2, 4, 6, 8], 3)! // => 2 |
| 467 | pub fn upper_bound[T](array []T, val T) !T { |
| 468 | if array.len == 0 { |
| 469 | return error('.upper_bound called on an empty array') |
| 470 | } |
| 471 | mut left, mut right := 0, array.len - 1 |
| 472 | for ; left <= right; { |
| 473 | idx := (left + right) / 2 |
| 474 | elem := array[idx] |
| 475 | if elem > val { |
| 476 | right = idx - 1 |
| 477 | } else { |
| 478 | left = idx + 1 |
| 479 | } |
| 480 | } |
| 481 | if right < 0 { |
| 482 | return error('') |
| 483 | } else { |
| 484 | return array[right] |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // binary_search, requires `array` to be sorted, returns index of found item or error. |
| 489 | // Binary searches on sorted lists can be faster than other array searches because at maximum |
| 490 | // the algorithm only has to traverse log N elements |
| 491 | // Example: arrays.binary_search([1, 2, 3, 4], 4)! // => 3 |
| 492 | pub fn binary_search[T](array []T, target T) !int { |
| 493 | mut left := 0 |
| 494 | mut right := array.len - 1 |
| 495 | for ; left <= right; { |
| 496 | idx := (left + right) / 2 |
| 497 | elem := array[idx] |
| 498 | if elem == target { |
| 499 | return idx |
| 500 | } |
| 501 | if elem < target { |
| 502 | left = idx + 1 |
| 503 | } else { |
| 504 | right = idx - 1 |
| 505 | } |
| 506 | } |
| 507 | return error('') |
| 508 | } |
| 509 | |
| 510 | // rotate_left rotates the array in-place. |
| 511 | // It does it in such a way, that the first `mid` elements of the array, move to the end, |
| 512 | // while the last `array.len - mid` elements move to the front. |
| 513 | // After calling `rotate_left`, the element previously at index `mid` will become the first element in the array. |
| 514 | // Example: |
| 515 | // ```v |
| 516 | // mut x := [1,2,3,4,5,6] |
| 517 | // arrays.rotate_left(mut x, 2) |
| 518 | // println(x) // [3, 4, 5, 6, 1, 2] |
| 519 | // ``` |
| 520 | pub fn rotate_left[T](mut array []T, mid int) { |
| 521 | assert mid <= array.len && mid >= 0 |
| 522 | k := array.len - mid |
| 523 | p := unsafe { &T(array.data) } |
| 524 | unsafe { |
| 525 | ptr_rotate[T](mid, &T(usize(voidptr(p)) + usize(sizeof(T)) * usize(mid)), k) |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | // rotate_right rotates the array in-place. |
| 530 | // It does it in such a way, that the first `array.len - k` elements of the array, move to the end, |
| 531 | // while the last `k` elements move to the front. |
| 532 | // After calling `rotate_right`, the element previously at index `array.len - k` will become the first element in the array. |
| 533 | // Example: |
| 534 | // ```v |
| 535 | // mut x := [1,2,3,4,5,6] |
| 536 | // arrays.rotate_right(mut x, 2) |
| 537 | // println(x) // [5, 6, 1, 2, 3, 4] |
| 538 | // ``` |
| 539 | pub fn rotate_right[T](mut array []T, k int) { |
| 540 | assert k <= array.len && k >= 0 |
| 541 | mid := array.len - k |
| 542 | p := unsafe { &T(array.data) } |
| 543 | unsafe { |
| 544 | ptr_rotate[T](mid, &T(usize(voidptr(p)) + usize(sizeof(T)) * usize(mid)), k) |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | @[unsafe] |
| 549 | fn ptr_rotate[T](left_ int, mid &T, right_ int) { |
| 550 | sz := usize(sizeof(T)) |
| 551 | mut left := usize(left_) |
| 552 | mut right := usize(right_) |
| 553 | limit := raw_array_cap[T]() |
| 554 | for { |
| 555 | delta := if left < right { left } else { right } |
| 556 | if delta <= usize(limit) { |
| 557 | break |
| 558 | } |
| 559 | unsafe { |
| 560 | swap_nonoverlapping[T](&T(usize(voidptr(mid)) - left * sz), &T(usize(voidptr(mid)) + |
| 561 | usize(right - delta) * sz), int(delta)) |
| 562 | } |
| 563 | if left <= right { |
| 564 | right -= delta |
| 565 | } else { |
| 566 | left -= delta |
| 567 | } |
| 568 | } |
| 569 | unsafe { |
| 570 | rawarray := malloc(raw_array_malloc_size[T]()) |
| 571 | defer { |
| 572 | free(rawarray) |
| 573 | } |
| 574 | dim := &T(usize(voidptr(mid)) - left * sz + right * sz) |
| 575 | if left <= right { |
| 576 | vmemcpy(rawarray, voidptr(usize(voidptr(mid)) - left * sz), isize(left * sz)) |
| 577 | vmemmove(voidptr(usize(voidptr(mid)) - left * sz), voidptr(mid), isize(right * sz)) |
| 578 | vmemcpy(voidptr(dim), rawarray, isize(left * sz)) |
| 579 | } else { |
| 580 | vmemcpy(rawarray, voidptr(mid), isize(right * sz)) |
| 581 | vmemmove(voidptr(dim), voidptr(usize(voidptr(mid)) - left * sz), isize(left * sz)) |
| 582 | vmemcpy(voidptr(usize(voidptr(mid)) - left * sz), rawarray, isize(right * sz)) |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | struct Block { |
| 588 | mut: |
| 589 | x u64 |
| 590 | y u64 |
| 591 | z u64 |
| 592 | w u64 |
| 593 | } |
| 594 | |
| 595 | struct UnalignedBlock { |
| 596 | mut: |
| 597 | x u64 |
| 598 | y u64 |
| 599 | z u64 |
| 600 | w u64 |
| 601 | } |
| 602 | |
| 603 | const extra_size = 32 * isize(sizeof(usize)) |
| 604 | |
| 605 | fn raw_array_cap[T]() isize { |
| 606 | size := isize(sizeof(T)) |
| 607 | if size > extra_size { |
| 608 | return 1 |
| 609 | } else { |
| 610 | return extra_size / size |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | fn raw_array_malloc_size[T]() isize { |
| 615 | size := isize(sizeof(T)) |
| 616 | if size > extra_size { |
| 617 | return size * 2 |
| 618 | } else { |
| 619 | return extra_size |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | @[unsafe] |
| 624 | fn memswap(x voidptr, y voidptr, len usize) { |
| 625 | block_size := isize(sizeof(Block)) |
| 626 | |
| 627 | mut i := usize(0) |
| 628 | for i + usize(block_size) <= len { |
| 629 | mut t_ := Block{} |
| 630 | t := voidptr(&t_) |
| 631 | |
| 632 | xi := usize(x) + i |
| 633 | yi := usize(y) + i |
| 634 | unsafe { |
| 635 | vmemcpy(t, voidptr(xi), block_size) |
| 636 | vmemcpy(voidptr(xi), voidptr(yi), block_size) |
| 637 | vmemcpy(voidptr(yi), t, block_size) |
| 638 | } |
| 639 | i += usize(block_size) |
| 640 | } |
| 641 | if i < len { |
| 642 | mut t_ := UnalignedBlock{} |
| 643 | t := voidptr(&t_) |
| 644 | rem := isize(len - i) |
| 645 | xi := usize(x) + i |
| 646 | yi := usize(y) + i |
| 647 | unsafe { |
| 648 | vmemcpy(t, voidptr(xi), rem) |
| 649 | vmemcpy(voidptr(xi), voidptr(yi), rem) |
| 650 | vmemcpy(voidptr(yi), t, rem) |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | @[unsafe] |
| 656 | fn swap_nonoverlapping[T](x_ &T, y_ &T, count int) { |
| 657 | x := voidptr(x_) |
| 658 | y := voidptr(y_) |
| 659 | |
| 660 | len := usize(sizeof(T)) * usize(count) |
| 661 | unsafe { |
| 662 | memswap(x, y, len) |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | // copy copies the `src` array elements to the `dst` array. |
| 667 | // The number of the elements copied is the minimum of the length of both arrays. |
| 668 | // Returns the number of elements copied. |
| 669 | pub fn copy[T](mut dst []T, src []T) int { |
| 670 | min := if dst.len < src.len { dst.len } else { src.len } |
| 671 | if min <= 0 { |
| 672 | return 0 |
| 673 | } |
| 674 | if can_copy_bits[T]() { |
| 675 | blen := min * isize(sizeof(T)) |
| 676 | unsafe { vmemmove(&T(dst.data), src.data, blen) } |
| 677 | } else { |
| 678 | for i in 0 .. min { |
| 679 | dst[i] = src[i] |
| 680 | } |
| 681 | } |
| 682 | return min |
| 683 | } |
| 684 | |
| 685 | // can_copy_bits determines if T can be copied using `memcpy`. |
| 686 | // false if autofree needs to intervene |
| 687 | // false if type is not copyable e.g. map |
| 688 | fn can_copy_bits[T]() bool { |
| 689 | // references, C pointers, integers, floats, runes |
| 690 | if T.name[0] in [`&`, `b`, `c`, `f`, `i`, `r`, `u`, `v`] { |
| 691 | return true |
| 692 | } |
| 693 | return false |
| 694 | } |
| 695 | |
| 696 | // carray_to_varray copies a C byte array into a V array of type `T`. |
| 697 | // See also: `cstring_to_vstring` |
| 698 | @[unsafe] |
| 699 | pub fn carray_to_varray[T](c_array_data voidptr, items int) []T { |
| 700 | mut v_array := []T{len: items} |
| 701 | total_size := items * isize(sizeof(T)) |
| 702 | unsafe { vmemcpy(v_array.data, c_array_data, total_size) } |
| 703 | return v_array |
| 704 | } |
| 705 | |
| 706 | // find_first returns the first element that matches the given predicate. |
| 707 | // Returns `none` if no match is found. |
| 708 | // Example: arrays.find_first([1, 2, 3, 4, 5], fn (i int) bool { return i == 3 })? // => 3 |
| 709 | pub fn find_first[T](array []T, predicate fn (elem T) bool) ?T { |
| 710 | if array.len == 0 { |
| 711 | return none |
| 712 | } |
| 713 | for item in array { |
| 714 | if predicate(item) { |
| 715 | return item |
| 716 | } |
| 717 | } |
| 718 | return none |
| 719 | } |
| 720 | |
| 721 | // find_last returns the last element that matches the given predicate. |
| 722 | // Returns `none` if no match is found. |
| 723 | // Example: arrays.find_last([1, 2, 3, 4, 5], fn (i int) bool { return i == 3})? // => 3 |
| 724 | pub fn find_last[T](array []T, predicate fn (elem T) bool) ?T { |
| 725 | if array.len == 0 { |
| 726 | return none |
| 727 | } |
| 728 | for idx := array.len - 1; idx >= 0; idx-- { |
| 729 | item := array[idx] |
| 730 | if predicate(item) { |
| 731 | return item |
| 732 | } |
| 733 | } |
| 734 | return none |
| 735 | } |
| 736 | |
| 737 | // join_to_string takes in a custom transform function and joins all elements into a string with |
| 738 | // the specified separator |
| 739 | @[manualfree] |
| 740 | pub fn join_to_string[T](array []T, separator string, transform fn (elem T) string) string { |
| 741 | mut sb := strings.new_builder(array.len * 2) |
| 742 | defer { |
| 743 | unsafe { sb.free() } |
| 744 | } |
| 745 | for i, item in array { |
| 746 | x := transform(item) |
| 747 | sb.write_string(x) |
| 748 | unsafe { x.free() } |
| 749 | if i < array.len - 1 { |
| 750 | sb.write_string(separator) |
| 751 | } |
| 752 | } |
| 753 | return sb.str() |
| 754 | } |
| 755 | |
| 756 | // partition splits the original array into pair of lists. |
| 757 | // The first list contains elements for which the predicate fn returned true, |
| 758 | // while the second list contains elements for which the predicate fn returned false. |
| 759 | pub fn partition[T](array []T, predicate fn (elem T) bool) ([]T, []T) { |
| 760 | mut matching, mut non_matching := []T{}, []T{} |
| 761 | for item in array { |
| 762 | if predicate(item) { |
| 763 | matching << item |
| 764 | } else { |
| 765 | non_matching << item |
| 766 | } |
| 767 | } |
| 768 | return matching, non_matching |
| 769 | } |
| 770 | |
| 771 | // each calls the callback fn `cb`, for each element of the given array `a`. |
| 772 | pub fn each[T](a []T, cb fn (elem T)) { |
| 773 | for item in a { |
| 774 | cb(item) |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | // each_indexed calls the callback fn `cb`, for each element of the given array `a`. |
| 779 | // It passes the callback both the index of the current element, and the element itself. |
| 780 | pub fn each_indexed[T](a []T, cb fn (i int, e T)) { |
| 781 | for idx, item in a { |
| 782 | cb(idx, item) |
| 783 | } |
| 784 | } |
| 785 | |