diff options
Diffstat (limited to 'tests/std/arr.toc')
-rw-r--r-- | tests/std/arr.toc | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/std/arr.toc b/tests/std/arr.toc new file mode 100644 index 0000000..a2d202b --- /dev/null +++ b/tests/std/arr.toc @@ -0,0 +1,29 @@ +// 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; + } + a.cap = n; + 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; +}; + +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; +}; + +len ::= fn(t ::=, a : Arr(t)) int { a.data.len }; |