From fc668c18d7b4caf9cad5e7ed39c1dbdcfc683cb6 Mon Sep 17 00:00:00 2001 From: Jasper Date: Fri, 5 Sep 2025 14:18:04 +0200 Subject: 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. --- matrix.h | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'matrix.h') diff --git a/matrix.h b/matrix.h index 5ee5dc3..dc09ef6 100644 --- a/matrix.h +++ b/matrix.h @@ -4,6 +4,13 @@ #include #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 -- cgit v1.2.3