From c20924140cf8cb4d79c2ff102fb98defa549561d Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 14:59:32 +0100 Subject: [PATCH] Vec.pop: the last element, taken off As List.pop does for a list; it keeps the memory, as resize does. Tested in VecTest, documented in raz-lib.md; its proposal is removed. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 5f19637..b4c48f2 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -649,26 +649,3 @@ Met in rrem's `RremUcd`, whose tables `tools/Ucd.raz` writes: before the tool's first run, each table needs a placeholder of one element, which the code reading it must then not take for a real one. - -## `Vec.pop` - -`Vec` can push but not pop: taking the last element off is two calls -and an index written out, - -``` -b := Backs.all(ADR(re^.stack))[re^.stack.len - 1]; -Backs.resize(ADR(re^.stack), re^.stack.len - 1); -``` - -where a stack wants one. The proposal, in `Vec`, beside `push`: - -``` -proc pop*(v: ^Vec): (T, BOOL); (* the last element, taken off; false if the array is empty *) -``` - -as `List.pop` gives the front of a list. It keeps the memory, as -`resize` does. It breaks no program, so it could come with any -release, not only a thaw. - -Met in rrem's machine, whose backtracking stack is a `Vec` popped in -`back` and `unwind`. diff --git a/doc/raz-lib.md b/doc/raz-lib.md index c2f6bee..a1d98f2 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -634,6 +634,7 @@ proc init*(v: ^Vec; a: ^Mem.Allocator); 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 pop*(v: ^Vec): (T, BOOL); (* the last element, taken off; false if empty *) 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 *) @@ -678,7 +679,8 @@ doubles when it is full, so `push` costs the same on average however long it grows. `all` gives the elements as a slice, indexed and checked as any is, and `v.len` is their number; the slice, and what -`add` gives, are good until the array next grows. `push`, `add` and +`add` gives, are good until the array next grows. `pop` takes the last +element off, keeping the memory. `push`, `add` and `resize` stop the program, as `Buf.add` does, if the allocator has no room; `room` asks first: `import Lines := Vec([]U8);`. `Fmt`'s format takes the next argument at each `{}`; `{{` and `}}` stand for `{` and `}`. A `Map` hashes and diff --git a/lib/Vec.raz b/lib/Vec.raz index d352979..f6594ce 100644 --- a/lib/Vec.raz +++ b/lib/Vec.raz @@ -66,6 +66,16 @@ INC(v^.len); end; +(* The last element, taken off; false if the array is empty. It keeps the + memory, as resize does. *) +proc pop*(v: ^Vec): (T, BOOL); +var x: T; +begin + if v^.len = 0 then return x, false; end; + DEC(v^.len); + return v^.data[v^.len], true; +end; + (* Adds an element at the end, zero, and gives it, to be filled in place: for a big T, which push would copy. Good until the array next grows. *) diff --git a/test/run/VecTest.expect b/test/run/VecTest.expect index bb466c2..30f50fd 100644 --- a/test/run/VecTest.expect +++ b/test/run/VecTest.expect @@ -2,5 +2,7 @@ 10000 1 10000 50005000 5 3 0 0 1 7 +7 true 0 +0 false 0 101 9801 -1 last diff --git a/test/run/VecTest.raz b/test/run/VecTest.raz index d365d51..03f3406 100644 --- a/test/run/VecTest.raz +++ b/test/run/VecTest.raz @@ -1,4 +1,4 @@ -(* Vec: pushed, added in place, resized both ways, emptied, given back; +(* Vec: pushed, added in place, resized both ways, popped, emptied, given back; of integers and of records, which grow past the first block. *) module VecTest; import Fmt, Heap, Ints := Vec(INT), Pts := Vec(Point); @@ -6,7 +6,7 @@ type Point = record x, y: INT; name: []U8; end; proc main*; -var v: Ints.Vec; w: Pts.Vec; i, sum: INT; p: ^Point; s: []INT; +var v: Ints.Vec; w: Pts.Vec; i, sum, top: INT; ok: BOOL; p: ^Point; s: []INT; begin Ints.init(ADR(v), Heap.heap); Fmt.print("{} {}\n", [Fmt.int(v.len), Fmt.int(LEN(Ints.all(ADR(v))))]); @@ -22,6 +22,10 @@ Ints.reset(ADR(v)); Ints.push(ADR(v), 7); Fmt.print("{} {}\n", [Fmt.int(v.len), Fmt.int(Ints.all(ADR(v))[0])]); + top, ok := Ints.pop(ADR(v)); + Fmt.print("{} {} {}\n", [Fmt.int(top), Fmt.bool(ok), Fmt.int(v.len)]); + top, ok := Ints.pop(ADR(v)); + Fmt.print("{} {}\n", [Fmt.int(top), Fmt.bool(ok)]); Ints.done(ADR(v)); Fmt.print("{}\n", [Fmt.int(v.len)]);