| 1 | (* Vec: pushed, added in place, resized both ways, popped, emptied, given back; |
| 2 | of integers and of records, which grow past the first block. *) |
| 3 | module VecTest; |
| 4 | import Fmt, Heap, Ints := Vec(INT), Pts := Vec(Point); |
| 5 | |
| 6 | type Point = record x, y: INT; name: []const U8; end; |
| 7 | |
| 8 | proc main*; |
| 9 | var v: Ints.Vec; w: Pts.Vec; i, sum, top: INT; ok: BOOL; p: ^Point; s: []INT; |
| 10 | begin |
| 11 | Ints.init(v, Heap.heap); |
| 12 | Fmt.print("{} {}\n", [Fmt.int(v.len), Fmt.int(LEN(Ints.all(v)))]); |
| 13 | for i := 1 to 10000 do Ints.push(v, i); end; |
| 14 | s := Ints.all(v); |
| 15 | sum := 0; |
| 16 | for i := 0 to LEN(s) - 1 do sum := sum + s[i]; end; |
| 17 | Fmt.print("{} {} {} {}\n", [Fmt.int(v.len), Fmt.int(s[0]), Fmt.int(s[9999]), Fmt.int(sum)]); |
| 18 | Ints.resize(v, 3); |
| 19 | Ints.resize(v, 5); |
| 20 | s := Ints.all(v); |
| 21 | Fmt.print("{} {} {} {}\n", [Fmt.int(LEN(s)), Fmt.int(s[2]), Fmt.int(s[3]), Fmt.int(s[4])]); |
| 22 | Ints.reset(v); |
| 23 | Ints.push(v, 7); |
| 24 | Fmt.print("{} {}\n", [Fmt.int(v.len), Fmt.int(Ints.all(v)[0])]); |
| 25 | top, ok := Ints.pop(v); |
| 26 | Fmt.print("{} {} {}\n", [Fmt.int(top), Fmt.bool(ok), Fmt.int(v.len)]); |
| 27 | top, ok := Ints.pop(v); |
| 28 | Fmt.print("{} {}\n", [Fmt.int(top), Fmt.bool(ok)]); |
| 29 | Ints.done(v); |
| 30 | Fmt.print("{}\n", [Fmt.int(v.len)]); |
| 31 | |
| 32 | Pts.init(w, Heap.heap); |
| 33 | for i := 0 to 99 do |
| 34 | p := Pts.add(w); |
| 35 | p^.x := i; p^.y := i * i; |
| 36 | end; |
| 37 | Pts.push(w, {x: -1, y: -2, name: "last"}); |
| 38 | Fmt.print("{} {} {} {}\n", [Fmt.int(w.len), Fmt.int(Pts.all(w)[99].y), |
| 39 | Fmt.int(Pts.all(w)[100].x), Fmt.str(Pts.all(w)[100].name)]); |
| 40 | Pts.done(w); |
| 41 | end; |
| 42 | |
| 43 | end VecTest. |