| 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; tryPush and |
| 9 | tryAdd ask and add in one call. *) |
| 10 | module Vec(T); |
| 11 | import Mem, Rt, Sys; |
| 12 | import SYSTEM; |
| 13 | |
| 14 | type |
| 15 | Vec* = record |
| 16 | a: ^Mem.Allocator; |
| 17 | data: ^T; |
| 18 | len*: INT; (* the elements in use *) |
| 19 | cap: INT; (* the elements there is room for *) |
| 20 | end; |
| 21 | |
| 22 | (* An empty array, which takes its memory from a. *) |
| 23 | proc init*(var v: Vec; a: ^Mem.Allocator); |
| 24 | begin |
| 25 | v := {a: a}; |
| 26 | end; |
| 27 | |
| 28 | (* Room for n more elements; false if the allocator has none. *) |
| 29 | proc room*(var v: Vec; n: INT): BOOL; |
| 30 | var cap: INT; p: ^T; |
| 31 | begin |
| 32 | if v.len + n <= v.cap then return true; end; |
| 33 | if v.a = nil then |
| 34 | (* as Buf: said plainly, rather than as a nil pointer in Mem.alloc *) |
| 35 | _ := Sys.write(Sys.stderr, "Vec used before Vec.init\n"); |
| 36 | Rt.trace; |
| 37 | Rt.exit(2); |
| 38 | end; |
| 39 | cap := v.cap * 2; |
| 40 | if cap < 16 then cap := 16; end; |
| 41 | while cap < v.len + n do cap := cap * 2; end; |
| 42 | (* 8: no type of raz needs its elements aligned more *) |
| 43 | p := ^T(Mem.alloc(v.a, cap * SIZE(T), 8)); |
| 44 | if p = nil then return false; end; |
| 45 | if v.len > 0 then COPY(p[0..v.len], v.data[0..v.len]); end; |
| 46 | if v.data # nil then Mem.free(v.a, ^U8(v.data)); end; |
| 47 | v.data := p; |
| 48 | v.cap := cap; |
| 49 | return true; |
| 50 | end; |
| 51 | |
| 52 | (* Room for n more elements, or the program stops. *) |
| 53 | proc need(var v: Vec; n: INT); |
| 54 | begin |
| 55 | if not room(v, n) then |
| 56 | _ := Sys.write(Sys.stderr, "Vec: out of memory\n"); |
| 57 | Rt.trace; |
| 58 | Rt.exit(2); |
| 59 | end; |
| 60 | end; |
| 61 | |
| 62 | (* Adds x at the end. *) |
| 63 | proc push*(var v: Vec; x: T); |
| 64 | begin |
| 65 | need(v, 1); |
| 66 | v.data[v.len] := x; |
| 67 | INC(v.len); |
| 68 | end; |
| 69 | |
| 70 | (* The last element, taken off; false if the array is empty. It keeps the |
| 71 | memory, as resize does. *) |
| 72 | proc pop*(var v: Vec): (T, BOOL); |
| 73 | var x: T; |
| 74 | begin |
| 75 | if v.len = 0 then return x, false; end; |
| 76 | DEC(v.len); |
| 77 | return v.data[v.len], true; |
| 78 | end; |
| 79 | |
| 80 | (* Adds an element at the end, zero, and gives it, to be filled in |
| 81 | place: for a big T, which push would copy. Good until the array |
| 82 | next grows. *) |
| 83 | proc add*(var v: Vec): ^T; |
| 84 | var zero: T; |
| 85 | begin |
| 86 | need(v, 1); |
| 87 | v.data[v.len] := zero; |
| 88 | INC(v.len); |
| 89 | return v.data + (v.len - 1); |
| 90 | end; |
| 91 | |
| 92 | (* push, or false, and the array as it was, if there is no room. *) |
| 93 | proc tryPush*(var v: Vec; x: T): BOOL; |
| 94 | begin |
| 95 | if not room(v, 1) then return false; end; |
| 96 | v.data[v.len] := x; |
| 97 | INC(v.len); |
| 98 | return true; |
| 99 | end; |
| 100 | |
| 101 | (* add, or nil and false, and the array as it was, if there is no room. *) |
| 102 | proc tryAdd*(var v: Vec): (^T, BOOL); |
| 103 | begin |
| 104 | if not room(v, 1) then return nil, false; end; |
| 105 | return add(v), true; |
| 106 | end; |
| 107 | |
| 108 | (* Makes the array n elements long: cut short, or grown with zeros. *) |
| 109 | proc resize*(var v: Vec; n: INT); |
| 110 | var zero: T; i: INT; |
| 111 | begin |
| 112 | if n > v.len then |
| 113 | need(v, n - v.len); |
| 114 | for i := v.len to n - 1 do v.data[i] := zero; end; |
| 115 | end; |
| 116 | v.len := n; |
| 117 | end; |
| 118 | |
| 119 | (* n zero elements opened at i, those from i on moved up; i from 0 to |
| 120 | v.len. Stops the program, as push does, if there is no room. *) |
| 121 | proc insert*(var v: Vec; i, n: INT); |
| 122 | var zero: T; k: INT; |
| 123 | begin |
| 124 | if (i < 0) or (i > v.len) or (n < 0) then Rt.stop("Vec.insert: out of range"); end; |
| 125 | need(v, n); |
| 126 | (* COPY goes the right way for the overlap *) |
| 127 | COPY(v.data[i + n..v.len + n], v.data[i..v.len]); |
| 128 | for k := i to i + n - 1 do v.data[k] := zero; end; |
| 129 | v.len := v.len + n; |
| 130 | end; |
| 131 | |
| 132 | (* The n elements from i taken out, those after them moved down; i + n |
| 133 | at most v.len. *) |
| 134 | proc remove*(var v: Vec; i, n: INT); |
| 135 | begin |
| 136 | if (i < 0) or (n < 0) or (i + n > v.len) then Rt.stop("Vec.remove: out of range"); end; |
| 137 | COPY(v.data[i..v.len - n], v.data[i + n..v.len]); |
| 138 | v.len := v.len - n; |
| 139 | end; |
| 140 | |
| 141 | (* The elements, until the array next grows. *) |
| 142 | proc all*(const v: Vec): []T; |
| 143 | var none: []T; |
| 144 | begin |
| 145 | if v.len = 0 then return none; end; |
| 146 | return v.data[0..v.len]; |
| 147 | end; |
| 148 | |
| 149 | (* Empties the array, keeping its memory for what comes next. *) |
| 150 | proc reset*(var v: Vec); |
| 151 | begin |
| 152 | v.len := 0; |
| 153 | end; |
| 154 | |
| 155 | (* Gives the array's memory back, leaving it empty. *) |
| 156 | proc done*(var v: Vec); |
| 157 | begin |
| 158 | if v.data # nil then Mem.free(v.a, ^U8(v.data)); end; |
| 159 | v.data := nil; |
| 160 | v.len := 0; |
| 161 | v.cap := 0; |
| 162 | end; |
| 163 | |
| 164 | end Vec. |