1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/builtin/array.v

666 lines
15 KiB
V
Raw Normal View History

2020-01-23 23:04:46 +03:00
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
2019-06-23 05:21:30 +03:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
2019-06-22 21:20:28 +03:00
module builtin
import strings
2019-08-11 00:02:48 +03:00
2020-01-12 21:59:57 +03:00
pub struct array {
pub:
element_size int
pub mut:
2020-04-14 19:27:30 +03:00
data voidptr// Using a void pointer allows to implement arrays without generics and without generating
2019-12-18 20:07:32 +03:00
// extra code for every type.
2019-06-22 21:20:28 +03:00
len int
cap int
}
// Internal function, used by V (`nums := []int`)
2020-04-25 09:42:23 +03:00
fn __new_array(mylen int, cap int, elm_size int) array {
cap_ := if cap < mylen { mylen } else { cap }
2019-12-18 20:07:32 +03:00
arr := array{
element_size: elm_size
data: vcalloc(cap_ * elm_size)
2020-05-18 22:38:06 +03:00
len: mylen
cap: cap_
2019-06-22 21:20:28 +03:00
}
return arr
}
2020-04-15 21:12:06 +03:00
2020-05-16 16:21:37 +03:00
fn __new_array_with_default(mylen int, cap int, elm_size int, val voidptr) array {
cap_ := if cap < mylen { mylen } else { cap }
mut arr := array{
element_size: elm_size
data: vcalloc(cap_ * elm_size)
2020-05-18 22:38:06 +03:00
len: mylen
cap: cap_
2020-05-16 16:21:37 +03:00
}
if val != 0 {
for i in 0..arr.len {
unsafe {
arr.set_unsafe(i, val)
}
2020-05-16 16:21:37 +03:00
}
}
return arr
}
2020-06-12 02:24:25 +03:00
fn __new_array_with_array_default(mylen int, cap int, elm_size int, val array) array {
cap_ := if cap < mylen { mylen } else { cap }
mut arr := array{
2020-06-12 02:24:25 +03:00
element_size: elm_size
data: vcalloc(cap_ * elm_size)
len: mylen
cap: cap_
}
for i in 0..arr.len {
val_clone := val.clone()
unsafe {
arr.set_unsafe(i, &val_clone)
}
2020-06-12 02:24:25 +03:00
}
return arr
}
2019-06-22 21:20:28 +03: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 {
cap_ := if cap < len { len } else { cap }
2020-03-21 21:52:19 +03:00
2019-12-18 20:07:32 +03:00
arr := array{
element_size: elm_size
data: vcalloc(cap_ * elm_size)
2020-05-18 22:38:06 +03:00
len: len
cap: cap_
2019-06-22 21:20:28 +03:00
}
// TODO Write all memory functions (like memcpy) in V
2020-01-04 02:06:01 +03:00
C.memcpy(arr.data, c_array, len * elm_size)
2019-06-22 21:20:28 +03:00
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 {
2019-12-18 20:07:32 +03:00
arr := array{
element_size: elm_size
data: c_array
2020-05-18 22:38:06 +03:00
len: len
cap: cap
2019-06-22 21:20:28 +03:00
}
return arr
}
// Private function. Doubles array capacity if needed
2020-04-27 08:39:33 +03:00
[inline]
2020-05-17 14:51:18 +03:00
fn (mut a array) ensure_cap(required int) {
2020-01-19 15:11:58 +03:00
if required <= a.cap {
return
}
2020-01-19 15:11:58 +03:00
mut cap := if a.cap == 0 { 2 } else { a.cap * 2 }
for required > cap {
cap *= 2
}
if a.cap == 0 {
a.data = vcalloc(cap * a.element_size)
2020-01-19 15:11:58 +03:00
}
else {
2020-07-11 13:37:51 +03:00
a.data = v_realloc(a.data, u32(cap * a.element_size))
2020-01-19 15:11:58 +03:00
}
a.cap = cap
}
2020-02-29 17:25:49 +03:00
// repeat returns new array with the given array elements repeated given times.
pub fn (a array) repeat(count int) array {
if count < 0 {
panic('array.repeat: count is negative: $count')
}
2020-02-29 17:25:49 +03:00
mut size := count * a.len * a.element_size
2019-12-16 22:22:04 +03:00
if size == 0 {
size = a.element_size
}
2019-12-18 20:07:32 +03:00
arr := array{
element_size: a.element_size
data: vcalloc(size)
2020-05-18 22:38:06 +03:00
len: count * a.len
cap: count * a.len
2019-09-14 23:48:30 +03:00
}
2020-02-29 17:25:49 +03:00
for i in 0..count {
if a.len > 0 && a.element_size == sizeof(array) {
ary := array{}
C.memcpy(&ary, a.data, sizeof(array))
ary_clone := ary.clone()
unsafe {
C.memcpy(arr.get_unsafe(i * a.len), &ary_clone, a.len * a.element_size)
}
} else {
unsafe {
C.memcpy(arr.get_unsafe(i * a.len), byteptr(a.data), a.len * a.element_size)
}
}
2019-09-14 23:48:30 +03:00
}
return arr
}
// array.sort sorts array in-place using given `compare` function as comparator
2020-05-17 14:51:18 +03:00
pub fn (mut a array) sort_with_compare(compare voidptr) {
2019-06-22 21:20:28 +03:00
C.qsort(a.data, a.len, a.element_size, compare)
}
// array.insert
2020-05-17 14:51:18 +03:00
pub fn (mut a array) insert(i int, val voidptr) {
$if !no_bounds_checking? {
if i < 0 || i > a.len {
panic('array.insert: index out of range (i == $i, a.len == $a.len)')
}
2019-06-22 21:20:28 +03:00
}
a.ensure_cap(a.len + 1)
unsafe {
C.memmove(a.get_unsafe(i + 1), a.get_unsafe(i), (a.len - i) * a.element_size)
a.set_unsafe(i, val)
}
a.len++
2019-06-22 21:20:28 +03:00
}
// array.insert_many
pub fn (mut a array) insert_many(i int, val voidptr, size int) {
$if !no_bounds_checking? {
if i < 0 || i > a.len {
panic('array.insert_many: index out of range (i == $i, a.len == $a.len)')
}
}
a.ensure_cap(a.len + size)
elem_size := a.element_size
unsafe {
iptr := a.get_unsafe(i)
C.memmove(a.get_unsafe(i + size), iptr, (a.len - i) * elem_size)
C.memcpy(iptr, val, size * elem_size)
}
a.len += size
}
// array.prepend
2020-05-17 14:51:18 +03:00
pub fn (mut a array) prepend(val voidptr) {
2019-06-22 21:20:28 +03:00
a.insert(0, val)
}
// array.prepend_many
pub fn (mut a array) prepend_many(val voidptr, size int) {
a.insert_many(0, val, size)
}
// array.delete deletes array element at the given index
2020-05-17 14:51:18 +03:00
pub fn (mut a array) delete(i int) {
$if !no_bounds_checking? {
if i < 0 || i >= a.len {
panic('array.delete: index out of range (i == $i, a.len == $a.len)')
}
}
2020-06-12 02:24:25 +03:00
// NB: if a is [12,34], a.len = 2, a.delete(0)
2020-05-24 11:10:41 +03:00
// should move (2-0-1) elements = 1 element (the 34) forward
unsafe {
C.memmove(a.get_unsafe(i), a.get_unsafe(i + 1), (a.len - i - 1) * a.element_size)
}
2019-06-22 21:20:28 +03:00
a.len--
}
2020-01-28 00:31:48 +03:00
// clears the array without deallocating the allocated data
2020-05-17 14:51:18 +03:00
pub fn (mut a array) clear() {
2020-01-28 00:31:48 +03:00
a.len = 0
}
2020-02-17 22:31:40 +03:00
// trims the array length to "index" without modifying the allocated data. If "index" is greater
// than len nothing will be changed
2020-05-17 14:51:18 +03:00
pub fn (mut a array) trim(index int) {
2020-02-17 22:31:40 +03:00
if index < a.len {
a.len = index
}
}
// we manually inline this for single operations for performance without -prod
[inline] [unsafe_fn]
fn (a array) get_unsafe(i int) voidptr {
unsafe {
return byteptr(a.data) + i * a.element_size
}
}
// Private function. Used to implement array[] operator
fn (a array) get(i int) voidptr {
$if !no_bounds_checking? {
if i < 0 || i >= a.len {
panic('array.get: index out of range (i == $i, a.len == $a.len)')
}
}
unsafe {
return byteptr(a.data) + i * a.element_size
}
}
2019-11-12 19:26:05 +03:00
// array.first returns the first element of the array
pub fn (a array) first() voidptr {
$if !no_bounds_checking? {
if a.len == 0 {
panic('array.first: array is empty')
}
2019-06-22 21:20:28 +03:00
}
2020-04-02 16:31:44 +03:00
return a.data
2019-06-22 21:20:28 +03:00
}
2019-11-12 19:26:05 +03:00
// array.last returns the last element of the array
pub fn (a array) last() voidptr {
$if !no_bounds_checking? {
if a.len == 0 {
panic('array.last: array is empty')
}
2019-06-22 21:20:28 +03:00
}
unsafe {
return byteptr(a.data) + (a.len - 1) * a.element_size
}
2019-06-22 21:20:28 +03:00
}
// 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 12:37:34 +03:00
fn (a array) slice(start, _end int) array {
2019-12-17 01:29:40 +03:00
mut end := _end
$if !no_bounds_checking? {
if start > end {
panic('array.slice: invalid slice index ($start > $end)')
}
if end > a.len {
panic('array.slice: slice bounds out of range ($end >= $a.len)')
}
if start < 0 {
panic('array.slice: slice bounds out of range ($start < 0)')
}
2019-12-17 01:29:40 +03:00
}
mut data := byteptr(0)
unsafe {
data = byteptr(a.data) + start * a.element_size
}
2019-12-17 01:29:40 +03:00
l := end - start
2019-12-18 20:07:32 +03:00
res := array{
2019-12-17 01:29:40 +03:00
element_size: a.element_size
data: data
2019-12-17 01:29:40 +03:00
len: l
cap: l
}
return res
}
2020-01-08 12:19:12 +03: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.clone_static returns an independent copy of a given array
// It should be used only in -autofree generated code.
fn (a array) clone_static() array {
return a.clone()
}
2020-01-08 12:19:12 +03:00
// array.clone returns an independent copy of a given array
2020-03-11 01:21:26 +03:00
pub fn (a &array) clone() array {
2020-01-08 12:19:12 +03:00
mut size := a.cap * a.element_size
if size == 0 {
size++
}
mut arr := array{
element_size: a.element_size
data: vcalloc(size)
2020-05-18 22:38:06 +03:00
len: a.len
cap: a.cap
2020-01-08 12:19:12 +03:00
}
2020-06-19 14:32:55 +03:00
// Recursively clone-generated elements if array element is array type
if a.element_size == sizeof(array) {
for i in 0..a.len {
ar := array{}
C.memcpy(&ar, a.get_unsafe(i), sizeof(array))
2020-06-19 14:32:55 +03:00
ar_clone := ar.clone()
arr.set_unsafe(i, &ar_clone)
2020-06-19 14:32:55 +03:00
}
} else {
C.memcpy(byteptr(arr.data), a.data, a.cap * a.element_size)
}
2020-01-08 12:19:12 +03:00
return arr
}
2020-03-11 01:21:26 +03:00
fn (a &array) slice_clone(start, _end int) array {
2019-06-22 21:20:28 +03:00
mut end := _end
$if !no_bounds_checking? {
if start > end {
panic('array.slice: invalid slice index ($start > $end)')
}
if end > a.len {
panic('array.slice: slice bounds out of range ($end >= $a.len)')
}
if start < 0 {
panic('array.slice: slice bounds out of range ($start < 0)')
}
2019-06-22 21:20:28 +03:00
}
mut data := byteptr(0)
unsafe {
data = byteptr(a.data) + start * a.element_size
}
2019-06-22 21:20:28 +03:00
l := end - start
2019-12-18 20:07:32 +03:00
res := array{
element_size: a.element_size
data: data
2019-06-22 21:20:28 +03:00
len: l
cap: l
}
2019-12-17 01:21:10 +03:00
return res.clone()
2019-06-22 21:20:28 +03:00
}
// we manually inline this for single operations for performance without -prod
[inline] [unsafe_fn]
fn (mut a array) set_unsafe(i int, val voidptr) {
unsafe {
C.memcpy(byteptr(a.data) + a.element_size * i, val, a.element_size)
}
}
// Private function. Used to implement assigment to the array element.
2020-05-17 14:51:18 +03:00
fn (mut a array) set(i int, val voidptr) {
$if !no_bounds_checking? {
if i < 0 || i >= a.len {
panic('array.set: index out of range (i == $i, a.len == $a.len)')
}
2019-06-22 21:20:28 +03:00
}
unsafe {
C.memcpy(byteptr(a.data) + a.element_size * i, val, a.element_size)
}
2019-06-22 21:20:28 +03:00
}
2020-05-17 14:51:18 +03:00
fn (mut a array) push(val voidptr) {
a.ensure_cap(a.len + 1)
unsafe {
C.memcpy(byteptr(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
2020-05-17 14:51:18 +03:00
pub fn (mut a3 array) push_many(val voidptr, size int) {
2020-03-09 00:11:56 +03:00
if a3.data == val {
// handle `arr << arr`
copy := a3.clone()
a3.ensure_cap(a3.len + size)
unsafe {
//C.memcpy(a.data, copy.data, copy.element_size * copy.len)
C.memcpy(a3.get_unsafe(a3.len), copy.data, a3.element_size * size)
}
2020-01-19 15:11:58 +03:00
} else {
2020-03-09 00:11:56 +03:00
a3.ensure_cap(a3.len + size)
unsafe {
C.memcpy(a3.get_unsafe(a3.len), val, a3.element_size * size)
}
2020-01-19 15:11:58 +03:00
}
2020-03-09 00:11:56 +03:00
a3.len += size
}
pub fn (mut a array) reverse_in_place() {
if a.len < 2 {
return
}
unsafe {
mut tmp_value := malloc(a.element_size)
for i in 0..a.len/2 {
C.memcpy(tmp_value, byteptr(a.data) + i * a.element_size, a.element_size)
C.memcpy(byteptr(a.data) + i * a.element_size, byteptr(a.data) + (a.len-1-i) * a.element_size, a.element_size)
C.memcpy(byteptr(a.data) + (a.len-1-i) * a.element_size, tmp_value, a.element_size)
}
free(tmp_value)
}
}
// array.reverse returns a new array with the elements of
// the original array in reverse order.
2019-07-17 19:17:07 +03:00
pub fn (a array) reverse() array {
2020-02-29 22:44:02 +03:00
if a.len < 2 {
return a
}
mut arr := array{
element_size: a.element_size
data: vcalloc(a.cap * a.element_size)
2020-05-18 22:38:06 +03:00
len: a.len
cap: a.cap
2019-07-17 19:17:07 +03:00
}
for i in 0..a.len {
unsafe {
arr.set_unsafe(i, a.get_unsafe(a.len - 1 - i))
}
2019-07-17 19:17:07 +03:00
}
return arr
}
2019-12-18 20:07:32 +03:00
// pub fn (a []int) free() {
[unsafe_fn]
pub fn (a &array) free() {
2020-07-11 14:22:16 +03:00
$if prealloc {
return
}
2019-12-18 20:07:32 +03:00
// if a.is_slice {
// return
// }
2019-06-22 21:20:28 +03:00
C.free(a.data)
}
// []string.str returns a string representation of the array of strings
2020-03-11 20:48:59 +03:00
// => '["a", "b", "c"]'
2019-06-30 14:06:46 +03:00
pub fn (a []string) str() string {
2019-08-31 02:35:05 +03:00
mut sb := strings.new_builder(a.len * 3)
sb.write('[')
for i in 0..a.len {
2019-06-22 21:20:28 +03:00
val := a[i]
2020-04-27 15:48:28 +03:00
sb.write("\'")
2019-09-14 23:48:30 +03:00
sb.write(val)
2020-04-27 15:48:28 +03:00
sb.write("\'")
2019-06-22 21:20:28 +03:00
if i < a.len - 1 {
2019-08-31 02:35:05 +03:00
sb.write(', ')
2019-06-22 21:20:28 +03:00
}
}
2019-08-31 02:35:05 +03:00
sb.write(']')
return sb.str()
2019-06-22 21:20:28 +03:00
}
// []byte.hex returns a string with the hexadecimal representation
// of the byte elements of the array
2019-07-15 18:49:01 +03:00
pub fn (b []byte) hex() string {
2019-12-18 20:07:32 +03:00
mut hex := malloc(b.len * 2 + 1)
mut dst_i := 0
for i in b {
2020-03-18 18:47:37 +03:00
n0 := i >> 4
hex[dst_i++] = if n0 < 10 { n0 + `0` } else { n0 + byte(87) }
n1 := i & 0xF
hex[dst_i++] = if n1 < 10 { n1 + `0` } else { n1 + byte(87) }
}
hex[dst_i] = `\0`
return tos(hex,dst_i)
2019-07-15 18:49:01 +03:00
}
2019-07-28 18:19:59 +03: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 18:19:59 +03:00
// TODO: implement for all types
pub fn copy(dst, src []byte) int {
if dst.len > 0 && src.len > 0 {
2020-02-02 16:31:54 +03:00
mut min := 0
min = if dst.len < src.len { dst.len } else { src.len }
2020-04-02 16:31:44 +03:00
C.memcpy(byteptr(dst.data), src[..min].data, dst.element_size * min)
2019-07-28 18:19:59 +03:00
return min
}
return 0
}
2019-09-01 20:55:34 +03:00
// Private function. Comparator for int type.
2019-11-19 00:27:27 +03:00
fn compare_ints(a, b &int) int {
if *a < *b {
2019-09-01 20:55:34 +03:00
return -1
}
2019-11-19 00:27:27 +03:00
if *a > *b {
2019-09-01 20:55:34 +03:00
return 1
}
return 0
}
// []int.sort sorts array of int in place in ascending order.
2020-05-17 14:51:18 +03:00
pub fn (mut a []int) sort() {
2019-09-01 20:55:34 +03:00
a.sort_with_compare(compare_ints)
}
2019-10-04 23:07:19 +03: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 23:07:19 +03:00
pub fn (a []string) index(v string) int {
for i in 0..a.len {
2019-10-04 23:07:19 +03:00
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 23:07:19 +03:00
pub fn (a []int) index(v int) int {
for i in 0..a.len {
2019-10-04 23:07:19 +03:00
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 23:07:19 +03:00
pub fn (a []byte) index(v byte) int {
for i in 0..a.len {
2019-10-04 23:07:19 +03:00
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 23:07:19 +03:00
pub fn (a []char) index(v char) int {
for i in 0..a.len {
2019-10-04 23:07:19 +03:00
if a[i] == v {
return i
}
}
return -1
}
2019-10-08 13:23:17 +03:00
// []int.reduce executes a given reducer function on each element of the array,
2019-10-11 04:12:40 +03:00
// resulting in a single output value.
2019-12-18 20:07:32 +03:00
pub fn (a []int) reduce(iter fn(accum, curr int)int, accum_start int) int {
mut accum_ := accum_start
for i in a {
accum_ = iter(accum_, i)
2019-10-11 04:12:40 +03:00
}
2020-02-29 17:25:49 +03:00
return accum_
2019-10-11 04:12:40 +03:00
}
// 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)
2020-02-18 20:13:34 +03:00
/*
fn array_eq<T>(a1, a2 []T) bool {
if a1.len != a2.len {
return false
}
for i in 0..a1.len {
if a1[i] != a2[i] {
return false
}
}
return true
}
2019-10-31 13:08:01 +03:00
pub fn (a []int) eq(a2 []int) bool {
return array_eq(a, a2)
}
2019-10-31 13:08:01 +03:00
pub fn (a []i64) eq(a2 []i64) bool {
return array_eq(a, a2)
}
2019-10-31 13:08:01 +03:00
pub fn (a []byte) eq(a2 []byte) bool {
return array_eq(a, a2)
}
2019-10-31 13:08:01 +03:00
pub fn (a []f32) eq(a2 []f32) bool {
return array_eq(a, a2)
}
2020-02-18 20:13:34 +03:00
*/
2019-12-19 23:49:40 +03:00
2020-02-18 22:31:08 +03:00
pub fn (a1 []string) eq(a2 []string) bool {
//return array_eq(a, a2)
if a1.len != a2.len {
return false
}
for i in 0..a1.len {
2020-02-18 22:31:08 +03:00
if a1[i] != a2[i] {
return false
}
}
return true
}
2020-01-21 20:49:30 +03:00
// compare_i64 for []f64 sort_with_compare()
// sort []i64 with quicksort
// usage :
// mut x := [i64(100),10,70,28,92]
// x.sort_with_compare(compare_i64)
// println(x) // Sorted i64 Array
// output:
// [10, 28, 70, 92, 100]
pub fn compare_i64(a, b &i64) int {
if *a < *b {
return -1
}
if *a > *b {
return 1
}
return 0
}
// compare_f64 for []f64 sort_with_compare()
// ref. compare_i64(...)
pub fn compare_f64(a, b &f64) int {
if *a < *b {
return -1
}
if *a > *b {
return 1
}
return 0
}
// compare_f32 for []f32 sort_with_compare()
// ref. compare_i64(...)
pub fn compare_f32(a, b &f32) int {
if *a < *b {
return -1
}
if *a > *b {
return 1
}
return 0
}
2020-03-09 00:11:56 +03:00
// a.pointers() returns a new array, where each element
// is the address of the corresponding element in a.
pub fn (a array) pointers() []voidptr {
mut res := []voidptr{}
for i in 0..a.len {
res << a.get_unsafe(i)
}
return res
}