-
Notifications
You must be signed in to change notification settings - Fork 0
/
gMultiStack.adb
290 lines (266 loc) · 8.29 KB
/
gMultiStack.adb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
with Ada.Text_IO; use Ada.Text_IO;
package body gMultiStack is
type NameType is (Burris, Zhou, Shashidhar, Shannon, Yang, Smith, Wei,
Rabieh, Song, Cho, Varol, Karabiyik, Cooper, McGuire, Najar, An, Deering, Hope,
Pray, NoHope);
package NameType_IO is new Ada.Text_IO.Enumeration_IO(NameType);
use NameType_IO;
package IIO is new Ada.Text_IO.Integer_IO(integer); use IIO;
function getLowerBound return integer is
low: integer;
Output: File_Type;
begin
Create(Output, Append_File, Output_File);
put("Enter lower bound for array: ");
put(Output, "Enter lower bound for array: ");
get(low);
put(Output, low'Image);
Close(Output);
return low;
end getLowerBound;
function getUpperBound return integer is
high: integer;
Output: File_Type;
begin
Open(Output, Append_File, Output_File);
put("Enter upper bound for array: ");
put(Output, "Enter upper bound for array: ");
get(high);
put(Output, high'Image);
Close(Output);
return high;
end getUpperBound;
function getNumOfStacks return integer is
num: integer;
Output: File_Type;
begin
Open(Output, Append_File, Output_File);
put("Enter number of stacks: ");
put(Output, "Enter number of stacks: ");
get(num);
put(Output, num'Image);
Close(Output);
return num;
end getNumOfStacks;
function floor(x: float) return integer is
temp: integer;
begin
temp := integer(x);
if (float(temp) <= x) then
return temp;
else
return temp - 1;
end if;
end floor;
procedure runMultiStack is
StackSpace: arrayType(getLowerBound..getUpperBound);
N: integer := getNumOfStacks;
L: integer;
M: integer;
K: integer;
Base: intArray(1..N + 1);
Top: intArray(1..N);
Info: intArray(1..N + 1); --holds OldTop, Growth, and NewBase
opcode: String := " ";
input: MyType;
temp: String := " ";
Output: File_Type;
begin
Open(Output, Append_File, Output_File);
put("Enter lower bound for stacks: ");
put(Output, "Enter lower bound for stacks: ");
IIO.get(L);
put_line(Output, L'Image);
put("Enter upper bound for stacks: ");
put(Output, "Enter upper bound for stacks: ");
IIO.get(M);
put_Line(Output, M'Image);
put_line(Output, "");
for J in 1..N loop
Base(j) := floor((float(J) - 1.0)/float(N)*(float(M) - float(L))) + L;
Top(j) := Base(J);
Info(J) := Top(J); --collect OldTop values
end loop;
Base(N + 1) := M;
Info(N + 1) := M;
new_line;
put("Begin Opcodes, type 'EX' to quit: "); new_line;
put(Output, "Begin Opcodes, type 'EX' to quit: ");
Close(Output);
while opcode /= "EX" loop --loop until exit or out of memory
put("Opcode: "); new_line;
Open(Output, Append_File, Output_File);
put_line(Output, "Opcode: ");
Ada.Text_IO.get(opcode);
put(Output, opcode); put(Output, " ");
if opcode(1) = 'I' then
get(input);
put(Output, convert(input)); put_Line(Output, ""); put_Line(Output, "");
temp(1) := opcode(2);
K := Integer'Value(temp);
Top(K) := Top(K) + 1;
if Top(K) > Base(K + 1) then
put("OVERFLOW: calling 'reallocate'..."); new_line;
put_line(Output, "OVERFLOW: calling 'reallocate'..."); put_Line(Output, "");
put_line(Output, "---------------------------------------------------------");
put_line(Output, "<><><>Before<><><>");
Close(Output);
print(StackSpace, L, M, N, Base, Top, Info);
if reallocate(StackSpace, Info, Base, Top, L, M, N, K, input) = false then
opcode := "EX";
end if;
if opcode /= "EX" then
Open(Output, Append_File, Output_File);
put_line(Output, "");
put_line(Output, "<><><>After<><><>");
Close(Output);
print(StackSpace, L, M, N, Base, Top, Info);
Open(Output, Append_File, Output_File);
put_line(Output, "---------------------------------------------------------");
put_Line(Output, "");
Close(Output);
end if;
else
StackSpace(Top(K)) := input;
Close(Output);
end if;
end if;
if opcode(1) = 'D' then
temp(1) := opcode(2);
K := Integer'Value(temp);
if Top(K) = Base(K) then
put("UNDERFLOW: nothing in this stack to delete!"); new_line;
put_Line(Output, ""); put_Line(Output, "");
put_line(Output, "UNDERFLOW: nothing in this stack to delete!");
put_Line(Output, "");
else
put_Line(Output, ""); put_Line(Output, "");
input := StackSpace(Top(K));
Top(K) := Top(K) - 1;
end if;
Close(Output);
end if;
new_line;
end loop;
end runMultiStack;
function reallocate(StackSpace: in out arrayType; Info: in out intArray;
Base: in out intArray; Top: in out intArray; L: integer; M: integer;
N: integer; K: integer; input: MyType) return boolean is
AvailSpace: integer;
TotalInc: integer := 0;
MinSpace: integer := floor(0.05*(float(M) - float(L)));
J: integer := N;
Alpha: float;
Beta: float;
Sigma: float;
Tau: float;
temp: integer;
Output: File_Type;
begin
if MinSpace = 0 then
MinSpace := 1;
end if;
AvailSpace := M - L;
while J > 0 loop
AvailSpace := AvailSpace - (Top(J) - Base(J));
if Top(J) > Info(J) then
Info(J) := Top(J) - Info(J);
TotalInc := TotalInc + Info(J);
else
Info(J) := 0;
end if;
J := J - 1;
end loop;
if AvailSpace < (MinSpace - 1) then
put("OUT OF MEMORY, terminating program...");
Open(Output, Append_File, Output_File);
put_Line(Output, "");
put(Output, "OUT OF MEMORY, terminating program...");
Close(Output);
return false;
end if;
Alpha := 0.15 * float(AvailSpace) / float(N);
Beta := 0.85 * float(AvailSpace) / float(TotalInc);
Sigma := 0.0;
temp := Info(1);
Info(1) := Base(1);
for J in 2..N loop
Tau := Sigma + Alpha + float(temp)*Beta;
temp := Info(J);
Info(J) := Info(J-1) + Top(J-1) - Base(J-1) + floor(Tau) - floor(Sigma);
Sigma := Tau;
end loop;
Top(K) := Top(K) - 1;
movestack(StackSpace, Info, Top, Base, N);
Top(K) := Top(K) + 1;
StackSpace(Top(K)) := input;
for J in 1..N loop
Info(J) := Top(J);
end loop;
return true;
end reallocate;
procedure movestack(StackSpace: in out arrayType; Info: intArray;
Top: in out intArray; Base: in out intArray; N: integer) is
Delt: integer;
begin
for J in 2..N loop
if Info(J) < Base(J) then
Delt := Base(J) - Info(J);
for L in (Base(J)+1)..Top(J) loop
StackSpace(L - Delt) := StackSpace(L);
end loop;
Base(J) := Info(J);
Top(J) := Top(J) - Delt;
end if;
end loop;
for J in reverse 2..N loop
if Info(J) > Base(J) then
Delt := Info(J) - Base(J);
for L in reverse (Base(J)+1)..Top(J) loop
StackSpace(L + Delt) := StackSpace(L);
end loop;
Base(J) := Info(J);
Top(J) := Top(J) + Delt;
end if;
end loop;
end movestack;
procedure print(StackSpace: arrayType; L: integer; M: integer;
N: integer; Base: intArray; Top: intArray; Info: intArray) is
Output: File_Type;
begin
Open(Output, Append_File, Output_File);
for J in 1..N+1 loop
put("Base["); put(J,0); put("]: "); put(Base(J),0); put(" ");
put(Output, "Base["); put(Output, J'Image); put(Output, "]: ");
put(Output, Base(J)'Image); put(Output, " ");
if J <= N then
put("Top["); put(J,0); put("]: "); put(Top(J),0); put(" ");
put(Output, "Top["); put(Output, J'Image); put(Output, "]: ");
put(Output, Top(J)'Image); put(Output, " ");
put("OldTop["); put(J,0); put("]: "); put(Info(J),0); put(" ");
put(Output, "OldTop["); put(Output, J'Image); put(Output, "]: ");
put(Output, Info(J)'Image); put_line(Output, " ");
end if;
new_line;
end loop;
put_line(Output, ""); put_line(Output, "");
new_line;
for j in 1..N loop
for i in Base(j)+1..Top(j) loop
if i <= Base(j+1) then
put(i,0); put(": "); put(StackSpace(i)); new_line;
put(Output, i'Image); put(Output, ": ");
put_line(Output, convert(StackSpace(i)));
end if;
end loop;
for i in Top(j)+1..Base(j+1) loop
put(i,0); put(":"); put(" "); new_line;
put(Output, i'Image); put(Output, ":"); put_line(Output, " ");
end loop;
end loop;
Close(Output);
new_line;
end print;
begin
runMultiStack;
end gMultiStack;