Vec: tryPush and tryAdd room and push, or add, in one call, so that a library returning an error when memory runs out can't leave the check out. Library growth that breaks no program, met in rrem.
| @@ -583,27 +583,6 @@ | ||
| 583 | 583 | |
| 584 | 584 | Met in rrem's parser, documenting `(?>x*)`. |
| 585 | 585 | |
| 586 | -## Adding to a `Vec` without stopping the program | |
| 587 | - | |
| 588 | -`Vec.push` and `Vec.add` stop the program when memory runs out. A | |
| 589 | -library that returns an error such as `NoRoom` instead must call | |
| 590 | -`Vec.room(v, 1)` before each one, and the two are apart, so the check | |
| 591 | -is easy to leave out: rrem's parser left it out once, and a pattern | |
| 592 | -too big for memory would have stopped the program, not returned the | |
| 593 | -error. | |
| 594 | - | |
| 595 | -The proposal, in `Vec`: | |
| 596 | - | |
| 597 | -``` | |
| 598 | -proc tryPush*(v: ^Vec; x: T): BOOL; (* x added at the end; false, and v as it was, if there is no room *) | |
| 599 | -proc tryAdd*(v: ^Vec): (^T, BOOL); (* a new element at the end, to fill; false if there is no room *) | |
| 600 | -``` | |
| 601 | - | |
| 602 | -They are `room` and `push` (or `add`) in one call. It breaks no | |
| 603 | -program, so it could come with any release. | |
| 604 | - | |
| 605 | -Met in rrem's parser, which builds its tree in a `Vec`. | |
| 606 | - | |
| 607 | 586 | ## A line to standard error |
| 608 | 587 | |
| 609 | 588 | A message to standard error takes a `Buf`, `Fmt.write` into it, |
| @@ -618,6 +618,8 @@ | ||
| 618 | 618 | proc room*(v: ^Vec; n: INT): BOOL; (* room for n more; false if the allocator has none *) |
| 619 | 619 | proc push*(v: ^Vec; x: T); (* x added at the end *) |
| 620 | 620 | proc add*(v: ^Vec): ^T; (* a new element at the end, zero, to fill in place *) |
| 621 | +proc tryPush*(v: ^Vec; x: T): BOOL; (* push; false, and v as it was, if there is no room *) | |
| 622 | +proc tryAdd*(v: ^Vec): (^T, BOOL); (* add; nil and false, and v as it was, if there is no room *) | |
| 621 | 623 | proc resize*(v: ^Vec; n: INT); (* n elements: cut short, or the new ones zero *) |
| 622 | 624 | proc all*(v: ^Vec): []T; (* the elements, until it next grows *) |
| 623 | 625 | proc reset*(v: ^Vec); (* emptied, its memory kept *) |
| @@ -5,7 +5,8 @@ | ||
| 5 | 5 | and index it as a slice. all gives that slice, checked as any is, |
| 6 | 6 | and good until the array next grows. Running out of memory stops |
| 7 | 7 | the program, as Buf.add does: an element dropped silently would be |
| 8 | - wrong data that nothing tells of. room asks first. *) | |
| 8 | + wrong data that nothing tells of. room asks first; tryPush and | |
| 9 | + tryAdd ask and add in one call. *) | |
| 9 | 10 | module Vec(T); |
| 10 | 11 | import Mem, Rt, Sys; |
| 11 | 12 | |
| @@ -75,6 +76,22 @@ | ||
| 75 | 76 | v^.data[v^.len] := zero; |
| 76 | 77 | INC(v^.len); |
| 77 | 78 | return v^.data + (v^.len - 1); |
| 79 | +end; | |
| 80 | + | |
| 81 | +(* push, or false, and the array as it was, if there is no room. *) | |
| 82 | +proc tryPush*(v: ^Vec; x: T): BOOL; | |
| 83 | +begin | |
| 84 | + if not room(v, 1) then return false; end; | |
| 85 | + v^.data[v^.len] := x; | |
| 86 | + INC(v^.len); | |
| 87 | + return true; | |
| 88 | +end; | |
| 89 | + | |
| 90 | +(* add, or nil and false, and the array as it was, if there is no room. *) | |
| 91 | +proc tryAdd*(v: ^Vec): (^T, BOOL); | |
| 92 | +begin | |
| 93 | + if not room(v, 1) then return nil, false; end; | |
| 94 | + return add(v), true; | |
| 78 | 95 | end; |
| 79 | 96 | |
| 80 | 97 | (* Makes the array n elements long: cut short, or grown with zeros. *) |
| @@ -0,0 +1,2 @@ | ||
| 1 | +256 256 false true | |
| 2 | +true 1 5 |
| @@ -0,0 +1,21 @@ | ||
| 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 | + | |
| 6 | +proc main*; | |
| 7 | +var ar: Arena.Arena; v: Ints.Vec; n: INT; p: ^INT; ok: BOOL; | |
| 8 | +begin | |
| 9 | + if not Arena.init(ADR(ar), 4096) then Rt.stop("no arena"); end; | |
| 10 | + Ints.init(ADR(v), ADR(ar.base)); | |
| 11 | + n := 0; | |
| 12 | + while Ints.tryPush(ADR(v), n) do INC(n); end; | |
| 13 | + p, ok := Ints.tryAdd(ADR(v)); | |
| 14 | + Fmt.print("{} {} {} {}\n", [Fmt.int(n), Fmt.int(v.len), Fmt.bool(ok), Fmt.bool(p = nil)]); | |
| 15 | + Ints.reset(ADR(v)); | |
| 16 | + p, ok := Ints.tryAdd(ADR(v)); | |
| 17 | + p^ := 5; | |
| 18 | + Fmt.print("{} {} {}\n", [Fmt.bool(ok), Fmt.int(v.len), Fmt.int(Ints.all(ADR(v))[0])]); | |
| 19 | +end; | |
| 20 | + | |
| 21 | +end VecTry. |