]> git.friedersdorff.com Git - max/gol.git/blobdiff - array.c
First working implementation
[max/gol.git] / array.c
diff --git a/array.c b/array.c
index c70c5707a08a295a5ed600f43073bfdaadd52e18..751bdfbe8a40646bc983184f3a56b6f357227450 100644 (file)
--- a/array.c
+++ b/array.c
@@ -1,34 +1,42 @@
 #include <stddef.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
 #include "array.h"
 
 /*
  * Implements a linear search.
  */
-int gol_is_live(struct gol_board *const state, int_least32_t x, int_least32_t y)
+int gol_is_live(struct gol_board *const state, int_least32_t x, int_least32_t y,
+       char *is_live)
 {
        for (unsigned int i = 0; i < state->n; i += 2) {
                if (state->live_cells[i] == x &&
                        state->live_cells[i+1] == y) {
+                       *is_live = 1;
                        return 1;
                }
        }
-       return 0;
+       *is_live = 0;
+       return 1;
 }
 
+/*
+ *  Return values of 0 represent irrecoverable errors. 1 is a success.
+ */
 int gol_vivify(struct gol_board *const state, int_least32_t x, int_least32_t y)
 {
        int_least32_t *tmp_cells = NULL;
        size_t tmp_size = 0;
+
        if (state->n == state->size) {
                tmp_size = 2 * state->size;
-               tmp_cells = malloc(tmp_size * sizeof(typeof(tmp_cells)));
+               tmp_cells = malloc(tmp_size * sizeof(int_least32_t));
                if (!tmp_cells) {
-                       return 1;
+                       return 0;
                }
                memcpy(tmp_cells, state->live_cells,
-                       tmp_size * sizeof(typeof(tmp_cells)));
+                       state->size * sizeof(int_least32_t));
                state->size = tmp_size;
                free(state->live_cells);
                state->live_cells = tmp_cells;
@@ -50,5 +58,6 @@ int gol_vivify(struct gol_board *const state, int_least32_t x, int_least32_t y)
        } else if (y < state->min_y) {
                state->min_y = y;
        }
+
        return 1;
 }