raz / lib / Vec.raz

164 lines · 4.5 KiB · raw · history · last change e0d008cd

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. *)
10module Vec(T);
11import Mem, Rt, Sys;
12import SYSTEM;
13
14type
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. *)
23proc init*(var v: Vec; a: ^Mem.Allocator);
24begin
25 v := {a: a};
26end;
27
28(* Room for n more elements; false if the allocator has none. *)
29proc room*(var v: Vec; n: INT): BOOL;
30var cap: INT; p: ^T;
31begin
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;
50end;
51
52(* Room for n more elements, or the program stops. *)
53proc need(var v: Vec; n: INT);
54begin
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;
60end;
61
62(* Adds x at the end. *)
63proc push*(var v: Vec; x: T);
64begin
65 need(v, 1);
66 v.data[v.len] := x;
67 INC(v.len);
68end;
69
70(* The last element, taken off; false if the array is empty. It keeps the
71 memory, as resize does. *)
72proc pop*(var v: Vec): (T, BOOL);
73var x: T;
74begin
75 if v.len = 0 then return x, false; end;
76 DEC(v.len);
77 return v.data[v.len], true;
78end;
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. *)
83proc add*(var v: Vec): ^T;
84var zero: T;
85begin
86 need(v, 1);
87 v.data[v.len] := zero;
88 INC(v.len);
89 return v.data + (v.len - 1);
90end;
91
92(* push, or false, and the array as it was, if there is no room. *)
93proc tryPush*(var v: Vec; x: T): BOOL;
94begin
95 if not room(v, 1) then return false; end;
96 v.data[v.len] := x;
97 INC(v.len);
98 return true;
99end;
100
101(* add, or nil and false, and the array as it was, if there is no room. *)
102proc tryAdd*(var v: Vec): (^T, BOOL);
103begin
104 if not room(v, 1) then return nil, false; end;
105 return add(v), true;
106end;
107
108(* Makes the array n elements long: cut short, or grown with zeros. *)
109proc resize*(var v: Vec; n: INT);
110var zero: T; i: INT;
111begin
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;
117end;
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. *)
121proc insert*(var v: Vec; i, n: INT);
122var zero: T; k: INT;
123begin
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;
130end;
131
132(* The n elements from i taken out, those after them moved down; i + n
133 at most v.len. *)
134proc remove*(var v: Vec; i, n: INT);
135begin
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;
139end;
140
141(* The elements, until the array next grows. *)
142proc all*(const v: Vec): []T;
143var none: []T;
144begin
145 if v.len = 0 then return none; end;
146 return v.data[0..v.len];
147end;
148
149(* Empties the array, keeping its memory for what comes next. *)
150proc reset*(var v: Vec);
151begin
152 v.len := 0;
153end;
154
155(* Gives the array's memory back, leaving it empty. *)
156proc done*(var v: Vec);
157begin
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;
162end;
163
164end Vec.