raz / commit 0f4a4cdd

commit
0f4a4cddcb356c45d47bc8a8c539ce28ab9a9b7c
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
821f83e6
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.

patch · log

16 files changed, +225 -222

STANDARDS.md (history) modified +5 -3

@@ -13,14 +13,16 @@
1313
1414 | The procedure | The parameter | Example |
1515 |---|---|---|
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)` |
1818 | changes it, and lets go of it when it returns | `var` | `Buf.add(var b: Buf; c: U8)` |
1919 | keeps it past the call, in a record, a module variable or another thread | a pointer, `^T` | `Thread.spawn(p, arg: ^U8)` |
2020
2121 A `const` parameter is passed in place, as a `var` one is, so nothing
2222 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).
2426
2527 So `ADR` at a call says the callee keeps what it is given. A reader
2628 who sees `Pg.start(q, ADR(c), sql)` knows that `q` is changed and `c`

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

@@ -648,34 +648,34 @@
648648 type
649649 Node* = record next*: ^Node; val*: T; end;
650650 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);
656656
657657 module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL);
658658 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 *)
665665
666666 module Vec(T);
667667 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 *)
679679
680680 module Sort(T; less: proc(a, b: T): BOOL);
681681 proc sort*(s: []T);
@@ -801,10 +801,10 @@
801801 sees each entry once if the map doesn't change during it:
802802
803803 ```
804-i, k, v := Names.next(ADR(names), 0);
804+i, k, v := Names.next(names, 0);
805805 while i >= 0 do
806806 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);
808808 end;
809809 ```
810810

doc/raz.7 (history) modified +3 -2

@@ -553,9 +553,10 @@
553553 .Cm const
554554 one is the caller's variable too, read in place and never changed, and
555555 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
557558 .Cm const
558-when it is more; what it changes as
559+when it is a record or an array; what it changes as
559560 .Cm var ;
560561 and a pointer only for what it keeps.
561562 .It

doc/raz.md (history) modified +4 -4

@@ -1461,10 +1461,10 @@
14611461 end;
14621462 ```
14631463
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.
14681468
14691469 **Results.** A procedure returns nothing, one value, or several. A call
14701470 that returns several values can only stand on the right of a multiple

examples/Words.raz (history) modified +7 -7

@@ -12,20 +12,20 @@
1212 counts: Count.Map; order: Names.List; n: ^Names.Node;
1313 k, total: INT; found, _ok: BOOL; w, rest: []U8;
1414 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);
1717 w, rest := Str.field(Text);
1818 while LEN(w) > 0 do
1919 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);
2323 w, rest := Str.field(rest);
2424 end;
25- Names.reverse(ADR(order));
25+ Names.reverse(order);
2626 n := order.head;
2727 while n # nil do
28- k, found := Count.get(ADR(counts), n^.val);
28+ k, found := Count.get(counts, n^.val);
2929 Fmt.print("{} {}\n", [Fmt.str(n^.val), Fmt.int(k)]);
3030 n := n^.next;
3131 end;

lib/List.raz (history) modified +17 -17

@@ -14,42 +14,42 @@
1414 end;
1515
1616 (* An empty list, its nodes from a. *)
17-proc init*(l: ^List; a: ^Mem.Allocator);
17+proc init*(var l: List; a: ^Mem.Allocator);
1818 begin
19- l^ := {a: a};
19+ l := {a: a};
2020 end;
2121
2222 (* 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;
2424 var n: ^Node;
2525 begin
26- NEW(n, l^.a);
26+ NEW(n, l.a);
2727 if n = nil then return false; end;
2828 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);
3232 return true;
3333 end;
3434
3535 (* 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);
3737 var n: ^Node; v: T;
3838 begin
39- n := l^.head;
39+ n := l.head;
4040 if n = nil then return v, false; end;
4141 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));
4545 return v, true;
4646 end;
4747
4848 (* The list backwards. *)
49-proc reverse*(l: ^List);
49+proc reverse*(var l: List);
5050 var p, q, next: ^Node;
5151 begin
52- p := l^.head;
52+ p := l.head;
5353 q := nil;
5454 while p # nil do
5555 next := p^.next;
@@ -57,14 +57,14 @@
5757 q := p;
5858 p := next;
5959 end;
60- l^.head := q;
60+ l.head := q;
6161 end;
6262
6363 (* Takes every value off, giving the nodes back. *)
64-proc clear*(l: ^List);
64+proc clear*(var l: List);
6565 var _ok: BOOL; _v: T;
6666 begin
67- while l^.head # nil do _v, _ok := pop(l); end;
67+ while l.head # nil do _v, _ok := pop(l); end;
6868 end;
6969
7070 end List.

