2020-02-03 07:00:36 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-09-14 23:48:30 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
module builtin
|
|
|
|
|
|
|
|
import strings
|
|
|
|
|
2019-10-24 22:58:01 +03:00
|
|
|
pub struct array {
|
2019-09-14 23:48:30 +03:00
|
|
|
pub:
|
|
|
|
data voidptr
|
|
|
|
len int
|
|
|
|
cap int
|
|
|
|
element_size int
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-04-26 17:25:54 +03:00
|
|
|
// Private function, used by V (`nums := []int{}`)
|
2019-09-14 23:48:30 +03:00
|
|
|
fn new_array(mylen, cap, elm_size int) array {
|
|
|
|
arr := array {
|
|
|
|
len: mylen
|
|
|
|
cap: cap
|
|
|
|
element_size: elm_size
|
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
pub fn _make(len, cap, elm_size int) array {
|
|
|
|
return new_array(len, cap, elm_size)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
2019-10-05 08:03:26 +03:00
|
|
|
|
2019-10-09 23:38:33 +03:00
|
|
|
pub fn make(len, cap, elm_size int) array {
|
2019-10-05 08:03:26 +03:00
|
|
|
return array{}
|
|
|
|
}
|
|
|
|
|
2019-09-14 23:48:30 +03:00
|
|
|
fn array_repeat(val voidptr, nr_repeats, elm_size int) array {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2019-09-19 05:22:24 +03:00
|
|
|
pub fn (a array) repeat(nr_repeats int) array {
|
2019-09-14 23:48:30 +03:00
|
|
|
#return Array(a[0]).fill(nr_repeats)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut a array) sort_with_compare(compare voidptr) {
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut a array) insert(i int, val voidptr) {
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut a array) prepend(val voidptr) {
|
2019-09-14 23:48:30 +03:00
|
|
|
a.insert(0, val)
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut a array) delete_elm(idx int) {
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
pub fn (a array) first() voidptr {
|
|
|
|
if a.len == 0 {
|
|
|
|
panic('array.first: empty array')
|
|
|
|
}
|
|
|
|
return a.data + 0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (a array) last() voidptr {
|
|
|
|
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 {
|
|
|
|
if n >= s.len {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return s.slice(0, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (s array) right(n int) array {
|
|
|
|
if n >= s.len {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return s.slice(n, s.len)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (s array) slice(start, _end int) array {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (a array) reverse() array {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2020-05-06 19:03:44 +03:00
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
2019-09-14 23:48:30 +03:00
|
|
|
pub fn (a array) clone() array {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2020-05-06 19:03:44 +03:00
|
|
|
pub fn (a &array) free() {
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// "[ 'a', 'b', 'c' ]"
|
|
|
|
pub fn (a []string) str() string {
|
|
|
|
mut sb := strings.new_builder(a.len * 3)
|
|
|
|
sb.write('[')
|
2020-02-24 19:55:16 +03:00
|
|
|
for i in 0..a.len {
|
2019-09-14 23:48:30 +03:00
|
|
|
val := a[i]
|
|
|
|
sb.write('"')
|
|
|
|
sb.write(val)
|
|
|
|
sb.write('"')
|
|
|
|
if i < a.len - 1 {
|
|
|
|
sb.write(', ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sb.write(']')
|
|
|
|
return sb.str()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (b []byte) hex() string {
|
|
|
|
return 'sdf'
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut arr array) push_many(val voidptr, size int) {
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn free(voidptr) {
|
|
|
|
|
2020-04-26 17:25:54 +03:00
|
|
|
}
|