From 9178cac428c188acc096984e704d5707bdab0a35 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Tue, 3 Mar 2020 18:12:55 -0500 Subject: fixing const param bug --- std/arr.toc | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) (limited to 'std') diff --git a/std/arr.toc b/std/arr.toc index 24fdf9f..779de48 100644 --- a/std/arr.toc +++ b/std/arr.toc @@ -1,20 +1,29 @@ -Arr ::= fn (t :: Type) Type { - struct { - data : []t; - cap : int; +// this could be made quite a bit faster with realloc + +Arr ::= struct (t :: Type) { + data : []t; + cap : int; +}; + +resv ::= fn(t ::=, a : &Arr(t), n: int) { + if a.cap >= n { + return; + } + new_data := new(t, a.cap); + new_data.len = a.data.len; + for x, i := &new_data { + *x = a.data[i]; } + a.data = new_data; + a.cap = n; }; -arr_add ::= fn(t ::=, a : &Arr(t), x : t) { - if a.data.len >= a.cap { - a.cap = a.cap * 2 + 2; - new_data := new(t, a.cap); - new_data.len = a.data.len; - for i := 0..a.data.len-1 { - new_data[i] = a.data[i]; - } - a.data = new_data; - } - a.data.len += 1; - a.data[a.data.len-1] = x; + +add ::= fn(t ::=, a : &Arr(t), x : t) { + if a.data.len >= a.cap { + resv(&a, a.cap * 2 + 2); + } + a.data.len += 1; + a.data[a.data.len-1] = x; }; -arr_len ::= fn(t ::=, a : Arr(t)) int { a.data.len }; + +len ::= fn(t ::=, a : Arr(t)) int { a.data.len }; -- cgit v1.2.3