summaryrefslogtreecommitdiff
path: root/std/arr.toc
blob: fcf6a45dddc6f93983bc0cfb8e7b89cd858823aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// array package
pkg "arr";

#export Arr ::= fn (t :: Type) Type {
	struct {
		   data : []t;
		   len, cap : int;
	}
};
#export arr_add ::= fn(t ::=, a : &Arr(t), x : t) {
		if a.len >= a.cap {
		   a.cap = a.cap * 2 + 2;
		   new_data := new(t, a.cap);
		   each i := 0..a.len-1 {
		   		new_data[i] = a.data[i];
		   }
		   a.data = new_data;
		}
		a.data[a.len] = x;
		a.len += 1;
};

#export arr_foreach ::= fn(t ::=, a : Arr(t), f : fn(&t)) {
		each i := 0..a.len-1 {
			 f(&a.data[i]);
		}
};