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.
| @@ -649,26 +649,3 @@ | ||
| 649 | 649 | Met in rrem's `RremUcd`, whose tables `tools/Ucd.raz` writes: before |
| 650 | 650 | the tool's first run, each table needs a placeholder of one element, |
| 651 | 651 | 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`. |
| @@ -634,6 +634,7 @@ | ||
| 634 | 634 | proc init*(v: ^Vec; a: ^Mem.Allocator); |
| 635 | 635 | proc room*(v: ^Vec; n: INT): BOOL; (* room for n more; false if the allocator has none *) |
| 636 | 636 | 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 *) | |
| 637 | 638 | proc add*(v: ^Vec): ^T; (* a new element at the end, zero, to fill in place *) |
| 638 | 639 | proc tryPush*(v: ^Vec; x: T): BOOL; (* push; false, and v as it was, if there is no room *) |
| 639 | 640 | proc tryAdd*(v: ^Vec): (^T, BOOL); (* add; nil and false, and v as it was, if there is no room *) |
| @@ -678,7 +679,8 @@ | ||
| 678 | 679 | doubles when it is full, so `push` costs the same on average however |
| 679 | 680 | long it grows. `all` gives the elements as a slice, indexed and |
| 680 | 681 | 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 | |
| 682 | 684 | `resize` stop the program, as `Buf.add` does, if the allocator has no |
| 683 | 685 | room; `room` asks first: `import Lines := Vec([]U8);`. `Fmt`'s format takes the next argument at each |
| 684 | 686 | `{}`; `{{` and `}}` stand for `{` and `}`. A `Map` hashes and |
| @@ -66,6 +66,16 @@ | ||
| 66 | 66 | INC(v^.len); |
| 67 | 67 | end; |
| 68 | 68 | |
| 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 | + | |
| 69 | 79 | (* Adds an element at the end, zero, and gives it, to be filled in |
| 70 | 80 | place: for a big T, which push would copy. Good until the array |
| 71 | 81 | next grows. *) |
| @@ -2,5 +2,7 @@ | ||
| 2 | 2 | 10000 1 10000 50005000 |
| 3 | 3 | 5 3 0 0 |
| 4 | 4 | 1 7 |
| 5 | +7 true 0 | |
| 6 | +0 false | |
| 5 | 7 | 0 |
| 6 | 8 | 101 9801 -1 last |
| @@ -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; | |
| 2 | 2 | of integers and of records, which grow past the first block. *) |
| 3 | 3 | module VecTest; |
| 4 | 4 | import Fmt, Heap, Ints := Vec(INT), Pts := Vec(Point); |
| @@ -6,7 +6,7 @@ | ||
| 6 | 6 | type Point = record x, y: INT; name: []U8; end; |
| 7 | 7 | |
| 8 | 8 | 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; | |
| 10 | 10 | begin |
| 11 | 11 | Ints.init(ADR(v), Heap.heap); |
| 12 | 12 | Fmt.print("{} {}\n", [Fmt.int(v.len), Fmt.int(LEN(Ints.all(ADR(v))))]); |
| @@ -22,6 +22,10 @@ | ||
| 22 | 22 | Ints.reset(ADR(v)); |
| 23 | 23 | Ints.push(ADR(v), 7); |
| 24 | 24 | 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)]); | |
| 25 | 29 | Ints.done(ADR(v)); |
| 26 | 30 | Fmt.print("{}\n", [Fmt.int(v.len)]); |
| 27 | 31 |