Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

563548 views
1
/****************************************************************************
2
**
3
*A AllocateSpace.c ANUPQ source Eamonn O'Brien
4
**
5
*Y Copyright 1995-2001, Lehrstuhl D fuer Mathematik, RWTH Aachen, Germany
6
*Y Copyright 1995-2001, School of Mathematical Sciences, ANU, Australia
7
**
8
*/
9
10
#include "pq_defs.h"
11
#include "pcp_vars.h"
12
#include "constants.h"
13
14
/* allocate space for array y */
15
16
void Allocate_WorkSpace(int work_space, struct pcp_vars *pcp)
17
{
18
if ((y_address = (int *)malloc((work_space + 1) * sizeof(int))) ==
19
(int *)0) {
20
perror("malloc failed in Allocate_WorkSpace ()");
21
exit(FAILURE);
22
}
23
24
/* initialise the pcp structure */
25
pcp->fronty = 1;
26
pcp->backy = work_space;
27
}
28
29
/* allocate space for a vector, a, of size n,
30
whose subscript commences at position start */
31
32
int *allocate_vector(int n, int start, Logical zero)
33
34
/* start may be 0 or 1 */
35
36
{
37
int *a;
38
39
#ifdef DEBUG
40
printf("allocate vector of size %d\n", n);
41
#endif
42
43
/* some versions of malloc crash when repeatedly asked to allocate
44
small amounts of space -- in particular, under AIX and Ultrix */
45
if (n < 4)
46
n = 4;
47
48
if (zero) {
49
if ((a = (int *)calloc(n, sizeof(int))) == (int *)0) {
50
perror("Call to allocate_vector");
51
exit(FAILURE);
52
}
53
} else if ((a = (int *)malloc(n * sizeof(int))) == (int *)0) {
54
perror("Call to allocate_vector");
55
exit(FAILURE);
56
}
57
58
while (start) {
59
--a;
60
--start;
61
}
62
63
return a;
64
}
65
66
/* allocate space for an n x m integer matrix a,
67
whose subscripts start at position 0 or 1 */
68
69
int **allocate_matrix(int n, int m, int start, Logical zero)
70
{
71
int **a;
72
int i;
73
74
#ifdef DEBUG
75
printf("allocate matrix %d x %d\n", n, m);
76
#endif
77
78
if (n == 0)
79
n = 1;
80
if (m < 4)
81
m = 4;
82
83
if ((a = (int **)malloc(n * sizeof(int *))) == (int **)0) {
84
perror("Call to allocate_matrix");
85
exit(FAILURE);
86
}
87
if (start != 0)
88
--a;
89
90
for (i = start; i < start + n; ++i) {
91
if (zero) {
92
if ((a[i] = (int *)calloc(m, sizeof(int))) == (int *)0) {
93
perror("Call to allocate_matrix");
94
exit(FAILURE);
95
}
96
} else if ((a[i] = (int *)malloc(m * sizeof(int))) == (int *)0) {
97
perror("Call to allocate_matrix");
98
exit(FAILURE);
99
}
100
if (start != 0)
101
--a[i];
102
}
103
104
return a;
105
}
106
107
/* allocate space for an n x m x r integer array a,
108
whose subscripts begin at 1, not 0 */
109
110
int ***allocate_array(int n, int m, int r, Logical zero)
111
{
112
int ***a;
113
register int i, j;
114
115
#ifdef DEBUG
116
printf("allocate array %d x %d x %d\n", n, m, r);
117
#endif
118
119
if (n == 0)
120
n = 1;
121
if (m == 0)
122
m = 1;
123
if (r < 4)
124
r = 4;
125
126
if ((a = (int ***)malloc(n * sizeof(int **))) == (int ***)0) {
127
perror("Call to allocate_array");
128
exit(FAILURE);
129
}
130
--a;
131
132
for (i = 1; i <= n; ++i) {
133
if ((a[i] = (int **)malloc(m * sizeof(int *))) == (int **)0) {
134
perror("Call to allocate_array");
135
exit(FAILURE);
136
}
137
--a[i];
138
for (j = 1; j <= m; ++j) {
139
if (zero) {
140
if ((a[i][j] = (int *)calloc(r, sizeof(int))) == (int *)0) {
141
perror("Call to allocate_array");
142
exit(FAILURE);
143
}
144
} else if ((a[i][j] = (int *)malloc(r * sizeof(int))) == (int *)0) {
145
perror("Call to allocate_array");
146
exit(FAILURE);
147
}
148
--a[i][j];
149
}
150
}
151
152
return a;
153
}
154
155
/* reallocate space for a vector, a, of size new which
156
was originally of size original */
157
158
int *reallocate_vector(int *a, int original, int new, int start, Logical zero)
159
{
160
int j;
161
162
#ifdef DEBUG
163
printf("reallocate vector\n");
164
#endif
165
166
if (original < 4)
167
original = 4;
168
169
if (start && original != 0)
170
++a;
171
172
#ifdef DEBUG
173
printf("In reallocate: original = %d; new = %d\n", original, new);
174
printf("before reallocate: a = %d\n", a);
175
#endif
176
177
if ((a = (int *)realloc(a, new * sizeof(int))) == (int *)0) {
178
#ifdef DEBUG
179
printf("Original size is %d; new size is %d\n", original, new);
180
#endif
181
perror("Call to reallocate_vector");
182
exit(FAILURE);
183
}
184
185
if (start)
186
--a;
187
188
if (zero)
189
for (j = start + original; j < start + new; ++j)
190
a[j] = 0;
191
192
#ifdef DEBUG
193
printf("after reallocate: a = %d\n", a);
194
#endif
195
return a;
196
}
197
198
/* reallocate space for an n x m integer matrix a, whose subscripts begin
199
at 1, not 0; the original sizes are supplied */
200
201
int **
202
reallocate_matrix(int **a, int orig_n, int orig_m, int n, int m, Logical zero)
203
{
204
register int i, j;
205
206
#ifdef DEBUG
207
printf("reallocate matrix\n");
208
#endif
209
210
if (orig_n == 0)
211
orig_n = 1;
212
if (orig_m < 4)
213
orig_m = 4;
214
215
if ((a = (int **)realloc(++a, n * sizeof(int *))) == (int **)0) {
216
perror("Call to reallocate_matrix");
217
exit(FAILURE);
218
}
219
--a;
220
221
for (i = 1; i <= n; ++i) {
222
if (i > orig_n) {
223
if ((a[i] = (int *)malloc(m * sizeof(int))) == (int *)0) {
224
perror("Call to reallocate_matrix");
225
exit(FAILURE);
226
}
227
} else {
228
if ((a[i] = (int *)realloc(++a[i], m * sizeof(int))) == (int *)0) {
229
perror("Call to reallocate_matrix");
230
exit(FAILURE);
231
}
232
}
233
--a[i];
234
}
235
236
if (zero) {
237
for (i = 1; i <= n; ++i)
238
for (j = 1; j <= m; ++j)
239
if (i > orig_n || j > orig_m)
240
a[i][j] = 0;
241
}
242
243
return a;
244
}
245
246
/* reallocate space for an n x m x r integer array a,
247
whose subscripts begin at 1, not 0; the original
248
sizes are supplied */
249
250
int ***reallocate_array(int ***a,
251
int orig_n,
252
int orig_m,
253
int orig_r,
254
int n,
255
int m,
256
int r,
257
Logical zero)
258
{
259
register int i, j, k;
260
261
#ifdef DEBUG
262
printf("reallocate array\n");
263
#endif
264
265
if (orig_n == 0)
266
orig_n = 1;
267
if (orig_m == 0)
268
orig_m = 1;
269
if (orig_r < 4)
270
orig_r = 4;
271
272
if ((a = (int ***)realloc(++a, n * sizeof(int **))) == (int ***)0) {
273
perror("Call to reallocate_array");
274
exit(FAILURE);
275
}
276
--a;
277
278
for (i = 1; i <= n; ++i) {
279
if (i > orig_n) {
280
if ((a[i] = (int **)malloc(m * sizeof(int *))) == (int **)0) {
281
perror("Call to reallocate_array");
282
exit(FAILURE);
283
}
284
} else {
285
if ((a[i] = (int **)realloc(++a[i], m * sizeof(int *))) == (int **)0) {
286
perror("Call to reallocate_array");
287
exit(FAILURE);
288
}
289
}
290
--a[i];
291
292
for (j = 1; j <= m; ++j) {
293
if (j > orig_m || i > orig_n) {
294
if ((a[i][j] = (int *)malloc(r * sizeof(int))) == (int *)0) {
295
perror("Call to allocate_array");
296
exit(FAILURE);
297
}
298
} else {
299
if ((a[i][j] = (int *)realloc(++a[i][j], r * sizeof(int))) ==
300
(int *)0) {
301
perror("Call to allocate_array");
302
exit(FAILURE);
303
}
304
}
305
--a[i][j];
306
}
307
}
308
309
if (zero) {
310
for (i = 1; i <= n; ++i)
311
for (j = 1; j <= m; ++j)
312
for (k = 1; k <= r; ++k)
313
if (i > orig_n || j > orig_m || k > orig_r)
314
a[i][j][k] = 0;
315
}
316
317
return a;
318
}
319
320
/* allocate space for a character vector, a, of size n,
321
whose subscript commences at position start */
322
323
char *allocate_char_vector(int n, int start, Logical zero)
324
{
325
char *a;
326
327
#ifdef DEBUG
328
printf("allocate char vector\n");
329
#endif
330
331
if (n < 4)
332
n = 4;
333
334
if (zero) {
335
if ((a = (char *)calloc(n, sizeof(char))) == (char *)0) {
336
perror("Call to allocate_char_vector");
337
exit(FAILURE);
338
}
339
} else if ((a = (char *)malloc(n * sizeof(char))) == (char *)0) {
340
perror("Call to allocate_char_vector");
341
exit(FAILURE);
342
}
343
344
while (start) {
345
--a;
346
--start;
347
}
348
349
return a;
350
}
351
352
/* allocate space for an n x m character matrix a,
353
whose subscripts start at position 0 or 1 */
354
355
char **allocate_char_matrix(int n, int m, int start, Logical zero)
356
{
357
char **a;
358
int i;
359
360
#ifdef DEBUG
361
printf("allocate char matrix\n");
362
#endif
363
364
if (n == 0)
365
n = 1;
366
if (m < 4)
367
m = 4;
368
369
if ((a = (char **)malloc(n * sizeof(char *))) == (char **)0) {
370
perror("Call to allocate_matrix");
371
exit(FAILURE);
372
}
373
if (start != 0)
374
--a;
375
376
for (i = start; i < start + n; ++i) {
377
if (zero) {
378
if ((a[i] = (char *)calloc(m, sizeof(char))) == (char *)0) {
379
perror("Call to allocate_matrix");
380
exit(FAILURE);
381
}
382
} else if ((a[i] = (char *)malloc(m * sizeof(char))) == (char *)0) {
383
perror("Call to allocate_matrix");
384
exit(FAILURE);
385
}
386
if (start != 0)
387
--a[i];
388
}
389
390
return a;
391
}
392
393
/* allocate space for an n x m x r character array a,
394
whose subscripts begin at 1, not 0 */
395
396
char ***allocate_char_array(int n, int m, int r, Logical zero)
397
{
398
char ***a;
399
register int i, j;
400
401
#ifdef DEBUG
402
printf("allocate char array\n");
403
#endif
404
405
if (n == 0)
406
n = 1;
407
if (m == 0)
408
m = 1;
409
if (r < 4)
410
r = 4;
411
412
if ((a = (char ***)malloc(n * sizeof(char **))) == (char ***)0) {
413
perror("Call to allocate_char_array");
414
exit(FAILURE);
415
}
416
--a;
417
418
for (i = 1; i <= n; ++i) {
419
if ((a[i] = (char **)malloc(m * sizeof(char *))) == (char **)0) {
420
perror("Call to allocate_char_array");
421
exit(FAILURE);
422
}
423
--a[i];
424
for (j = 1; j <= m; ++j) {
425
if (zero) {
426
if ((a[i][j] = (char *)calloc(r, sizeof(char))) == (char *)0) {
427
perror("Call to allocate_char_array");
428
exit(FAILURE);
429
}
430
} else {
431
if ((a[i][j] = (char *)malloc(r * sizeof(char))) == (char *)0) {
432
perror("Call to allocate_char_array");
433
exit(FAILURE);
434
}
435
}
436
--a[i][j];
437
}
438
}
439
440
return a;
441
}
442
443