<commit_before>#include <stdlib.h>
#include <assert.h>

#include <gsl/gsl_randist.h>
#include <gsl/gsl_rng.h>

#include "grid.h"
#include "grid_cell.h"

#define THRESDIST 0.20

/* FUNCTION PROTOTYPES */

void gr_set_random_grid(Grid *grid, gsl_rng *rng);
void gr_set_mixed_grid(Grid *grid, gsl_rng *rng);
void gr_set_uniform_grid(Grid *grid, gsl_rng *rng);
void gr_set_disturb_grid(Grid *grid, double thresDist, gsl_rng *rng);

GridCell *gr_get_cell(Grid *grid, size_t x, size_t y) {

  assert(grid);

  assert(y < grid->yDim);
  assert(x < grid->xDim);

  // note the arrow operator: grid->yDim === (*grid).yDim
  size_t  index = grid->xDim * y + x; 
  

  // printf("Coord(%d, %d): %d \n",(int)x,(int)y,(int)index);
  return &(grid->gridData[index]);

}

void gr_set_cell(Grid *grid, State chosenState, size_t x, size_t y) {
  // pointers validation
  assert(grid);
  GridCell *cell = gr_get_cell(grid, x, y);
  *(cell->currentState) = chosenState;
}

void gr_compute_prevalence(Grid *grid, size_t x, size_t y) {

  // pointer validation
  assert(grid);

  size_t nbSize = 8;
  double count_D = 0.0, count_C = 0.0, count_T = 0.0, count_M = 0.0;
  double increment = 1 / nbSize;

  // set aside some memory for the neighbors
  State *neighborStates = malloc(nbSize * sizeof(State));

  gr_get_neighbor_states(grid, neighborStates, x, y, nbSize);

  // Compute prevalence

  for (int i = 0; i < nbSize; i++) {
    switch (neighborStates[i]) {

    case TRANSITIONAL:
      count_T = count_T + increment;
      break;

    case MIXED:
      count_M = count_M + increment;
      break;

    case DECIDUOUS:
      count_D = count_D + increment;
      break;

    case CONIFEROUS:
      count_C = count_C + increment;
      break;

    default:
      break;
    }
  }

  // Stored in cell
  gr_get_cell(grid, x, y)->prevalence[0] = count_C;
  gr_get_cell(grid, x, y)->prevalence[1] = count_D;
  gr_get_cell(grid, x, y)->prevalence[2] = count_M;
  gr_get_cell(grid, x, y)->prevalence[3] = count_T;

  free(neighborStates);
}

void gr_get_neighbor_states(Grid *grid, State *dest, size_t x, size_t y, size_t neighborhoodSize) {

// Von neumann neighboors
  dest[0] = *(gr_get_cell(grid, x, y - 1)->currentState);
  dest[1] = *(gr_get_cell(grid, x, y + 1)->currentState);
  dest[2] = *(gr_get_cell(grid, x + 1, y)->currentState);
  dest[3] = *(gr_get_cell(grid, x - 1, y)->currentState);

// Moore neighboors
  if (neighborhoodSize > 4) {
    dest[4] = *(gr_get_cell(grid, x - 1, y + 1)->currentState);
    dest[5] = *(gr_get_cell(grid, x - 1, y - 1)->currentState);
    dest[6] = *(gr_get_cell(grid, x + 1, y + 1)->currentState);
    dest[7] = *(gr_get_cell(grid, x + 1, y - 1)->currentState);
  }
}

Grid * gr_make_grid(size_t xsize, size_t ysize, size_t numTimeSteps, GridType gridType, gsl_rng *rng) {
<commit_msg>Implemant new enum NeighType in gc_get_neighbors<commit_after>#include <stdlib.h>
#include <assert.h>

#include <gsl/gsl_randist.h>
#include <gsl/gsl_rng.h>

#include "grid.h"
#include "grid_cell.h"

#define THRESDIST 0.20

/* FUNCTION PROTOTYPES */

void gr_set_random_grid(Grid *grid, gsl_rng *rng);
void gr_set_mixed_grid(Grid *grid, gsl_rng *rng);
void gr_set_uniform_grid(Grid *grid, gsl_rng *rng);
void gr_set_disturb_grid(Grid *grid, double thresDist, gsl_rng *rng);

GridCell *gr_get_cell(Grid *grid, size_t x, size_t y) {

  assert(grid);

  assert(y < grid->yDim);
  assert(x < grid->xDim);

  // note the arrow operator: grid->yDim === (*grid).yDim
  size_t  index = grid->xDim * y + x; 
  

  // printf("Coord(%d, %d): %d \n",(int)x,(int)y,(int)index);
  return &(grid->gridData[index]);

}

void gr_set_cell(Grid *grid, State chosenState, size_t x, size_t y) {
  // pointers validation
  assert(grid);
  GridCell *cell = gr_get_cell(grid, x, y);
  *(cell->currentState) = chosenState;
}

void gr_compute_prevalence(Grid *grid, size_t x, size_t y) {

  // pointer validation
  assert(grid);

  NeighType neighType = MOORE;
  size_t nbSize;

  if(neighType == MOORE){
    nbSize = 8;  
  } 
  else if(neighType == VONNE){
    nbSize = 4;  
  }
  

  double count_D = 0.0, count_C = 0.0, count_T = 0.0, count_M = 0.0;
  double increment = 1 / nbSize;

  // set aside some memory for the neighbors
  State *neighborStates = malloc(nbSize * sizeof(State));
  gr_get_neighbor_states(grid, neighborStates, x, y, neighType );

  // Compute prevalence

  for (int i = 0; i < nbSize; i++) {
    switch (neighborStates[i]) {

    case TRANSITIONAL:
      count_T = count_T + increment;
      break;

    case MIXED:
      count_M = count_M + increment;
      break;

    case DECIDUOUS:
      count_D = count_D + increment;
      break;

    case CONIFEROUS:
      count_C = count_C + increment;
      break;

    default:
      break;
    }
  }

  // Stored in cell
  gr_get_cell(grid, x, y)->prevalence[0] = count_C;
  gr_get_cell(grid, x, y)->prevalence[1] = count_D;
  gr_get_cell(grid, x, y)->prevalence[2] = count_M;
  gr_get_cell(grid, x, y)->prevalence[3] = count_T;

  free(neighborStates);
}

void gr_get_neighbor_states(Grid *grid, State *dest, size_t x, size_t y, NeighType neighType) {

  assert(y < grid->yDim);
  assert(x < grid->xDim);


// Von neumann neighboors
  dest[0] = *(gr_get_cell(grid, x, y - 1)->currentState);
  dest[1] = *(gr_get_cell(grid, x, y + 1)->currentState);
  dest[2] = *(gr_get_cell(grid, x + 1, y)->currentState);
  dest[3] = *(gr_get_cell(grid, x - 1, y)->currentState);

// Moore neighboors
  if (neighType == MOORE) {
    dest[4] = *(gr_get_cell(grid, x - 1, y + 1)->currentState);
    dest[5] = *(gr_get_cell(grid, x - 1, y - 1)->currentState);
    dest[6] = *(gr_get_cell(grid, x + 1, y + 1)->currentState);
    dest[7] = *(gr_get_cell(grid, x + 1, y - 1)->currentState);
  }

}

Grid * gr_make_grid(size_t xsize, size_t ysize, size_t numTimeSteps, GridType gridType, gsl_rng *rng) {

{'fraction_non_alphanumeric': 0.15408320493066255, 'fraction_numerical': 0.01335387776065742, 'mean_word_length': 3.6145339652448656, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6454068', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2344, 'n_words': 540}