📄 util.c
字号:
if (linked_int->next != NULL) { printf ("Error in alloc_ivector_and_copy_int_list:\n Copied %d elements, " "but list at %p contains more.\n", num_items, list_head); exit (1); } linked_int->next = *free_list_head_ptr; *free_list_head_ptr = list_head; *list_head_ptr = NULL;}static int cont; /* line continued? */char *my_fgets(char *buf, int max_size, FILE *fp) { /* Get an input line, update the line number and cut off * * any comment part. A \ at the end of a line with no * * comment part (#) means continue. */ char *val; int i; cont = 0; val = fgets(buf,max_size,fp); linenum++; if (val == NULL) return(val);/* Check that line completely fit into buffer. (Flags long line * * truncation). */ for (i=0;i<max_size;i++) { if (buf[i] == '\n') break; if (buf[i] == '\0') { printf("Error on line %d -- line is too long for input buffer.\n", linenum); printf("All lines must be at most %d characters long.\n",BUFSIZE-2); printf("The problem could also be caused by a missing newline.\n"); exit (1); } } for (i=0;i<max_size && buf[i] != '\0';i++) { if (buf[i] == '#') { buf[i] = '\0'; break; } } if (i<2) return (val); if (buf[i-1] == '\n' && buf[i-2] == '\\') { cont = 1; /* line continued */ buf[i-2] = '\n'; /* May need this for tokens */ buf[i-1] = '\0'; } return(val);}char *my_strtok(char *ptr, char *tokens, FILE *fp, char *buf) {/* Get next token, and wrap to next line if \ at end of line. * * There is a bit of a "gotcha" in strtok. It does not make a * * copy of the character array which you pass by pointer on the * * first call. Thus, you must make sure this array exists for * * as long as you are using strtok to parse that line. Don't * * use local buffers in a bunch of subroutines calling each * * other; the local buffer may be overwritten when the stack is * * restored after return from the subroutine. */ char *val; val = strtok(ptr,tokens); while (1) { if (val != NULL || cont == 0) return(val); /* return unless we have a null value and a continuation line */ if (my_fgets(buf,BUFSIZE,fp) == NULL) return(NULL); val = strtok(buf,tokens); }}void free_ivec_vector (struct s_ivec *ivec_vector, int nrmin, int nrmax) {/* Frees a 1D array of integer vectors. */ int i; for (i=nrmin;i<=nrmax;i++) if (ivec_vector[i].nelem != 0) free (ivec_vector[i].list); free (ivec_vector + nrmin);}void free_ivec_matrix (struct s_ivec **ivec_matrix, int nrmin, int nrmax, int ncmin, int ncmax) {/* Frees a 2D matrix of integer vectors (ivecs). */ int i, j; for (i=nrmin;i<=nrmax;i++) { for (j=ncmin;j<=ncmax;j++) { if (ivec_matrix[i][j].nelem != 0) { free (ivec_matrix[i][j].list); } } } free_matrix (ivec_matrix, nrmin, nrmax, ncmin, sizeof (struct s_ivec));}void free_ivec_matrix3 (struct s_ivec ***ivec_matrix3, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax) {/* Frees a 3D matrix of integer vectors (ivecs). */ int i, j, k; for (i=nrmin;i<=nrmax;i++) { for (j=ncmin;j<=ncmax;j++) { for (k=ndmin;k<=ndmax;k++) { if (ivec_matrix3[i][j][k].nelem != 0) { free (ivec_matrix3[i][j][k].list); } } } } free_matrix3 (ivec_matrix3, nrmin, nrmax, ncmin, ncmax, ndmin, sizeof (struct s_ivec));}void **alloc_matrix (int nrmin, int nrmax, int ncmin, int ncmax, size_t elsize) {/* allocates an generic matrix with nrmax-nrmin + 1 rows and ncmax - * * ncmin + 1 columns, with each element of size elsize. i.e. * * returns a pointer to a storage block [nrmin..nrmax][ncmin..ncmax].* * Simply cast the returned array pointer to the proper type. */ int i; char **cptr; cptr = (char **) my_malloc ((nrmax - nrmin + 1) * sizeof (char *)); cptr -= nrmin; for (i=nrmin;i<=nrmax;i++) { cptr[i] = (char *) my_malloc ((ncmax - ncmin + 1) * elsize); cptr[i] -= ncmin * elsize / sizeof(char); /* sizeof(char) = 1 */ } return ((void **) cptr);}/* NB: need to make the pointer type void * instead of void ** to allow * * any pointer to be passed in without a cast. */void free_matrix (void *vptr, int nrmin, int nrmax, int ncmin, size_t elsize) { int i; char **cptr; cptr = (char **) vptr; for (i=nrmin;i<=nrmax;i++) free (cptr[i] + ncmin * elsize / sizeof (char)); free (cptr + nrmin); } void ***alloc_matrix3 (int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, size_t elsize) {/* allocates a 3D generic matrix with nrmax-nrmin + 1 rows, ncmax - * * ncmin + 1 columns, and a depth of ndmax-ndmin + 1, with each * * element of size elsize. i.e. returns a pointer to a storage block * * [nrmin..nrmax][ncmin..ncmax][ndmin..ndmax]. Simply cast the * * returned array pointer to the proper type. */ int i, j; char ***cptr; cptr = (char ***) my_malloc ((nrmax - nrmin + 1) * sizeof (char **)); cptr -= nrmin; for (i=nrmin;i<=nrmax;i++) { cptr[i] = (char **) my_malloc ((ncmax - ncmin + 1) * sizeof (char *)); cptr[i] -= ncmin; for (j=ncmin;j<=ncmax;j++) { cptr[i][j] = (char *) my_malloc ((ndmax - ndmin + 1) * elsize); cptr[i][j] -= ndmin * elsize / sizeof(char); /* sizeof(char) = 1) */ } } return ((void ***) cptr);}void free_matrix3 (void *vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, size_t elsize) { int i, j; char ***cptr; cptr = (char ***) vptr; for (i=nrmin;i<=nrmax;i++) { for (j=ncmin;j<=ncmax;j++) free (cptr[i][j] + ndmin * elsize / sizeof (char)); free (cptr[i] + ncmin); } free (cptr + nrmin); } /* Portable random number generator defined below. Taken from ANSI C by * * K & R. Not a great generator, but fast, and good enough for my needs. */#define IA 1103515245u#define IC 12345u#define IM 2147483648u#define CHECK_RAND static unsigned int current_random = 0;void my_srandom (int seed) { current_random = (unsigned int) seed;}int my_irand (int imax) {/* Creates a random integer between 0 and imax, inclusive. i.e. [0..imax] */ int ival;/* current_random = (current_random * IA + IC) % IM; */ current_random = current_random * IA + IC; /* Use overflow to wrap */ ival = current_random & (IM - 1); /* Modulus */ ival = (int) ((float) ival * (float) (imax + 0.999) / (float) IM);#ifdef CHECK_RAND if ((ival < 0) || (ival > imax)) { printf("Bad value in my_irand, imax = %d ival = %d\n",imax,ival); exit(1); }#endif return(ival);} float my_frand (void) { /* Creates a random float between 0 and 1. i.e. [0..1). */ float fval; int ival; current_random = current_random * IA + IC; /* Use overflow to wrap */ ival = current_random & (IM - 1); /* Modulus */ fval = (float) ival / (float) IM;#ifdef CHECK_RAND if ((fval < 0) || (fval > 1.)) { printf("Bad value in my_frand, fval = %g\n",fval); exit(1); }#endif return(fval);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -