GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
/****************************************************************************1**2*A AllocateSpace.c ANUPQ source Eamonn O'Brien3**4*Y Copyright 1995-2001, Lehrstuhl D fuer Mathematik, RWTH Aachen, Germany5*Y Copyright 1995-2001, School of Mathematical Sciences, ANU, Australia6**7*/89#include "pq_defs.h"10#include "pcp_vars.h"11#include "constants.h"1213/* allocate space for array y */1415void Allocate_WorkSpace(int work_space, struct pcp_vars *pcp)16{17if ((y_address = (int *)malloc((work_space + 1) * sizeof(int))) ==18(int *)0) {19perror("malloc failed in Allocate_WorkSpace ()");20exit(FAILURE);21}2223/* initialise the pcp structure */24pcp->fronty = 1;25pcp->backy = work_space;26}2728/* allocate space for a vector, a, of size n,29whose subscript commences at position start */3031int *allocate_vector(int n, int start, Logical zero)3233/* start may be 0 or 1 */3435{36int *a;3738#ifdef DEBUG39printf("allocate vector of size %d\n", n);40#endif4142/* some versions of malloc crash when repeatedly asked to allocate43small amounts of space -- in particular, under AIX and Ultrix */44if (n < 4)45n = 4;4647if (zero) {48if ((a = (int *)calloc(n, sizeof(int))) == (int *)0) {49perror("Call to allocate_vector");50exit(FAILURE);51}52} else if ((a = (int *)malloc(n * sizeof(int))) == (int *)0) {53perror("Call to allocate_vector");54exit(FAILURE);55}5657while (start) {58--a;59--start;60}6162return a;63}6465/* allocate space for an n x m integer matrix a,66whose subscripts start at position 0 or 1 */6768int **allocate_matrix(int n, int m, int start, Logical zero)69{70int **a;71int i;7273#ifdef DEBUG74printf("allocate matrix %d x %d\n", n, m);75#endif7677if (n == 0)78n = 1;79if (m < 4)80m = 4;8182if ((a = (int **)malloc(n * sizeof(int *))) == (int **)0) {83perror("Call to allocate_matrix");84exit(FAILURE);85}86if (start != 0)87--a;8889for (i = start; i < start + n; ++i) {90if (zero) {91if ((a[i] = (int *)calloc(m, sizeof(int))) == (int *)0) {92perror("Call to allocate_matrix");93exit(FAILURE);94}95} else if ((a[i] = (int *)malloc(m * sizeof(int))) == (int *)0) {96perror("Call to allocate_matrix");97exit(FAILURE);98}99if (start != 0)100--a[i];101}102103return a;104}105106/* allocate space for an n x m x r integer array a,107whose subscripts begin at 1, not 0 */108109int ***allocate_array(int n, int m, int r, Logical zero)110{111int ***a;112register int i, j;113114#ifdef DEBUG115printf("allocate array %d x %d x %d\n", n, m, r);116#endif117118if (n == 0)119n = 1;120if (m == 0)121m = 1;122if (r < 4)123r = 4;124125if ((a = (int ***)malloc(n * sizeof(int **))) == (int ***)0) {126perror("Call to allocate_array");127exit(FAILURE);128}129--a;130131for (i = 1; i <= n; ++i) {132if ((a[i] = (int **)malloc(m * sizeof(int *))) == (int **)0) {133perror("Call to allocate_array");134exit(FAILURE);135}136--a[i];137for (j = 1; j <= m; ++j) {138if (zero) {139if ((a[i][j] = (int *)calloc(r, sizeof(int))) == (int *)0) {140perror("Call to allocate_array");141exit(FAILURE);142}143} else if ((a[i][j] = (int *)malloc(r * sizeof(int))) == (int *)0) {144perror("Call to allocate_array");145exit(FAILURE);146}147--a[i][j];148}149}150151return a;152}153154/* reallocate space for a vector, a, of size new which155was originally of size original */156157int *reallocate_vector(int *a, int original, int new, int start, Logical zero)158{159int j;160161#ifdef DEBUG162printf("reallocate vector\n");163#endif164165if (original < 4)166original = 4;167168if (start && original != 0)169++a;170171#ifdef DEBUG172printf("In reallocate: original = %d; new = %d\n", original, new);173printf("before reallocate: a = %d\n", a);174#endif175176if ((a = (int *)realloc(a, new * sizeof(int))) == (int *)0) {177#ifdef DEBUG178printf("Original size is %d; new size is %d\n", original, new);179#endif180perror("Call to reallocate_vector");181exit(FAILURE);182}183184if (start)185--a;186187if (zero)188for (j = start + original; j < start + new; ++j)189a[j] = 0;190191#ifdef DEBUG192printf("after reallocate: a = %d\n", a);193#endif194return a;195}196197/* reallocate space for an n x m integer matrix a, whose subscripts begin198at 1, not 0; the original sizes are supplied */199200int **201reallocate_matrix(int **a, int orig_n, int orig_m, int n, int m, Logical zero)202{203register int i, j;204205#ifdef DEBUG206printf("reallocate matrix\n");207#endif208209if (orig_n == 0)210orig_n = 1;211if (orig_m < 4)212orig_m = 4;213214if ((a = (int **)realloc(++a, n * sizeof(int *))) == (int **)0) {215perror("Call to reallocate_matrix");216exit(FAILURE);217}218--a;219220for (i = 1; i <= n; ++i) {221if (i > orig_n) {222if ((a[i] = (int *)malloc(m * sizeof(int))) == (int *)0) {223perror("Call to reallocate_matrix");224exit(FAILURE);225}226} else {227if ((a[i] = (int *)realloc(++a[i], m * sizeof(int))) == (int *)0) {228perror("Call to reallocate_matrix");229exit(FAILURE);230}231}232--a[i];233}234235if (zero) {236for (i = 1; i <= n; ++i)237for (j = 1; j <= m; ++j)238if (i > orig_n || j > orig_m)239a[i][j] = 0;240}241242return a;243}244245/* reallocate space for an n x m x r integer array a,246whose subscripts begin at 1, not 0; the original247sizes are supplied */248249int ***reallocate_array(int ***a,250int orig_n,251int orig_m,252int orig_r,253int n,254int m,255int r,256Logical zero)257{258register int i, j, k;259260#ifdef DEBUG261printf("reallocate array\n");262#endif263264if (orig_n == 0)265orig_n = 1;266if (orig_m == 0)267orig_m = 1;268if (orig_r < 4)269orig_r = 4;270271if ((a = (int ***)realloc(++a, n * sizeof(int **))) == (int ***)0) {272perror("Call to reallocate_array");273exit(FAILURE);274}275--a;276277for (i = 1; i <= n; ++i) {278if (i > orig_n) {279if ((a[i] = (int **)malloc(m * sizeof(int *))) == (int **)0) {280perror("Call to reallocate_array");281exit(FAILURE);282}283} else {284if ((a[i] = (int **)realloc(++a[i], m * sizeof(int *))) == (int **)0) {285perror("Call to reallocate_array");286exit(FAILURE);287}288}289--a[i];290291for (j = 1; j <= m; ++j) {292if (j > orig_m || i > orig_n) {293if ((a[i][j] = (int *)malloc(r * sizeof(int))) == (int *)0) {294perror("Call to allocate_array");295exit(FAILURE);296}297} else {298if ((a[i][j] = (int *)realloc(++a[i][j], r * sizeof(int))) ==299(int *)0) {300perror("Call to allocate_array");301exit(FAILURE);302}303}304--a[i][j];305}306}307308if (zero) {309for (i = 1; i <= n; ++i)310for (j = 1; j <= m; ++j)311for (k = 1; k <= r; ++k)312if (i > orig_n || j > orig_m || k > orig_r)313a[i][j][k] = 0;314}315316return a;317}318319/* allocate space for a character vector, a, of size n,320whose subscript commences at position start */321322char *allocate_char_vector(int n, int start, Logical zero)323{324char *a;325326#ifdef DEBUG327printf("allocate char vector\n");328#endif329330if (n < 4)331n = 4;332333if (zero) {334if ((a = (char *)calloc(n, sizeof(char))) == (char *)0) {335perror("Call to allocate_char_vector");336exit(FAILURE);337}338} else if ((a = (char *)malloc(n * sizeof(char))) == (char *)0) {339perror("Call to allocate_char_vector");340exit(FAILURE);341}342343while (start) {344--a;345--start;346}347348return a;349}350351/* allocate space for an n x m character matrix a,352whose subscripts start at position 0 or 1 */353354char **allocate_char_matrix(int n, int m, int start, Logical zero)355{356char **a;357int i;358359#ifdef DEBUG360printf("allocate char matrix\n");361#endif362363if (n == 0)364n = 1;365if (m < 4)366m = 4;367368if ((a = (char **)malloc(n * sizeof(char *))) == (char **)0) {369perror("Call to allocate_matrix");370exit(FAILURE);371}372if (start != 0)373--a;374375for (i = start; i < start + n; ++i) {376if (zero) {377if ((a[i] = (char *)calloc(m, sizeof(char))) == (char *)0) {378perror("Call to allocate_matrix");379exit(FAILURE);380}381} else if ((a[i] = (char *)malloc(m * sizeof(char))) == (char *)0) {382perror("Call to allocate_matrix");383exit(FAILURE);384}385if (start != 0)386--a[i];387}388389return a;390}391392/* allocate space for an n x m x r character array a,393whose subscripts begin at 1, not 0 */394395char ***allocate_char_array(int n, int m, int r, Logical zero)396{397char ***a;398register int i, j;399400#ifdef DEBUG401printf("allocate char array\n");402#endif403404if (n == 0)405n = 1;406if (m == 0)407m = 1;408if (r < 4)409r = 4;410411if ((a = (char ***)malloc(n * sizeof(char **))) == (char ***)0) {412perror("Call to allocate_char_array");413exit(FAILURE);414}415--a;416417for (i = 1; i <= n; ++i) {418if ((a[i] = (char **)malloc(m * sizeof(char *))) == (char **)0) {419perror("Call to allocate_char_array");420exit(FAILURE);421}422--a[i];423for (j = 1; j <= m; ++j) {424if (zero) {425if ((a[i][j] = (char *)calloc(r, sizeof(char))) == (char *)0) {426perror("Call to allocate_char_array");427exit(FAILURE);428}429} else {430if ((a[i][j] = (char *)malloc(r * sizeof(char))) == (char *)0) {431perror("Call to allocate_char_array");432exit(FAILURE);433}434}435--a[i][j];436}437}438439return a;440}441442443