From 0f4a4cddcb356c45d47bc8a8c539ce28ab9a9b7c Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 17 Oct 2026 08:52:30 +0000 Subject: [PATCH] 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. --- diff --git a/STANDARDS.md b/STANDARDS.md index cd0e287..c403944 100644 --- a/STANDARDS.md +++ b/STANDARDS.md @@ -13,14 +13,16 @@ | The procedure | The parameter | Example | |---|---|---| -| reads it, a few words at most (a handle, a slice, a small record) | a value | `Buf.bytes(b: Buf)` | -| reads it, more than a few words | `const` | `Opt.number(const p: Parser; lo, hi: I64)` | +| reads it, a single value or a slice | a value | `Sys.write(fd: INT; s: []U8)` | +| reads it, a record or an array | `const` | `Map.get(const m: Map; k: K)` | | changes it, and lets go of it when it returns | `var` | `Buf.add(var b: Buf; c: U8)` | | keeps it past the call, in a record, a module variable or another thread | a pointer, `^T` | `Thread.spawn(p, arg: ^U8)` | A `const` parameter is passed in place, as a `var` one is, so nothing is copied; and since it can't be changed, `var` in a heading always -means the procedure changes what it is given. +means the procedure changes what it is given. A record of only four +words passed as a value, copied at each call, made a loop of `Map.get` +and `Vec.all` half again as slow as `const` did (2026-10.33). So `ADR` at a call says the callee keeps what it is given. A reader who sees `Pg.start(q, ADR(c), sql)` knows that `q` is changed and `c` diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 2495cb5..e46c509 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -648,34 +648,34 @@ type Node* = record next*: ^Node; val*: T; end; List* = record head*: ^Node; len*: INT; ... end; -proc init*(l: ^List; a: ^Mem.Allocator); -proc push*(l: ^List; v: T): BOOL; (* v at the front; false if the allocator has no room *) -proc pop*(l: ^List): (T, BOOL); (* the front value, taken off; false if the list is empty *) -proc reverse*(l: ^List); -proc clear*(l: ^List); +proc init*(var l: List; a: ^Mem.Allocator); +proc push*(var l: List; v: T): BOOL; (* v at the front; false if the allocator has no room *) +proc pop*(var l: List): (T, BOOL); (* the front value, taken off; false if the list is empty *) +proc reverse*(var l: List); +proc clear*(var l: List); module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); type Map* = record len*: INT; ... end; -proc init*(m: ^Map; a: ^Mem.Allocator); -proc put*(m: ^Map; k: K; v: V): BOOL; (* k now maps to v; false if there was no room *) -proc remove*(m: ^Map; k: K): BOOL; (* false if k wasn't there *) -proc get*(m: ^Map; k: K): (V, BOOL); (* what k maps to, and whether it maps to anything *) -proc next*(m: ^Map; i: INT): (INT, K, V); (* the entry after i *) -proc done*(m: ^Map); (* its memory given back, leaving it empty *) +proc init*(var m: Map; a: ^Mem.Allocator); +proc put*(var m: Map; k: K; v: V): BOOL; (* k now maps to v; false if there was no room *) +proc remove*(var m: Map; k: K): BOOL; (* false if k wasn't there *) +proc get*(const m: Map; k: K): (V, BOOL); (* what k maps to, and whether it maps to anything *) +proc next*(const m: Map; i: INT): (INT, K, V); (* the entry after i *) +proc done*(var m: Map); (* its memory given back, leaving it empty *) module Vec(T); type Vec* = record len*: INT; ... end; -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 the array is 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 *) -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 *) -proc done*(v: ^Vec); (* its memory given back *) +proc init*(var v: Vec; a: ^Mem.Allocator); +proc room*(var v: Vec; n: INT): BOOL; (* room for n more; false if the allocator has none *) +proc push*(var v: Vec; x: T); (* x added at the end *) +proc pop*(var v: Vec): (T, BOOL); (* the last element, taken off; false if the array is empty *) +proc add*(var v: Vec): ^T; (* a new element at the end, zero, to fill in place *) +proc tryPush*(var v: Vec; x: T): BOOL; (* push; false, and v as it was, if there is no room *) +proc tryAdd*(var v: Vec): (^T, BOOL); (* add; nil and false, and v as it was, if there is no room *) +proc resize*(var v: Vec; n: INT); (* n elements: cut short, or the new ones zero *) +proc all*(const v: Vec): []T; (* the elements, until it next grows *) +proc reset*(var v: Vec); (* emptied, its memory kept *) +proc done*(var v: Vec); (* its memory given back *) module Sort(T; less: proc(a, b: T): BOOL); proc sort*(s: []T); @@ -801,10 +801,10 @@ sees each entry once if the map doesn't change during it: ``` -i, k, v := Names.next(ADR(names), 0); +i, k, v := Names.next(names, 0); while i >= 0 do Fmt.print("{} is {}\n", [Fmt.str(k), Fmt.int(v)]); - i, k, v := Names.next(ADR(names), i); + i, k, v := Names.next(names, i); end; ``` diff --git a/doc/raz.7 b/doc/raz.7 index b1f0e3a..a760c19 100644 --- a/doc/raz.7 +++ b/doc/raz.7 @@ -553,9 +553,10 @@ .Cm const one is the caller's variable too, read in place and never changed, and takes any expression of its type. -A procedure takes what it reads as a value when it is a few words, as +A procedure takes what it reads as a value when it is a single value +or a slice, as .Cm const -when it is more; what it changes as +when it is a record or an array; what it changes as .Cm var ; and a pointer only for what it keeps. .It diff --git a/doc/raz.md b/doc/raz.md index 1482adb..36eb44d 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -1461,10 +1461,10 @@ end; ``` -A procedure takes what it reads as a value when it is a few words, and -as `const` when it is more; what it changes, as `var`; and a pointer -only for what it keeps past the call, what may be `nil`, an allocator, -or what C or the system takes. +A procedure takes what it reads as a value when it is a single value +or a slice, and as `const` when it is a record or an array; what it +changes, as `var`; and a pointer only for what it keeps past the call, +what may be `nil`, an allocator, or what C or the system takes. **Results.** A procedure returns nothing, one value, or several. A call that returns several values can only stand on the right of a multiple diff --git a/examples/Words.raz b/examples/Words.raz index 4fa6441..189e972 100644 --- a/examples/Words.raz +++ b/examples/Words.raz @@ -12,20 +12,20 @@ counts: Count.Map; order: Names.List; n: ^Names.Node; k, total: INT; found, _ok: BOOL; w, rest: []U8; begin - Count.init(ADR(counts), Heap.heap); - Names.init(ADR(order), Heap.heap); + Count.init(counts, Heap.heap); + Names.init(order, Heap.heap); w, rest := Str.field(Text); while LEN(w) > 0 do INC(total); - k, found := Count.get(ADR(counts), w); - if not found then _ok := Names.push(ADR(order), w); end; - _ok := Count.put(ADR(counts), w, k + 1); + k, found := Count.get(counts, w); + if not found then _ok := Names.push(order, w); end; + _ok := Count.put(counts, w, k + 1); w, rest := Str.field(rest); end; - Names.reverse(ADR(order)); + Names.reverse(order); n := order.head; while n # nil do - k, found := Count.get(ADR(counts), n^.val); + k, found := Count.get(counts, n^.val); Fmt.print("{} {}\n", [Fmt.str(n^.val), Fmt.int(k)]); n := n^.next; end; diff --git a/lib/List.raz b/lib/List.raz index 7b4d694..bb39183 100644 --- a/lib/List.raz +++ b/lib/List.raz @@ -14,42 +14,42 @@ end; (* An empty list, its nodes from a. *) -proc init*(l: ^List; a: ^Mem.Allocator); +proc init*(var l: List; a: ^Mem.Allocator); begin - l^ := {a: a}; + l := {a: a}; end; (* v at the front; false if the allocator has no room. *) -proc push*(l: ^List; v: T): BOOL; +proc push*(var l: List; v: T): BOOL; var n: ^Node; begin - NEW(n, l^.a); + NEW(n, l.a); if n = nil then return false; end; n^.val := v; - n^.next := l^.head; - l^.head := n; - INC(l^.len); + n^.next := l.head; + l.head := n; + INC(l.len); return true; end; (* The front value, taken off; false if the list is empty. *) -proc pop*(l: ^List): (T, BOOL); +proc pop*(var l: List): (T, BOOL); var n: ^Node; v: T; begin - n := l^.head; + n := l.head; if n = nil then return v, false; end; v := n^.val; - l^.head := n^.next; - DEC(l^.len); - Mem.free(l^.a, ^U8(n)); + l.head := n^.next; + DEC(l.len); + Mem.free(l.a, ^U8(n)); return v, true; end; (* The list backwards. *) -proc reverse*(l: ^List); +proc reverse*(var l: List); var p, q, next: ^Node; begin - p := l^.head; + p := l.head; q := nil; while p # nil do next := p^.next; @@ -57,14 +57,14 @@ q := p; p := next; end; - l^.head := q; + l.head := q; end; (* Takes every value off, giving the nodes back. *) -proc clear*(l: ^List); +proc clear*(var l: List); var _ok: BOOL; _v: T; begin - while l^.head # nil do _v, _ok := pop(l); end; + while l.head # nil do _v, _ok := pop(l); end; end; end List. diff --git a/lib/Map.raz b/lib/Map.raz index 1779381..a368d5d 100644 --- a/lib/Map.raz +++ b/lib/Map.raz @@ -18,98 +18,98 @@ end; (* An empty map, its memory from a. *) -proc init*(m: ^Map; a: ^Mem.Allocator); +proc init*(var m: Map; a: ^Mem.Allocator); begin - m^ := {a: a}; + m := {a: a}; end; (* The slot of k, or of the empty slot where it would go. *) -proc slot(m: ^Map; k: K): INT; +proc slot(const m: Map; k: K): INT; var i: INT; begin - i := INT(hash(k) & U64(m^.cap - 1)); - while m^.slots[i].used and not eq(m^.slots[i].key, k) do - i := (i + 1) & (m^.cap - 1); + i := INT(hash(k) & U64(m.cap - 1)); + while m.slots[i].used and not eq(m.slots[i].key, k) do + i := (i + 1) & (m.cap - 1); end; return i; end; -proc grow(m: ^Map): BOOL; +proc grow(var m: Map): BOOL; var old: ^Entry; n, i, j: INT; begin - old := m^.slots; - n := m^.cap; - if n = 0 then m^.cap := 16; else m^.cap := n * 2; end; - m^.slots := ^Entry(Mem.alloc(m^.a, m^.cap * SIZE(Entry), 8)); - if m^.slots = nil then m^.slots := old; m^.cap := n; return false; end; - for i := 0 to m^.cap - 1 do m^.slots[i].used := false; end; + old := m.slots; + n := m.cap; + if n = 0 then m.cap := 16; else m.cap := n * 2; end; + m.slots := ^Entry(Mem.alloc(m.a, m.cap * SIZE(Entry), 8)); + if m.slots = nil then m.slots := old; m.cap := n; return false; end; + for i := 0 to m.cap - 1 do m.slots[i].used := false; end; for i := 0 to n - 1 do if old[i].used then j := slot(m, old[i].key); - m^.slots[j] := old[i]; + m.slots[j] := old[i]; end; end; - if old # nil then Mem.free(m^.a, ^U8(old)); end; + if old # nil then Mem.free(m.a, ^U8(old)); end; return true; end; (* k now maps to v; false if there was no room. *) -proc put*(m: ^Map; k: K; v: V): BOOL; +proc put*(var m: Map; k: K; v: V): BOOL; var i: INT; begin - if (m^.len + 1) * 4 > m^.cap * 3 then + if (m.len + 1) * 4 > m.cap * 3 then if not grow(m) then return false; end; end; i := slot(m, k); - if not m^.slots[i].used then - m^.slots[i] := {key: k, used: true}; - INC(m^.len); + if not m.slots[i].used then + m.slots[i] := {key: k, used: true}; + INC(m.len); end; - m^.slots[i].val := v; + m.slots[i].val := v; return true; end; (* k maps to nothing; false if it didn't map to anything. The entries after it that were pushed on past its slot move back, so that a search for each still finds it before an empty slot. *) -proc remove*(m: ^Map; k: K): BOOL; +proc remove*(var m: Map; k: K): BOOL; var i, j, home: INT; begin - if m^.cap = 0 then return false; end; + if m.cap = 0 then return false; end; i := slot(m, k); - if not m^.slots[i].used then return false; end; - j := (i + 1) & (m^.cap - 1); - while m^.slots[j].used do + if not m.slots[i].used then return false; end; + j := (i + 1) & (m.cap - 1); + while m.slots[j].used do (* the entry at j may fill i if i lies between its home and j *) - home := INT(hash(m^.slots[j].key) & U64(m^.cap - 1)); - if (j - home) & (m^.cap - 1) >= (j - i) & (m^.cap - 1) then - m^.slots[i] := m^.slots[j]; + home := INT(hash(m.slots[j].key) & U64(m.cap - 1)); + if (j - home) & (m.cap - 1) >= (j - i) & (m.cap - 1) then + m.slots[i] := m.slots[j]; i := j; end; - j := (j + 1) & (m^.cap - 1); + j := (j + 1) & (m.cap - 1); end; - m^.slots[i].used := false; - DEC(m^.len); + m.slots[i].used := false; + DEC(m.len); return true; end; (* What k maps to, and whether it maps to anything. *) -proc get*(m: ^Map; k: K): (V, BOOL); +proc get*(const m: Map; k: K): (V, BOOL); var i: INT; none: V; begin - if m^.cap = 0 then return none, false; end; + if m.cap = 0 then return none, false; end; i := slot(m, k); - if not m^.slots[i].used then return none, false; end; - return m^.slots[i].val, true; + if not m.slots[i].used then return none, false; end; + return m.slots[i].val, true; end; (* The entry after i, walking from 0: the number to pass for the one after it, or -1 if there are no more, then its key and value. *) -proc next*(m: ^Map; i: INT): (INT, K, V); +proc next*(const m: Map; i: INT): (INT, K, V); var k: K; v: V; begin - while i < m^.cap do - if m^.slots[i].used then return i + 1, m^.slots[i].key, m^.slots[i].val; end; + while i < m.cap do + if m.slots[i].used then return i + 1, m.slots[i].key, m.slots[i].val; end; INC(i); end; return -1, k, v; @@ -117,12 +117,12 @@ (* Gives the map's memory back to its allocator, leaving it empty, as init does: it may be used again, and grows anew. *) -proc done*(m: ^Map); +proc done*(var m: Map); begin - if m^.slots # nil then Mem.free(m^.a, ^U8(m^.slots)); end; - m^.slots := nil; - m^.cap := 0; - m^.len := 0; + if m.slots # nil then Mem.free(m.a, ^U8(m.slots)); end; + m.slots := nil; + m.cap := 0; + m.len := 0; end; end Map. diff --git a/lib/Vec.raz b/lib/Vec.raz index f6594ce..c79eb1e 100644 --- a/lib/Vec.raz +++ b/lib/Vec.raz @@ -19,37 +19,37 @@ end; (* An empty array, which takes its memory from a. *) -proc init*(v: ^Vec; a: ^Mem.Allocator); +proc init*(var v: Vec; a: ^Mem.Allocator); begin - v^ := {a: a}; + v := {a: a}; end; (* Room for n more elements; false if the allocator has none. *) -proc room*(v: ^Vec; n: INT): BOOL; +proc room*(var v: Vec; n: INT): BOOL; var cap: INT; p: ^T; begin - if v^.len + n <= v^.cap then return true; end; - if v^.a = nil then + if v.len + n <= v.cap then return true; end; + if v.a = nil then (* as Buf: said plainly, rather than as a nil pointer in Mem.alloc *) _ := Sys.write(Sys.stderr, "Vec used before Vec.init\n"); Rt.trace; Rt.exit(2); end; - cap := v^.cap * 2; + cap := v.cap * 2; if cap < 16 then cap := 16; end; - while cap < v^.len + n do cap := cap * 2; end; + while cap < v.len + n do cap := cap * 2; end; (* 8: no type of raz needs its elements aligned more *) - p := ^T(Mem.alloc(v^.a, cap * SIZE(T), 8)); + p := ^T(Mem.alloc(v.a, cap * SIZE(T), 8)); if p = nil then return false; end; - if v^.len > 0 then COPY(p[0..v^.len], v^.data[0..v^.len]); end; - if v^.data # nil then Mem.free(v^.a, ^U8(v^.data)); end; - v^.data := p; - v^.cap := cap; + if v.len > 0 then COPY(p[0..v.len], v.data[0..v.len]); end; + if v.data # nil then Mem.free(v.a, ^U8(v.data)); end; + v.data := p; + v.cap := cap; return true; end; (* Room for n more elements, or the program stops. *) -proc need(v: ^Vec; n: INT); +proc need(var v: Vec; n: INT); begin if not room(v, n) then _ := Sys.write(Sys.stderr, "Vec: out of memory\n"); @@ -59,83 +59,83 @@ end; (* Adds x at the end. *) -proc push*(v: ^Vec; x: T); +proc push*(var v: Vec; x: T); begin need(v, 1); - v^.data[v^.len] := x; - INC(v^.len); + v.data[v.len] := x; + 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); +proc pop*(var 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; + 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. *) -proc add*(v: ^Vec): ^T; +proc add*(var v: Vec): ^T; var zero: T; begin need(v, 1); - v^.data[v^.len] := zero; - INC(v^.len); - return v^.data + (v^.len - 1); + 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; +proc tryPush*(var v: Vec; x: T): BOOL; begin if not room(v, 1) then return false; end; - v^.data[v^.len] := x; - INC(v^.len); + 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); +proc tryAdd*(var 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. *) -proc resize*(v: ^Vec; n: INT); +proc resize*(var v: Vec; n: INT); var zero: T; i: INT; begin - if n > v^.len then - need(v, n - v^.len); - for i := v^.len to n - 1 do v^.data[i] := zero; end; + if n > v.len then + need(v, n - v.len); + for i := v.len to n - 1 do v.data[i] := zero; end; end; - v^.len := n; + v.len := n; end; (* The elements, until the array next grows. *) -proc all*(v: ^Vec): []T; +proc all*(const v: Vec): []T; var none: []T; begin - if v^.len = 0 then return none; end; - return v^.data[0..v^.len]; + if v.len = 0 then return none; end; + return v.data[0..v.len]; end; (* Empties the array, keeping its memory for what comes next. *) -proc reset*(v: ^Vec); +proc reset*(var v: Vec); begin - v^.len := 0; + v.len := 0; end; (* Gives the array's memory back, leaving it empty. *) -proc done*(v: ^Vec); +proc done*(var v: Vec); begin - if v^.data # nil then Mem.free(v^.a, ^U8(v^.data)); end; - v^.data := nil; - v^.len := 0; - v^.cap := 0; + if v.data # nil then Mem.free(v.a, ^U8(v.data)); end; + v.data := nil; + v.len := 0; + v.cap := 0; end; end Vec. diff --git a/site/content/advanced/03-collections.txt b/site/content/advanced/03-collections.txt index 1537bac..8d32a9a 100644 --- a/site/content/advanced/03-collections.txt +++ b/site/content/advanced/03-collections.txt @@ -15,14 +15,14 @@ proc main*; var l: Ints.List; n: ^Ints.Node; v: INT; begin - Ints.init(ADR(l), Heap.heap); - defer Ints.clear(ADR(l)); - for v := 1 to 5 do _ := Ints.push(ADR(l), v * v); end; - Ints.reverse(ADR(l)); (* oldest first *) + Ints.init(l, Heap.heap); + defer Ints.clear(l); + for v := 1 to 5 do _ := Ints.push(l, v * v); end; + Ints.reverse(l); (* oldest first *) n := l.head; while n # nil do Out.int(n^.val); Out.char(' '); n := n^.next; end; Out.ln; - v, _ := Ints.pop(ADR(l)); + v, _ := Ints.pop(l); Out.int(v); Out.str(" popped, "); Out.int(l.len); Out.str(" left\n"); end; @@ -40,13 +40,13 @@ proc main*; var v: Ints.Vec; i: INT; s: []INT; begin - Ints.init(ADR(v), Heap.heap); - defer Ints.done(ADR(v)); - for i := 1 to 1000 do Ints.push(ADR(v), i * i); end; - s := Ints.all(ADR(v)); + Ints.init(v, Heap.heap); + defer Ints.done(v); + for i := 1 to 1000 do Ints.push(v, i * i); end; + s := Ints.all(v); Out.int(v.len); Out.str(" squares, the last "); Out.int(s[LEN(s) - 1]); Out.ln; - Ints.resize(ADR(v), 3); - for i := 0 to v.len - 1 do Out.int(Ints.all(ADR(v))[i]); Out.char(' '); end; + Ints.resize(v, 3); + for i := 0 to v.len - 1 do Out.int(Ints.all(v)[i]); Out.char(' '); end; Out.ln; end; @@ -64,22 +64,22 @@ proc main*; var m: Book.Map; age: INT; found, ok: BOOL; i: INT; v: INT; begin - Book.init(ADR(m), Heap.heap); - _ := Book.put(ADR(m), "ada", 36); - _ := Book.put(ADR(m), "grace", 45); - _ := Book.put(ADR(m), "alan", 41); - _ := Book.put(ADR(m), "ada", 37); (* replaces *) - age, found := Book.get(ADR(m), "ada"); + Book.init(m, Heap.heap); + _ := Book.put(m, "ada", 36); + _ := Book.put(m, "grace", 45); + _ := Book.put(m, "alan", 41); + _ := Book.put(m, "ada", 37); (* replaces *) + age, found := Book.get(m, "ada"); Fmt.print("ada: {} ({})\n", [Fmt.int(age), Fmt.bool(found)]); - age, found := Book.get(ADR(m), "linus"); + age, found := Book.get(m, "linus"); Fmt.print("linus: {} ({})\n", [Fmt.int(age), Fmt.bool(found)]); - ok := Book.remove(ADR(m), "alan"); + ok := Book.remove(m, "alan"); Fmt.print("{} entries, removed {}\n", [Fmt.int(m.len), Fmt.bool(ok)]); v := 0; - i, _, age := Book.next(ADR(m), 0); + i, _, age := Book.next(m, 0); while i >= 0 do INC(v, age); - i, _, age := Book.next(ADR(m), i); + i, _, age := Book.next(m, i); end; Out.int(v); Out.str(" years in all\n"); end; @@ -120,22 +120,22 @@ m: Count.Map; entries: Entries.Vec; i, k, found: INT; w, rest: []U8; key: []U8; v: INT; all: []Entry; begin - Count.init(ADR(m), Heap.heap); - defer Count.done(ADR(m)); + Count.init(m, Heap.heap); + defer Count.done(m); w, rest := Str.field(Text); while LEN(w) > 0 do - k, _ := Count.get(ADR(m), w); - _ := Count.put(ADR(m), w, k + 1); + k, _ := Count.get(m, w); + _ := Count.put(m, w, k + 1); w, rest := Str.field(rest); end; - Entries.init(ADR(entries), Heap.heap); - defer Entries.done(ADR(entries)); - found, key, v := Count.next(ADR(m), 0); + Entries.init(entries, Heap.heap); + defer Entries.done(entries); + found, key, v := Count.next(m, 0); while found >= 0 do - Entries.push(ADR(entries), {word: key, n: v}); - found, key, v := Count.next(ADR(m), found); + Entries.push(entries, {word: key, n: v}); + found, key, v := Count.next(m, found); end; - all := Entries.all(ADR(entries)); + all := Entries.all(entries); Rank.sort(all); for i := 0 to LEN(all) - 1 do Fmt.print("{} {}\n", [Fmt.int(all[i].n), Fmt.str(all[i].word)]); diff --git a/site/content/advanced/06-allocators.txt b/site/content/advanced/06-allocators.txt index d310af1..ed55dfd 100644 --- a/site/content/advanced/06-allocators.txt +++ b/site/content/advanced/06-allocators.txt @@ -153,10 +153,10 @@ [Fmt.int(b.len), Fmt.int(c.allocs)]); Buf.done(b); - Names.init(ADR(l), ADR(c.base)); - for i := 1 to 10 do _ := Names.push(ADR(l), "x"); end; + Names.init(l, ADR(c.base)); + for i := 1 to 10 do _ := Names.push(l, "x"); end; Fmt.print("{} allocations in all, {} frees so far\n", [Fmt.int(c.allocs), Fmt.int(c.frees)]); - Names.clear(ADR(l)); + Names.clear(l); Fmt.print("{} frees after clear\n", [Fmt.int(c.frees)]); p := Mem.alloc(ADR(c.base), 64, 8); diff --git a/test/run/MapDone.raz b/test/run/MapDone.raz index d5c0fdd..fabedf4 100644 --- a/test/run/MapDone.raz +++ b/test/run/MapDone.raz @@ -22,17 +22,17 @@ begin counting := {alloc: alloc, free: free}; keys := ["node_modules", "target", "build"]; - Names.init(ADR(m), ADR(counting)); - Names.done(ADR(m)); (* nothing taken yet *) - for i := 0 to 99 do _ok := Names.put(ADR(m), keys[i mod 3], i); end; + Names.init(m, ADR(counting)); + Names.done(m); (* nothing taken yet *) + for i := 0 to 99 do _ok := Names.put(m, keys[i mod 3], i); end; Fmt.print("{} entries, {} block\n", [Fmt.int(m.len), Fmt.int(live)]); - Names.done(ADR(m)); - v, found := Names.get(ADR(m), "target"); + Names.done(m); + v, found := Names.get(m, "target"); Fmt.print("{} entries, {} blocks, found {}\n", [Fmt.int(m.len), Fmt.int(live), Fmt.bool(found)]); - _ok := Names.put(ADR(m), "target", 7); - v, found := Names.get(ADR(m), "target"); + _ok := Names.put(m, "target", 7); + v, found := Names.get(m, "target"); Fmt.print("{} entry, {}\n", [Fmt.int(m.len), Fmt.int(v)]); - Names.done(ADR(m)); + Names.done(m); Fmt.print("{} blocks\n", [Fmt.int(live)]); end; diff --git a/test/run/MapRemove.raz b/test/run/MapRemove.raz index d40f37d..eaf3b51 100644 --- a/test/run/MapRemove.raz +++ b/test/run/MapRemove.raz @@ -21,34 +21,34 @@ m: Ints.Map; want: [N]I64; i: INT; k, v, n, sum, seed: I64; found, _ok: BOOL; begin - Ints.init(ADR(m), Heap.heap); + Ints.init(m, Heap.heap); seed := 1; for i := 1 to 20000 do seed := (seed * 1103515245 + 12345) & 7FFFFFFFH; k := seed mod N; if seed div N mod 3 = 0 then - if Ints.remove(ADR(m), k) # (want[k] # 0) then Rt.stop("remove: wrong answer"); end; + if Ints.remove(m, k) # (want[k] # 0) then Rt.stop("remove: wrong answer"); end; want[k] := 0; else - _ok := Ints.put(ADR(m), k, i); + _ok := Ints.put(m, k, i); want[k] := i; end; end; for k := 0 to N - 1 do - v, found := Ints.get(ADR(m), k); + v, found := Ints.get(m, k); if (found # (want[k] # 0)) or (v # want[k]) then Rt.stop("get: not what was put"); end; if found then INC(n); INC(sum, v); end; end; - if Ints.remove(ADR(m), N) then Rt.stop("remove: a key never put"); end; + if Ints.remove(m, N) then Rt.stop("remove: a key never put"); end; Fmt.print("{} entries, {} of them, summing {}\n", [Fmt.int(m.len), Fmt.int(n), Fmt.int(sum)]); (* the walk sees each entry once *) n := 0; sum := 0; - i, k, v := Ints.next(ADR(m), 0); + i, k, v := Ints.next(m, 0); while i >= 0 do if want[k] # v then Rt.stop("next: not what was put"); end; want[k] := 0; INC(n); INC(sum, v); - i, k, v := Ints.next(ADR(m), i); + i, k, v := Ints.next(m, i); end; Fmt.print("walked {}, summing {}\n", [Fmt.int(n), Fmt.int(sum)]); end; diff --git a/test/run/VecFull.raz b/test/run/VecFull.raz index 237ba87..44c2df9 100644 --- a/test/run/VecFull.raz +++ b/test/run/VecFull.raz @@ -7,8 +7,8 @@ var ar: Arena.Arena; v: Ints.Vec; i: INT; begin if not Arena.init(ar, 4096) then Rt.stop("no arena"); end; - Ints.init(ADR(v), ADR(ar.base)); - for i := 1 to 10000 do Ints.push(ADR(v), i); end; + Ints.init(v, ADR(ar.base)); + for i := 1 to 10000 do Ints.push(v, i); end; end; end VecFull. diff --git a/test/run/VecFull.stderr b/test/run/VecFull.stderr index 3638fa9..9612fb8 100644 --- a/test/run/VecFull.stderr +++ b/test/run/VecFull.stderr @@ -1,4 +1,4 @@ Vec: out of memory - in Vec(INT).need at lib/Vec.raz:56:9: v = 0x?, n = 1 - in Vec(INT).push at lib/Vec.raz:64:5: v = 0x?, x = 257 + in Vec(INT).need at lib/Vec.raz:56:9: n = 1 + in Vec(INT).push at lib/Vec.raz:64:5: x = 257 in VecFull.main at test/run/VecFull.raz:11:28: i = 257 diff --git a/test/run/VecTest.raz b/test/run/VecTest.raz index 03f3406..c92dec7 100644 --- a/test/run/VecTest.raz +++ b/test/run/VecTest.raz @@ -8,36 +8,36 @@ proc main*; 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))))]); - for i := 1 to 10000 do Ints.push(ADR(v), i); end; - s := Ints.all(ADR(v)); + Ints.init(v, Heap.heap); + Fmt.print("{} {}\n", [Fmt.int(v.len), Fmt.int(LEN(Ints.all(v)))]); + for i := 1 to 10000 do Ints.push(v, i); end; + s := Ints.all(v); sum := 0; for i := 0 to LEN(s) - 1 do sum := sum + s[i]; end; Fmt.print("{} {} {} {}\n", [Fmt.int(v.len), Fmt.int(s[0]), Fmt.int(s[9999]), Fmt.int(sum)]); - Ints.resize(ADR(v), 3); - Ints.resize(ADR(v), 5); - s := Ints.all(ADR(v)); + Ints.resize(v, 3); + Ints.resize(v, 5); + s := Ints.all(v); Fmt.print("{} {} {} {}\n", [Fmt.int(LEN(s)), Fmt.int(s[2]), Fmt.int(s[3]), Fmt.int(s[4])]); - 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)); + Ints.reset(v); + Ints.push(v, 7); + Fmt.print("{} {}\n", [Fmt.int(v.len), Fmt.int(Ints.all(v)[0])]); + top, ok := Ints.pop(v); Fmt.print("{} {} {}\n", [Fmt.int(top), Fmt.bool(ok), Fmt.int(v.len)]); - top, ok := Ints.pop(ADR(v)); + top, ok := Ints.pop(v); Fmt.print("{} {}\n", [Fmt.int(top), Fmt.bool(ok)]); - Ints.done(ADR(v)); + Ints.done(v); Fmt.print("{}\n", [Fmt.int(v.len)]); - Pts.init(ADR(w), Heap.heap); + Pts.init(w, Heap.heap); for i := 0 to 99 do - p := Pts.add(ADR(w)); + p := Pts.add(w); p^.x := i; p^.y := i * i; end; - Pts.push(ADR(w), {x: -1, y: -2, name: "last"}); - Fmt.print("{} {} {} {}\n", [Fmt.int(w.len), Fmt.int(Pts.all(ADR(w))[99].y), - Fmt.int(Pts.all(ADR(w))[100].x), Fmt.str(Pts.all(ADR(w))[100].name)]); - Pts.done(ADR(w)); + Pts.push(w, {x: -1, y: -2, name: "last"}); + Fmt.print("{} {} {} {}\n", [Fmt.int(w.len), Fmt.int(Pts.all(w)[99].y), + Fmt.int(Pts.all(w)[100].x), Fmt.str(Pts.all(w)[100].name)]); + Pts.done(w); end; end VecTest. diff --git a/test/run/VecTry.raz b/test/run/VecTry.raz index a879226..672f310 100644 --- a/test/run/VecTry.raz +++ b/test/run/VecTry.raz @@ -7,15 +7,15 @@ var ar: Arena.Arena; v: Ints.Vec; n: INT; p: ^INT; ok: BOOL; begin if not Arena.init(ar, 4096) then Rt.stop("no arena"); end; - Ints.init(ADR(v), ADR(ar.base)); + Ints.init(v, ADR(ar.base)); n := 0; - while Ints.tryPush(ADR(v), n) do INC(n); end; - p, ok := Ints.tryAdd(ADR(v)); + while Ints.tryPush(v, n) do INC(n); end; + p, ok := Ints.tryAdd(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)); + Ints.reset(v); + p, ok := Ints.tryAdd(v); p^ := 5; - Fmt.print("{} {} {}\n", [Fmt.bool(ok), Fmt.int(v.len), Fmt.int(Ints.all(ADR(v))[0])]); + Fmt.print("{} {} {}\n", [Fmt.bool(ok), Fmt.int(v.len), Fmt.int(Ints.all(v)[0])]); end; end VecTry.