From 1f6535afb0e875d18b88a2346d5b0b56a63a5c3d Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 10 Oct 2019 01:15:19 +0300 Subject: [PATCH] array/map: remove _get(), _push() etc --- vlib/builtin/array.v | 43 +--------------------------------------- vlib/builtin/map.v | 15 +------------- vlib/strings/builder_c.v | 4 ++-- 3 files changed, 4 insertions(+), 58 deletions(-) diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 01c7f4d100..34e0a330a6 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -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) { diff --git a/vlib/builtin/map.v b/vlib/builtin/map.v index 7f6cb6c16e..55e75bf969 100644 --- a/vlib/builtin/map.v +++ b/vlib/builtin/map.v @@ -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++ { diff --git a/vlib/strings/builder_c.v b/vlib/strings/builder_c.v index 10d68d2c9c..b26ca5e4ec 100644 --- a/vlib/strings/builder_c.v +++ b/vlib/strings/builder_c.v @@ -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