lib/Map.raz (history) modified +44 -44

@@ -18,98 +18,98 @@
1818 end;
1919
2020 (* An empty map, its memory from a. *)
21-proc init*(m: ^Map; a: ^Mem.Allocator);
21+proc init*(var m: Map; a: ^Mem.Allocator);
2222 begin
23- m^ := {a: a};
23+ m := {a: a};
2424 end;
2525
2626 (* 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;
2828 var i: INT;
2929 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);
3333 end;
3434 return i;
3535 end;
3636
37-proc grow(m: ^Map): BOOL;
37+proc grow(var m: Map): BOOL;
3838 var old: ^Entry; n, i, j: INT;
3939 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;
4646 for i := 0 to n - 1 do
4747 if old[i].used then
4848 j := slot(m, old[i].key);
49- m^.slots[j] := old[i];
49+ m.slots[j] := old[i];
5050 end;
5151 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;
5353 return true;
5454 end;
5555
5656 (* 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;
5858 var i: INT;
5959 begin
60- if (m^.len + 1) * 4 > m^.cap * 3 then
60+ if (m.len + 1) * 4 > m.cap * 3 then
6161 if not grow(m) then return false; end;
6262 end;
6363 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);
6767 end;
68- m^.slots[i].val := v;
68+ m.slots[i].val := v;
6969 return true;
7070 end;
7171
7272 (* k maps to nothing; false if it didn't map to anything. The entries
7373 after it that were pushed on past its slot move back, so that a
7474 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;
7676 var i, j, home: INT;
7777 begin
78- if m^.cap = 0 then return false; end;
78+ if m.cap = 0 then return false; end;
7979 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
8383 (* 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];
8787 i := j;
8888 end;
89- j := (j + 1) & (m^.cap - 1);
89+ j := (j + 1) & (m.cap - 1);
9090 end;
91- m^.slots[i].used := false;
92- DEC(m^.len);
91+ m.slots[i].used := false;
92+ DEC(m.len);
9393 return true;
9494 end;
9595
9696 (* 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);
9898 var i: INT; none: V;
9999 begin
100- if m^.cap = 0 then return none, false; end;
100+ if m.cap = 0 then return none, false; end;
101101 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;
104104 end;
105105
106106 (* The entry after i, walking from 0: the number to pass for the one
107107 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);
109109 var k: K; v: V;
110110 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;
113113 INC(i);
114114 end;
115115 return -1, k, v;
@@ -117,12 +117,12 @@
117117
118118 (* Gives the map's memory back to its allocator, leaving it empty, as
119119 init does: it may be used again, and grows anew. *)
120-proc done*(m: ^Map);
120+proc done*(var m: Map);
121121 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;
126126 end;
127127
128128 end Map.

lib/Vec.raz (history) modified +43 -43

@@ -19,37 +19,37 @@
1919 end;
2020
2121 (* 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);
2323 begin
24- v^ := {a: a};
24+ v := {a: a};
2525 end;
2626
2727 (* 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;
2929 var cap: INT; p: ^T;
3030 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
3333 (* as Buf: said plainly, rather than as a nil pointer in Mem.alloc *)
3434 _ := Sys.write(Sys.stderr, "Vec used before Vec.init\n");
3535 Rt.trace;
3636 Rt.exit(2);
3737 end;
38- cap := v^.cap * 2;
38+ cap := v.cap * 2;
3939 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;
4141 (* 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));
4343 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;
4848 return true;
4949 end;
5050
5151 (* Room for n more elements, or the program stops. *)
52-proc need(v: ^Vec; n: INT);
52+proc need(var v: Vec; n: INT);
5353 begin
5454 if not room(v, n) then
5555 _ := Sys.write(Sys.stderr, "Vec: out of memory\n");
@@ -59,83 +59,83 @@
5959 end;
6060
6161 (* Adds x at the end. *)
62-proc push*(v: ^Vec; x: T);
62+proc push*(var v: Vec; x: T);
6363 begin
6464 need(v, 1);
65- v^.data[v^.len] := x;
66- INC(v^.len);
65+ v.data[v.len] := x;
66+ INC(v.len);
6767 end;
6868
6969 (* The last element, taken off; false if the array is empty. It keeps the
7070 memory, as resize does. *)
71-proc pop*(v: ^Vec): (T, BOOL);
71+proc pop*(var v: Vec): (T, BOOL);
7272 var x: T;
7373 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;
7777 end;
7878
7979 (* Adds an element at the end, zero, and gives it, to be filled in
8080 place: for a big T, which push would copy. Good until the array
8181 next grows. *)
82-proc add*(v: ^Vec): ^T;
82+proc add*(var v: Vec): ^T;
8383 var zero: T;
8484 begin
8585 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);
8989 end;
9090
9191 (* 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;
9393 begin
9494 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);
9797 return true;
9898 end;
9999
100100 (* 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);
102102 begin
103103 if not room(v, 1) then return nil, false; end;
104104 return add(v), true;
105105 end;
106106
107107 (* 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);
109109 var zero: T; i: INT;
110110 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;
114114 end;
115- v^.len := n;
115+ v.len := n;
116116 end;
117117
118118 (* The elements, until the array next grows. *)
119-proc all*(v: ^Vec): []T;
119+proc all*(const v: Vec): []T;
120120 var none: []T;
121121 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];
124124 end;
125125
126126 (* Empties the array, keeping its memory for what comes next. *)
127-proc reset*(v: ^Vec);
127+proc reset*(var v: Vec);
128128 begin
129- v^.len := 0;
129+ v.len := 0;
130130 end;
131131
132132 (* Gives the array's memory back, leaving it empty. *)
133-proc done*(v: ^Vec);
133+proc done*(var v: Vec);
134134 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;
139139 end;
140140
141141 end Vec.

site/content/advanced/03-collections.txt (history) modified +31 -31

@@ -15,14 +15,14 @@
1515 proc main*;
1616 var l: Ints.List; n: ^Ints.Node; v: INT;
1717 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 *)
2222 n := l.head;
2323 while n # nil do Out.int(n^.val); Out.char(' '); n := n^.next; end;
2424 Out.ln;
25- v, _ := Ints.pop(ADR(l));
25+ v, _ := Ints.pop(l);
2626 Out.int(v); Out.str(" popped, "); Out.int(l.len); Out.str(" left\n");
2727 end;
2828
@@ -40,13 +40,13 @@
4040 proc main*;
4141 var v: Ints.Vec; i: INT; s: []INT;
4242 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);
4747 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;
5050 Out.ln;
5151 end;
5252
@@ -64,22 +64,22 @@
6464 proc main*;
6565 var m: Book.Map; age: INT; found, ok: BOOL; i: INT; v: INT;
6666 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");
7373 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");
7575 Fmt.print("linus: {} ({})\n", [Fmt.int(age), Fmt.bool(found)]);
76- ok := Book.remove(ADR(m), "alan");
76+ ok := Book.remove(m, "alan");
7777 Fmt.print("{} entries, removed {}\n", [Fmt.int(m.len), Fmt.bool(ok)]);
7878 v := 0;
79- i, _, age := Book.next(ADR(m), 0);
79+ i, _, age := Book.next(m, 0);
8080 while i >= 0 do
8181 INC(v, age);
82- i, _, age := Book.next(ADR(m), i);
82+ i, _, age := Book.next(m, i);
8383 end;
8484 Out.int(v); Out.str(" years in all\n");
8585 end;
@@ -120,22 +120,22 @@
120120 m: Count.Map; entries: Entries.Vec; i, k, found: INT;
121121 w, rest: []U8; key: []U8; v: INT; all: []Entry;
122122 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);
125125 w, rest := Str.field(Text);
126126 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);
129129 w, rest := Str.field(rest);
130130 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);
134134 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);
137137 end;
138- all := Entries.all(ADR(entries));
138+ all := Entries.all(entries);
139139 Rank.sort(all);
140140 for i := 0 to LEN(all) - 1 do
141141 Fmt.print("{} {}\n", [Fmt.int(all[i].n), Fmt.str(all[i].word)]);

