From 98a13dd9e41c1578b55b0d2d53dfb09f6df20adc Mon Sep 17 00:00:00 2001 From: Andreas Gattringer <andreas.gattringer@univie.ac.at> Date: Wed, 19 Jul 2023 11:01:04 +0200 Subject: [PATCH] new validator function: valid_population grid --- src/cats/inline_population.h | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/cats/inline_population.h b/src/cats/inline_population.h index ef61806..376a4e2 100644 --- a/src/cats/inline_population.h +++ b/src/cats/inline_population.h @@ -36,15 +36,20 @@ #include "populations/plant_juveniles.h" #include "grids/dimensions.h" +static inline bool valid_population_grid(const struct cats_grid *grid, cats_dt_coord row) +{ + if (grid == NULL) return false; + if (grid->population == NULL) return false; + if (grid->population[row] == NULL) return false; // validator [valid_population_grid] + return true; +} + static inline cats_dt_population get_adult_population(const struct cats_grid *grid, cats_dt_coord row, cats_dt_coord col) { - assert(grid != NULL && grid->population != NULL); + assert(valid_population_grid(grid, row)); assert(valid_coordinates(&grid->dimension, row, col)); - assert(grid->population[row] != NULL); - assert(grid->population[row][col] >= 0); - return grid->population[row][col]; // getter } @@ -81,16 +86,12 @@ static inline cats_dt_population reduce_population_by(const struct cats_grid *grid, cats_dt_coord row, cats_dt_coord col, cats_dt_population to_reduce) { assert(to_reduce >= 0); - assert(grid != NULL); - assert(grid->population != NULL); + assert(valid_population_grid(grid, row)); assert(valid_coordinates(&grid->dimension, row, col)); - assert(grid->population[row] != NULL); - //cats_dt_population cc = get_carrying_capacity(grid, row, col); cats_dt_population pop = get_adult_population(grid, row, col); if (to_reduce > pop) { to_reduce = pop; } - grid->population[row][col] = pop - to_reduce;// setter; return to_reduce; @@ -101,9 +102,8 @@ static inline void set_population_ignore_cc(const struct cats_grid *grid, const cats_dt_coord row, const cats_dt_coord col, const cats_dt_population pop) { - assert(grid != NULL && grid->population != NULL); + assert(valid_population_grid(grid, row)); assert(valid_coordinates(&grid->dimension, row, col)); - assert(grid->population[row] != NULL); if (pop > CATS_MAX_POPULATION || pop < 0.0) { log_message(LOG_ERROR, "%s: population %d out of allowed population range [0, %d].", @@ -166,10 +166,8 @@ static inline cats_dt_population get_population_ts(const struct cats_grid *grid, const cats_dt_coord row, const cats_dt_coord col, const int threshold, int64_t *unfiltered) { - assert(grid != NULL); - assert(grid->population != NULL); + assert(valid_population_grid(grid, row)); assert(valid_coordinates(&grid->dimension, row, col)); - assert(grid->population[row] != NULL); cats_dt_population tmp = grid->population[row][col]; // getter -- GitLab