v/vlib/builtin/array.v

466 lines
11 KiB
V
Raw Normal View History

2019-06-23 04:21:30 +02:00
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
2019-06-22 20:20:28 +02:00
module builtin
2019-08-31 01:35:05 +02:00
import strings
2019-08-10 23:02:48 +02:00
2019-06-22 20:20:28 +02:00
struct array {
pub:
2019-06-22 20:20:28 +02:00
// Using a void pointer allows to implement arrays without generics and without generating
// extra code for every type.
2019-06-23 00:24:16 +02:00
data voidptr
2019-06-22 20:20:28 +02:00
len int
cap int
element_size int
}
// Private function, used by V (`nums := []int`)
fn new_array(mylen, cap, elm_size int) array {
2019-12-16 20:22:04 +01:00
cap_ := if cap == 0 { 1 } else { cap }
2019-06-22 20:20:28 +02:00
arr := array {
len: mylen
cap: cap
element_size: elm_size
2019-12-16 20:22:04 +01:00
data: calloc(cap_ * elm_size)
2019-06-22 20:20:28 +02:00
}
return arr
}
2019-08-31 01:35:05 +02:00
// TODO
pub fn make(len, cap, elm_size int) array {
2019-08-31 01:35:05 +02:00
return new_array(len, cap, elm_size)
}
2019-06-22 20:20:28 +02:00
// Private function, used by V (`nums := [1, 2, 3]`)
fn new_array_from_c_array(len, cap, elm_size int, c_array voidptr) array {
2019-12-16 20:22:04 +01:00
cap_ := if cap == 0 { 1 } else { cap }
2019-06-22 20:20:28 +02:00
arr := array {
len: len
cap: cap
element_size: elm_size
2019-12-16 20:22:04 +01:00
data: calloc(cap_ * elm_size)
2019-06-22 20:20:28 +02:00
}
// TODO Write all memory functions (like memcpy) in V
C.memcpy(arr.data, c_array, len * elm_size)
return arr
}
// Private function, used by V (`nums := [1, 2, 3] !`)
fn new_array_from_c_array_no_alloc(len, cap, elm_size int, c_array voidptr) array {
arr := array {
len: len
cap: cap
element_size: elm_size
data: c_array
}
return arr
}
// Private function. Doubles array capacity if needed
fn (a mut array) ensure_cap(required int) {
if required > a.cap {
mut cap := if a.cap == 0 { 2 } else { a.cap * 2 }
for required > cap { cap *= 2 }
if a.cap == 0 {
a.data = calloc(cap * a.element_size)
}
else {
a.data = C.realloc(a.data, cap * a.element_size)
}
a.cap = cap
}
}
2019-06-22 20:20:28 +02:00
// Private function, used by V (`[0; 100]`)
2019-09-14 22:48:30 +02:00
fn array_repeat_old(val voidptr, nr_repeats, elm_size int) array {
if nr_repeats < 0 {
panic('[0; len]: `len` is negative (len == $nr_repeats)')
}
2019-06-22 20:20:28 +02:00
arr := array {
len: nr_repeats
cap: nr_repeats
element_size: elm_size
2019-09-21 01:29:09 +02:00
data: calloc(nr_repeats * elm_size)
2019-06-22 20:20:28 +02:00
}
for i := 0; i < nr_repeats; i++ {
C.memcpy(arr.data + i * elm_size, val, elm_size)
}
return arr
}
// array.repeat returns new array with the given array elements
// repeated `nr_repeat` times
2019-09-14 22:48:30 +02:00
pub fn (a array) repeat(nr_repeats int) array {
if nr_repeats < 0 {
panic('array.repeat: count is negative (count == $nr_repeats)')
}
2019-12-16 20:22:04 +01:00
mut size := nr_repeats * a.len * a.element_size
if size == 0 {
size = a.element_size
}
2019-09-14 22:48:30 +02:00
arr := array {
len: nr_repeats * a.len
cap: nr_repeats * a.len
2019-09-14 22:48:30 +02:00
element_size: a.element_size
2019-12-16 20:22:04 +01:00
data: calloc(size)
2019-09-14 22:48:30 +02:00
}
for i := 0; i < nr_repeats; i++ {
C.memcpy(arr.data + i * a.len * a.element_size, a.data, a.len * a.element_size)
2019-09-14 22:48:30 +02:00
}
return arr
}
// array.sort sorts array in-place using given `compare` function as comparator
pub fn (a mut array) sort_with_compare(compare voidptr) {
2019-06-22 20:20:28 +02:00
C.qsort(a.data, a.len, a.element_size, compare)
}
// TODO array.insert is broken
// Cannot pass literal or primitive type as it cannot be cast to voidptr.
// In the current state only that would work:
// i := 3
// a.insert(0, &i)
// ----------------------------
pub fn (a mut array) insert(i int, val voidptr) {
if i < 0 || i > a.len {
panic('array.insert: index out of range (i == $i, a.len == $a.len)')
2019-06-22 20:20:28 +02:00
}
a.ensure_cap(a.len + 1)
2019-06-22 20:20:28 +02:00
size := a.element_size
C.memmove(a.data + (i + 1) * size, a.data + i * size, (a.len - i) * size)
C.memcpy(a.data + i * size, val, size)
a.len++
2019-06-22 20:20:28 +02:00
}
// TODO array.prepend is broken
// It depends on array.insert
// -----------------------------
pub fn (a mut array) prepend(val voidptr) {
2019-06-22 20:20:28 +02:00
a.insert(0, val)
}
// array.delete deletes array element at the given index
pub fn (a mut array) delete(i int) {
if i < 0 || i >= a.len {
panic('array.delete: index out of range (i == $i, a.len == $a.len)')
}
2019-06-22 20:20:28 +02:00
size := a.element_size
C.memmove(a.data + i * size, a.data + (i + 1) * size, (a.len - i) * size)
2019-06-22 20:20:28 +02:00
a.len--
}
// Private function. Used to implement array[] operator
fn (a array) get(i int) voidptr {
if i < 0 || i >= a.len {
panic('array.get: index out of range (i == $i, a.len == $a.len)')
}
return a.data + i * a.element_size
}
2019-11-12 17:26:05 +01:00
// array.first returns the first element of the array
pub fn (a array) first() voidptr {
2019-06-22 20:20:28 +02:00
if a.len == 0 {
panic('array.first: array is empty')
2019-06-22 20:20:28 +02:00
}
return a.data + 0
}
2019-11-12 17:26:05 +01:00
// array.last returns the last element of the array
pub fn (a array) last() voidptr {
2019-06-22 20:20:28 +02:00
if a.len == 0 {
panic('array.last: array is empty')
2019-06-22 20:20:28 +02:00
}
return a.data + (a.len - 1) * a.element_size
}
// array.left returns a new array using the same buffer as the given array
// with the first `n` elements of the given array.
2019-11-30 10:37:34 +01:00
fn (a array) left(n int) array {
if n < 0 {
panic('array.left: index is negative (n == $n)')
}
if n >= a.len {
return a.slice(0, a.len)
2019-06-22 20:20:28 +02:00
}
return a.slice(0, n)
2019-06-22 20:20:28 +02:00
}
// array.right returns an array using same buffer as the given array
// but starting with the element of the given array beyond the index `n`.
// If `n` is bigger or equal to the length of the given array,
// returns an empty array of the same type as the given array.
2019-11-30 10:37:34 +01:00
fn (a array) right(n int) array {
if n < 0 {
panic('array.right: index is negative (n == $n)')
2019-06-22 20:20:28 +02:00
}
if n >= a.len {
return new_array(0, 0, a.element_size)
}
return a.slice(n, a.len)
2019-06-22 20:20:28 +02:00
}
// used internally for [2..4]
fn (a array) slice2(start, _end int, end_max bool) array {
end := if end_max { a.len } else { _end }
return a.slice(start, end)
}
// array.slice returns an array using the same buffer as original array
// but starting from the `start` element and ending with the element before
// the `end` element of the original array with the length and capacity
// set to the number of the elements in the slice.
2019-11-30 10:37:34 +01:00
fn (a array) slice(start, _end int) array {
2019-06-22 20:20:28 +02:00
mut end := _end
if start > end {
panic('array.slice: invalid slice index ($start > $end)')
2019-06-22 20:20:28 +02:00
}
if end > a.len {
panic('array.slice: slice bounds out of range ($end >= $a.len)')
}
2019-08-31 01:35:05 +02:00
if start < 0 {
panic('array.slice: slice bounds out of range ($start < 0)')
2019-06-22 20:20:28 +02:00
}
l := end - start
res := array {
element_size: a.element_size
data: a.data + start * a.element_size
2019-06-22 20:20:28 +02:00
len: l
cap: l
}
return res
}
// Private function. Used to implement assigment to the array element.
fn (a mut array) set(i int, val voidptr) {
if i < 0 || i >= a.len {
panic('array.set: index out of range (i == $i, a.len == $a.len)')
2019-06-22 20:20:28 +02:00
}
C.memcpy(a.data + a.element_size * i, val, a.element_size)
2019-06-22 20:20:28 +02:00
}
fn (a mut array) push(val voidptr) {
a.ensure_cap(a.len + 1)
C.memcpy(a.data + a.element_size * a.len, val, a.element_size)
a.len++
}
// `val` is array.data
// TODO make private, right now it's used by strings.Builder
pub fn (a mut array) push_many(val voidptr, size int) {
a.ensure_cap(a.len + size)
C.memcpy(a.data + a.element_size * a.len, val, a.element_size * size)
a.len += size
}
// array.reverse returns a new array with the elements of
// the original array in reverse order.
2019-07-17 18:17:07 +02:00
pub fn (a array) reverse() array {
arr := array {
len: a.len
cap: a.cap
element_size: a.element_size
2019-09-21 01:29:09 +02:00
data: calloc(a.cap * a.element_size)
2019-07-17 18:17:07 +02:00
}
for i := 0; i < a.len; i++ {
C.memcpy(arr.data + i * arr.element_size, &a[a.len-1-i], arr.element_size)
}
return arr
}
// array.clone returns an independent copy of a given array
2019-08-12 13:41:34 +02:00
pub fn (a array) clone() array {
2019-12-16 20:22:04 +01:00
mut size := a.cap * a.element_size
if size == 0 {
size++
}
2019-08-12 13:41:34 +02:00
arr := array {
len: a.len
cap: a.cap
element_size: a.element_size
2019-12-16 20:22:04 +01:00
data: calloc(size)
2019-08-12 13:41:34 +02:00
}
2019-08-31 01:35:05 +02:00
C.memcpy(arr.data, a.data, a.cap * a.element_size)
return arr
}
2019-08-12 13:41:34 +02:00
//pub fn (a []int) free() {
[unsafe_fn]
pub fn (a array) free() {
2019-07-21 15:05:47 +02:00
//if a.is_slice {
2019-08-31 01:35:05 +02:00
//return
//}
2019-06-22 20:20:28 +02:00
C.free(a.data)
}
// []string.str returns a string representation of the array of strings
2019-06-22 20:20:28 +02:00
// "[ 'a', 'b', 'c' ]"
2019-06-30 13:06:46 +02:00
pub fn (a []string) str() string {
2019-08-31 01:35:05 +02:00
mut sb := strings.new_builder(a.len * 3)
sb.write('[')
2019-06-22 20:20:28 +02:00
for i := 0; i < a.len; i++ {
val := a[i]
2019-09-14 22:48:30 +02:00
sb.write('"')
sb.write(val)
sb.write('"')
2019-06-22 20:20:28 +02:00
if i < a.len - 1 {
2019-08-31 01:35:05 +02:00
sb.write(', ')
2019-06-22 20:20:28 +02:00
}
}
2019-08-31 01:35:05 +02:00
sb.write(']')
return sb.str()
2019-06-22 20:20:28 +02:00
}
// []bool.str returns a string representation of the array of bools
// "[true, true, false]"
pub fn (a []bool) str() string {
mut sb := strings.new_builder(a.len * 3)
sb.write('[')
for i := 0; i < a.len; i++ {
val := a[i]
if val {
sb.write('true')
} else {
sb.write('false')
2019-11-30 10:37:34 +01:00
}
if i < a.len - 1 {
sb.write(', ')
}
}
sb.write(']')
return sb.str()
}
// []byte.hex returns a string with the hexadecimal representation
// of the byte elements of the array
2019-07-15 17:49:01 +02:00
pub fn (b []byte) hex() string {
mut hex := malloc(b.len*2+1)
mut ptr := &hex[0]
for i := 0; i < b.len ; i++ {
2019-12-01 08:33:26 +01:00
ptr += C.sprintf(charptr(ptr), '%02x', b[i])
2019-07-15 17:49:01 +02:00
}
return string(hex)
}
2019-07-28 17:19:59 +02:00
// copy copies the `src` byte array elements to the `dst` byte array.
// The number of the elements copied is the minimum of the length of both arrays.
// Returns the number of elements copied.
2019-07-28 17:19:59 +02:00
// TODO: implement for all types
pub fn copy(dst, src []byte) int {
if dst.len > 0 && src.len > 0 {
min := if dst.len < src.len { dst.len } else { src.len }
C.memcpy(dst.data, src.left(min).data, dst.element_size*min)
return min
}
return 0
}
2019-09-01 19:55:34 +02:00
// Private function. Comparator for int type.
2019-11-18 22:27:27 +01:00
fn compare_ints(a, b &int) int {
if *a < *b {
2019-09-01 19:55:34 +02:00
return -1
}
2019-11-18 22:27:27 +01:00
if *a > *b {
2019-09-01 19:55:34 +02:00
return 1
}
return 0
}
// []int.sort sorts array of int in place in ascending order.
2019-09-01 19:55:34 +02:00
pub fn (a mut []int) sort() {
a.sort_with_compare(compare_ints)
}
2019-10-04 22:07:19 +02:00
// []string.index returns the index of the first element equal to the given value,
// or -1 if the value is not found in the array.
2019-10-04 22:07:19 +02:00
pub fn (a []string) index(v string) int {
for i := 0; i < a.len; i++ {
if a[i] == v {
return i
}
}
return -1
}
// []int.index returns the index of the first element equal to the given value,
// or -1 if the value is not found in the array.
2019-10-04 22:07:19 +02:00
pub fn (a []int) index(v int) int {
for i := 0; i < a.len; i++ {
if a[i] == v {
return i
}
}
return -1
}
// []byte.index returns the index of the first element equal to the given value,
// or -1 if the value is not found in the array.
2019-10-04 22:07:19 +02:00
pub fn (a []byte) index(v byte) int {
for i := 0; i < a.len; i++ {
if a[i] == v {
return i
}
}
return -1
}
// []char.index returns the index of the first element equal to the given value,
// or -1 if the value is not found in the array.
// TODO is `char` type yet in the language?
2019-10-04 22:07:19 +02:00
pub fn (a []char) index(v char) int {
for i := 0; i < a.len; i++ {
if a[i] == v {
return i
}
}
return -1
}
2019-10-08 12:23:17 +02:00
// []int.reduce executes a given reducer function on each element of the array,
2019-10-11 03:12:40 +02:00
// resulting in a single output value.
2019-10-11 23:06:30 +02:00
pub fn (a []int) reduce(iter fn (accum, curr int) int, accum_start int) int {
2019-10-11 03:12:40 +02:00
mut _accum := 0
_accum = accum_start
for i := 0; i < a.len; i++ {
_accum = iter(_accum, a[i])
}
return _accum
}
// array_eq<T> checks if two arrays contain all the same elements in the same order.
// []int == []int (also for: i64, f32, f64, byte, string)
fn array_eq<T>(a1, a2 []T) bool {
if a1.len != a2.len {
return false
}
for i := 0; i < a1.len; i++ {
if a1[i] != a2[i] {
return false
}
}
return true
}
2019-10-31 11:08:01 +01:00
pub fn (a []int) eq(a2 []int) bool {
return array_eq(a, a2)
}
2019-10-31 11:08:01 +01:00
pub fn (a []i64) eq(a2 []i64) bool {
return array_eq(a, a2)
}
2019-10-31 11:08:01 +01:00
pub fn (a []string) eq(a2 []string) bool {
return array_eq(a, a2)
}
2019-10-31 11:08:01 +01:00
pub fn (a []byte) eq(a2 []byte) bool {
return array_eq(a, a2)
}
2019-10-31 11:08:01 +01:00
pub fn (a []f32) eq(a2 []f32) bool {
return array_eq(a, a2)
}