diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-03-03 18:12:55 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-03-03 18:12:55 -0500 |
commit | 9178cac428c188acc096984e704d5707bdab0a35 (patch) | |
tree | 7127671f7ce61041625881a540a550217fe26c51 /std | |
parent | abbd3d0e252d3694edb151ad2b5010cc2c5c4736 (diff) |
fixing const param bug
Diffstat (limited to 'std')
-rw-r--r-- | std/arr.toc | 43 |
1 files changed, 26 insertions, 17 deletions
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 }; |