module CellularC

Public Class Methods

dup_state(p1) click to toggle source
VALUE method_dup_state(VALUE self, VALUE state) {
    int height = (int)RARRAY_LEN(state);
    VALUE dup =  rb_ary_new2(height);
    for(int y = 0; y < height; y++) {
        VALUE row = rb_ary_dup(rb_ary_entry(state, y));
        rb_ary_push(dup, row);
    }
    return dup;
}
next_state(p1, p2) click to toggle source
VALUE method_next_state(VALUE self, VALUE state, VALUE rule) {
  VALUE next_state = method_dup_state(self, state);
  int height = (int)RARRAY_LEN(state);
  int width  = (int)RARRAY_LEN(rb_ary_entry(state, 0));
  for(int row = 0; row < height; row++) {
      VALUE row_array = rb_ary_entry(next_state, row);
      for(int col = 0; col < width; col++) {
          int new_value = cell_value(rule, neighbor_population_of(state, col, row));
          if(new_value == -1) {
            continue;
          }
          rb_ary_store(row_array, col, INT2FIX(new_value));
      }
  }
  return next_state;
}