Vec, Map and List take what they work on by var, and const to read it Vec, Map and List take var v, m and l where they change them; Map.get, next and slot and Vec.all, which only read, take const. As values, a copy of four words at each call made a loop of Map.get and Vec.all half again as slow (650 ms against 410); as const they make the same code as the pointer did. STANDARDS.md and the report now say so: a value for a single value or a slice, const for a record or an array. Buf.bytes stays a value until the seed, which doesn't know const, is gone. A trace shows no var parameter, so VecFull's standard error loses its v = 0x? lines. Callers, volume 2 and the site's collections lesson say the new signatures.
| @@ -13,14 +13,16 @@ | ||
| 13 | 13 | |
| 14 | 14 | | The procedure | The parameter | Example | |
| 15 | 15 | |---|---|---| |
| 16 | -| reads it, a few words at most (a handle, a slice, a small record) | a value | `Buf.bytes(b: Buf)` | | |
| 17 | -| reads it, more than a few words | `const` | `Opt.number(const p: Parser; lo, hi: I64)` | | |
| 16 | +| reads it, a single value or a slice | a value | `Sys.write(fd: INT; s: []U8)` | | |
| 17 | +| reads it, a record or an array | `const` | `Map.get(const m: Map; k: K)` | | |
| 18 | 18 | | changes it, and lets go of it when it returns | `var` | `Buf.add(var b: Buf; c: U8)` | |
| 19 | 19 | | keeps it past the call, in a record, a module variable or another thread | a pointer, `^T` | `Thread.spawn(p, arg: ^U8)` | |
| 20 | 20 | |
| 21 | 21 | A `const` parameter is passed in place, as a `var` one is, so nothing |
| 22 | 22 | is copied; and since it can't be changed, `var` in a heading always |
| 23 | -means the procedure changes what it is given. | |
| 23 | +means the procedure changes what it is given. A record of only four | |
| 24 | +words passed as a value, copied at each call, made a loop of `Map.get` | |
| 25 | +and `Vec.all` half again as slow as `const` did (2026-10.33). | |
| 24 | 26 | |
| 25 | 27 | So `ADR` at a call says the callee keeps what it is given. A reader |
| 26 | 28 | who sees `Pg.start(q, ADR(c), sql)` knows that `q` is changed and `c` |
| @@ -648,34 +648,34 @@ | ||
| 648 | 648 | type |
| 649 | 649 | Node* = record next*: ^Node; val*: T; end; |
| 650 | 650 | List* = record head*: ^Node; len*: INT; ... end; |
| 651 | -proc init*(l: ^List; a: ^Mem.Allocator); | |
| 652 | -proc push*(l: ^List; v: T): BOOL; (* v at the front; false if the allocator has no room *) | |
| 653 | -proc pop*(l: ^List): (T, BOOL); (* the front value, taken off; false if the list is empty *) | |
| 654 | -proc reverse*(l: ^List); | |
| 655 | -proc clear*(l: ^List); | |
| 651 | +proc init*(var l: List; a: ^Mem.Allocator); | |
| 652 | +proc push*(var l: List; v: T): BOOL; (* v at the front; false if the allocator has no room *) | |
| 653 | +proc pop*(var l: List): (T, BOOL); (* the front value, taken off; false if the list is empty *) | |
| 654 | +proc reverse*(var l: List); | |
| 655 | +proc clear*(var l: List); | |
| 656 | 656 | |
| 657 | 657 | module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); |
| 658 | 658 | type Map* = record len*: INT; ... end; |
| 659 | -proc init*(m: ^Map; a: ^Mem.Allocator); | |
| 660 | -proc put*(m: ^Map; k: K; v: V): BOOL; (* k now maps to v; false if there was no room *) | |
| 661 | -proc remove*(m: ^Map; k: K): BOOL; (* false if k wasn't there *) | |
| 662 | -proc get*(m: ^Map; k: K): (V, BOOL); (* what k maps to, and whether it maps to anything *) | |
| 663 | -proc next*(m: ^Map; i: INT): (INT, K, V); (* the entry after i *) | |
| 664 | -proc done*(m: ^Map); (* its memory given back, leaving it empty *) | |
| 659 | +proc init*(var m: Map; a: ^Mem.Allocator); | |
| 660 | +proc put*(var m: Map; k: K; v: V): BOOL; (* k now maps to v; false if there was no room *) | |
| 661 | +proc remove*(var m: Map; k: K): BOOL; (* false if k wasn't there *) | |
| 662 | +proc get*(const m: Map; k: K): (V, BOOL); (* what k maps to, and whether it maps to anything *) | |
| 663 | +proc next*(const m: Map; i: INT): (INT, K, V); (* the entry after i *) | |
| 664 | +proc done*(var m: Map); (* its memory given back, leaving it empty *) | |
| 665 | 665 | |
| 666 | 666 | module Vec(T); |
| 667 | 667 | type Vec* = record len*: INT; ... end; |
| 668 | -proc init*(v: ^Vec; a: ^Mem.Allocator); | |
| 669 | -proc room*(v: ^Vec; n: INT): BOOL; (* room for n more; false if the allocator has none *) | |
| 670 | -proc push*(v: ^Vec; x: T); (* x added at the end *) | |
| 671 | -proc pop*(v: ^Vec): (T, BOOL); (* the last element, taken off; false if the array is empty *) | |
| 672 | -proc add*(v: ^Vec): ^T; (* a new element at the end, zero, to fill in place *) | |
| 673 | -proc tryPush*(v: ^Vec; x: T): BOOL; (* push; false, and v as it was, if there is no room *) | |
| 674 | -proc tryAdd*(v: ^Vec): (^T, BOOL); (* add; nil and false, and v as it was, if there is no room *) | |
| 675 | -proc resize*(v: ^Vec; n: INT); (* n elements: cut short, or the new ones zero *) | |
| 676 | -proc all*(v: ^Vec): []T; (* the elements, until it next grows *) | |
| 677 | -proc reset*(v: ^Vec); (* emptied, its memory kept *) | |
| 678 | -proc done*(v: ^Vec); (* its memory given back *) | |
| 668 | +proc init*(var v: Vec; a: ^Mem.Allocator); | |
| 669 | +proc room*(var v: Vec; n: INT): BOOL; (* room for n more; false if the allocator has none *) | |
| 670 | +proc push*(var v: Vec; x: T); (* x added at the end *) | |
| 671 | +proc pop*(var v: Vec): (T, BOOL); (* the last element, taken off; false if the array is empty *) | |
| 672 | +proc add*(var v: Vec): ^T; (* a new element at the end, zero, to fill in place *) | |
| 673 | +proc tryPush*(var v: Vec; x: T): BOOL; (* push; false, and v as it was, if there is no room *) | |
| 674 | +proc tryAdd*(var v: Vec): (^T, BOOL); (* add; nil and false, and v as it was, if there is no room *) | |
| 675 | +proc resize*(var v: Vec; n: INT); (* n elements: cut short, or the new ones zero *) | |
| 676 | +proc all*(const v: Vec): []T; (* the elements, until it next grows *) | |
| 677 | +proc reset*(var v: Vec); (* emptied, its memory kept *) | |
| 678 | +proc done*(var v: Vec); (* its memory given back *) | |
| 679 | 679 | |
| 680 | 680 | module Sort(T; less: proc(a, b: T): BOOL); |
| 681 | 681 | proc sort*(s: []T); |
| @@ -801,10 +801,10 @@ | ||
| 801 | 801 | sees each entry once if the map doesn't change during it: |
| 802 | 802 | |
| 803 | 803 | ``` |
| 804 | -i, k, v := Names.next(ADR(names), 0); | |
| 804 | +i, k, v := Names.next(names, 0); | |
| 805 | 805 | while i >= 0 do |
| 806 | 806 | Fmt.print("{} is {}\n", [Fmt.str(k), Fmt.int(v)]); |
| 807 | - i, k, v := Names.next(ADR(names), i); | |
| 807 | + i, k, v := Names.next(names, i); | |
| 808 | 808 | end; |
| 809 | 809 | ``` |
| 810 | 810 |
| @@ -553,9 +553,10 @@ | ||
| 553 | 553 | .Cm const |
| 554 | 554 | one is the caller's variable too, read in place and never changed, and |
| 555 | 555 | takes any expression of its type. |
| 556 | -A procedure takes what it reads as a value when it is a few words, as | |
| 556 | +A procedure takes what it reads as a value when it is a single value | |
| 557 | +or a slice, as | |
| 557 | 558 | .Cm const |
| 558 | -when it is more; what it changes as | |
| 559 | +when it is a record or an array; what it changes as | |
| 559 | 560 | .Cm var ; |
| 560 | 561 | and a pointer only for what it keeps. |
| 561 | 562 | .It |
| @@ -1461,10 +1461,10 @@ | ||
| 1461 | 1461 | end; |
| 1462 | 1462 | ``` |
| 1463 | 1463 | |
| 1464 | -A procedure takes what it reads as a value when it is a few words, and | |
| 1465 | -as `const` when it is more; what it changes, as `var`; and a pointer | |
| 1466 | -only for what it keeps past the call, what may be `nil`, an allocator, | |
| 1467 | -or what C or the system takes. | |
| 1464 | +A procedure takes what it reads as a value when it is a single value | |
| 1465 | +or a slice, and as `const` when it is a record or an array; what it | |
| 1466 | +changes, as `var`; and a pointer only for what it keeps past the call, | |
| 1467 | +what may be `nil`, an allocator, or what C or the system takes. | |
| 1468 | 1468 | |
| 1469 | 1469 | **Results.** A procedure returns nothing, one value, or several. A call |
| 1470 | 1470 | that returns several values can only stand on the right of a multiple |
| @@ -12,20 +12,20 @@ | ||
| 12 | 12 | counts: Count.Map; order: Names.List; n: ^Names.Node; |
| 13 | 13 | k, total: INT; found, _ok: BOOL; w, rest: []U8; |
| 14 | 14 | begin |
| 15 | - Count.init(ADR(counts), Heap.heap); | |
| 16 | - Names.init(ADR(order), Heap.heap); | |
| 15 | + Count.init(counts, Heap.heap); | |
| 16 | + Names.init(order, Heap.heap); | |
| 17 | 17 | w, rest := Str.field(Text); |
| 18 | 18 | while LEN(w) > 0 do |
| 19 | 19 | INC(total); |
| 20 | - k, found := Count.get(ADR(counts), w); | |
| 21 | - if not found then _ok := Names.push(ADR(order), w); end; | |
| 22 | - _ok := Count.put(ADR(counts), w, k + 1); | |
| 20 | + k, found := Count.get(counts, w); | |
| 21 | + if not found then _ok := Names.push(order, w); end; | |
| 22 | + _ok := Count.put(counts, w, k + 1); | |
| 23 | 23 | w, rest := Str.field(rest); |
| 24 | 24 | end; |
| 25 | - Names.reverse(ADR(order)); | |
| 25 | + Names.reverse(order); | |
| 26 | 26 | n := order.head; |
| 27 | 27 | while n # nil do |
| 28 | - k, found := Count.get(ADR(counts), n^.val); | |
| 28 | + k, found := Count.get(counts, n^.val); | |
| 29 | 29 | Fmt.print("{} {}\n", [Fmt.str(n^.val), Fmt.int(k)]); |
| 30 | 30 | n := n^.next; |
| 31 | 31 | end; |
| @@ -14,42 +14,42 @@ | ||
| 14 | 14 | end; |
| 15 | 15 | |
| 16 | 16 | (* An empty list, its nodes from a. *) |
| 17 | -proc init*(l: ^List; a: ^Mem.Allocator); | |
| 17 | +proc init*(var l: List; a: ^Mem.Allocator); | |
| 18 | 18 | begin |
| 19 | - l^ := {a: a}; | |
| 19 | + l := {a: a}; | |
| 20 | 20 | end; |
| 21 | 21 | |
| 22 | 22 | (* v at the front; false if the allocator has no room. *) |
| 23 | -proc push*(l: ^List; v: T): BOOL; | |
| 23 | +proc push*(var l: List; v: T): BOOL; | |
| 24 | 24 | var n: ^Node; |
| 25 | 25 | begin |
| 26 | - NEW(n, l^.a); | |
| 26 | + NEW(n, l.a); | |
| 27 | 27 | if n = nil then return false; end; |
| 28 | 28 | n^.val := v; |
| 29 | - n^.next := l^.head; | |
| 30 | - l^.head := n; | |
| 31 | - INC(l^.len); | |
| 29 | + n^.next := l.head; | |
| 30 | + l.head := n; | |
| 31 | + INC(l.len); | |
| 32 | 32 | return true; |
| 33 | 33 | end; |
| 34 | 34 | |
| 35 | 35 | (* The front value, taken off; false if the list is empty. *) |
| 36 | -proc pop*(l: ^List): (T, BOOL); | |
| 36 | +proc pop*(var l: List): (T, BOOL); | |
| 37 | 37 | var n: ^Node; v: T; |
| 38 | 38 | begin |
| 39 | - n := l^.head; | |
| 39 | + n := l.head; | |
| 40 | 40 | if n = nil then return v, false; end; |
| 41 | 41 | v := n^.val; |
| 42 | - l^.head := n^.next; | |
| 43 | - DEC(l^.len); | |
| 44 | - Mem.free(l^.a, ^U8(n)); | |
| 42 | + l.head := n^.next; | |
| 43 | + DEC(l.len); | |
| 44 | + Mem.free(l.a, ^U8(n)); | |
| 45 | 45 | return v, true; |
| 46 | 46 | end; |
| 47 | 47 | |
| 48 | 48 | (* The list backwards. *) |
| 49 | -proc reverse*(l: ^List); | |
| 49 | +proc reverse*(var l: List); | |
| 50 | 50 | var p, q, next: ^Node; |
| 51 | 51 | begin |
| 52 | - p := l^.head; | |
| 52 | + p := l.head; | |
| 53 | 53 | q := nil; |
| 54 | 54 | while p # nil do |
| 55 | 55 | next := p^.next; |
| @@ -57,14 +57,14 @@ | ||
| 57 | 57 | q := p; |
| 58 | 58 | p := next; |
| 59 | 59 | end; |
| 60 | - l^.head := q; | |
| 60 | + l.head := q; | |
| 61 | 61 | end; |
| 62 | 62 | |
| 63 | 63 | (* Takes every value off, giving the nodes back. *) |
| 64 | -proc clear*(l: ^List); | |
| 64 | +proc clear*(var l: List); | |
| 65 | 65 | var _ok: BOOL; _v: T; |
| 66 | 66 | begin |
| 67 | - while l^.head # nil do _v, _ok := pop(l); end; | |
| 67 | + while l.head # nil do _v, _ok := pop(l); end; | |
| 68 | 68 | end; |
| 69 | 69 | |
| 70 | 70 | end List. |
| @@ -18,98 +18,98 @@ | ||
| 18 | 18 | end; |
| 19 | 19 | |
| 20 | 20 | (* An empty map, its memory from a. *) |
| 21 | -proc init*(m: ^Map; a: ^Mem.Allocator); | |
| 21 | +proc init*(var m: Map; a: ^Mem.Allocator); | |
| 22 | 22 | begin |
| 23 | - m^ := {a: a}; | |
| 23 | + m := {a: a}; | |
| 24 | 24 | end; |
| 25 | 25 | |
| 26 | 26 | (* The slot of k, or of the empty slot where it would go. *) |
| 27 | -proc slot(m: ^Map; k: K): INT; | |
| 27 | +proc slot(const m: Map; k: K): INT; | |
| 28 | 28 | var i: INT; |
| 29 | 29 | begin |
| 30 | - i := INT(hash(k) & U64(m^.cap - 1)); | |
| 31 | - while m^.slots[i].used and not eq(m^.slots[i].key, k) do | |
| 32 | - i := (i + 1) & (m^.cap - 1); | |
| 30 | + i := INT(hash(k) & U64(m.cap - 1)); | |
| 31 | + while m.slots[i].used and not eq(m.slots[i].key, k) do | |
| 32 | + i := (i + 1) & (m.cap - 1); | |
| 33 | 33 | end; |
| 34 | 34 | return i; |
| 35 | 35 | end; |
| 36 | 36 | |
| 37 | -proc grow(m: ^Map): BOOL; | |
| 37 | +proc grow(var m: Map): BOOL; | |
| 38 | 38 | var old: ^Entry; n, i, j: INT; |
| 39 | 39 | begin |
| 40 | - old := m^.slots; | |
| 41 | - n := m^.cap; | |
| 42 | - if n = 0 then m^.cap := 16; else m^.cap := n * 2; end; | |
| 43 | - m^.slots := ^Entry(Mem.alloc(m^.a, m^.cap * SIZE(Entry), 8)); | |
| 44 | - if m^.slots = nil then m^.slots := old; m^.cap := n; return false; end; | |
| 45 | - for i := 0 to m^.cap - 1 do m^.slots[i].used := false; end; | |
| 40 | + old := m.slots; | |
| 41 | + n := m.cap; | |
| 42 | + if n = 0 then m.cap := 16; else m.cap := n * 2; end; | |
| 43 | + m.slots := ^Entry(Mem.alloc(m.a, m.cap * SIZE(Entry), 8)); | |
| 44 | + if m.slots = nil then m.slots := old; m.cap := n; return false; end; | |
| 45 | + for i := 0 to m.cap - 1 do m.slots[i].used := false; end; | |
| 46 | 46 | for i := 0 to n - 1 do |
| 47 | 47 | if old[i].used then |
| 48 | 48 | j := slot(m, old[i].key); |
| 49 | - m^.slots[j] := old[i]; | |
| 49 | + m.slots[j] := old[i]; | |
| 50 | 50 | end; |
| 51 | 51 | end; |
| 52 | - if old # nil then Mem.free(m^.a, ^U8(old)); end; | |
| 52 | + if old # nil then Mem.free(m.a, ^U8(old)); end; | |
| 53 | 53 | return true; |
| 54 | 54 | end; |
| 55 | 55 | |
| 56 | 56 | (* k now maps to v; false if there was no room. *) |
| 57 | -proc put*(m: ^Map; k: K; v: V): BOOL; | |
| 57 | +proc put*(var m: Map; k: K; v: V): BOOL; | |
| 58 | 58 | var i: INT; |
| 59 | 59 | begin |
| 60 | - if (m^.len + 1) * 4 > m^.cap * 3 then | |
| 60 | + if (m.len + 1) * 4 > m.cap * 3 then | |
| 61 | 61 | if not grow(m) then return false; end; |
| 62 | 62 | end; |
| 63 | 63 | i := slot(m, k); |
| 64 | - if not m^.slots[i].used then | |
| 65 | - m^.slots[i] := {key: k, used: true}; | |
| 66 | - INC(m^.len); | |
| 64 | + if not m.slots[i].used then | |
| 65 | + m.slots[i] := {key: k, used: true}; | |
| 66 | + INC(m.len); | |
| 67 | 67 | end; |
| 68 | - m^.slots[i].val := v; | |
| 68 | + m.slots[i].val := v; | |
| 69 | 69 | return true; |
| 70 | 70 | end; |
| 71 | 71 | |
| 72 | 72 | (* k maps to nothing; false if it didn't map to anything. The entries |
| 73 | 73 | after it that were pushed on past its slot move back, so that a |
| 74 | 74 | search for each still finds it before an empty slot. *) |
| 75 | -proc remove*(m: ^Map; k: K): BOOL; | |
| 75 | +proc remove*(var m: Map; k: K): BOOL; | |
| 76 | 76 | var i, j, home: INT; |
| 77 | 77 | begin |
| 78 | - if m^.cap = 0 then return false; end; | |
| 78 | + if m.cap = 0 then return false; end; | |
| 79 | 79 | i := slot(m, k); |
| 80 | - if not m^.slots[i].used then return false; end; | |
| 81 | - j := (i + 1) & (m^.cap - 1); | |
| 82 | - while m^.slots[j].used do | |
| 80 | + if not m.slots[i].used then return false; end; | |
| 81 | + j := (i + 1) & (m.cap - 1); | |
| 82 | + while m.slots[j].used do | |
| 83 | 83 | (* the entry at j may fill i if i lies between its home and j *) |
| 84 | - home := INT(hash(m^.slots[j].key) & U64(m^.cap - 1)); | |
| 85 | - if (j - home) & (m^.cap - 1) >= (j - i) & (m^.cap - 1) then | |
| 86 | - m^.slots[i] := m^.slots[j]; | |
| 84 | + home := INT(hash(m.slots[j].key) & U64(m.cap - 1)); | |
| 85 | + if (j - home) & (m.cap - 1) >= (j - i) & (m.cap - 1) then | |
| 86 | + m.slots[i] := m.slots[j]; | |
| 87 | 87 | i := j; |
| 88 | 88 | end; |
| 89 | - j := (j + 1) & (m^.cap - 1); | |
| 89 | + j := (j + 1) & (m.cap - 1); | |
| 90 | 90 | end; |
| 91 | - m^.slots[i].used := false; | |
| 92 | - DEC(m^.len); | |
| 91 | + m.slots[i].used := false; | |
| 92 | + DEC(m.len); | |
| 93 | 93 | return true; |
| 94 | 94 | end; |
| 95 | 95 | |
| 96 | 96 | (* What k maps to, and whether it maps to anything. *) |
| 97 | -proc get*(m: ^Map; k: K): (V, BOOL); | |
| 97 | +proc get*(const m: Map; k: K): (V, BOOL); | |
| 98 | 98 | var i: INT; none: V; |
| 99 | 99 | begin |
| 100 | - if m^.cap = 0 then return none, false; end; | |
| 100 | + if m.cap = 0 then return none, false; end; | |
| 101 | 101 | i := slot(m, k); |
| 102 | - if not m^.slots[i].used then return none, false; end; | |
| 103 | - return m^.slots[i].val, true; | |
| 102 | + if not m.slots[i].used then return none, false; end; | |
| 103 | + return m.slots[i].val, true; | |
| 104 | 104 | end; |
| 105 | 105 | |
| 106 | 106 | (* The entry after i, walking from 0: the number to pass for the one |
| 107 | 107 | after it, or -1 if there are no more, then its key and value. *) |
| 108 | -proc next*(m: ^Map; i: INT): (INT, K, V); | |
| 108 | +proc next*(const m: Map; i: INT): (INT, K, V); | |
| 109 | 109 | var k: K; v: V; |
| 110 | 110 | begin |
| 111 | - while i < m^.cap do | |
| 112 | - if m^.slots[i].used then return i + 1, m^.slots[i].key, m^.slots[i].val; end; | |
| 111 | + while i < m.cap do | |
| 112 | + if m.slots[i].used then return i + 1, m.slots[i].key, m.slots[i].val; end; | |
| 113 | 113 | INC(i); |
| 114 | 114 | end; |
| 115 | 115 | return -1, k, v; |
| @@ -117,12 +117,12 @@ | ||
| 117 | 117 | |
| 118 | 118 | (* Gives the map's memory back to its allocator, leaving it empty, as |
| 119 | 119 | init does: it may be used again, and grows anew. *) |
| 120 | -proc done*(m: ^Map); | |
| 120 | +proc done*(var m: Map); | |
| 121 | 121 | begin |
| 122 | - if m^.slots # nil then Mem.free(m^.a, ^U8(m^.slots)); end; | |
| 123 | - m^.slots := nil; | |
| 124 | - m^.cap := 0; | |
| 125 | - m^.len := 0; | |
| 122 | + if m.slots # nil then Mem.free(m.a, ^U8(m.slots)); end; | |
| 123 | + m.slots := nil; | |
| 124 | + m.cap := 0; | |
| 125 | + m.len := 0; | |
| 126 | 126 | end; |
| 127 | 127 | |
| 128 | 128 | end Map. |
| @@ -19,37 +19,37 @@ | ||
| 19 | 19 | end; |
| 20 | 20 | |
| 21 | 21 | (* An empty array, which takes its memory from a. *) |
| 22 | -proc init*(v: ^Vec; a: ^Mem.Allocator); | |
| 22 | +proc init*(var v: Vec; a: ^Mem.Allocator); | |
| 23 | 23 | begin |
| 24 | - v^ := {a: a}; | |
| 24 | + v := {a: a}; | |
| 25 | 25 | end; |
| 26 | 26 | |
| 27 | 27 | (* Room for n more elements; false if the allocator has none. *) |
| 28 | -proc room*(v: ^Vec; n: INT): BOOL; | |
| 28 | +proc room*(var v: Vec; n: INT): BOOL; | |
| 29 | 29 | var cap: INT; p: ^T; |
| 30 | 30 | begin |
| 31 | - if v^.len + n <= v^.cap then return true; end; | |
| 32 | - if v^.a = nil then | |
| 31 | + if v.len + n <= v.cap then return true; end; | |
| 32 | + if v.a = nil then | |
| 33 | 33 | (* as Buf: said plainly, rather than as a nil pointer in Mem.alloc *) |
| 34 | 34 | _ := Sys.write(Sys.stderr, "Vec used before Vec.init\n"); |
| 35 | 35 | Rt.trace; |
| 36 | 36 | Rt.exit(2); |
| 37 | 37 | end; |
| 38 | - cap := v^.cap * 2; | |
| 38 | + cap := v.cap * 2; | |
| 39 | 39 | if cap < 16 then cap := 16; end; |
| 40 | - while cap < v^.len + n do cap := cap * 2; end; | |
| 40 | + while cap < v.len + n do cap := cap * 2; end; | |
| 41 | 41 | (* 8: no type of raz needs its elements aligned more *) |
| 42 | - p := ^T(Mem.alloc(v^.a, cap * SIZE(T), 8)); | |
| 42 | + p := ^T(Mem.alloc(v.a, cap * SIZE(T), 8)); | |
| 43 | 43 | if p = nil then return false; end; |
| 44 | - if v^.len > 0 then COPY(p[0..v^.len], v^.data[0..v^.len]); end; | |
| 45 | - if v^.data # nil then Mem.free(v^.a, ^U8(v^.data)); end; | |
| 46 | - v^.data := p; | |
| 47 | - v^.cap := cap; | |
| 44 | + if v.len > 0 then COPY(p[0..v.len], v.data[0..v.len]); end; | |
| 45 | + if v.data # nil then Mem.free(v.a, ^U8(v.data)); end; | |
| 46 | + v.data := p; | |
| 47 | + v.cap := cap; | |
| 48 | 48 | return true; |
| 49 | 49 | end; |
| 50 | 50 | |
| 51 | 51 | (* Room for n more elements, or the program stops. *) |
| 52 | -proc need(v: ^Vec; n: INT); | |
| 52 | +proc need(var v: Vec; n: INT); | |
| 53 | 53 | begin |
| 54 | 54 | if not room(v, n) then |
| 55 | 55 | _ := Sys.write(Sys.stderr, "Vec: out of memory\n"); |
| @@ -59,83 +59,83 @@ | ||
| 59 | 59 | end; |
| 60 | 60 | |
| 61 | 61 | (* Adds x at the end. *) |
| 62 | -proc push*(v: ^Vec; x: T); | |
| 62 | +proc push*(var v: Vec; x: T); | |
| 63 | 63 | begin |
| 64 | 64 | need(v, 1); |
| 65 | - v^.data[v^.len] := x; | |
| 66 | - INC(v^.len); | |
| 65 | + v.data[v.len] := x; | |
| 66 | + INC(v.len); | |
| 67 | 67 | end; |
| 68 | 68 | |
| 69 | 69 | (* The last element, taken off; false if the array is empty. It keeps the |
| 70 | 70 | memory, as resize does. *) |
| 71 | -proc pop*(v: ^Vec): (T, BOOL); | |
| 71 | +proc pop*(var v: Vec): (T, BOOL); | |
| 72 | 72 | var x: T; |
| 73 | 73 | begin |
| 74 | - if v^.len = 0 then return x, false; end; | |
| 75 | - DEC(v^.len); | |
| 76 | - return v^.data[v^.len], true; | |
| 74 | + if v.len = 0 then return x, false; end; | |
| 75 | + DEC(v.len); | |
| 76 | + return v.data[v.len], true; | |
| 77 | 77 | end; |
| 78 | 78 | |
| 79 | 79 | (* Adds an element at the end, zero, and gives it, to be filled in |
| 80 | 80 | place: for a big T, which push would copy. Good until the array |
| 81 | 81 | next grows. *) |
| 82 | -proc add*(v: ^Vec): ^T; | |
| 82 | +proc add*(var v: Vec): ^T; | |
| 83 | 83 | var zero: T; |
| 84 | 84 | begin |
| 85 | 85 | need(v, 1); |
| 86 | - v^.data[v^.len] := zero; | |
| 87 | - INC(v^.len); | |
| 88 | - return v^.data + (v^.len - 1); | |
| 86 | + v.data[v.len] := zero; | |
| 87 | + INC(v.len); | |
| 88 | + return v.data + (v.len - 1); | |
| 89 | 89 | end; |
| 90 | 90 | |
| 91 | 91 | (* push, or false, and the array as it was, if there is no room. *) |
| 92 | -proc tryPush*(v: ^Vec; x: T): BOOL; | |
| 92 | +proc tryPush*(var v: Vec; x: T): BOOL; | |
| 93 | 93 | begin |
| 94 | 94 | if not room(v, 1) then return false; end; |
| 95 | - v^.data[v^.len] := x; | |
| 96 | - INC(v^.len); | |
| 95 | + v.data[v.len] := x; | |
| 96 | + INC(v.len); | |
| 97 | 97 | return true; |
| 98 | 98 | end; |
| 99 | 99 | |
| 100 | 100 | (* add, or nil and false, and the array as it was, if there is no room. *) |
| 101 | -proc tryAdd*(v: ^Vec): (^T, BOOL); | |
| 101 | +proc tryAdd*(var v: Vec): (^T, BOOL); | |
| 102 | 102 | begin |
| 103 | 103 | if not room(v, 1) then return nil, false; end; |
| 104 | 104 | return add(v), true; |
| 105 | 105 | end; |
| 106 | 106 | |
| 107 | 107 | (* Makes the array n elements long: cut short, or grown with zeros. *) |
| 108 | -proc resize*(v: ^Vec; n: INT); | |
| 108 | +proc resize*(var v: Vec; n: INT); | |
| 109 | 109 | var zero: T; i: INT; |
| 110 | 110 | begin |
| 111 | - if n > v^.len then | |
| 112 | - need(v, n - v^.len); | |
| 113 | - for i := v^.len to n - 1 do v^.data[i] := zero; end; | |
| 111 | + if n > v.len then | |
| 112 | + need(v, n - v.len); | |
| 113 | + for i := v.len to n - 1 do v.data[i] := zero; end; | |
| 114 | 114 | end; |
| 115 | - v^.len := n; | |
| 115 | + v.len := n; | |
| 116 | 116 | end; |
| 117 | 117 | |
| 118 | 118 | (* The elements, until the array next grows. *) |
| 119 | -proc all*(v: ^Vec): []T; | |
| 119 | +proc all*(const v: Vec): []T; | |
| 120 | 120 | var none: []T; |
| 121 | 121 | begin |
| 122 | - if v^.len = 0 then return none; end; | |
| 123 | - return v^.data[0..v^.len]; | |
| 122 | + if v.len = 0 then return none; end; | |
| 123 | + return v.data[0..v.len]; | |
| 124 | 124 | end; |
| 125 | 125 | |
| 126 | 126 | (* Empties the array, keeping its memory for what comes next. *) |
| 127 | -proc reset*(v: ^Vec); | |
| 127 | +proc reset*(var v: Vec); | |
| 128 | 128 | begin |
| 129 | - v^.len := 0; | |
| 129 | + v.len := 0; | |
| 130 | 130 | end; |
| 131 | 131 | |
| 132 | 132 | (* Gives the array's memory back, leaving it empty. *) |
| 133 | -proc done*(v: ^Vec); | |
| 133 | +proc done*(var v: Vec); | |
| 134 | 134 | begin |
| 135 | - if v^.data # nil then Mem.free(v^.a, ^U8(v^.data)); end; | |
| 136 | - v^.data := nil; | |
| 137 | - v^.len := 0; | |
| 138 | - v^.cap := 0; | |
| 135 | + if v.data # nil then Mem.free(v.a, ^U8(v.data)); end; | |
| 136 | + v.data := nil; | |
| 137 | + v.len := 0; | |
| 138 | + v.cap := 0; | |
| 139 | 139 | end; |
| 140 | 140 | |
| 141 | 141 | end Vec. |
| @@ -15,14 +15,14 @@ | ||
| 15 | 15 | proc main*; |
| 16 | 16 | var l: Ints.List; n: ^Ints.Node; v: INT; |
| 17 | 17 | begin |
| 18 | - Ints.init(ADR(l), Heap.heap); | |
| 19 | - defer Ints.clear(ADR(l)); | |
| 20 | - for v := 1 to 5 do _ := Ints.push(ADR(l), v * v); end; | |
| 21 | - Ints.reverse(ADR(l)); (* oldest first *) | |
| 18 | + Ints.init(l, Heap.heap); | |
| 19 | + defer Ints.clear(l); | |
| 20 | + for v := 1 to 5 do _ := Ints.push(l, v * v); end; | |
| 21 | + Ints.reverse(l); (* oldest first *) | |
| 22 | 22 | n := l.head; |
| 23 | 23 | while n # nil do Out.int(n^.val); Out.char(' '); n := n^.next; end; |
| 24 | 24 | Out.ln; |
| 25 | - v, _ := Ints.pop(ADR(l)); | |
| 25 | + v, _ := Ints.pop(l); | |
| 26 | 26 | Out.int(v); Out.str(" popped, "); Out.int(l.len); Out.str(" left\n"); |
| 27 | 27 | end; |
| 28 | 28 | |
| @@ -40,13 +40,13 @@ | ||
| 40 | 40 | proc main*; |
| 41 | 41 | var v: Ints.Vec; i: INT; s: []INT; |
| 42 | 42 | begin |
| 43 | - Ints.init(ADR(v), Heap.heap); | |
| 44 | - defer Ints.done(ADR(v)); | |
| 45 | - for i := 1 to 1000 do Ints.push(ADR(v), i * i); end; | |
| 46 | - s := Ints.all(ADR(v)); | |
| 43 | + Ints.init(v, Heap.heap); | |
| 44 | + defer Ints.done(v); | |
| 45 | + for i := 1 to 1000 do Ints.push(v, i * i); end; | |
| 46 | + s := Ints.all(v); | |
| 47 | 47 | Out.int(v.len); Out.str(" squares, the last "); Out.int(s[LEN(s) - 1]); Out.ln; |
| 48 | - Ints.resize(ADR(v), 3); | |
| 49 | - for i := 0 to v.len - 1 do Out.int(Ints.all(ADR(v))[i]); Out.char(' '); end; | |
| 48 | + Ints.resize(v, 3); | |
| 49 | + for i := 0 to v.len - 1 do Out.int(Ints.all(v)[i]); Out.char(' '); end; | |
| 50 | 50 | Out.ln; |
| 51 | 51 | end; |
| 52 | 52 | |
| @@ -64,22 +64,22 @@ | ||
| 64 | 64 | proc main*; |
| 65 | 65 | var m: Book.Map; age: INT; found, ok: BOOL; i: INT; v: INT; |
| 66 | 66 | begin |
| 67 | - Book.init(ADR(m), Heap.heap); | |
| 68 | - _ := Book.put(ADR(m), "ada", 36); | |
| 69 | - _ := Book.put(ADR(m), "grace", 45); | |
| 70 | - _ := Book.put(ADR(m), "alan", 41); | |
| 71 | - _ := Book.put(ADR(m), "ada", 37); (* replaces *) | |
| 72 | - age, found := Book.get(ADR(m), "ada"); | |
| 67 | + Book.init(m, Heap.heap); | |
| 68 | + _ := Book.put(m, "ada", 36); | |
| 69 | + _ := Book.put(m, "grace", 45); | |
| 70 | + _ := Book.put(m, "alan", 41); | |
| 71 | + _ := Book.put(m, "ada", 37); (* replaces *) | |
| 72 | + age, found := Book.get(m, "ada"); | |
| 73 | 73 | Fmt.print("ada: {} ({})\n", [Fmt.int(age), Fmt.bool(found)]); |
| 74 | - age, found := Book.get(ADR(m), "linus"); | |
| 74 | + age, found := Book.get(m, "linus"); | |
| 75 | 75 | Fmt.print("linus: {} ({})\n", [Fmt.int(age), Fmt.bool(found)]); |
| 76 | - ok := Book.remove(ADR(m), "alan"); | |
| 76 | + ok := Book.remove(m, "alan"); | |
| 77 | 77 | Fmt.print("{} entries, removed {}\n", [Fmt.int(m.len), Fmt.bool(ok)]); |
| 78 | 78 | v := 0; |
| 79 | - i, _, age := Book.next(ADR(m), 0); | |
| 79 | + i, _, age := Book.next(m, 0); | |
| 80 | 80 | while i >= 0 do |
| 81 | 81 | INC(v, age); |
| 82 | - i, _, age := Book.next(ADR(m), i); | |
| 82 | + i, _, age := Book.next(m, i); | |
| 83 | 83 | end; |
| 84 | 84 | Out.int(v); Out.str(" years in all\n"); |
| 85 | 85 | end; |
| @@ -120,22 +120,22 @@ | ||
| 120 | 120 | m: Count.Map; entries: Entries.Vec; i, k, found: INT; |
| 121 | 121 | w, rest: []U8; key: []U8; v: INT; all: []Entry; |
| 122 | 122 | begin |
| 123 | - Count.init(ADR(m), Heap.heap); | |
| 124 | - defer Count.done(ADR(m)); | |
| 123 | + Count.init(m, Heap.heap); | |
| 124 | + defer Count.done(m); | |
| 125 | 125 | w, rest := Str.field(Text); |
| 126 | 126 | while LEN(w) > 0 do |
| 127 | - k, _ := Count.get(ADR(m), w); | |
| 128 | - _ := Count.put(ADR(m), w, k + 1); | |
| 127 | + k, _ := Count.get(m, w); | |
| 128 | + _ := Count.put(m, w, k + 1); | |
| 129 | 129 | w, rest := Str.field(rest); |
| 130 | 130 | end; |
| 131 | - Entries.init(ADR(entries), Heap.heap); | |
| 132 | - defer Entries.done(ADR(entries)); | |
| 133 | - found, key, v := Count.next(ADR(m), 0); | |
| 131 | + Entries.init(entries, Heap.heap); | |
| 132 | + defer Entries.done(entries); | |
| 133 | + found, key, v := Count.next(m, 0); | |
| 134 | 134 | while found >= 0 do |
| 135 | - Entries.push(ADR(entries), {word: key, n: v}); | |
| 136 | - found, key, v := Count.next(ADR(m), found); | |
| 135 | + Entries.push(entries, {word: key, n: v}); | |
| 136 | + found, key, v := Count.next(m, found); | |
| 137 | 137 | end; |
| 138 | - all := Entries.all(ADR(entries)); | |
| 138 | + all := Entries.all(entries); | |
| 139 | 139 | Rank.sort(all); |
| 140 | 140 | for i := 0 to LEN(all) - 1 do |
| 141 | 141 | Fmt.print("{} {}\n", [Fmt.int(all[i].n), Fmt.str(all[i].word)]); |
| @@ -153,10 +153,10 @@ | ||
| 153 | 153 | [Fmt.int(b.len), Fmt.int(c.allocs)]); |
| 154 | 154 | Buf.done(b); |
| 155 | 155 | |
| 156 | - Names.init(ADR(l), ADR(c.base)); | |
| 157 | - for i := 1 to 10 do _ := Names.push(ADR(l), "x"); end; | |
| 156 | + Names.init(l, ADR(c.base)); | |
| 157 | + for i := 1 to 10 do _ := Names.push(l, "x"); end; | |
| 158 | 158 | Fmt.print("{} allocations in all, {} frees so far\n", [Fmt.int(c.allocs), Fmt.int(c.frees)]); |
| 159 | - Names.clear(ADR(l)); | |
| 159 | + Names.clear(l); | |
| 160 | 160 | Fmt.print("{} frees after clear\n", [Fmt.int(c.frees)]); |
| 161 | 161 | |
| 162 | 162 | p := Mem.alloc(ADR(c.base), 64, 8); |
| @@ -22,17 +22,17 @@ | ||
| 22 | 22 | begin |
| 23 | 23 | counting := {alloc: alloc, free: free}; |
| 24 | 24 | keys := ["node_modules", "target", "build"]; |
| 25 | - Names.init(ADR(m), ADR(counting)); | |
| 26 | - Names.done(ADR(m)); (* nothing taken yet *) | |
| 27 | - for i := 0 to 99 do _ok := Names.put(ADR(m), keys[i mod 3], i); end; | |
| 25 | + Names.init(m, ADR(counting)); | |
| 26 | + Names.done(m); (* nothing taken yet *) | |
| 27 | + for i := 0 to 99 do _ok := Names.put(m, keys[i mod 3], i); end; | |
| 28 | 28 | Fmt.print("{} entries, {} block\n", [Fmt.int(m.len), Fmt.int(live)]); |
| 29 | - Names.done(ADR(m)); | |
| 30 | - v, found := Names.get(ADR(m), "target"); | |
| 29 | + Names.done(m); | |
| 30 | + v, found := Names.get(m, "target"); | |
| 31 | 31 | Fmt.print("{} entries, {} blocks, found {}\n", [Fmt.int(m.len), Fmt.int(live), Fmt.bool(found)]); |
| 32 | - _ok := Names.put(ADR(m), "target", 7); | |
| 33 | - v, found := Names.get(ADR(m), "target"); | |
| 32 | + _ok := Names.put(m, "target", 7); | |
| 33 | + v, found := Names.get(m, "target"); | |
| 34 | 34 | Fmt.print("{} entry, {}\n", [Fmt.int(m.len), Fmt.int(v)]); |
| 35 | - Names.done(ADR(m)); | |
| 35 | + Names.done(m); | |
| 36 | 36 | Fmt.print("{} blocks\n", [Fmt.int(live)]); |
| 37 | 37 | end; |
| 38 | 38 |
| @@ -21,34 +21,34 @@ | ||
| 21 | 21 | m: Ints.Map; want: [N]I64; i: INT; k, v, n, sum, seed: I64; |
| 22 | 22 | found, _ok: BOOL; |
| 23 | 23 | begin |
| 24 | - Ints.init(ADR(m), Heap.heap); | |
| 24 | + Ints.init(m, Heap.heap); | |
| 25 | 25 | seed := 1; |
| 26 | 26 | for i := 1 to 20000 do |
| 27 | 27 | seed := (seed * 1103515245 + 12345) & 7FFFFFFFH; |
| 28 | 28 | k := seed mod N; |
| 29 | 29 | if seed div N mod 3 = 0 then |
| 30 | - if Ints.remove(ADR(m), k) # (want[k] # 0) then Rt.stop("remove: wrong answer"); end; | |
| 30 | + if Ints.remove(m, k) # (want[k] # 0) then Rt.stop("remove: wrong answer"); end; | |
| 31 | 31 | want[k] := 0; |
| 32 | 32 | else |
| 33 | - _ok := Ints.put(ADR(m), k, i); | |
| 33 | + _ok := Ints.put(m, k, i); | |
| 34 | 34 | want[k] := i; |
| 35 | 35 | end; |
| 36 | 36 | end; |
| 37 | 37 | for k := 0 to N - 1 do |
| 38 | - v, found := Ints.get(ADR(m), k); | |
| 38 | + v, found := Ints.get(m, k); | |
| 39 | 39 | if (found # (want[k] # 0)) or (v # want[k]) then Rt.stop("get: not what was put"); end; |
| 40 | 40 | if found then INC(n); INC(sum, v); end; |
| 41 | 41 | end; |
| 42 | - if Ints.remove(ADR(m), N) then Rt.stop("remove: a key never put"); end; | |
| 42 | + if Ints.remove(m, N) then Rt.stop("remove: a key never put"); end; | |
| 43 | 43 | Fmt.print("{} entries, {} of them, summing {}\n", [Fmt.int(m.len), Fmt.int(n), Fmt.int(sum)]); |
| 44 | 44 | (* the walk sees each entry once *) |
| 45 | 45 | n := 0; sum := 0; |
| 46 | - i, k, v := Ints.next(ADR(m), 0); | |
| 46 | + i, k, v := Ints.next(m, 0); | |
| 47 | 47 | while i >= 0 do |
| 48 | 48 | if want[k] # v then Rt.stop("next: not what was put"); end; |
| 49 | 49 | want[k] := 0; |
| 50 | 50 | INC(n); INC(sum, v); |
| 51 | - i, k, v := Ints.next(ADR(m), i); | |
| 51 | + i, k, v := Ints.next(m, i); | |
| 52 | 52 | end; |
| 53 | 53 | Fmt.print("walked {}, summing {}\n", [Fmt.int(n), Fmt.int(sum)]); |
| 54 | 54 | end; |
| @@ -7,8 +7,8 @@ | ||
| 7 | 7 | var ar: Arena.Arena; v: Ints.Vec; i: INT; |
| 8 | 8 | begin |
| 9 | 9 | if not Arena.init(ar, 4096) then Rt.stop("no arena"); end; |
| 10 | - Ints.init(ADR(v), ADR(ar.base)); | |
| 11 | - for i := 1 to 10000 do Ints.push(ADR(v), i); end; | |
| 10 | + Ints.init(v, ADR(ar.base)); | |
| 11 | + for i := 1 to 10000 do Ints.push(v, i); end; | |
| 12 | 12 | end; |
| 13 | 13 | |
| 14 | 14 | end VecFull. |
| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | Vec: out of memory |
| 2 | - in Vec(INT).need at lib/Vec.raz:56:9: v = 0x?, n = 1 | |
| 3 | - in Vec(INT).push at lib/Vec.raz:64:5: v = 0x?, x = 257 | |
| 2 | + in Vec(INT).need at lib/Vec.raz:56:9: n = 1 | |
| 3 | + in Vec(INT).push at lib/Vec.raz:64:5: x = 257 | |
| 4 | 4 | in VecFull.main at test/run/VecFull.raz:11:28: i = 257 |
| @@ -8,36 +8,36 @@ | ||
| 8 | 8 | proc main*; |
| 9 | 9 | var v: Ints.Vec; w: Pts.Vec; i, sum, top: INT; ok: BOOL; p: ^Point; s: []INT; |
| 10 | 10 | begin |
| 11 | - Ints.init(ADR(v), Heap.heap); | |
| 12 | - Fmt.print("{} {}\n", [Fmt.int(v.len), Fmt.int(LEN(Ints.all(ADR(v))))]); | |
| 13 | - for i := 1 to 10000 do Ints.push(ADR(v), i); end; | |
| 14 | - s := Ints.all(ADR(v)); | |
| 11 | + Ints.init(v, Heap.heap); | |
| 12 | + Fmt.print("{} {}\n", [Fmt.int(v.len), Fmt.int(LEN(Ints.all(v)))]); | |
| 13 | + for i := 1 to 10000 do Ints.push(v, i); end; | |
| 14 | + s := Ints.all(v); | |
| 15 | 15 | sum := 0; |
| 16 | 16 | for i := 0 to LEN(s) - 1 do sum := sum + s[i]; end; |
| 17 | 17 | Fmt.print("{} {} {} {}\n", [Fmt.int(v.len), Fmt.int(s[0]), Fmt.int(s[9999]), Fmt.int(sum)]); |
| 18 | - Ints.resize(ADR(v), 3); | |
| 19 | - Ints.resize(ADR(v), 5); | |
| 20 | - s := Ints.all(ADR(v)); | |
| 18 | + Ints.resize(v, 3); | |
| 19 | + Ints.resize(v, 5); | |
| 20 | + s := Ints.all(v); | |
| 21 | 21 | Fmt.print("{} {} {} {}\n", [Fmt.int(LEN(s)), Fmt.int(s[2]), Fmt.int(s[3]), Fmt.int(s[4])]); |
| 22 | - Ints.reset(ADR(v)); | |
| 23 | - Ints.push(ADR(v), 7); | |
| 24 | - Fmt.print("{} {}\n", [Fmt.int(v.len), Fmt.int(Ints.all(ADR(v))[0])]); | |
| 25 | - top, ok := Ints.pop(ADR(v)); | |
| 22 | + Ints.reset(v); | |
| 23 | + Ints.push(v, 7); | |
| 24 | + Fmt.print("{} {}\n", [Fmt.int(v.len), Fmt.int(Ints.all(v)[0])]); | |
| 25 | + top, ok := Ints.pop(v); | |
| 26 | 26 | Fmt.print("{} {} {}\n", [Fmt.int(top), Fmt.bool(ok), Fmt.int(v.len)]); |
| 27 | - top, ok := Ints.pop(ADR(v)); | |
| 27 | + top, ok := Ints.pop(v); | |
| 28 | 28 | Fmt.print("{} {}\n", [Fmt.int(top), Fmt.bool(ok)]); |
| 29 | - Ints.done(ADR(v)); | |
| 29 | + Ints.done(v); | |
| 30 | 30 | Fmt.print("{}\n", [Fmt.int(v.len)]); |
| 31 | 31 | |
| 32 | - Pts.init(ADR(w), Heap.heap); | |
| 32 | + Pts.init(w, Heap.heap); | |
| 33 | 33 | for i := 0 to 99 do |
| 34 | - p := Pts.add(ADR(w)); | |
| 34 | + p := Pts.add(w); | |
| 35 | 35 | p^.x := i; p^.y := i * i; |
| 36 | 36 | end; |
| 37 | - Pts.push(ADR(w), {x: -1, y: -2, name: "last"}); | |
| 38 | - Fmt.print("{} {} {} {}\n", [Fmt.int(w.len), Fmt.int(Pts.all(ADR(w))[99].y), | |
| 39 | - Fmt.int(Pts.all(ADR(w))[100].x), Fmt.str(Pts.all(ADR(w))[100].name)]); | |
| 40 | - Pts.done(ADR(w)); | |
| 37 | + Pts.push(w, {x: -1, y: -2, name: "last"}); | |
| 38 | + Fmt.print("{} {} {} {}\n", [Fmt.int(w.len), Fmt.int(Pts.all(w)[99].y), | |
| 39 | + Fmt.int(Pts.all(w)[100].x), Fmt.str(Pts.all(w)[100].name)]); | |
| 40 | + Pts.done(w); | |
| 41 | 41 | end; |
| 42 | 42 | |
| 43 | 43 | end VecTest. |
| @@ -7,15 +7,15 @@ | ||
| 7 | 7 | var ar: Arena.Arena; v: Ints.Vec; n: INT; p: ^INT; ok: BOOL; |
| 8 | 8 | begin |
| 9 | 9 | if not Arena.init(ar, 4096) then Rt.stop("no arena"); end; |
| 10 | - Ints.init(ADR(v), ADR(ar.base)); | |
| 10 | + Ints.init(v, ADR(ar.base)); | |
| 11 | 11 | n := 0; |
| 12 | - while Ints.tryPush(ADR(v), n) do INC(n); end; | |
| 13 | - p, ok := Ints.tryAdd(ADR(v)); | |
| 12 | + while Ints.tryPush(v, n) do INC(n); end; | |
| 13 | + p, ok := Ints.tryAdd(v); | |
| 14 | 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)); | |
| 15 | + Ints.reset(v); | |
| 16 | + p, ok := Ints.tryAdd(v); | |
| 17 | 17 | p^ := 5; |
| 18 | - Fmt.print("{} {} {}\n", [Fmt.bool(ok), Fmt.int(v.len), Fmt.int(Ints.all(ADR(v))[0])]); | |
| 18 | + Fmt.print("{} {} {}\n", [Fmt.bool(ok), Fmt.int(v.len), Fmt.int(Ints.all(v)[0])]); | |
| 19 | 19 | end; |
| 20 | 20 | |
| 21 | 21 | end VecTry. |