2020-09-07 13:50:53 +02:00
|
|
|
module arrays
|
|
|
|
|
|
|
|
// 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 15:13:32 +02: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 15:12:33 +01: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 13:50:53 +02:00
|
|
|
|
2021-09-15 14:17:55 +02:00
|
|
|
// min returns the minimum value in the array
|
2022-01-07 12:28:50 +01:00
|
|
|
// Example: arrays.min([1,2,3,0,9]) // => 0
|
2021-09-15 14:17:55 +02:00
|
|
|
pub fn min<T>(a []T) ?T {
|
2020-10-14 23:39:09 +02:00
|
|
|
if a.len == 0 {
|
2021-09-15 14:17:55 +02:00
|
|
|
return error('.min called on an empty array')
|
2020-10-14 23:39:09 +02:00
|
|
|
}
|
2020-09-07 13:50:53 +02:00
|
|
|
mut val := a[0]
|
2021-02-20 14:27:36 +01:00
|
|
|
for e in a {
|
|
|
|
if e < val {
|
|
|
|
val = e
|
2020-09-07 13:50:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2021-09-15 14:17:55 +02:00
|
|
|
// max returns the maximum the maximum value in the array
|
2022-01-07 12:28:50 +01:00
|
|
|
// Example: arrays.max([1,2,3,0,9]) // => 9
|
2021-09-15 14:17:55 +02:00
|
|
|
pub fn max<T>(a []T) ?T {
|
2020-10-14 23:39:09 +02:00
|
|
|
if a.len == 0 {
|
2021-09-15 14:17:55 +02:00
|
|
|
return error('.max called on an empty array')
|
2020-10-14 23:39:09 +02:00
|
|
|
}
|
2020-09-07 13:50:53 +02:00
|
|
|
mut val := a[0]
|
2021-02-20 14:27:36 +01:00
|
|
|
for e in a {
|
|
|
|
if e > val {
|
|
|
|
val = e
|
2020-09-07 13:50:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2021-09-15 14:17:55 +02:00
|
|
|
// idx_min returns the index of the minimum value in the array
|
2022-01-07 12:28:50 +01:00
|
|
|
// Example: arrays.idx_min([1,2,3,0,9]) // => 3
|
2021-09-15 14:17:55 +02:00
|
|
|
pub fn idx_min<T>(a []T) ?int {
|
2020-10-14 23:39:09 +02:00
|
|
|
if a.len == 0 {
|
2021-09-15 14:17:55 +02:00
|
|
|
return error('.idx_min called on an empty array')
|
2020-10-14 23:39:09 +02:00
|
|
|
}
|
2020-09-07 13:50:53 +02:00
|
|
|
mut idx := 0
|
|
|
|
mut val := a[0]
|
2021-02-20 14:27:36 +01:00
|
|
|
for i, e in a {
|
|
|
|
if e < val {
|
|
|
|
val = e
|
2020-09-07 13:50:53 +02:00
|
|
|
idx = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
|
2021-09-15 14:17:55 +02:00
|
|
|
// idx_max returns the index of the maximum value in the array
|
2022-01-07 12:28:50 +01:00
|
|
|
// Example: arrays.idx_max([1,2,3,0,9]) // => 4
|
2021-09-15 14:17:55 +02:00
|
|
|
pub fn idx_max<T>(a []T) ?int {
|
2020-10-14 23:39:09 +02:00
|
|
|
if a.len == 0 {
|
2021-09-15 14:17:55 +02:00
|
|
|
return error('.idx_max called on an empty array')
|
2020-10-14 23:39:09 +02:00
|
|
|
}
|
2020-09-07 13:50:53 +02:00
|
|
|
mut idx := 0
|
|
|
|
mut val := a[0]
|
2021-02-20 14:27:36 +01:00
|
|
|
for i, e in a {
|
|
|
|
if e > val {
|
|
|
|
val = e
|
2020-09-07 13:50:53 +02:00
|
|
|
idx = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
|
|
|
|
// merge two sorted arrays (ascending) and maintain sorted order
|
2022-01-07 12:28:50 +01:00
|
|
|
// Example: arrays.merge([1,3,5,7], [2,4,6,8]) // => [1,2,3,4,5,6,7,8]
|
2020-09-07 13:50:53 +02:00
|
|
|
[direct_array_access]
|
2020-10-21 11:23:03 +02:00
|
|
|
pub fn merge<T>(a []T, b []T) []T {
|
2020-10-14 23:39:09 +02:00
|
|
|
mut m := []T{len: a.len + b.len}
|
2020-09-07 13:50:53 +02:00
|
|
|
mut ia := 0
|
|
|
|
mut ib := 0
|
|
|
|
mut j := 0
|
|
|
|
// TODO efficient approach to merge_desc where: a[ia] >= b[ib]
|
2020-10-14 23:39:09 +02:00
|
|
|
for ia < a.len && ib < b.len {
|
2020-09-07 13:50:53 +02: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 19:24:38 +01:00
|
|
|
|
2021-03-07 10:58:13 +01:00
|
|
|
// group n arrays into a single array of arrays with n elements
|
2022-01-09 15:12:33 +01: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.
|
2022-01-07 12:28:50 +01:00
|
|
|
// Example: arrays.group<int>([1,2,3],[4,5,6]) // => [[1, 4], [2, 5], [3, 6]]
|
2021-03-07 10:58:13 +01:00
|
|
|
pub fn group<T>(lists ...[]T) [][]T {
|
|
|
|
mut length := if lists.len > 0 { lists[0].len } else { 0 }
|
|
|
|
// calculate length of output by finding shortest input array
|
|
|
|
for ndx in 1 .. lists.len {
|
|
|
|
if lists[ndx].len < length {
|
|
|
|
length = lists[ndx].len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if length > 0 {
|
|
|
|
mut arr := [][]T{cap: length}
|
|
|
|
// append all combined arrays into the resultant array
|
|
|
|
for ndx in 0 .. length {
|
2022-01-09 15:12:33 +01:00
|
|
|
mut grouped := []T{cap: lists.len}
|
2021-03-07 10:58:13 +01:00
|
|
|
// combine each list item for the ndx position into one array
|
|
|
|
for list_ndx in 0 .. lists.len {
|
2022-01-09 15:12:33 +01:00
|
|
|
grouped << lists[list_ndx][ndx]
|
2021-03-07 10:58:13 +01:00
|
|
|
}
|
2022-01-09 15:12:33 +01:00
|
|
|
arr << grouped
|
2021-03-07 10:58:13 +01:00
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
|
|
|
return [][]T{}
|
|
|
|
}
|
2021-09-13 15:13:32 +02:00
|
|
|
|
2022-01-07 12:28:50 +01: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]]
|
2021-09-13 15:13:32 +02:00
|
|
|
pub fn chunk<T>(list []T, size int) [][]T {
|
|
|
|
// allocate chunk array
|
|
|
|
mut chunks := [][]T{cap: list.len / size + if list.len % size == 0 { 0 } else { 1 }}
|
|
|
|
|
|
|
|
for i := 0; true; {
|
|
|
|
// check chunk size is greater than remaining element size
|
|
|
|
if list.len < i + size {
|
|
|
|
// check if there's no more element to chunk
|
|
|
|
if list.len <= i {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
chunks << list[i..]
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
chunks << list[i..i + size]
|
|
|
|
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 15:49:23 +02:00
|
|
|
//
|
2022-04-02 17:29:12 +02:00
|
|
|
// Example: arrays.window([1, 2, 3, 4], size: 2) // => [[1, 2], [2, 3], [3, 4]]
|
2022-01-07 12:28:50 +01: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]]
|
2021-09-13 15:13:32 +02:00
|
|
|
pub fn window<T>(list []T, attr WindowAttribute) [][]T {
|
|
|
|
// allocate snapshot array
|
|
|
|
mut windows := [][]T{cap: list.len - attr.size + 1}
|
|
|
|
|
|
|
|
for i := 0; true; {
|
|
|
|
// check remaining elements size is less than snapshot size
|
|
|
|
if list.len < i + attr.size {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
windows << list[i..i + attr.size]
|
|
|
|
i += attr.step
|
|
|
|
}
|
|
|
|
|
|
|
|
return windows
|
|
|
|
}
|
2021-09-14 15:49:23 +02:00
|
|
|
|
|
|
|
// sum up array, return nothing when array has no elements
|
2022-01-07 12:28:50 +01:00
|
|
|
//
|
|
|
|
// NOTICE: currently V has bug that cannot make sum function takes custom struct with + operator overloaded
|
2021-09-14 15:49:23 +02:00
|
|
|
// which means you can only pass array of numbers for now.
|
2022-01-07 12:28:50 +01:00
|
|
|
// TODO: Fix generic operator overloading detection issue.
|
|
|
|
// Example: arrays.sum<int>([1, 2, 3, 4, 5])? // => 15
|
2021-09-14 15:49:23 +02:00
|
|
|
pub fn sum<T>(list []T) ?T {
|
|
|
|
if list.len == 0 {
|
|
|
|
return error('Cannot sum up array of nothing.')
|
|
|
|
} else {
|
|
|
|
mut head := list[0]
|
|
|
|
|
|
|
|
for i, e in list {
|
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
head += e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return head
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 19:04:49 +01:00
|
|
|
// reduce sets `acc = list[0]`, then successively calls `acc = reduce_op(acc, elem)` for each remaining element in `list`.
|
|
|
|
// returns the accumulated value in `acc`.
|
|
|
|
// returns an error if the array is empty.
|
|
|
|
// See also: [fold](#fold).
|
2022-01-07 12:28:50 +01:00
|
|
|
// Example: arrays.reduce([1, 2, 3, 4, 5], fn (t1 int, t2 int) int { return t1 * t2 })? // => 120
|
2021-09-14 15:49:23 +02:00
|
|
|
pub fn reduce<T>(list []T, reduce_op fn (t1 T, t2 T) T) ?T {
|
|
|
|
if list.len == 0 {
|
|
|
|
return error('Cannot reduce array of nothing.')
|
|
|
|
} else {
|
|
|
|
mut value := list[0]
|
|
|
|
|
|
|
|
for i, e in list {
|
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
value = reduce_op(value, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 19:04:49 +01:00
|
|
|
// fold sets `acc = init`, then successively calls `acc = fold_op(acc, elem)` for each element in `list`.
|
|
|
|
// returns `acc`.
|
|
|
|
// Example:
|
|
|
|
// ```v
|
|
|
|
// // Sum the length of each string in an array
|
|
|
|
// a := ['Hi', 'all']
|
|
|
|
// r := arrays.fold<string, int>(a, 0,
|
|
|
|
// fn (r int, t string) int { return r + t.len })
|
|
|
|
// assert r == 5
|
|
|
|
// ```
|
2021-09-14 15:49:23 +02:00
|
|
|
pub fn fold<T, R>(list []T, init R, fold_op fn (r R, t T) R) R {
|
|
|
|
mut value := init
|
|
|
|
|
|
|
|
for e in list {
|
|
|
|
value = fold_op(value, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
// flattens n + 1 dimensional array into n dimensional array
|
2022-01-07 12:28:50 +01:00
|
|
|
// Example: arrays.flatten<int>([[1, 2, 3], [4, 5]]) // => [1, 2, 3, 4, 5]
|
2021-09-14 15:49:23 +02:00
|
|
|
pub fn flatten<T>(list [][]T) []T {
|
|
|
|
// calculate required capacity
|
|
|
|
mut required_size := 0
|
|
|
|
|
|
|
|
for e1 in list {
|
|
|
|
for _ in e1 {
|
|
|
|
required_size += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate flattened array
|
|
|
|
mut result := []T{cap: required_size}
|
|
|
|
|
|
|
|
for e1 in list {
|
|
|
|
for e2 in e1 {
|
|
|
|
result << e2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-03-09 19:04:49 +01:00
|
|
|
// group_by groups together elements, for which the `grouping_op` callback produced the same result.
|
2022-01-07 12:28:50 +01:00
|
|
|
// Example: arrays.group_by<int, string>(['H', 'el', 'lo'], fn (v string) int { return v.len }) // => {1: ['H'], 2: ['el', 'lo']}
|
2021-09-14 15:49:23 +02:00
|
|
|
pub fn group_by<K, V>(list []V, grouping_op fn (v V) K) map[K][]V {
|
|
|
|
mut result := map[K][]V{}
|
|
|
|
|
|
|
|
for v in list {
|
|
|
|
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 18:41:50 +02:00
|
|
|
|
2022-01-07 12:28:50 +01: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
|
|
|
|
[deprecated]
|
2021-09-26 18:41:50 +02:00
|
|
|
pub fn concat<T>(a []T, b ...T) []T {
|
|
|
|
mut m := []T{cap: a.len + b.len}
|
|
|
|
|
|
|
|
m << a
|
|
|
|
m << b
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
2021-10-03 07:14:39 +02:00
|
|
|
|
|
|
|
// returns the smallest element >= val, requires `arr` to be sorted
|
2022-01-07 12:28:50 +01:00
|
|
|
// Example: arrays.lower_bound([2, 4, 6, 8], 3)? // => 4
|
2021-10-03 07:14:39 +02:00
|
|
|
pub fn lower_bound<T>(arr []T, val T) ?T {
|
|
|
|
if arr.len == 0 {
|
|
|
|
return error('.lower_bound called on an empty array')
|
|
|
|
}
|
|
|
|
mut left, mut right := 0, arr.len - 1
|
|
|
|
for ; left <= right; {
|
|
|
|
idx := (left + right) / 2
|
|
|
|
elem := arr[idx]
|
|
|
|
if elem < val {
|
|
|
|
left = idx + 1
|
|
|
|
} else {
|
|
|
|
right = idx - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if left >= arr.len {
|
|
|
|
return error('')
|
|
|
|
} else {
|
|
|
|
return arr[left]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the largest element <= val, requires `arr` to be sorted
|
2022-01-07 12:28:50 +01:00
|
|
|
// Example: arrays.upper_bound([2, 4, 6, 8], 3)? // => 2
|
2021-10-03 07:14:39 +02:00
|
|
|
pub fn upper_bound<T>(arr []T, val T) ?T {
|
|
|
|
if arr.len == 0 {
|
|
|
|
return error('.upper_bound called on an empty array')
|
|
|
|
}
|
|
|
|
mut left, mut right := 0, arr.len - 1
|
|
|
|
for ; left <= right; {
|
|
|
|
idx := (left + right) / 2
|
|
|
|
elem := arr[idx]
|
|
|
|
if elem > val {
|
|
|
|
right = idx - 1
|
|
|
|
} else {
|
|
|
|
left = idx + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if right < 0 {
|
|
|
|
return error('')
|
|
|
|
} else {
|
|
|
|
return arr[right]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 12:28:50 +01:00
|
|
|
// binary search, requires `arr` to be sorted, returns index of found item or error.
|
|
|
|
// Binary searches on sorted lists can be faster than other array searches because at maximum
|
|
|
|
// the algorithm only has to traverse log N elements
|
|
|
|
// Example: arrays.binary_search([1, 2, 3, 4], 4) ? // => 3
|
2021-10-03 07:14:39 +02:00
|
|
|
pub fn binary_search<T>(arr []T, target T) ?int {
|
|
|
|
mut left := 0
|
|
|
|
mut right := arr.len - 1
|
|
|
|
for ; left <= right; {
|
|
|
|
idx := (left + right) / 2
|
|
|
|
elem := arr[idx]
|
|
|
|
if elem == target {
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
if elem < target {
|
|
|
|
left = idx + 1
|
|
|
|
} else {
|
|
|
|
right = idx - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error('')
|
|
|
|
}
|
2022-02-07 12:20:45 +01:00
|
|
|
|
|
|
|
// rotate_left rotates the array in-place such that the first `mid` elements of the array move to the end
|
|
|
|
// while the last `arr.len - mid` elements move to the front. After calling `rotate_left`, the element
|
|
|
|
// 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 17:29:12 +02:00
|
|
|
// arrays.rotate_left(mut x, 2)
|
2022-02-07 12:20:45 +01:00
|
|
|
// println(x) // [3, 4, 5, 6, 1, 2]
|
|
|
|
// ```
|
|
|
|
pub fn rotate_left<T>(mut arr []T, mid int) {
|
|
|
|
assert mid <= arr.len && mid >= 0
|
|
|
|
k := arr.len - mid
|
|
|
|
p := &T(arr.data)
|
|
|
|
unsafe {
|
|
|
|
ptr_rotate<T>(mid, &T(usize(voidptr(p)) + usize(sizeof(T)) * usize(mid)), k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// rotate_right rotates the array in-place such that the first `arr.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 `arr.len - k`
|
|
|
|
// 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]
|
|
|
|
// ```
|
|
|
|
pub fn rotate_right<T>(mut arr []T, k int) {
|
|
|
|
assert k <= arr.len && k >= 0
|
|
|
|
mid := arr.len - k
|
|
|
|
p := &T(arr.data)
|
|
|
|
unsafe {
|
|
|
|
ptr_rotate<T>(mid, &T(usize(voidptr(p)) + usize(sizeof(T)) * usize(mid)), k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[unsafe]
|
|
|
|
fn ptr_rotate<T>(left_ int, mid &T, right_ int) {
|
|
|
|
mut left := usize(left_)
|
|
|
|
mut right := usize(right_)
|
|
|
|
for {
|
|
|
|
delta := if left < right { left } else { right }
|
|
|
|
|
|
|
|
if delta <= raw_array_cap<T>() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
swap_nonoverlapping<T>(&T(usize(voidptr(mid)) - left * usize(sizeof(T))),
|
|
|
|
&T(usize(voidptr(mid)) + usize(right - delta) * usize(sizeof(T))), int(delta))
|
|
|
|
}
|
|
|
|
if left <= right {
|
|
|
|
right -= delta
|
|
|
|
} else {
|
|
|
|
left -= delta
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
sz := usize(sizeof(T))
|
|
|
|
rawarray := C.malloc(raw_array_malloc_size<T>())
|
|
|
|
dim := &T(usize(voidptr(mid)) - left * sz + right * sz)
|
|
|
|
if left <= right {
|
|
|
|
C.memcpy(rawarray, voidptr(usize(voidptr(mid)) - left * sz), left * sz)
|
|
|
|
C.memmove(voidptr(usize(voidptr(mid)) - left * sz), voidptr(mid), right * sz)
|
|
|
|
C.memcpy(voidptr(dim), rawarray, left * sz)
|
|
|
|
} else {
|
|
|
|
C.memcpy(rawarray, voidptr(mid), right * sz)
|
|
|
|
C.memmove(voidptr(dim), voidptr(usize(voidptr(mid)) - left * sz), left * sz)
|
|
|
|
C.memcpy(voidptr(usize(voidptr(mid)) - left * sz), rawarray, right * sz)
|
|
|
|
}
|
|
|
|
C.free(rawarray)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Block {
|
|
|
|
mut:
|
|
|
|
x u64
|
|
|
|
y u64
|
|
|
|
z u64
|
|
|
|
w u64
|
|
|
|
}
|
|
|
|
|
|
|
|
struct UnalignedBlock {
|
|
|
|
mut:
|
|
|
|
x u64
|
|
|
|
y u64
|
|
|
|
z u64
|
|
|
|
w u64
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
extra_size = 32 * sizeof(usize)
|
|
|
|
)
|
|
|
|
|
|
|
|
fn raw_array_cap<T>() usize {
|
|
|
|
if sizeof(T) > arrays.extra_size {
|
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
return arrays.extra_size / sizeof(T)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn raw_array_malloc_size<T>() usize {
|
|
|
|
if sizeof(T) > arrays.extra_size {
|
|
|
|
return usize(sizeof(T)) * 2
|
|
|
|
} else {
|
|
|
|
return 32 * usize(sizeof(usize))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[unsafe]
|
|
|
|
fn memswap(x voidptr, y voidptr, len usize) {
|
|
|
|
block_size := sizeof(Block)
|
|
|
|
|
|
|
|
mut i := usize(0)
|
|
|
|
for i + block_size <= len {
|
|
|
|
mut t_ := Block{}
|
|
|
|
t := voidptr(&t_)
|
|
|
|
|
|
|
|
xi := usize(x) + i
|
|
|
|
yi := usize(y) + i
|
|
|
|
unsafe {
|
|
|
|
C.memcpy(t, voidptr(xi), block_size)
|
|
|
|
C.memcpy(voidptr(xi), voidptr(yi), block_size)
|
|
|
|
C.memcpy(t, voidptr(yi), block_size)
|
|
|
|
}
|
|
|
|
i += block_size
|
|
|
|
}
|
|
|
|
if i < len {
|
|
|
|
mut t_ := UnalignedBlock{}
|
|
|
|
t := voidptr(&t_)
|
|
|
|
rem := len - i
|
|
|
|
xi := usize(x) + i
|
|
|
|
yi := usize(y) + i
|
|
|
|
unsafe {
|
|
|
|
C.memcpy(t, voidptr(xi), rem)
|
|
|
|
C.memcpy(voidptr(xi), voidptr(yi), rem)
|
|
|
|
C.memcpy(voidptr(yi), t, rem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[unsafe]
|
|
|
|
fn swap_nonoverlapping<T>(x_ &T, y_ &T, count int) {
|
|
|
|
x := voidptr(x_)
|
|
|
|
y := voidptr(y_)
|
|
|
|
|
|
|
|
len := usize(sizeof(T)) * usize(count)
|
|
|
|
unsafe {
|
|
|
|
memswap(x, y, len)
|
|
|
|
}
|
|
|
|
}
|
2022-03-08 08:44:04 +01: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-03-09 19:26:00 +01:00
|
|
|
pub fn copy<T>(mut dst []T, src []T) int {
|
2022-03-08 08:44:04 +01:00
|
|
|
min := if dst.len < src.len { dst.len } else { src.len }
|
2022-03-09 23:30:51 +01:00
|
|
|
if min <= 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if can_copy_bits<T>() {
|
2022-03-08 08:44:04 +01:00
|
|
|
blen := min * int(sizeof(T))
|
|
|
|
unsafe { vmemmove(&T(dst.data), src.data, blen) }
|
2022-03-09 23:30:51 +01:00
|
|
|
} else {
|
|
|
|
for i in 0 .. min {
|
|
|
|
dst[i] = src[i]
|
|
|
|
}
|
2022-03-08 08:44:04 +01:00
|
|
|
}
|
|
|
|
return min
|
|
|
|
}
|
2022-03-09 23:30:51 +01:00
|
|
|
|
|
|
|
// determines if T can be copied using `memcpy`
|
|
|
|
// false if autofree needs to intervene
|
|
|
|
// false if type is not copyable e.g. map
|
|
|
|
fn can_copy_bits<T>() bool {
|
|
|
|
// references, C pointers, integers, floats, runes
|
|
|
|
if T.name[0] in [`&`, `b`, `c`, `f`, `i`, `r`, `u`, `v`] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|