raz / commit c2092414

commit
c20924140cf8cb4d79c2ff102fb98defa549561d
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
1a6a8fe0
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.

patch · log

5 files changed, +21 -26

PROPOSALS.md modified +0 -23

@@ -649,26 +649,3 @@
649649 Met in rrem's `RremUcd`, whose tables `tools/Ucd.raz` writes: before
650650 the tool's first run, each table needs a placeholder of one element,
651651 which the code reading it must then not take for a real one.
652-
653-## `Vec.pop`
654-
655-`Vec` can push but not pop: taking the last element off is two calls
656-and an index written out,
657-
658-```
659-b := Backs.all(ADR(re^.stack))[re^.stack.len - 1];
660-Backs.resize(ADR(re^.stack), re^.stack.len - 1);
661-```
662-
663-where a stack wants one. The proposal, in `Vec`, beside `push`:
664-
665-```
666-proc pop*(v: ^Vec): (T, BOOL); (* the last element, taken off; false if the array is empty *)
667-```
668-
669-as `List.pop` gives the front of a list. It keeps the memory, as
670-`resize` does. It breaks no program, so it could come with any
671-release, not only a thaw.
672-
673-Met in rrem's machine, whose backtracking stack is a `Vec` popped in
674-`back` and `unwind`.

doc/raz-lib.md (history) modified +3 -1

@@ -634,6 +634,7 @@
634634 proc init*(v: ^Vec; a: ^Mem.Allocator);
635635 proc room*(v: ^Vec; n: INT): BOOL; (* room for n more; false if the allocator has none *)
636636 proc push*(v: ^Vec; x: T); (* x added at the end *)
637+proc pop*(v: ^Vec): (T, BOOL); (* the last element, taken off; false if empty *)
637638 proc add*(v: ^Vec): ^T; (* a new element at the end, zero, to fill in place *)
638639 proc tryPush*(v: ^Vec; x: T): BOOL; (* push; false, and v as it was, if there is no room *)
639640 proc tryAdd*(v: ^Vec): (^T, BOOL); (* add; nil and false, and v as it was, if there is no room *)
@@ -678,7 +679,8 @@
678679 doubles when it is full, so `push` costs the same on average however
679680 long it grows. `all` gives the elements as a slice, indexed and
680681 checked as any is, and `v.len` is their number; the slice, and what
681-`add` gives, are good until the array next grows. `push`, `add` and
682+`add` gives, are good until the array next grows. `pop` takes the last
683+element off, keeping the memory. `push`, `add` and
682684 `resize` stop the program, as `Buf.add` does, if the allocator has no
683685 room; `room` asks first: `import Lines := Vec([]U8);`. `Fmt`'s format takes the next argument at each
684686 `{}`; `{{` and `}}` stand for `{` and `}`. A `Map` hashes and

lib/Vec.raz (history) modified +10 -0

@@ -66,6 +66,16 @@
6666 INC(v^.len);
6767 end;
6868
69+(* The last element, taken off; false if the array is empty. It keeps the
70+ memory, as resize does. *)
71+proc pop*(v: ^Vec): (T, BOOL);
72+var x: T;
73+begin
74+ if v^.len = 0 then return x, false; end;
75+ DEC(v^.len);
76+ return v^.data[v^.len], true;
77+end;
78+
6979 (* Adds an element at the end, zero, and gives it, to be filled in
7080 place: for a big T, which push would copy. Good until the array
7181 next grows. *)

test/run/VecTest.expect (history) modified +2 -0

@@ -2,5 +2,7 @@
22 10000 1 10000 50005000
33 5 3 0 0
44 1 7
5+7 true 0
6+0 false
57 0
68 101 9801 -1 last

test/run/VecTest.raz (history) modified +6 -2

@@ -1,4 +1,4 @@
1-(* Vec: pushed, added in place, resized both ways, emptied, given back;
1+(* Vec: pushed, added in place, resized both ways, popped, emptied, given back;
22 of integers and of records, which grow past the first block. *)
33 module VecTest;
44 import Fmt, Heap, Ints := Vec(INT), Pts := Vec(Point);
@@ -6,7 +6,7 @@
66 type Point = record x, y: INT; name: []U8; end;
77
88 proc main*;
9-var v: Ints.Vec; w: Pts.Vec; i, sum: INT; p: ^Point; s: []INT;
9+var v: Ints.Vec; w: Pts.Vec; i, sum, top: INT; ok: BOOL; p: ^Point; s: []INT;
1010 begin
1111 Ints.init(ADR(v), Heap.heap);
1212 Fmt.print("{} {}\n", [Fmt.int(v.len), Fmt.int(LEN(Ints.all(ADR(v))))]);
@@ -22,6 +22,10 @@
2222 Ints.reset(ADR(v));
2323 Ints.push(ADR(v), 7);
2424 Fmt.print("{} {}\n", [Fmt.int(v.len), Fmt.int(Ints.all(ADR(v))[0])]);
25+ top, ok := Ints.pop(ADR(v));
26+ Fmt.print("{} {} {}\n", [Fmt.int(top), Fmt.bool(ok), Fmt.int(v.len)]);
27+ top, ok := Ints.pop(ADR(v));
28+ Fmt.print("{} {}\n", [Fmt.int(top), Fmt.bool(ok)]);
2529 Ints.done(ADR(v));
2630 Fmt.print("{}\n", [Fmt.int(v.len)]);
2731