| 1 | (* Vec's tryPush and tryAdd: false when the allocator runs out, the |
| 2 | array as it was, and the program going on. *) |
| 3 | module VecTry; |
| 4 | import Arena, Fmt, Rt, Ints := Vec(INT); |
| 5 | import SYSTEM; |
| 6 | |
| 7 | proc main*; |
| 8 | var ar: Arena.Arena; v: Ints.Vec; n: INT; p: ^INT; ok: BOOL; |
| 9 | begin |
| 10 | if not Arena.init(ar, 4096) then Rt.stop("no arena"); end; |
| 11 | Ints.init(v, SYSTEM.ADR(ar.base)); |
| 12 | n := 0; |
| 13 | while Ints.tryPush(v, n) do INC(n); end; |
| 14 | p, ok := Ints.tryAdd(v); |
| 15 | Fmt.print("{} {} {} {}\n", [Fmt.int(n), Fmt.int(v.len), Fmt.bool(ok), Fmt.bool(p = nil)]); |
| 16 | Ints.reset(v); |
| 17 | p, ok := Ints.tryAdd(v); |
| 18 | p^ := 5; |
| 19 | Fmt.print("{} {} {}\n", [Fmt.bool(ok), Fmt.int(v.len), Fmt.int(Ints.all(v)[0])]); |
| 20 | end; |
| 21 | |
| 22 | end VecTry. |