site/content/advanced/06-allocators.txt (history) modified +3 -3

@@ -153,10 +153,10 @@
153153 [Fmt.int(b.len), Fmt.int(c.allocs)]);
154154 Buf.done(b);
155155
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;
158158 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);
160160 Fmt.print("{} frees after clear\n", [Fmt.int(c.frees)]);
161161
162162 p := Mem.alloc(ADR(c.base), 64, 8);

test/run/MapDone.raz (history) modified +8 -8

@@ -22,17 +22,17 @@
2222 begin
2323 counting := {alloc: alloc, free: free};
2424 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;
2828 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");
3131 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");
3434 Fmt.print("{} entry, {}\n", [Fmt.int(m.len), Fmt.int(v)]);
35- Names.done(ADR(m));
35+ Names.done(m);
3636 Fmt.print("{} blocks\n", [Fmt.int(live)]);
3737 end;
3838

test/run/MapRemove.raz (history) modified +7 -7

@@ -21,34 +21,34 @@
2121 m: Ints.Map; want: [N]I64; i: INT; k, v, n, sum, seed: I64;
2222 found, _ok: BOOL;
2323 begin
24- Ints.init(ADR(m), Heap.heap);
24+ Ints.init(m, Heap.heap);
2525 seed := 1;
2626 for i := 1 to 20000 do
2727 seed := (seed * 1103515245 + 12345) & 7FFFFFFFH;
2828 k := seed mod N;
2929 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;
3131 want[k] := 0;
3232 else
33- _ok := Ints.put(ADR(m), k, i);
33+ _ok := Ints.put(m, k, i);
3434 want[k] := i;
3535 end;
3636 end;
3737 for k := 0 to N - 1 do
38- v, found := Ints.get(ADR(m), k);
38+ v, found := Ints.get(m, k);
3939 if (found # (want[k] # 0)) or (v # want[k]) then Rt.stop("get: not what was put"); end;
4040 if found then INC(n); INC(sum, v); end;
4141 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;
4343 Fmt.print("{} entries, {} of them, summing {}\n", [Fmt.int(m.len), Fmt.int(n), Fmt.int(sum)]);
4444 (* the walk sees each entry once *)
4545 n := 0; sum := 0;
46- i, k, v := Ints.next(ADR(m), 0);
46+ i, k, v := Ints.next(m, 0);
4747 while i >= 0 do
4848 if want[k] # v then Rt.stop("next: not what was put"); end;
4949 want[k] := 0;
5050 INC(n); INC(sum, v);
51- i, k, v := Ints.next(ADR(m), i);
51+ i, k, v := Ints.next(m, i);
5252 end;
5353 Fmt.print("walked {}, summing {}\n", [Fmt.int(n), Fmt.int(sum)]);
5454 end;

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

@@ -7,8 +7,8 @@
77 var ar: Arena.Arena; v: Ints.Vec; i: INT;
88 begin
99 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;
1212 end;
1313
1414 end VecFull.

test/run/VecFull.stderr (history) modified +2 -2

@@ -1,4 +1,4 @@
11 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
44 in VecFull.main at test/run/VecFull.raz:11:28: i = 257

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

@@ -8,36 +8,36 @@
88 proc main*;
99 var v: Ints.Vec; w: Pts.Vec; i, sum, top: INT; ok: BOOL; p: ^Point; s: []INT;
1010 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);
1515 sum := 0;
1616 for i := 0 to LEN(s) - 1 do sum := sum + s[i]; end;
1717 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);
2121 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);
2626 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);
2828 Fmt.print("{} {}\n", [Fmt.int(top), Fmt.bool(ok)]);
29- Ints.done(ADR(v));
29+ Ints.done(v);
3030 Fmt.print("{}\n", [Fmt.int(v.len)]);
3131
32- Pts.init(ADR(w), Heap.heap);
32+ Pts.init(w, Heap.heap);
3333 for i := 0 to 99 do
34- p := Pts.add(ADR(w));
34+ p := Pts.add(w);
3535 p^.x := i; p^.y := i * i;
3636 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);
4141 end;
4242
4343 end VecTest.

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

@@ -7,15 +7,15 @@
77 var ar: Arena.Arena; v: Ints.Vec; n: INT; p: ^INT; ok: BOOL;
88 begin
99 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));
1111 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);
1414 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);
1717 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])]);
1919 end;
2020
2121 end VecTry.