From 08fcd58893b5ea754ebddbf63b2745dde34d4830 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 05:11:35 +0300 Subject: publish --- src/cellular-automaton.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/cellular-automaton.c (limited to 'src/cellular-automaton.c') diff --git a/src/cellular-automaton.c b/src/cellular-automaton.c new file mode 100644 index 0000000..183684a --- /dev/null +++ b/src/cellular-automaton.c @@ -0,0 +1,49 @@ +#include +#include "cellular-automaton.h" +#include "utils.h" + +void +cellular_automaton_init (struct cellular_automaton *cellular_automaton, + uint32_t function, uint32_t rows, uint32_t cols) +{ + board_init (&cellular_automaton->board, rows, cols); + cellular_automaton->back_board_bitarray = + malloc (cellular_automaton->board.bitarray.size); + cellular_automaton->function = function; +} + +void +cellular_automaton_progress (struct cellular_automaton *cellular_automaton) +{ + // uint32_t alive = 0; + for (int32_t row = 0; row < cellular_automaton->board.rows; ++row) { + for (int32_t col = 0; col < cellular_automaton->board.cols; ++col) { + struct board *board = &cellular_automaton->board; + uint8_t state = + board_get (board, row - 1, col + 0) << 3 | + board_get (board, row + 0, col - 1) << 1 | + board_get (board, row + 0, col + 0) << 0 | + board_get (board, row + 0, col + 1) << 2 | + board_get (board, row + 1, col + 0) << 4; + + // if (board_get (board, row, col)) { + // // printf ("%d/%d, ", row, col); + // ++alive; + // } + + setbit (cellular_automaton->back_board_bitarray, + row*board->cols + col, + cellular_automaton->function >> state & 1); + } + } // printf ("%u\n", alive); + + SWAP (cellular_automaton->back_board_bitarray, + cellular_automaton->board.bitarray.bytes); +} + +void +cellular_automaton_free (struct cellular_automaton *cellular_automaton) +{ + board_free (&cellular_automaton->board); + free (cellular_automaton->back_board_bitarray); +} -- cgit v1.3.1