diff --git a/src/cats/inline_population.h b/src/cats/inline_population.h index ef618066aef06271f05b826de1ac553b8ee49243..376a4e2aa9b50bbcbfbc5181bc4b12a9710980f4 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