(* 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; tryPush and tryAdd ask and add in one call. *) module Vec(T); import Mem, Rt, Sys; import SYSTEM; 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*(var v: Vec; a: ^Mem.Allocator); begin v := {a: a}; end; (* Room for n more elements; false if the allocator has none. *) 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 (* 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(var 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*(var v: Vec; x: T); begin need(v, 1); 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*(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; 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*(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); end; (* push, or false, and the array as it was, if there is no room. *) 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); return true; end; (* add, or nil and false, and the array as it was, if there is no room. *) 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*(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; end; v.len := n; end; (* n zero elements opened at i, those from i on moved up; i from 0 to v.len. Stops the program, as push does, if there is no room. *) proc insert*(var v: Vec; i, n: INT); var zero: T; k: INT; begin if (i < 0) or (i > v.len) or (n < 0) then Rt.stop("Vec.insert: out of range"); end; need(v, n); (* COPY goes the right way for the overlap *) COPY(v.data[i + n..v.len + n], v.data[i..v.len]); for k := i to i + n - 1 do v.data[k] := zero; end; v.len := v.len + n; end; (* The n elements from i taken out, those after them moved down; i + n at most v.len. *) proc remove*(var v: Vec; i, n: INT); begin if (i < 0) or (n < 0) or (i + n > v.len) then Rt.stop("Vec.remove: out of range"); end; COPY(v.data[i..v.len - n], v.data[i + n..v.len]); v.len := v.len - n; end; (* The elements, until the array next grows. *) proc all*(const 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*(var v: Vec); begin v.len := 0; end; (* Gives the array's memory back, leaving it empty. *) 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; end; end Vec.