This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void *array_alloc(int ndims, const int dims[ndims], int size) { | |
int n = 1; | |
for(int i = 0; i < ndims; i += 1) | |
n *= dims[i]; | |
return calloc(n, size); | |
} | |
int array_idx(int ndims, const int dims[ndims], const int idx[ndims]) { | |
int i = 0, s = 1; | |
for(int j = ndims - 1; j >= 0; j -= 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void *alloc_array(int ndims, const int dims[ndims], int size) { | |
if(ndims <= 0) exit(1); | |
if(ndims == 1) return calloc(dims[0], size); | |
int hsize = 0, n; | |
for(int n = 1, i = 0; i < ndims - 1; i += 1) | |
n *= dims[i], hsize += n; | |
void **head = calloc(hsize, sizeof(void*)), **ret = head; | |
for(n = dims[0]; ndims > 2; ndims -= 1, dims += 1) { | |
for(int i = 0, j = 0; i < n; i += 1, j += dims[1]) | |
head[i] = head + n + j; |