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.
| @@ -106,7 +106,7 @@ | ||
| 106 | 106 | # raz-lib(3), the library's manual page, made by tools/Doc.raz from |
| 107 | 107 | # its sources, in the order of doc/raz-lib.md |
| 108 | 108 | LIBDOC = Rt Mem Heap Arena Lock Thread.linux-x86_64 Out Sys.linux-x86_64 \ |
| 109 | - Str Bytes.x86_64 Buf Fmt List Map Sort Math Files Opt C | |
| 109 | + Str Bytes.x86_64 Buf Fmt List Map Vec Sort Math Files Opt C | |
| 110 | 110 | |
| 111 | 111 | man: build/raz-lib.3 |
| 112 | 112 |
| @@ -368,38 +368,3 @@ | ||
| 368 | 368 | `ret` does. D's experience is that the corners (fields, generics, |
| 369 | 369 | procedure values) take years; the narrow form here leaves record |
| 370 | 370 | lifetimes out, and so the first false alarm above, on purpose. |
| 371 | - | |
| 372 | -## `Vec(T)`, a growable array | |
| 373 | - | |
| 374 | -The library has `List`, linked, and `Map`, a hash; nothing that grows | |
| 375 | -in place and can be indexed. A program that keeps what it learns the | |
| 376 | -length of as it reads (lines, entries, changes) writes one: sauce's | |
| 377 | -`Svec`, 90 lines, imported four times. Every program of size will. | |
| 378 | - | |
| 379 | -A generic module of the standard library, not the core: like `List` | |
| 380 | -and `Map` it takes its memory from the allocator `init` gives it, and | |
| 381 | -a target with no heap to speak of may leave it out. It is `Buf` for | |
| 382 | -any `T`, and works as `Buf` does: | |
| 383 | - | |
| 384 | -``` | |
| 385 | -module Vec(T); | |
| 386 | -type Vec* = record len*: INT; ... end; | |
| 387 | -proc init*(v: ^Vec; a: ^Mem.Allocator); | |
| 388 | -proc room*(v: ^Vec; n: INT): BOOL; (* room for n more; false if the allocator has none *) | |
| 389 | -proc push*(v: ^Vec; x: T); (* x added at the end *) | |
| 390 | -proc add*(v: ^Vec): ^T; (* a new element at the end, zero, to fill in place *) | |
| 391 | -proc resize*(v: ^Vec; n: INT); (* n elements, the new ones zero *) | |
| 392 | -proc all*(v: ^Vec): []T; (* the elements, until it next grows *) | |
| 393 | -proc reset*(v: ^Vec); (* emptied, its memory kept *) | |
| 394 | -proc done*(v: ^Vec); (* its memory given back *) | |
| 395 | -``` | |
| 396 | - | |
| 397 | -`all` gives a slice, so that indexing is checked; `v.len` is its | |
| 398 | -length. `push`, `add` and `resize` stop the program when the | |
| 399 | -allocator has no room, as `Buf.add` does, rather than returning a | |
| 400 | -`BOOL` as `List.push` does: a `push` whose result is dropped loses an | |
| 401 | -element silently, and an array is pushed to far more often than a | |
| 402 | -list. `room` asks first. No `pop`, `insert` or `remove` until a | |
| 403 | -program needs one: `v.len := v.len - 1` and `Sort` cover most. | |
| 404 | - | |
| 405 | -A new module breaks no program, so it can come with any release. |
| @@ -25,6 +25,7 @@ | ||
| 25 | 25 | | `Fmt` | formatted output: `Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)])`, or into a `Buf` with `Fmt.write` | |
| 26 | 26 | | `List` | a generic singly linked list, `List(T)` | |
| 27 | 27 | | `Map` | a generic hash map, `Map(K, V; hash, eq)` | |
| 28 | +| `Vec` | a generic growable array, `Vec(T)`, indexed as a slice | | |
| 28 | 29 | | `Sort` | sorting a slice in place, `Sort(T; less)` | |
| 29 | 30 | | `Math` | roots, powers, logarithms, rounding and trigonometry, and `pi`, for `F64` | |
| 30 | 31 | | `Files` | an argument as a string, and a whole file read or written | |
| @@ -504,6 +505,17 @@ | ||
| 504 | 505 | proc remove*(m: ^Map; k: K): BOOL; (* false if k wasn't there *) |
| 505 | 506 | proc get*(m: ^Map; k: K): (V, BOOL); (* what k maps to, and whether it maps to anything *) |
| 506 | 507 | proc next*(m: ^Map; i: INT): (INT, K, V); (* the entry after i *) |
| 508 | + | |
| 509 | +module Vec(T); | |
| 510 | +type Vec* = record len*: INT; ... end; | |
| 511 | +proc init*(v: ^Vec; a: ^Mem.Allocator); | |
| 512 | +proc room*(v: ^Vec; n: INT): BOOL; (* room for n more; false if the allocator has none *) | |
| 513 | +proc push*(v: ^Vec; x: T); (* x added at the end *) | |
| 514 | +proc add*(v: ^Vec): ^T; (* a new element at the end, zero, to fill in place *) | |
| 515 | +proc resize*(v: ^Vec; n: INT); (* n elements: cut short, or the new ones zero *) | |
| 516 | +proc all*(v: ^Vec): []T; (* the elements, until it next grows *) | |
| 517 | +proc reset*(v: ^Vec); (* emptied, its memory kept *) | |
| 518 | +proc done*(v: ^Vec); (* its memory given back *) | |
| 507 | 519 | |
| 508 | 520 | module Sort(T; less: proc(a, b: T): BOOL); |
| 509 | 521 | proc sort*(s: []T); |
| @@ -532,12 +544,18 @@ | ||
| 532 | 544 | proc toReal*(s: []U8): (F64, BOOL); (* s as a real, the F64 nearest it; false if it isn't one *) |
| 533 | 545 | ``` |
| 534 | 546 | |
| 535 | -A `Buf`, a `List` and a `Map` take their memory from the allocator | |
| 536 | -`init` gives them. `Buf.add`, `str` and `spare` stop the program, as a | |
| 547 | +A `Buf`, a `List`, a `Map` and a `Vec` take their memory from the | |
| 548 | +allocator `init` gives them. `Buf.add`, `str` and `spare` stop the program, as a | |
| 537 | 549 | failed check does, if the allocator has no room; `room` asks first. A |
| 538 | 550 | program reads into a `Buf` through `spare`, then adds what it read to |
| 539 | 551 | `len`: `s := Buf.spare(b, 4096); n := Sys.read(fd, s); if n > 0 then |
| 540 | -INC(b^.len, n); end;`. `Fmt`'s format takes the next argument at each | |
| 552 | +INC(b^.len, n); end;`. A `Vec` is `Buf` for any `T`: one block, which | |
| 553 | +doubles when it is full, so `push` costs the same on average however | |
| 554 | +long it grows. `all` gives the elements as a slice, indexed and | |
| 555 | +checked as any is, and `v.len` is their number; the slice, and what | |
| 556 | +`add` gives, are good until the array next grows. `push`, `add` and | |
| 557 | +`resize` stop the program, as `Buf.add` does, if the allocator has no | |
| 558 | +room; `room` asks first: `import Lines := Vec([]U8);`. `Fmt`'s format takes the next argument at each | |
| 541 | 559 | `{}`; `{{` and `}}` stand for `{` and `}`. A `Map` hashes and |
| 542 | 560 | compares its keys with the procedures it is given: |
| 543 | 561 | `import Count := Map([]U8, INT, Str.hash, Str.eq);`. |
| @@ -0,0 +1,114 @@ | ||
| 1 | +(* A growable array of T, its memory from an allocator: Buf for any T. | |
| 2 | + | |
| 3 | + It keeps its elements in one block, which doubles when it is full, | |
| 4 | + so that a program can add to it without knowing how many will come | |
| 5 | + and index it as a slice. all gives that slice, checked as any is, | |
| 6 | + and good until the array next grows. Running out of memory stops | |
| 7 | + the program, as Buf.add does: an element dropped silently would be | |
| 8 | + wrong data that nothing tells of. room asks first. *) | |
| 9 | +module Vec(T); | |
| 10 | +import Mem, Rt, Sys; | |
| 11 | + | |
| 12 | +type | |
| 13 | + Vec* = record | |
| 14 | + a: ^Mem.Allocator; | |
| 15 | + data: ^T; | |
| 16 | + len*: INT; (* the elements in use *) | |
| 17 | + cap: INT; (* the elements there is room for *) | |
| 18 | + end; | |
| 19 | + | |
| 20 | +(* An empty array, which takes its memory from a. *) | |
| 21 | +proc init*(v: ^Vec; a: ^Mem.Allocator); | |
| 22 | +begin | |
| 23 | + v^ := {a: a}; | |
| 24 | +end; | |
| 25 | + | |
| 26 | +(* Room for n more elements; false if the allocator has none. *) | |
| 27 | +proc room*(v: ^Vec; n: INT): BOOL; | |
| 28 | +var cap: INT; p: ^T; | |
| 29 | +begin | |
| 30 | + if v^.len + n <= v^.cap then return true; end; | |
| 31 | + if v^.a = nil then | |
| 32 | + (* as Buf: said plainly, rather than as a nil pointer in Mem.alloc *) | |
| 33 | + _ := Sys.write(Sys.stderr, "Vec used before Vec.init\n"); | |
| 34 | + Rt.trace; | |
| 35 | + Rt.exit(2); | |
| 36 | + end; | |
| 37 | + cap := v^.cap * 2; | |
| 38 | + if cap < 16 then cap := 16; end; | |
| 39 | + while cap < v^.len + n do cap := cap * 2; end; | |
| 40 | + (* 8: no type of raz needs its elements aligned more *) | |
| 41 | + p := ^T(Mem.alloc(v^.a, cap * SIZE(T), 8)); | |
| 42 | + if p = nil then return false; end; | |
| 43 | + if v^.len > 0 then COPY(p[0..v^.len], v^.data[0..v^.len]); end; | |
| 44 | + if v^.data # nil then Mem.free(v^.a, ^U8(v^.data)); end; | |
| 45 | + v^.data := p; | |
| 46 | + v^.cap := cap; | |
| 47 | + return true; | |
| 48 | +end; | |
| 49 | + | |
| 50 | +(* Room for n more elements, or the program stops. *) | |
| 51 | +proc need(v: ^Vec; n: INT); | |
| 52 | +begin | |
| 53 | + if not room(v, n) then | |
| 54 | + _ := Sys.write(Sys.stderr, "Vec: out of memory\n"); | |
| 55 | + Rt.trace; | |
| 56 | + Rt.exit(2); | |
| 57 | + end; | |
| 58 | +end; | |
| 59 | + | |
| 60 | +(* Adds x at the end. *) | |
| 61 | +proc push*(v: ^Vec; x: T); | |
| 62 | +begin | |
| 63 | + need(v, 1); | |
| 64 | + v^.data[v^.len] := x; | |
| 65 | + INC(v^.len); | |
| 66 | +end; | |
| 67 | + | |
| 68 | +(* Adds an element at the end, zero, and gives it, to be filled in | |
| 69 | + place: for a big T, which push would copy. Good until the array | |
| 70 | + next grows. *) | |
| 71 | +proc add*(v: ^Vec): ^T; | |
| 72 | +var zero: T; | |
| 73 | +begin | |
| 74 | + need(v, 1); | |
| 75 | + v^.data[v^.len] := zero; | |
| 76 | + INC(v^.len); | |
| 77 | + return v^.data + (v^.len - 1); | |
| 78 | +end; | |
| 79 | + | |
| 80 | +(* Makes the array n elements long: cut short, or grown with zeros. *) | |
| 81 | +proc resize*(v: ^Vec; n: INT); | |
| 82 | +var zero: T; i: INT; | |
| 83 | +begin | |
| 84 | + if n > v^.len then | |
| 85 | + need(v, n - v^.len); | |
| 86 | + for i := v^.len to n - 1 do v^.data[i] := zero; end; | |
| 87 | + end; | |
| 88 | + v^.len := n; | |
| 89 | +end; | |
| 90 | + | |
| 91 | +(* The elements, until the array next grows. *) | |
| 92 | +proc all*(v: ^Vec): []T; | |
| 93 | +var none: []T; | |
| 94 | +begin | |
| 95 | + if v^.len = 0 then return none; end; | |
| 96 | + return v^.data[0..v^.len]; | |
| 97 | +end; | |
| 98 | + | |
| 99 | +(* Empties the array, keeping its memory for what comes next. *) | |
| 100 | +proc reset*(v: ^Vec); | |
| 101 | +begin | |
| 102 | + v^.len := 0; | |
| 103 | +end; | |
| 104 | + | |
| 105 | +(* Gives the array's memory back, leaving it empty. *) | |
| 106 | +proc done*(v: ^Vec); | |
| 107 | +begin | |
| 108 | + if v^.data # nil then Mem.free(v^.a, ^U8(v^.data)); end; | |
| 109 | + v^.data := nil; | |
| 110 | + v^.len := 0; | |
| 111 | + v^.cap := 0; | |
| 112 | +end; | |
| 113 | + | |
| 114 | +end Vec. |
An empty file.
| @@ -0,0 +1,14 @@ | ||
| 1 | +module VecFull; | |
| 2 | +(* a Vec whose allocator runs out stops the program, with the calls | |
| 3 | + under way, rather than dropping what it was given *) | |
| 4 | +import Arena, Rt, Ints := Vec(INT); | |
| 5 | + | |
| 6 | +proc main*; | |
| 7 | +var ar: Arena.Arena; v: Ints.Vec; i: INT; | |
| 8 | +begin | |
| 9 | + if not Arena.init(ADR(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; | |
| 12 | +end; | |
| 13 | + | |
| 14 | +end VecFull. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -0,0 +1,4 @@ | ||
| 1 | +Vec: out of memory | |
| 2 | + in Vec(INT).need at lib/Vec.raz:55:9: v = 0x?, n = 1 | |
| 3 | + in Vec(INT).push at lib/Vec.raz:63:5: v = 0x?, x = 257 | |
| 4 | + in VecFull.main at test/run/VecFull.raz:11:28: i = 257 |
| @@ -0,0 +1,6 @@ | ||
| 1 | +0 0 | |
| 2 | +10000 1 10000 50005000 | |
| 3 | +5 3 0 0 | |
| 4 | +1 7 | |
| 5 | +0 | |
| 6 | +101 9801 -1 last |
| @@ -0,0 +1,39 @@ | ||
| 1 | +(* Vec: pushed, added in place, resized both ways, emptied, given back; | |
| 2 | + of integers and of records, which grow past the first block. *) | |
| 3 | +module VecTest; | |
| 4 | +import Fmt, Heap, Ints := Vec(INT), Pts := Vec(Point); | |
| 5 | + | |
| 6 | +type Point = record x, y: INT; name: []U8; end; | |
| 7 | + | |
| 8 | +proc main*; | |
| 9 | +var v: Ints.Vec; w: Pts.Vec; i, sum: INT; p: ^Point; s: []INT; | |
| 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)); | |
| 15 | + sum := 0; | |
| 16 | + for i := 0 to LEN(s) - 1 do sum := sum + s[i]; end; | |
| 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)); | |
| 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 | + Ints.done(ADR(v)); | |
| 26 | + Fmt.print("{}\n", [Fmt.int(v.len)]); | |
| 27 | + | |
| 28 | + Pts.init(ADR(w), Heap.heap); | |
| 29 | + for i := 0 to 99 do | |
| 30 | + p := Pts.add(ADR(w)); | |
| 31 | + p^.x := i; p^.y := i * i; | |
| 32 | + end; | |
| 33 | + Pts.push(ADR(w), {x: -1, y: -2, name: "last"}); | |
| 34 | + Fmt.print("{} {} {} {}\n", [Fmt.int(w.len), Fmt.int(Pts.all(ADR(w))[99].y), | |
| 35 | + Fmt.int(Pts.all(ADR(w))[100].x), Fmt.str(Pts.all(ADR(w))[100].name)]); | |
| 36 | + Pts.done(ADR(w)); | |
| 37 | +end; | |
| 38 | + | |
| 39 | +end VecTest. |