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

357 lines
7.2 KiB
V
Raw Normal View History

2019-06-23 05:21:30 +03: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 21:20:28 +03:00
module builtin
2019-08-31 02:35:05 +03:00
import strings
2019-08-11 00:02:48 +03:00
2019-06-22 21:20:28 +03:00
struct array {
pub:
2019-06-22 21:20:28 +03:00
// Using a void pointer allows to implement arrays without generics and without generating
// extra code for every type.
2019-06-23 01:24:16 +03:00
data voidptr
2019-06-22 21:20:28 +03: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-06-22 21:20:28 +03:00
arr := array {
len: mylen
cap: cap
element_size: elm_size
2019-09-03 20:15:08 +03:00
data: calloc(cap * elm_size)
2019-06-22 21:20:28 +03:00
}
return arr
}
2019-08-31 02:35:05 +03:00
// TODO
pub fn make(len, cap, elm_size int) array {
2019-08-31 02:35:05 +03:00
return new_array(len, cap, elm_size)
}
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 {
arr := array {
len: len
cap: cap
element_size: elm_size
2019-09-21 02:29:09 +03:00
data: calloc(cap * elm_size)
2019-06-22 21:20:28 +03: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, used by V (`[0; 100]`)
2019-09-14 23:48:30 +03:00
fn array_repeat_old(val voidptr, nr_repeats, elm_size int) array {
2019-06-22 21:20:28 +03:00
arr := array {
len: nr_repeats
cap: nr_repeats
element_size: elm_size
2019-09-21 02:29:09 +03:00
data: calloc(nr_repeats * elm_size)
2019-06-22 21:20:28 +03:00
}
for i := 0; i < nr_repeats; i++ {
C.memcpy(arr.data + i * elm_size, val, elm_size)
}
return arr
}
2019-09-14 23:48:30 +03:00
pub fn (a array) repeat(nr_repeats int) array {
arr := array {
len: nr_repeats
cap: nr_repeats
element_size: a.element_size
2019-09-21 02:29:09 +03:00
data: calloc(nr_repeats * a.element_size)
2019-09-14 23:48:30 +03:00
}
val := a.data + 0 //nr_repeats * a.element_size
for i := 0; i < nr_repeats; i++ {
C.memcpy(arr.data + i * a.element_size, val, a.element_size)
}
return arr
}
pub fn (a mut array) sort_with_compare(compare voidptr) {
2019-06-22 21:20:28 +03:00
C.qsort(a.data, a.len, a.element_size, compare)
}
pub fn (a mut array) insert(i int, val voidptr) {
2019-06-22 21:20:28 +03:00
if i >= a.len {
panic('array.insert: index larger than length')
}
2019-10-10 01:15:19 +03:00
a.push(val)
2019-06-22 21:20:28 +03:00
size := a.element_size
C.memmove(a.data + (i + 1) * size, a.data + i * size, (a.len - i) * size)
a.set(i, val)
}
pub fn (a mut array) prepend(val voidptr) {
2019-06-22 21:20:28 +03:00
a.insert(0, val)
}
pub fn (a mut array) delete(idx int) {
2019-06-22 21:20:28 +03:00
size := a.element_size
C.memmove(a.data + idx * size, a.data + (idx + 1) * size, (a.len - idx) * size)
a.len--
a.cap--
}
fn (a array) get(i int) voidptr {
if i < 0 || i >= a.len {
panic('array index out of range: $i/$a.len')
}
return a.data + i * a.element_size
}
pub fn (a array) first() voidptr {
2019-06-22 21:20:28 +03:00
if a.len == 0 {
panic('array.first: empty array')
}
return a.data + 0
}
pub fn (a array) last() voidptr {
2019-06-22 21:20:28 +03:00
if a.len == 0 {
panic('array.last: empty array')
}
return a.data + (a.len - 1) * a.element_size
}
pub fn (s array) left(n int) array {
2019-06-22 21:20:28 +03:00
if n >= s.len {
return s
}
return s.slice(0, n)
}
pub fn (s array) right(n int) array {
2019-06-22 21:20:28 +03:00
if n >= s.len {
return s
}
return s.slice(n, s.len)
}
pub fn (s array) slice(start, _end int) array {
mut end := _end
if start > end {
panic('invalid slice index: $start > $end')
}
if end > s.len {
2019-08-31 02:35:05 +03:00
panic('runtime error: slice bounds out of range ($end >= $s.len)')
}
2019-08-31 02:35:05 +03:00
if start < 0 {
panic('runtime error: slice bounds out of range ($start < 0)')
2019-06-22 21:20:28 +03:00
}
l := end - start
res := array {
element_size: s.element_size
data: s.data + start * s.element_size
len: l
cap: l
2019-08-31 02:35:05 +03:00
//is_slice: true
2019-06-22 21:20:28 +03:00
}
return res
}
2019-08-31 02:35:05 +03:00
fn (a mut array) set(idx int, val voidptr) {
2019-06-22 21:20:28 +03:00
if idx < 0 || idx >= a.len {
panic('array index out of range: $idx / $a.len')
}
C.memcpy(a.data + a.element_size * idx, val, a.element_size)
}
fn (arr mut array) push(val voidptr) {
if arr.len >= arr.cap - 1 {
cap := (arr.len + 1) * 2
// println('_push: realloc, new cap=$cap')
if arr.cap == 0 {
arr.data = calloc(cap * arr.element_size)
}
else {
arr.data = C.realloc(arr.data, cap * arr.element_size)
}
arr.cap = cap
}
C.memcpy(arr.data + arr.element_size * arr.len, val, arr.element_size)
arr.len++
}
// `val` is array.data
// TODO make private, right now it's used by strings.Builder
pub fn (arr mut array) push_many(val voidptr, size int) {
if arr.len >= arr.cap - size {
cap := (arr.len + size) * 2
// println('_push: realloc, new cap=$cap')
if arr.cap == 0 {
arr.data = calloc(cap * arr.element_size)
}
else {
arr.data = C.realloc(arr.data, cap * arr.element_size)
}
arr.cap = cap
}
C.memcpy(arr.data + arr.element_size * arr.len, val, arr.element_size * size)
arr.len += size
}
2019-07-17 19:17:07 +03:00
pub fn (a array) reverse() array {
arr := array {
len: a.len
cap: a.cap
element_size: a.element_size
2019-09-21 02:29:09 +03:00
data: calloc(a.cap * a.element_size)
2019-07-17 19:17:07 +03: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
}
2019-08-12 14:41:34 +03:00
pub fn (a array) clone() array {
arr := array {
len: a.len
cap: a.cap
element_size: a.element_size
2019-09-21 02:29:09 +03:00
data: calloc(a.cap * a.element_size)
2019-08-12 14:41:34 +03:00
}
2019-08-31 02:35:05 +03:00
C.memcpy(arr.data, a.data, a.cap * a.element_size)
return arr
}
2019-08-12 14:41:34 +03:00
//pub fn (a []int) free() {
pub fn (a array) free() {
2019-07-21 16:05:47 +03:00
//if a.is_slice {
2019-08-31 02:35:05 +03:00
//return
//}
2019-06-22 21:20:28 +03:00
C.free(a.data)
}
// "[ '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('[')
2019-06-22 21:20:28 +03:00
for i := 0; i < a.len; i++ {
val := a[i]
2019-09-14 23:48:30 +03:00
sb.write('"')
sb.write(val)
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
}
2019-07-15 18:49:01 +03: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++ {
ptr += C.sprintf(*char(ptr), '%02x', b[i])
2019-07-15 18:49:01 +03:00
}
return string(hex)
}
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 {
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 20:55:34 +03:00
2019-09-01 22:51:16 +03:00
fn compare_ints(a, b &int) int {
2019-09-01 20:55:34 +03:00
if a < b {
return -1
}
if a > b {
return 1
}
return 0
}
pub fn (a mut []int) sort() {
a.sort_with_compare(compare_ints)
}
2019-10-04 23:07:19 +03:00
// Looking for an array index based on value.
2019-10-04 23:07:19 +03:00
// If there is, it will return the index and if not, it will return `-1`
// TODO: Implement for all types
pub fn (a []string) index(v string) int {
for i := 0; i < a.len; i++ {
if a[i] == v {
return i
}
}
return -1
}
pub fn (a []int) index(v int) int {
for i := 0; i < a.len; i++ {
if a[i] == v {
return i
}
}
return -1
}
pub fn (a []byte) index(v byte) int {
for i := 0; i < a.len; i++ {
if a[i] == v {
return i
}
}
return -1
}
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 13:23:17 +03:00
////////////// FILTER //////////////
// Creates a new array with all elements that pass the test implemented by the provided function.
pub fn (a []string) filter(predicate fn(p_val string, p_i int, p_arr []string) bool) []string
{
mut res := []string
for i := 0; i < a.len; i++ {
if predicate(a[i], i, a) {
res << a[i]
}
}
return res
}
pub fn (a []int) filter(predicate fn(p_val int, p_i int, p_arr []int) bool) []int
{
mut res := []int
for i := 0; i < a.len; i++ {
if predicate(a[i], i, a) {
res << a[i]
}
}
return res
}