2020-09-07 14:50:53 +03:00
|
|
|
module arrays
|
|
|
|
|
2023-07-07 06:52:08 +03:00
|
|
|
import strings
|
|
|
|
|
2020-09-07 14:50:53 +03:00
|
|
|
// Common arrays functions:
|
|
|
|
// - min / max - return the value of the minumum / maximum
|
|
|
|
// - idx_min / idx_max - return the index of the first minumum / maximum
|
|
|
|
// - merge - combine two sorted arrays and maintain sorted order
|
2021-09-13 16:13:32 +03:00
|
|
|
// - chunk - chunk array to arrays with n elements
|
|
|
|
// - window - get snapshots of the window of the given size sliding along array with the given step, where each snapshot is an array
|
2022-01-09 17:12:33 +03:00
|
|
|
// - group - merge two arrays by interleaving e.g. arrays.group([1,3,5], [2,4,6]) => [[1,2],[3,4],[5,6]]
|
|
|
|
// - flatten - reduce dimensionality of array by one. e.g. arrays.flatten([[1,2],[3,4],[5,6]]) => [1,2,3,4,5,6]
|
2020-09-07 14:50:53 +03:00
|
|
|
|
2021-09-15 15:17:55 +03:00
|
|
|
// min returns the minimum value in the array
|
2023-06-11 00:52:23 +03:00
|
|
|
// Example: arrays.min([1, 2, 3, 0, 9])! // => 0
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn min[T](array []T) !T {
|
2022-10-04 10:07:36 +03:00
|
|
|
if array.len == 0 {
|
2021-09-15 15:17:55 +03:00
|
|
|
return error('.min called on an empty array')
|
2020-10-15 00:39:09 +03:00
|
|
|
}
|
2022-10-04 10:07:36 +03:00
|
|
|
mut val := array[0]
|
|
|
|
for e in array {
|
2021-02-20 16:27:36 +03:00
|
|
|
if e < val {
|
|
|
|
val = e
|
2020-09-07 14:50:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2022-04-28 13:17:53 +03:00
|
|
|
// max returns the maximum value in the array
|
2023-06-11 00:52:23 +03:00
|
|
|
// Example: arrays.max([1, 2, 3, 0, 9])! // => 9
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn max[T](array []T) !T {
|
2022-10-04 10:07:36 +03:00
|
|
|
if array.len == 0 {
|
2021-09-15 15:17:55 +03:00
|
|
|
return error('.max called on an empty array')
|
2020-10-15 00:39:09 +03:00
|
|
|
}
|
2022-10-04 10:07:36 +03:00
|
|
|
mut val := array[0]
|
|
|
|
for e in array {
|
2021-02-20 16:27:36 +03:00
|
|
|
if e > val {
|
|
|
|
val = e
|
2020-09-07 14:50:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2021-09-15 15:17:55 +03:00
|
|
|
// idx_min returns the index of the minimum value in the array
|
2023-06-11 00:52:23 +03:00
|
|
|
// Example: arrays.idx_min([1, 2, 3, 0, 9])! // => 3
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn idx_min[T](array []T) !int {
|
2022-10-04 10:07:36 +03:00
|
|
|
if array.len == 0 {
|
2021-09-15 15:17:55 +03:00
|
|
|
return error('.idx_min called on an empty array')
|
2020-10-15 00:39:09 +03:00
|
|
|
}
|
2020-09-07 14:50:53 +03:00
|
|
|
mut idx := 0
|
2022-10-04 10:07:36 +03:00
|
|
|
mut val := array[0]
|
|
|
|
for i, e in array {
|
2021-02-20 16:27:36 +03:00
|
|
|
if e < val {
|
|
|
|
val = e
|
2020-09-07 14:50:53 +03:00
|
|
|
idx = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
|
2021-09-15 15:17:55 +03:00
|
|
|
// idx_max returns the index of the maximum value in the array
|
2023-06-11 00:52:23 +03:00
|
|
|
// Example: arrays.idx_max([1, 2, 3, 0, 9])! // => 4
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn idx_max[T](array []T) !int {
|
2022-10-04 10:07:36 +03:00
|
|
|
if array.len == 0 {
|
2021-09-15 15:17:55 +03:00
|
|
|
return error('.idx_max called on an empty array')
|
2020-10-15 00:39:09 +03:00
|
|
|
}
|
2020-09-07 14:50:53 +03:00
|
|
|
mut idx := 0
|
2022-10-04 10:07:36 +03:00
|
|
|
mut val := array[0]
|
|
|
|
for i, e in array {
|
2021-02-20 16:27:36 +03:00
|
|
|
if e > val {
|
|
|
|
val = e
|
2020-09-07 14:50:53 +03:00
|
|
|
idx = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
|
|
|
|
// merge two sorted arrays (ascending) and maintain sorted order
|
2023-06-11 00:52:23 +03:00
|
|
|
// Example: arrays.merge([1, 3, 5, 7], [2, 4, 6, 8]) // => [1, 2, 3, 4, 5, 6, 7, 8]
|
2020-09-07 14:50:53 +03:00
|
|
|
[direct_array_access]
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn merge[T](a []T, b []T) []T {
|
2020-10-15 00:39:09 +03:00
|
|
|
mut m := []T{len: a.len + b.len}
|
2020-09-07 14:50:53 +03:00
|
|
|
mut ia := 0
|
|
|
|
mut ib := 0
|
|
|
|
mut j := 0
|
|
|
|
// TODO efficient approach to merge_desc where: a[ia] >= b[ib]
|
2020-10-15 00:39:09 +03:00
|
|
|
for ia < a.len && ib < b.len {
|
2020-09-07 14:50:53 +03:00
|
|
|
if a[ia] <= b[ib] {
|
|
|
|
m[j] = a[ia]
|
|
|
|
ia++
|
|
|
|
} else {
|
|
|
|
m[j] = b[ib]
|
|
|
|
ib++
|
|
|
|
}
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
// a leftovers
|
|
|
|
for ia < a.len {
|
|
|
|
m[j] = a[ia]
|
|
|
|
ia++
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
// b leftovers
|
|
|
|
for ib < b.len {
|
|
|
|
m[j] = b[ib]
|
|
|
|
ib++
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
2021-02-05 21:24:38 +03:00
|
|
|
|
2021-03-07 12:58:13 +03:00
|
|
|
// group n arrays into a single array of arrays with n elements
|
2022-01-09 17:12:33 +03:00
|
|
|
//
|
|
|
|
// This function is analogous to the "zip" function of other languages.
|
|
|
|
// To fully interleave two arrays, follow this function with a call to `flatten`.
|
|
|
|
//
|
|
|
|
// NOTE: An error will be generated if the type annotation is omitted.
|
2023-06-11 00:52:23 +03:00
|
|
|
// Example: arrays.group[int]([1, 2, 3], [4, 5, 6]) // => [[1, 4], [2, 5], [3, 6]]
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn group[T](arrays ...[]T) [][]T {
|
2022-10-04 10:07:36 +03:00
|
|
|
mut length := if arrays.len > 0 { arrays[0].len } else { 0 }
|
2021-03-07 12:58:13 +03:00
|
|
|
// calculate length of output by finding shortest input array
|
2022-10-04 10:07:36 +03:00
|
|
|
for ndx in 1 .. arrays.len {
|
|
|
|
if arrays[ndx].len < length {
|
|
|
|
length = arrays[ndx].len
|
2021-03-07 12:58:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if length > 0 {
|
|
|
|
mut arr := [][]T{cap: length}
|
|
|
|
// append all combined arrays into the resultant array
|
|
|
|
for ndx in 0 .. length {
|
2022-10-04 10:07:36 +03:00
|
|
|
mut grouped := []T{cap: arrays.len}
|
2021-03-07 12:58:13 +03:00
|
|
|
// combine each list item for the ndx position into one array
|
2022-10-04 10:07:36 +03:00
|
|
|
for arr_ndx in 0 .. arrays.len {
|
|
|
|
grouped << arrays[arr_ndx][ndx]
|
2021-03-07 12:58:13 +03:00
|
|
|
}
|
2022-01-09 17:12:33 +03:00
|
|
|
arr << grouped
|
2021-03-07 12:58:13 +03:00
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
|
|
|
return [][]T{}
|
|
|
|
}
|
2021-09-13 16:13:32 +03:00
|
|
|
|
2022-01-07 14:28:50 +03:00
|
|
|
// chunk array into a single array of arrays where each element is the next `size` elements of the original
|
|
|
|
// Example: arrays.chunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 2)) // => [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn chunk[T](array []T, size int) [][]T {
|
2021-09-13 16:13:32 +03:00
|
|
|
// allocate chunk array
|
2022-10-04 10:07:36 +03:00
|
|
|
mut chunks := [][]T{cap: array.len / size + if array.len % size == 0 { 0 } else { 1 }}
|
2021-09-13 16:13:32 +03:00
|
|
|
|
|
|
|
for i := 0; true; {
|
|
|
|
// check chunk size is greater than remaining element size
|
2022-10-04 10:07:36 +03:00
|
|
|
if array.len < i + size {
|
2021-09-13 16:13:32 +03:00
|
|
|
// check if there's no more element to chunk
|
2022-10-04 10:07:36 +03:00
|
|
|
if array.len <= i {
|
2021-09-13 16:13:32 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
chunks << array[i..]
|
2021-09-13 16:13:32 +03:00
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
chunks << array[i..i + size]
|
2021-09-13 16:13:32 +03:00
|
|
|
i += size
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunks
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct WindowAttribute {
|
|
|
|
size int
|
|
|
|
step int = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// get snapshots of the window of the given size sliding along array with the given step, where each snapshot is an array.
|
|
|
|
// - `size` - snapshot size
|
|
|
|
// - `step` - gap size between each snapshot, default is 1.
|
2021-09-14 16:49:23 +03:00
|
|
|
//
|
2022-04-02 18:29:12 +03:00
|
|
|
// Example: arrays.window([1, 2, 3, 4], size: 2) // => [[1, 2], [2, 3], [3, 4]]
|
2022-01-07 14:28:50 +03:00
|
|
|
// 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]]
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn window[T](array []T, attr WindowAttribute) [][]T {
|
2021-09-13 16:13:32 +03:00
|
|
|
// allocate snapshot array
|
2022-10-04 10:07:36 +03:00
|
|
|
mut windows := [][]T{cap: array.len - attr.size + 1}
|
2021-09-13 16:13:32 +03:00
|
|
|
|
|
|
|
for i := 0; true; {
|
|
|
|
// check remaining elements size is less than snapshot size
|
2022-10-04 10:07:36 +03:00
|
|
|
if array.len < i + attr.size {
|
2021-09-13 16:13:32 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
windows << array[i..i + attr.size]
|
2021-09-13 16:13:32 +03:00
|
|
|
i += attr.step
|
|
|
|
}
|
|
|
|
|
|
|
|
return windows
|
|
|
|
}
|
2021-09-14 16:49:23 +03:00
|
|
|
|
|
|
|
// sum up array, return nothing when array has no elements
|
2022-01-07 14:28:50 +03:00
|
|
|
//
|
|
|
|
// NOTICE: currently V has bug that cannot make sum function takes custom struct with + operator overloaded
|
2021-09-14 16:49:23 +03:00
|
|
|
// which means you can only pass array of numbers for now.
|
2022-01-07 14:28:50 +03:00
|
|
|
// TODO: Fix generic operator overloading detection issue.
|
2023-06-11 00:52:23 +03:00
|
|
|
// Example: arrays.sum[int]([1, 2, 3, 4, 5])! // => 15
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn sum[T](array []T) !T {
|
2022-10-04 10:07:36 +03:00
|
|
|
if array.len == 0 {
|
2021-09-14 16:49:23 +03:00
|
|
|
return error('Cannot sum up array of nothing.')
|
|
|
|
} else {
|
2022-10-04 10:07:36 +03:00
|
|
|
mut head := array[0]
|
2021-09-14 16:49:23 +03:00
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
for i, e in array {
|
2021-09-14 16:49:23 +03:00
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
head += e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return head
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
// reduce sets `acc = array[0]`, then successively calls `acc = reduce_op(acc, elem)` for each remaining element in `array`.
|
2022-03-09 21:04:49 +03:00
|
|
|
// returns the accumulated value in `acc`.
|
|
|
|
// returns an error if the array is empty.
|
|
|
|
// See also: [fold](#fold).
|
2022-10-20 22:14:33 +03:00
|
|
|
// Example: arrays.reduce([1, 2, 3, 4, 5], fn (t1 int, t2 int) int { return t1 * t2 })! // => 120
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn reduce[T](array []T, reduce_op fn (acc T, elem T) T) !T {
|
2022-10-04 10:07:36 +03:00
|
|
|
if array.len == 0 {
|
2021-09-14 16:49:23 +03:00
|
|
|
return error('Cannot reduce array of nothing.')
|
|
|
|
} else {
|
2022-10-04 10:07:36 +03:00
|
|
|
mut value := array[0]
|
2021-09-14 16:49:23 +03:00
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
for i, e in array {
|
2021-09-14 16:49:23 +03:00
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
value = reduce_op(value, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
// reduce_indexed sets `acc = array[0]`, then successively calls `acc = reduce_op(idx, acc, elem)` for each remaining element in `array`.
|
2022-10-03 10:42:36 +03:00
|
|
|
// returns the accumulated value in `acc`.
|
|
|
|
// returns an error if the array is empty.
|
|
|
|
// See also: [fold_indexed](#fold_indexed).
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn reduce_indexed[T](array []T, reduce_op fn (idx int, acc T, elem T) T) !T {
|
2022-10-04 10:07:36 +03:00
|
|
|
if array.len == 0 {
|
2022-10-03 10:42:36 +03:00
|
|
|
return error('Cannot reduce array of nothing.')
|
|
|
|
} else {
|
2022-10-04 10:07:36 +03:00
|
|
|
mut value := array[0]
|
2022-10-03 10:42:36 +03:00
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
for i, e in array {
|
2022-10-03 10:42:36 +03:00
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
value = reduce_op(i, value, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
// filter_indexed filters elements based on `predicate` function
|
2022-10-03 10:42:36 +03:00
|
|
|
// being invoked on each element with its index in the original array.
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn filter_indexed[T](array []T, predicate fn (idx int, elem T) bool) []T {
|
2022-10-04 10:07:36 +03:00
|
|
|
mut result := []T{cap: array.len}
|
2022-10-03 10:42:36 +03:00
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
for i, e in array {
|
2022-10-03 10:42:36 +03:00
|
|
|
if predicate(i, e) {
|
|
|
|
result << e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
// fold sets `acc = init`, then successively calls `acc = fold_op(acc, elem)` for each element in `array`.
|
2022-03-09 21:04:49 +03:00
|
|
|
// returns `acc`.
|
|
|
|
// Example:
|
|
|
|
// ```v
|
|
|
|
// // Sum the length of each string in an array
|
|
|
|
// a := ['Hi', 'all']
|
2023-06-11 00:52:23 +03:00
|
|
|
// r := arrays.fold[string, int](a, 0,
|
2022-03-09 21:04:49 +03:00
|
|
|
// fn (r int, t string) int { return r + t.len })
|
|
|
|
// assert r == 5
|
|
|
|
// ```
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn fold[T, R](array []T, init R, fold_op fn (acc R, elem T) R) R {
|
2021-09-14 16:49:23 +03:00
|
|
|
mut value := init
|
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
for e in array {
|
2021-09-14 16:49:23 +03:00
|
|
|
value = fold_op(value, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
// fold_indexed sets `acc = init`, then successively calls `acc = fold_op(idx, acc, elem)` for each element in `array`.
|
2022-10-03 10:42:36 +03:00
|
|
|
// returns `acc`.
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn fold_indexed[T, R](array []T, init R, fold_op fn (idx int, acc R, elem T) R) R {
|
2022-10-03 10:42:36 +03:00
|
|
|
mut value := init
|
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
for i, e in array {
|
2022-10-03 10:42:36 +03:00
|
|
|
value = fold_op(i, value, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
// flatten flattens n + 1 dimensional array into n dimensional array
|
2022-01-07 14:28:50 +03:00
|
|
|
// Example: arrays.flatten<int>([[1, 2, 3], [4, 5]]) // => [1, 2, 3, 4, 5]
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn flatten[T](array [][]T) []T {
|
2021-09-14 16:49:23 +03:00
|
|
|
// calculate required capacity
|
|
|
|
mut required_size := 0
|
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
for e1 in array {
|
2021-09-14 16:49:23 +03:00
|
|
|
for _ in e1 {
|
|
|
|
required_size += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate flattened array
|
|
|
|
mut result := []T{cap: required_size}
|
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
for e1 in array {
|
2021-09-14 16:49:23 +03:00
|
|
|
for e2 in e1 {
|
|
|
|
result << e2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-10-03 10:42:36 +03:00
|
|
|
// flat_map creates a new array populated with the flattened result of calling transform function
|
|
|
|
// being invoked on each element of `list`.
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn flat_map[T, R](array []T, transform fn (elem T) []R) []R {
|
2022-10-04 10:07:36 +03:00
|
|
|
mut result := [][]R{cap: array.len}
|
2022-10-03 10:42:36 +03:00
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
for v in array {
|
2022-10-03 10:42:36 +03:00
|
|
|
result << transform(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return flatten(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// flat_map_indexed creates a new array populated with the flattened result of calling the `transform` function
|
|
|
|
// being invoked on each element with its index in the original array.
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn flat_map_indexed[T, R](array []T, transform fn (idx int, elem T) []R) []R {
|
2022-10-04 10:07:36 +03:00
|
|
|
mut result := [][]R{cap: array.len}
|
2022-10-03 10:42:36 +03:00
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
for i, v in array {
|
2022-10-03 10:42:36 +03:00
|
|
|
result << transform(i, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return flatten(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// map_indexed creates a new array populated with the result of calling the `transform` function
|
|
|
|
// being invoked on each element with its index in the original array.
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn map_indexed[T, R](array []T, transform fn (idx int, elem T) R) []R {
|
2022-10-04 10:07:36 +03:00
|
|
|
mut result := []R{cap: array.len}
|
2022-10-03 10:42:36 +03:00
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
for i, v in array {
|
2022-10-03 10:42:36 +03:00
|
|
|
result << transform(i, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-03-09 21:04:49 +03:00
|
|
|
// group_by groups together elements, for which the `grouping_op` callback produced the same result.
|
2023-06-11 00:52:23 +03:00
|
|
|
// Example: arrays.group_by[int, string](['H', 'el', 'lo'], fn (v string) int { return v.len }) // => {1: ['H'], 2: ['el', 'lo']}
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn group_by[K, V](array []V, grouping_op fn (val V) K) map[K][]V {
|
2021-09-14 16:49:23 +03:00
|
|
|
mut result := map[K][]V{}
|
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
for v in array {
|
2021-09-14 16:49:23 +03:00
|
|
|
key := grouping_op(v)
|
|
|
|
|
|
|
|
// check if key exists, if not, then create a new array with matched value, otherwise append.
|
|
|
|
if key in result {
|
|
|
|
result[key] << v
|
|
|
|
} else {
|
|
|
|
result[key] = [v]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2021-09-26 19:41:50 +03:00
|
|
|
|
2022-01-07 14:28:50 +03:00
|
|
|
// concatenate an array with an arbitrary number of additional values
|
|
|
|
//
|
|
|
|
// NOTE: if you have two arrays, you should simply use the `<<` operator directly
|
|
|
|
// Example: arrays.concat([1, 2, 3], 4, 5, 6) == [1, 2, 3, 4, 5, 6] // => true
|
|
|
|
// Example: arrays.concat([1, 2, 3], ...[4, 5, 6]) == [1, 2, 3, 4, 5, 6] // => true
|
|
|
|
// Example: arr << [4, 5, 6] // does what you need if arr is mutable
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn concat[T](a []T, b ...T) []T {
|
2021-09-26 19:41:50 +03:00
|
|
|
mut m := []T{cap: a.len + b.len}
|
|
|
|
|
|
|
|
m << a
|
|
|
|
m << b
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
2021-10-03 08:14:39 +03:00
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
// returns the smallest element >= val, requires `array` to be sorted
|
2023-06-11 00:52:23 +03:00
|
|
|
// Example: arrays.lower_bound([2, 4, 6, 8], 3)! // => 4
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn lower_bound[T](array []T, val T) !T {
|
2022-10-04 10:07:36 +03:00
|
|
|
if array.len == 0 {
|
2021-10-03 08:14:39 +03:00
|
|
|
return error('.lower_bound called on an empty array')
|
|
|
|
}
|
2022-10-04 10:07:36 +03:00
|
|
|
mut left, mut right := 0, array.len - 1
|
2021-10-03 08:14:39 +03:00
|
|
|
for ; left <= right; {
|
|
|
|
idx := (left + right) / 2
|
2022-10-04 10:07:36 +03:00
|
|
|
elem := array[idx]
|
2021-10-03 08:14:39 +03:00
|
|
|
if elem < val {
|
|
|
|
left = idx + 1
|
|
|
|
} else {
|
|
|
|
right = idx - 1
|
|
|
|
}
|
|
|
|
}
|
2022-10-04 10:07:36 +03:00
|
|
|
if left >= array.len {
|
2021-10-03 08:14:39 +03:00
|
|
|
return error('')
|
|
|
|
} else {
|
2022-10-04 10:07:36 +03:00
|
|
|
return array[left]
|
2021-10-03 08:14:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
// returns the largest element <= val, requires `array` to be sorted
|
2022-10-20 22:14:33 +03:00
|
|
|
// Example: arrays.upper_bound([2, 4, 6, 8], 3)! // => 2
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn upper_bound[T](array []T, val T) !T {
|
2022-10-04 10:07:36 +03:00
|
|
|
if array.len == 0 {
|
2021-10-03 08:14:39 +03:00
|
|
|
return error('.upper_bound called on an empty array')
|
|
|
|
}
|
2022-10-04 10:07:36 +03:00
|
|
|
mut left, mut right := 0, array.len - 1
|
2021-10-03 08:14:39 +03:00
|
|
|
for ; left <= right; {
|
|
|
|
idx := (left + right) / 2
|
2022-10-04 10:07:36 +03:00
|
|
|
elem := array[idx]
|
2021-10-03 08:14:39 +03:00
|
|
|
if elem > val {
|
|
|
|
right = idx - 1
|
|
|
|
} else {
|
|
|
|
left = idx + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if right < 0 {
|
|
|
|
return error('')
|
|
|
|
} else {
|
2022-10-04 10:07:36 +03:00
|
|
|
return array[right]
|
2021-10-03 08:14:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
// binary search, requires `array` to be sorted, returns index of found item or error.
|
2022-01-07 14:28:50 +03:00
|
|
|
// Binary searches on sorted lists can be faster than other array searches because at maximum
|
|
|
|
// the algorithm only has to traverse log N elements
|
2023-06-11 00:52:23 +03:00
|
|
|
// Example: arrays.binary_search([1, 2, 3, 4], 4)! // => 3
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn binary_search[T](array []T, target T) !int {
|
2021-10-03 08:14:39 +03:00
|
|
|
mut left := 0
|
2022-10-04 10:07:36 +03:00
|
|
|
mut right := array.len - 1
|
2021-10-03 08:14:39 +03:00
|
|
|
for ; left <= right; {
|
|
|
|
idx := (left + right) / 2
|
2022-10-04 10:07:36 +03:00
|
|
|
elem := array[idx]
|
2021-10-03 08:14:39 +03:00
|
|
|
if elem == target {
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
if elem < target {
|
|
|
|
left = idx + 1
|
|
|
|
} else {
|
|
|
|
right = idx - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error('')
|
|
|
|
}
|
2022-02-07 14:20:45 +03:00
|
|
|
|
|
|
|
// rotate_left rotates the array in-place such that the first `mid` elements of the array move to the end
|
2022-10-04 10:07:36 +03:00
|
|
|
// while the last `array.len - mid` elements move to the front. After calling `rotate_left`, the element
|
2022-02-07 14:20:45 +03:00
|
|
|
// previously at index `mid` will become the first element in the array.
|
|
|
|
// Example:
|
|
|
|
// ```v
|
|
|
|
// mut x := [1,2,3,4,5,6]
|
2022-04-02 18:29:12 +03:00
|
|
|
// arrays.rotate_left(mut x, 2)
|
2022-02-07 14:20:45 +03:00
|
|
|
// println(x) // [3, 4, 5, 6, 1, 2]
|
|
|
|
// ```
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn rotate_left[T](mut array []T, mid int) {
|
2022-10-04 10:07:36 +03:00
|
|
|
assert mid <= array.len && mid >= 0
|
|
|
|
k := array.len - mid
|
|
|
|
p := &T(array.data)
|
2022-02-07 14:20:45 +03:00
|
|
|
unsafe {
|
2022-11-26 19:23:26 +03:00
|
|
|
ptr_rotate[T](mid, &T(usize(voidptr(p)) + usize(sizeof(T)) * usize(mid)), k)
|
2022-02-07 14:20:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:07:36 +03:00
|
|
|
// rotate_right rotates the array in-place such that the first `array.len - k` elements of the array move to the end
|
|
|
|
// while the last `k` elements move to the front. After calling `rotate_right`, the element previously at index `array.len - k`
|
2022-02-07 14:20:45 +03:00
|
|
|
// will become the first element in the array.
|
|
|
|
// Example:
|
|
|
|
// ```v
|
|
|
|
// mut x := [1,2,3,4,5,6]
|
|
|
|
// arrays.rotate_right(mut x, 2)
|
|
|
|
// println(x) // [5, 6, 1, 2, 3, 4]
|
|
|
|
// ```
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn rotate_right[T](mut array []T, k int) {
|
2022-10-04 10:07:36 +03:00
|
|
|
assert k <= array.len && k >= 0
|
|
|
|
mid := array.len - k
|
|
|
|
p := &T(array.data)
|
2022-02-07 14:20:45 +03:00
|
|
|
unsafe {
|
2022-11-26 19:23:26 +03:00
|
|
|
ptr_rotate[T](mid, &T(usize(voidptr(p)) + usize(sizeof(T)) * usize(mid)), k)
|
2022-02-07 14:20:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[unsafe]
|
2022-11-26 19:23:26 +03:00
|
|
|
fn ptr_rotate[T](left_ int, mid &T, right_ int) {
|
2023-02-19 20:01:18 +03:00
|
|
|
sz := usize(sizeof(T))
|
2022-02-07 14:20:45 +03:00
|
|
|
mut left := usize(left_)
|
|
|
|
mut right := usize(right_)
|
2023-02-19 20:01:18 +03:00
|
|
|
limit := raw_array_cap[T]()
|
2022-02-07 14:20:45 +03:00
|
|
|
for {
|
|
|
|
delta := if left < right { left } else { right }
|
2023-02-19 20:01:18 +03:00
|
|
|
if delta <= usize(limit) {
|
2022-02-07 14:20:45 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
unsafe {
|
2023-02-19 20:01:18 +03:00
|
|
|
swap_nonoverlapping[T](&T(usize(voidptr(mid)) - left * sz), &T(usize(voidptr(mid)) +
|
|
|
|
usize(right - delta) * sz), int(delta))
|
2022-02-07 14:20:45 +03:00
|
|
|
}
|
|
|
|
if left <= right {
|
|
|
|
right -= delta
|
|
|
|
} else {
|
|
|
|
left -= delta
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe {
|
2023-02-19 20:01:18 +03:00
|
|
|
rawarray := malloc(raw_array_malloc_size[T]())
|
|
|
|
defer {
|
|
|
|
free(rawarray)
|
|
|
|
}
|
2022-02-07 14:20:45 +03:00
|
|
|
dim := &T(usize(voidptr(mid)) - left * sz + right * sz)
|
|
|
|
if left <= right {
|
2023-02-19 20:01:18 +03:00
|
|
|
vmemcpy(rawarray, voidptr(usize(voidptr(mid)) - left * sz), isize(left * sz))
|
|
|
|
vmemmove(voidptr(usize(voidptr(mid)) - left * sz), voidptr(mid), isize(right * sz))
|
|
|
|
vmemcpy(voidptr(dim), rawarray, isize(left * sz))
|
2022-02-07 14:20:45 +03:00
|
|
|
} else {
|
2023-02-19 20:01:18 +03:00
|
|
|
vmemcpy(rawarray, voidptr(mid), isize(right * sz))
|
|
|
|
vmemmove(voidptr(dim), voidptr(usize(voidptr(mid)) - left * sz), isize(left * sz))
|
|
|
|
vmemcpy(voidptr(usize(voidptr(mid)) - left * sz), rawarray, isize(right * sz))
|
2022-02-07 14:20:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Block {
|
|
|
|
mut:
|
|
|
|
x u64
|
|
|
|
y u64
|
|
|
|
z u64
|
|
|
|
w u64
|
|
|
|
}
|
|
|
|
|
|
|
|
struct UnalignedBlock {
|
|
|
|
mut:
|
|
|
|
x u64
|
|
|
|
y u64
|
|
|
|
z u64
|
|
|
|
w u64
|
|
|
|
}
|
|
|
|
|
2023-02-19 20:01:18 +03:00
|
|
|
const extra_size = 32 * isize(sizeof(usize))
|
2022-02-07 14:20:45 +03:00
|
|
|
|
2023-02-19 20:01:18 +03:00
|
|
|
fn raw_array_cap[T]() isize {
|
|
|
|
size := isize(sizeof(T))
|
|
|
|
if size > arrays.extra_size {
|
2022-02-07 14:20:45 +03:00
|
|
|
return 1
|
|
|
|
} else {
|
2023-02-19 20:01:18 +03:00
|
|
|
return arrays.extra_size / size
|
2022-02-07 14:20:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-19 20:01:18 +03:00
|
|
|
fn raw_array_malloc_size[T]() isize {
|
|
|
|
size := isize(sizeof(T))
|
|
|
|
if size > arrays.extra_size {
|
|
|
|
return size * 2
|
2022-02-07 14:20:45 +03:00
|
|
|
} else {
|
2023-02-19 20:01:18 +03:00
|
|
|
return arrays.extra_size
|
2022-02-07 14:20:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[unsafe]
|
|
|
|
fn memswap(x voidptr, y voidptr, len usize) {
|
2023-02-19 20:01:18 +03:00
|
|
|
block_size := isize(sizeof(Block))
|
2022-02-07 14:20:45 +03:00
|
|
|
|
|
|
|
mut i := usize(0)
|
2023-02-19 20:01:18 +03:00
|
|
|
for i + usize(block_size) <= len {
|
2022-02-07 14:20:45 +03:00
|
|
|
mut t_ := Block{}
|
|
|
|
t := voidptr(&t_)
|
|
|
|
|
|
|
|
xi := usize(x) + i
|
|
|
|
yi := usize(y) + i
|
|
|
|
unsafe {
|
2023-02-19 20:01:18 +03:00
|
|
|
vmemcpy(t, voidptr(xi), block_size)
|
|
|
|
vmemcpy(voidptr(xi), voidptr(yi), block_size)
|
|
|
|
vmemcpy(t, voidptr(yi), block_size)
|
2022-02-07 14:20:45 +03:00
|
|
|
}
|
2023-02-19 20:01:18 +03:00
|
|
|
i += usize(block_size)
|
2022-02-07 14:20:45 +03:00
|
|
|
}
|
|
|
|
if i < len {
|
|
|
|
mut t_ := UnalignedBlock{}
|
|
|
|
t := voidptr(&t_)
|
2023-02-19 20:01:18 +03:00
|
|
|
rem := isize(len - i)
|
2022-02-07 14:20:45 +03:00
|
|
|
xi := usize(x) + i
|
|
|
|
yi := usize(y) + i
|
|
|
|
unsafe {
|
2023-02-19 20:01:18 +03:00
|
|
|
vmemcpy(t, voidptr(xi), rem)
|
|
|
|
vmemcpy(voidptr(xi), voidptr(yi), rem)
|
|
|
|
vmemcpy(voidptr(yi), t, rem)
|
2022-02-07 14:20:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[unsafe]
|
2022-11-26 19:23:26 +03:00
|
|
|
fn swap_nonoverlapping[T](x_ &T, y_ &T, count int) {
|
2022-02-07 14:20:45 +03:00
|
|
|
x := voidptr(x_)
|
|
|
|
y := voidptr(y_)
|
|
|
|
|
|
|
|
len := usize(sizeof(T)) * usize(count)
|
|
|
|
unsafe {
|
|
|
|
memswap(x, y, len)
|
|
|
|
}
|
|
|
|
}
|
2022-03-08 10:44:04 +03:00
|
|
|
|
|
|
|
// copy copies the `src` array elements to the `dst` array.
|
|
|
|
// The number of the elements copied is the minimum of the length of both arrays.
|
|
|
|
// Returns the number of elements copied.
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn copy[T](mut dst []T, src []T) int {
|
2022-03-08 10:44:04 +03:00
|
|
|
min := if dst.len < src.len { dst.len } else { src.len }
|
2022-03-10 01:30:51 +03:00
|
|
|
if min <= 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2022-11-26 19:23:26 +03:00
|
|
|
if can_copy_bits[T]() {
|
2023-02-19 20:01:18 +03:00
|
|
|
blen := min * isize(sizeof(T))
|
2022-03-08 10:44:04 +03:00
|
|
|
unsafe { vmemmove(&T(dst.data), src.data, blen) }
|
2022-03-10 01:30:51 +03:00
|
|
|
} else {
|
|
|
|
for i in 0 .. min {
|
|
|
|
dst[i] = src[i]
|
|
|
|
}
|
2022-03-08 10:44:04 +03:00
|
|
|
}
|
|
|
|
return min
|
|
|
|
}
|
2022-03-10 01:30:51 +03:00
|
|
|
|
|
|
|
// determines if T can be copied using `memcpy`
|
|
|
|
// false if autofree needs to intervene
|
|
|
|
// false if type is not copyable e.g. map
|
2022-11-26 19:23:26 +03:00
|
|
|
fn can_copy_bits[T]() bool {
|
2022-03-10 01:30:51 +03:00
|
|
|
// references, C pointers, integers, floats, runes
|
|
|
|
if T.name[0] in [`&`, `b`, `c`, `f`, `i`, `r`, `u`, `v`] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-08-23 11:12:50 +03:00
|
|
|
|
|
|
|
// carray_to_varray copies a C byte array into a V array of type `T`.
|
|
|
|
// See also: `cstring_to_vstring`
|
|
|
|
[unsafe]
|
2023-02-19 20:01:18 +03:00
|
|
|
pub fn carray_to_varray[T](c_array_data voidptr, items int) []T {
|
|
|
|
mut v_array := []T{len: items}
|
|
|
|
total_size := items * isize(sizeof(T))
|
|
|
|
unsafe { vmemcpy(v_array.data, c_array_data, total_size) }
|
2022-08-23 11:12:50 +03:00
|
|
|
return v_array
|
|
|
|
}
|
2023-07-07 06:52:08 +03:00
|
|
|
|
|
|
|
// find_first returns the first element that matches the given predicate
|
|
|
|
// returns `none`, if there is no match found
|
|
|
|
// Example: arrays.find_first([1, 2, 3, 4, 5], fn (arr int) bool { arr == 3}) // => 3
|
|
|
|
pub fn find_first[T](array []T, predicate fn (elem T) bool) ?T {
|
|
|
|
if array.len == 0 {
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
for item in array {
|
|
|
|
if predicate(item) {
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
// find_last returns the last element that matches the given predicate
|
|
|
|
// returns `none`, if there is no match found
|
|
|
|
// Example: arrays.find_last([1, 2, 3, 4, 5], fn (arr int) bool { arr == 3}) // => 3
|
|
|
|
pub fn find_last[T](array []T, predicate fn (elem T) bool) ?T {
|
|
|
|
if array.len == 0 {
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
for idx := array.len - 1; idx >= 0; idx-- {
|
|
|
|
item := array[idx]
|
|
|
|
if predicate(item) {
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
// join_to_string takes in a custom transform function and joins all elements into a string with
|
|
|
|
// the specified separator
|
|
|
|
[manualfree]
|
|
|
|
pub fn join_to_string[T](array []T, separator string, transform fn (elem T) string) string {
|
|
|
|
mut sb := strings.new_builder(array.len * 2)
|
|
|
|
defer {
|
|
|
|
unsafe { sb.free() }
|
|
|
|
}
|
|
|
|
for i, item in array {
|
|
|
|
x := transform(item)
|
|
|
|
sb.write_string(x)
|
|
|
|
unsafe { x.free() }
|
|
|
|
if i < array.len - 1 {
|
|
|
|
sb.write_string(separator)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sb.str()
|
|
|
|
}
|