From 2818832ec4b879b99ff75ccaa28555fe4ee86540 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 11:59:09 +0100 Subject: [PATCH] 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. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 95c113f..8ca650a 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -583,27 +583,6 @@ Met in rrem's parser, documenting `(?>x*)`. -## Adding to a `Vec` without stopping the program - -`Vec.push` and `Vec.add` stop the program when memory runs out. A -library that returns an error such as `NoRoom` instead must call -`Vec.room(v, 1)` before each one, and the two are apart, so the check -is easy to leave out: rrem's parser left it out once, and a pattern -too big for memory would have stopped the program, not returned the -error. - -The proposal, in `Vec`: - -``` -proc tryPush*(v: ^Vec; x: T): BOOL; (* x added at the end; false, and v as it was, if there is no room *) -proc tryAdd*(v: ^Vec): (^T, BOOL); (* a new element at the end, to fill; false if there is no room *) -``` - -They are `room` and `push` (or `add`) in one call. It breaks no -program, so it could come with any release. - -Met in rrem's parser, which builds its tree in a `Vec`. - ## A line to standard error A message to standard error takes a `Buf`, `Fmt.write` into it, diff --git a/doc/raz-lib.md b/doc/raz-lib.md index e1f5acb..0b389a8 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -618,6 +618,8 @@ proc room*(v: ^Vec; n: INT): BOOL; (* room for n more; false if the allocator has none *) proc push*(v: ^Vec; x: T); (* x added at the end *) proc add*(v: ^Vec): ^T; (* a new element at the end, zero, to fill in place *) +proc tryPush*(v: ^Vec; x: T): BOOL; (* push; false, and v as it was, if there is no room *) +proc tryAdd*(v: ^Vec): (^T, BOOL); (* add; nil and false, and v as it was, if there is no room *) proc resize*(v: ^Vec; n: INT); (* n elements: cut short, or the new ones zero *) proc all*(v: ^Vec): []T; (* the elements, until it next grows *) proc reset*(v: ^Vec); (* emptied, its memory kept *) diff --git a/lib/Vec.raz b/lib/Vec.raz index eacac39..d352979 100644 --- a/lib/Vec.raz +++ b/lib/Vec.raz @@ -5,7 +5,8 @@ and index it as a slice. all gives that slice, checked as any is, and good until the array next grows. Running out of memory stops the program, as Buf.add does: an element dropped silently would be - wrong data that nothing tells of. room asks first. *) + wrong data that nothing tells of. room asks first; tryPush and + tryAdd ask and add in one call. *) module Vec(T); import Mem, Rt, Sys; @@ -75,6 +76,22 @@ v^.data[v^.len] := zero; INC(v^.len); return v^.data + (v^.len - 1); +end; + +(* push, or false, and the array as it was, if there is no room. *) +proc tryPush*(v: ^Vec; x: T): BOOL; +begin + if not room(v, 1) then return false; end; + v^.data[v^.len] := x; + INC(v^.len); + return true; +end; + +(* add, or nil and false, and the array as it was, if there is no room. *) +proc tryAdd*(v: ^Vec): (^T, BOOL); +begin + if not room(v, 1) then return nil, false; end; + return add(v), true; end; (* Makes the array n elements long: cut short, or grown with zeros. *) diff --git a/test/run/VecTry.expect b/test/run/VecTry.expect new file mode 100644 index 0000000..588cf66 --- /dev/null +++ b/test/run/VecTry.expect @@ -0,0 +1,2 @@ +256 256 false true +true 1 5 diff --git a/test/run/VecTry.raz b/test/run/VecTry.raz new file mode 100644 index 0000000..a32fe06 --- /dev/null +++ b/test/run/VecTry.raz @@ -0,0 +1,21 @@ +(* Vec's tryPush and tryAdd: false when the allocator runs out, the + array as it was, and the program going on. *) +module VecTry; +import Arena, Fmt, Rt, Ints := Vec(INT); + +proc main*; +var ar: Arena.Arena; v: Ints.Vec; n: INT; p: ^INT; ok: BOOL; +begin + if not Arena.init(ADR(ar), 4096) then Rt.stop("no arena"); end; + Ints.init(ADR(v), ADR(ar.base)); + n := 0; + while Ints.tryPush(ADR(v), n) do INC(n); end; + p, ok := Ints.tryAdd(ADR(v)); + Fmt.print("{} {} {} {}\n", [Fmt.int(n), Fmt.int(v.len), Fmt.bool(ok), Fmt.bool(p = nil)]); + Ints.reset(ADR(v)); + p, ok := Ints.tryAdd(ADR(v)); + p^ := 5; + Fmt.print("{} {} {}\n", [Fmt.bool(ok), Fmt.int(v.len), Fmt.int(Ints.all(ADR(v))[0])]); +end; + +end VecTry.