diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-10-11 14:09:59 -0600 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-10-11 14:09:59 -0600 |
commit | faaef032d866f956665653e58086af9872c3c093 (patch) | |
tree | a3eb7311af47755c8ad50e7d8e0af4d892c5c200 /inc/macros.h | |
parent | 1ce75ab5fe461ddd3823403754f5b4c543016abc (diff) | |
download | cmath-faaef032d866f956665653e58086af9872c3c093.tar.gz cmath-faaef032d866f956665653e58086af9872c3c093.zip |
matrices
Diffstat (limited to 'inc/macros.h')
-rw-r--r-- | inc/macros.h | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/inc/macros.h b/inc/macros.h index 841084d..f6e531e 100644 --- a/inc/macros.h +++ b/inc/macros.h @@ -11,15 +11,20 @@ size_t size; \ } Array_##TYPE +#define DEFINE_MATRIX(TYPE) \ + typedef struct { \ + Array_##TYPE **data; \ + size_t cols; \ + size_t rows; \ + } Matrix_##TYPE + #define InitArray(TYPE, ...) \ ({ \ TYPE temp[] = __VA_ARGS__; \ Array_##TYPE *arr = malloc(sizeof(Array_##TYPE)); \ arr->size = sizeof(temp) / sizeof(temp[0]); \ arr->data = malloc(arr->size * sizeof(TYPE)); \ - if (arr->data) { \ - memcpy(arr->data, temp, arr->size * sizeof(TYPE)); \ - } \ + memcpy(arr->data, temp, arr->size * sizeof(TYPE)); \ arr; \ }) @@ -28,13 +33,22 @@ Array_##TYPE *arr = malloc(sizeof(Array_##TYPE)); \ arr->size = SIZE; \ arr->data = malloc(arr->size * sizeof(TYPE)); \ - if (arr->data) { \ - for (size_t i = 0; i < arr->size; i++) \ - arr->data[i] = INIT_VALUE; \ - } \ + for (size_t i = 0; i < arr->size; i++) \ + arr->data[i] = INIT_VALUE; \ arr; \ }) +#define InitMatrixWithSize(TYPE, ROWS, COLS, INIT_VALUE) \ + ({ \ + Matrix_##TYPE *matrix = malloc(sizeof(Matrix_##TYPE)); \ + matrix->rows = ROWS; \ + matrix->cols = COLS; \ + matrix->data = malloc(matrix->rows * sizeof(Array_##TYPE *)); \ + for (size_t y = 0; y < matrix->rows; y++) \ + matrix->data[y] = InitArrayWithSize(TYPE, COLS, INIT_VALUE); \ + matrix; \ + }) + #define c_max(x, y) (((x) >= (y)) ? (x) : (y)) #define c_min(x, y) (((x) <= (y)) ? (x) : (y)) |