raz / commit 2818832e

commit
2818832ec4b879b99ff75ccaa28555fe4ee86540
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
f703dc3f
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.

patch · log

5 files changed, +43 -22

PROPOSALS.md modified +0 -21

@@ -583,27 +583,6 @@
583583
584584 Met in rrem's parser, documenting `(?>x*)`.
585585
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-
607586 ## A line to standard error
608587
609588 A message to standard error takes a `Buf`, `Fmt.write` into it,

doc/raz-lib.md (history) modified +2 -0

@@ -618,6 +618,8 @@
618618 proc room*(v: ^Vec; n: INT): BOOL; (* room for n more; false if the allocator has none *)
619619 proc push*(v: ^Vec; x: T); (* x added at the end *)
620620 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 *)
621623 proc resize*(v: ^Vec; n: INT); (* n elements: cut short, or the new ones zero *)
622624 proc all*(v: ^Vec): []T; (* the elements, until it next grows *)
623625 proc reset*(v: ^Vec); (* emptied, its memory kept *)

lib/Vec.raz (history) modified +18 -1

@@ -5,7 +5,8 @@
55 and index it as a slice. all gives that slice, checked as any is,
66 and good until the array next grows. Running out of memory stops
77 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. *)
910 module Vec(T);
1011 import Mem, Rt, Sys;
1112
@@ -75,6 +76,22 @@
7576 v^.data[v^.len] := zero;
7677 INC(v^.len);
7778 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;
7895 end;
7996
8097 (* Makes the array n elements long: cut short, or grown with zeros. *)

test/run/VecTry.expect (history) added +2 -0

@@ -0,0 +1,2 @@
1+256 256 false true
2+true 1 5

test/run/VecTry.raz (history) added +21 -0

@@ -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.