From 8b97e6ce96e12f8a4c0eb92c23cfd5d4913ac67e Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 15 Oct 2026 17:44:58 +0100 Subject: [PATCH] Vec(T): a growable array, in the standard library Buf for any T: one block that doubles, all as a checked slice, push, add (zero, filled in place), resize, reset, done; out of memory stops the program, room asks first, and a Vec used before init says so. Not the core. The proposal leaves PROPOSALS.md for raz-lib.md and raz-lib(3); test/run/VecTest and VecFull. --- diff --git a/Makefile b/Makefile index 2873998..10fac71 100644 --- a/Makefile +++ b/Makefile @@ -106,7 +106,7 @@ # raz-lib(3), the library's manual page, made by tools/Doc.raz from # its sources, in the order of doc/raz-lib.md LIBDOC = Rt Mem Heap Arena Lock Thread.linux-x86_64 Out Sys.linux-x86_64 \ - Str Bytes.x86_64 Buf Fmt List Map Sort Math Files Opt C + Str Bytes.x86_64 Buf Fmt List Map Vec Sort Math Files Opt C man: build/raz-lib.3 diff --git a/PROPOSALS.md b/PROPOSALS.md index c528f3d..b1c5851 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -368,38 +368,3 @@ `ret` does. D's experience is that the corners (fields, generics, procedure values) take years; the narrow form here leaves record lifetimes out, and so the first false alarm above, on purpose. - -## `Vec(T)`, a growable array - -The library has `List`, linked, and `Map`, a hash; nothing that grows -in place and can be indexed. A program that keeps what it learns the -length of as it reads (lines, entries, changes) writes one: sauce's -`Svec`, 90 lines, imported four times. Every program of size will. - -A generic module of the standard library, not the core: like `List` -and `Map` it takes its memory from the allocator `init` gives it, and -a target with no heap to speak of may leave it out. It is `Buf` for -any `T`, and works as `Buf` does: - -``` -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 add*(v: ^Vec): ^T; (* a new element at the end, zero, to fill in place *) -proc resize*(v: ^Vec; n: INT); (* n elements, 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 *) -``` - -`all` gives a slice, so that indexing is checked; `v.len` is its -length. `push`, `add` and `resize` stop the program when the -allocator has no room, as `Buf.add` does, rather than returning a -`BOOL` as `List.push` does: a `push` whose result is dropped loses an -element silently, and an array is pushed to far more often than a -list. `room` asks first. No `pop`, `insert` or `remove` until a -program needs one: `v.len := v.len - 1` and `Sort` cover most. - -A new module breaks no program, so it can come with any release. diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 582937c..7b2acb4 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -25,6 +25,7 @@ | `Fmt` | formatted output: `Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)])`, or into a `Buf` with `Fmt.write` | | `List` | a generic singly linked list, `List(T)` | | `Map` | a generic hash map, `Map(K, V; hash, eq)` | +| `Vec` | a generic growable array, `Vec(T)`, indexed as a slice | | `Sort` | sorting a slice in place, `Sort(T; less)` | | `Math` | roots, powers, logarithms, rounding and trigonometry, and `pi`, for `F64` | | `Files` | an argument as a string, and a whole file read or written | @@ -504,6 +505,17 @@ 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 *) + +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 add*(v: ^Vec): ^T; (* a new element at the end, zero, to fill in place *) +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 *) module Sort(T; less: proc(a, b: T): BOOL); proc sort*(s: []T); @@ -532,12 +544,18 @@ proc toReal*(s: []U8): (F64, BOOL); (* s as a real, the F64 nearest it; false if it isn't one *) ``` -A `Buf`, a `List` and a `Map` take their memory from the allocator -`init` gives them. `Buf.add`, `str` and `spare` stop the program, as a +A `Buf`, a `List`, a `Map` and a `Vec` take their memory from the +allocator `init` gives them. `Buf.add`, `str` and `spare` stop the program, as a failed check does, if the allocator has no room; `room` asks first. A program reads into a `Buf` through `spare`, then adds what it read to `len`: `s := Buf.spare(b, 4096); n := Sys.read(fd, s); if n > 0 then -INC(b^.len, n); end;`. `Fmt`'s format takes the next argument at each +INC(b^.len, n); end;`. A `Vec` is `Buf` for any `T`: one block, which +doubles when it is full, so `push` costs the same on average however +long it grows. `all` gives the elements as a slice, indexed and +checked as any is, and `v.len` is their number; the slice, and what +`add` gives, are good until the array next grows. `push`, `add` and +`resize` stop the program, as `Buf.add` does, if the allocator has no +room; `room` asks first: `import Lines := Vec([]U8);`. `Fmt`'s format takes the next argument at each `{}`; `{{` and `}}` stand for `{` and `}`. A `Map` hashes and compares its keys with the procedures it is given: `import Count := Map([]U8, INT, Str.hash, Str.eq);`. diff --git a/lib/Vec.raz b/lib/Vec.raz new file mode 100644 index 0000000..eacac39 --- /dev/null +++ b/lib/Vec.raz @@ -0,0 +1,114 @@ +(* A growable array of T, its memory from an allocator: Buf for any T. + + It keeps its elements in one block, which doubles when it is full, + so that a program can add to it without knowing how many will come + and index it as a slice. all gives that slice, checked as any is, + and good until the array next grows. Running out of memory stops + the program, as Buf.add does: an element dropped silently would be + wrong data that nothing tells of. room asks first. *) +module Vec(T); +import Mem, Rt, Sys; + +type + Vec* = record + a: ^Mem.Allocator; + data: ^T; + len*: INT; (* the elements in use *) + cap: INT; (* the elements there is room for *) + end; + +(* An empty array, which takes its memory from a. *) +proc init*(v: ^Vec; a: ^Mem.Allocator); +begin + v^ := {a: a}; +end; + +(* Room for n more elements; false if the allocator has none. *) +proc room*(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 + (* 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; + if cap < 16 then cap := 16; 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)); + 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; + return true; +end; + +(* Room for n more elements, or the program stops. *) +proc need(v: ^Vec; n: INT); +begin + if not room(v, n) then + _ := Sys.write(Sys.stderr, "Vec: out of memory\n"); + Rt.trace; + Rt.exit(2); + end; +end; + +(* Adds x at the end. *) +proc push*(v: ^Vec; x: T); +begin + need(v, 1); + v^.data[v^.len] := x; + INC(v^.len); +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; +var zero: T; +begin + need(v, 1); + v^.data[v^.len] := zero; + INC(v^.len); + return v^.data + (v^.len - 1); +end; + +(* Makes the array n elements long: cut short, or grown with zeros. *) +proc resize*(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; + end; + v^.len := n; +end; + +(* The elements, until the array next grows. *) +proc all*(v: ^Vec): []T; +var none: []T; +begin + 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); +begin + v^.len := 0; +end; + +(* Gives the array's memory back, leaving it empty. *) +proc done*(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; +end; + +end Vec. diff --git a/test/run/VecFull.expect b/test/run/VecFull.expect new file mode 100644 index 0000000..e69de29 diff --git a/test/run/VecFull.raz b/test/run/VecFull.raz new file mode 100644 index 0000000..fe3695b --- /dev/null +++ b/test/run/VecFull.raz @@ -0,0 +1,14 @@ +module VecFull; +(* a Vec whose allocator runs out stops the program, with the calls + under way, rather than dropping what it was given *) +import Arena, Rt, Ints := Vec(INT); + +proc main*; +var ar: Arena.Arena; v: Ints.Vec; i: INT; +begin + if not Arena.init(ADR(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; +end; + +end VecFull. diff --git a/test/run/VecFull.status b/test/run/VecFull.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/VecFull.status @@ -0,0 +1 @@ +2 diff --git a/test/run/VecFull.stderr b/test/run/VecFull.stderr new file mode 100644 index 0000000..c09734e --- /dev/null +++ b/test/run/VecFull.stderr @@ -0,0 +1,4 @@ +Vec: out of memory + in Vec(INT).need at lib/Vec.raz:55:9: v = 0x?, n = 1 + in Vec(INT).push at lib/Vec.raz:63:5: v = 0x?, x = 257 + in VecFull.main at test/run/VecFull.raz:11:28: i = 257 diff --git a/test/run/VecTest.expect b/test/run/VecTest.expect new file mode 100644 index 0000000..bb466c2 --- /dev/null +++ b/test/run/VecTest.expect @@ -0,0 +1,6 @@ +0 0 +10000 1 10000 50005000 +5 3 0 0 +1 7 +0 +101 9801 -1 last diff --git a/test/run/VecTest.raz b/test/run/VecTest.raz new file mode 100644 index 0000000..d365d51 --- /dev/null +++ b/test/run/VecTest.raz @@ -0,0 +1,39 @@ +(* Vec: pushed, added in place, resized both ways, emptied, given back; + of integers and of records, which grow past the first block. *) +module VecTest; +import Fmt, Heap, Ints := Vec(INT), Pts := Vec(Point); + +type Point = record x, y: INT; name: []U8; end; + +proc main*; +var v: Ints.Vec; w: Pts.Vec; i, sum: INT; 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)); + 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)); + 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])]); + Ints.done(ADR(v)); + Fmt.print("{}\n", [Fmt.int(v.len)]); + + Pts.init(ADR(w), Heap.heap); + for i := 0 to 99 do + p := Pts.add(ADR(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)); +end; + +end VecTest.