diff options
author | Jasper | 2025-09-05 14:18:04 +0200 |
---|---|---|
committer | Jasper | 2025-09-05 14:18:04 +0200 |
commit | fc668c18d7b4caf9cad5e7ed39c1dbdcfc683cb6 (patch) | |
tree | e09c4689b13acdffa9c9689288145ca2d78dfb38 /matrix.h | |
parent | 4d3a7eb1bf238b5f7abeca8efd09e35254bba1a0 (diff) |
Added macros to iterate over matrix
This avoids having to write double for-loops again and again.
- 'matrix_loop' just replaces the loops and gives access to the indices i
and j
- 'matrix_foreach' initializes a double pointer to the current element
- 'matrix_foreach_idx' is combination of the two above
All for-loops that simply iterate over a matrix have been replaced.
Diffstat (limited to 'matrix.h')
-rw-r--r-- | matrix.h | 7 |
1 files changed, 7 insertions, 0 deletions
@@ -4,6 +4,13 @@ #include <stddef.h> #define matrix_at(mat, i, j) ((mat)->xs[(i) * (mat)->n + (j)]) + +#define matrix_loop(mat, i, j) for (size_t i = 0; i < (mat)->m; i++) for (size_t j = 0; j < (mat)->m; j++) + +#define matrix_foreach(mat, it) for (double *it = (mat)->xs; it < (mat)->xs + ((mat)->m * (mat)->n); ++it) + +#define matrix_foreach_idx(mat, it, i, j) double *it = (mat)->xs; for (size_t i = 0; i < (mat)->m; i++) for (size_t j = 0; j < (mat)->n; j++, ++it) + #define TRUE 1 #define FALSE 0 |