array/map: remove _get(), _push() etc

pull/2272/head
Alexander Medvednikov 2019-10-10 01:15:19 +03:00
parent cdfb7428c8
commit 1f6535afb0
3 changed files with 4 additions and 58 deletions

View File

@ -94,7 +94,7 @@ pub fn (a mut array) insert(i int, val voidptr) {
if i >= a.len {
panic('array.insert: index larger than length')
}
a._push(val)
a.push(val)
size := a.element_size
C.memmove(a.data + (i + 1) * size, a.data + i * size, (a.len - i) * size)
a.set(i, val)
@ -118,13 +118,6 @@ fn (a array) get(i int) voidptr {
return a.data + i * a.element_size
}
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 {
if a.len == 0 {
panic('array.first: empty array')
@ -198,40 +191,6 @@ fn (arr mut array) push(val voidptr) {
arr.len++
}
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
}
// `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) {

View File

@ -36,7 +36,7 @@ fn new_map_init(cap, elm_size int, keys &string, vals voidptr) map {
root: 0
}
for i in 0 .. cap {
res._set(keys[i], vals + i * elm_size)
res.set(keys[i], vals + i * elm_size)
}
return res
}
@ -116,15 +116,6 @@ fn (n & mapnode) find2(key string, element_size int) bool{
}
}
fn (m mut map) _set(key string, val voidptr) {
if isnil(m.root) {
m.root = new_node(key, val, m.element_size)
m.size++
return
}
m.insert(mut m.root, key, val)
}
fn (m mut map) set(key string, val voidptr) {
if isnil(m.root) {
m.root = new_node(key, val, m.element_size)
@ -223,10 +214,6 @@ fn (m map) exists(key string) bool {
return !isnil(m.root) && m.root.find2(key, m.element_size)
}
fn (m map) _exists(key string) bool {
return !isnil(m.root) && m.root.find2(key, m.element_size)
}
pub fn (m map) print() {
println('<<<<<<<<')
//for i := 0; i < m.entries.len; i++ {

View File

@ -18,13 +18,13 @@ pub fn new_builder(initial_size int) Builder {
}
pub fn (b mut Builder) write(s string) {
b.buf._push_many(s.str, s.len)
b.buf.push_many(s.str, s.len)
//b.buf << []byte(s) // TODO
b.len += s.len
}
pub fn (b mut Builder) writeln(s string) {
b.buf._push_many(s.str, s.len)
b.buf.push_many(s.str, s.len)
//b.buf << []byte(s) // TODO
b.buf << `\n`
b.len += s.len + 1