From d0b60da673e11afa299173c1973cb974b9ddc6bb Mon Sep 17 00:00:00 2001 From: Florian Burgener <florian.burgener@hesge.ch> Date: Tue, 16 Nov 2021 20:36:39 +0100 Subject: [PATCH] Refactoring --- matrix.c | 39 ++++++++++++++++++++++++++++----------- matrix.h | 4 ++-- matrix_compute.c | 6 ++---- matrix_test.c | 2 +- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/matrix.c b/matrix.c index 306733f..d584685 100644 --- a/matrix.c +++ b/matrix.c @@ -1,6 +1,6 @@ /** * @file matrix.c - * @author Florian Burgener + * @author Florian Burgener (florian.burgener@etu.hesge.ch) * @brief Implémentation de librairie matrix. * @version 1.0 * @date 2021-11-16 @@ -11,7 +11,6 @@ #include "matrix.h" -#include <math.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> @@ -22,15 +21,19 @@ error_code matrix_alloc(matrix *mat, int32_t m, int32_t n) { mat->n = n; int32_t **data = (int32_t **)malloc(sizeof(int32_t *) * mat->m); - if (data == NULL) + if (data == NULL) { return err; + } + mat->data = data; for (int32_t i = 0; i < m; i += 1) { int32_t *row = (int32_t *)malloc(sizeof(int32_t) * n); - if (row == NULL) + if (row == NULL) { return err; + } + mat->data[i] = row; } @@ -38,7 +41,9 @@ error_code matrix_alloc(matrix *mat, int32_t m, int32_t n) { } error_code matrix_init(matrix *mat, int32_t m, int32_t n, int32_t val) { - matrix_alloc(mat, m, n); + if (matrix_alloc(mat, m, n) == err) { + return err; + } for (int32_t y = 0; y < m; y += 1) { for (int32_t x = 0; x < n; x += 1) { @@ -75,9 +80,13 @@ error_code matrix_destroy(matrix *mat) { } error_code matrix_init_from_array(matrix *mat, int32_t m, int32_t n, int32_t data[], int32_t s) { - if (m * n != s) + if (m * n != s) { + return err; + } + + if (matrix_alloc(mat, m, n) == err) { return err; - matrix_alloc(mat, m, n); + } for (int32_t y = 0; y < mat->m; y += 1) { for (int32_t x = 0; x < mat->n; x += 1) { @@ -89,7 +98,9 @@ error_code matrix_init_from_array(matrix *mat, int32_t m, int32_t n, int32_t dat } error_code matrix_clone(matrix *cloned, const matrix mat) { - matrix_alloc(cloned, mat.m, mat.n); + if (matrix_alloc(cloned, mat.m, mat.n) == err) { + return err; + } for (int32_t y = 0; y < mat.m; y += 1) { for (int32_t x = 0; x < mat.n; x += 1) { @@ -101,7 +112,9 @@ error_code matrix_clone(matrix *cloned, const matrix mat) { } error_code matrix_transpose(matrix *transposed, const matrix mat) { - matrix_alloc(transposed, mat.n, mat.m); + if (matrix_alloc(transposed, mat.n, mat.m) == err) { + return err; + } for (int32_t y = 0; y < mat.m; y += 1) { for (int32_t x = 0; x < mat.n; x += 1) { @@ -113,9 +126,13 @@ error_code matrix_transpose(matrix *transposed, const matrix mat) { } error_code matrix_extract_submatrix(matrix *sub, const matrix mat, int32_t m0, int32_t m1, int32_t n0, int32_t n1) { - if (m1 < m0 || n1 < n0 || m0 < 0 || n0 < 0 || m1 > mat.m || n1 > mat.n) + if (m1 <= m0 || n1 <= n0 || m0 < 0 || n0 < 0 || m1 > mat.m || n1 > mat.n) { return err; - matrix_alloc(sub, m1 - m0, n1 - n0); + } + + if (matrix_alloc(sub, m1 - m0, n1 - n0) == err) { + return err; + } for (int32_t y = m0; y < m1; y += 1) { for (int32_t x = n0; x < n1; x += 1) { diff --git a/matrix.h b/matrix.h index d202555..7de4b72 100644 --- a/matrix.h +++ b/matrix.h @@ -1,6 +1,6 @@ /** * @file matrix.h - * @author Florian Burgener + * @author Florian Burgener (florian.burgener@etu.hesge.ch) * @brief Header de librairie matrix. * @version 1.0 * @date 2021-11-16 @@ -11,7 +11,7 @@ #ifndef MATRIX_HEADER #define MATRIX_HEADER -#include <math.h> + #include <stdbool.h> #include <stdint.h> diff --git a/matrix_compute.c b/matrix_compute.c index a18c184..d01f29a 100644 --- a/matrix_compute.c +++ b/matrix_compute.c @@ -1,6 +1,6 @@ /** * @file matrix_compute.c - * @author Florian Burgener + * @author Florian Burgener (florian.burgener@etu.hesge.ch) * @brief Démonstration de libraire matrix. * @version 1.0 * @date 2021-11-16 @@ -9,11 +9,9 @@ * */ -#include <math.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> -#include <time.h> #include "matrix.h" @@ -42,7 +40,7 @@ int main() { printf("******\n"); matrix mat7; - matrix_extract_submatrix(&mat7, mat4, 1, 3, 1, 2); + matrix_extract_submatrix(&mat7, mat4, 1, 4, 1, 3); matrix_print(mat7); printf("*******\n"); diff --git a/matrix_test.c b/matrix_test.c index 4350a5c..c82db06 100644 --- a/matrix_test.c +++ b/matrix_test.c @@ -1,6 +1,6 @@ /** * @file matrix_test.c - * @author Florian Burgener et Quentin Fasler + * @author Florian Burgener (florian.burgener@etu.hesge.ch) et Quentin Fasler * @brief Seul les tests ont été fait en commun pour gagner du temps. * @version 1.0 * @date 2021-11-16 -- GitLab