parallel-cellular-automata
Framework for building parallel cellular automata.
sequential_automaton.hpp
Go to the documentation of this file.
1 
9 #ifndef PARALLEL_CELLULAR_AUTOMATA_SEQUENTIAL_AUTOMATON_HPP
10 #define PARALLEL_CELLULAR_AUTOMATA_SEQUENTIAL_AUTOMATON_HPP
11 #include <functional>
12 #include <iostream>
13 #include <tuple>
14 
15 #ifndef PARALLEL_CELLULAR_AUTOMATA_CELLULAR_AUTOMATA_HPP
16 #include "cellular_automata.hpp"
17 #endif
18 #include "grid.hpp"
19 
20 namespace ca
21 {
26 namespace seq
27 {
36 template <typename T>
38 {
39  public:
47  CellularAutomaton(Grid<T> &grid, std::function<T(T, T, T, T, T, T, T, T, T)> update_function)
49 
57  {
58  // move what's movable and copying numeric types.
59  grid = std::move(other.grid);
60  update_function = std::move(other.update_function);
61  }
62 
70  CellularAutomaton(const CellularAutomaton &other) = delete;
71 
79  virtual void simulate(unsigned steps = 1)
80  {
81  if (steps == 0)
82  return;
83  // allocate new grid
84  auto new_grid = ca::Grid<int>::newWithSameSize(grid);
85 
86  // compute state and put values on the new grid.
87  while (steps > 0)
88  {
89  for (size_t r{0}; r < grid.rows(); ++r)
90  {
91  for (size_t c{0}; c < grid.columns(); ++c)
92  {
93  auto cell = std::make_tuple(grid(r, c));
94  new_grid(r, c) = std::apply(update_function, std::tuple_cat(cell, get_neighborhood(r, c)));
95  }
96  }
97  // swap grids so grid contains the final value.
98  grid.swap(new_grid);
99 
100  ++generation;
101  --steps;
102  }
103  }
104 
110  virtual size_t get_generation() const
111  {
112  return generation;
113  }
123  friend std::ostream &operator<<(std::ostream &os, const CellularAutomaton &ca)
124  {
125 
126  return os << ca.grid;
127  }
128 
129  protected:
135 
140  size_t generation;
151  std::function<T(T, T, T, T, T, T, T, T, T)> update_function;
170  virtual std::tuple<T, T, T, T, T, T, T, T> get_neighborhood(int row, int col) const
171 
172  {
173  unsigned rows = grid.rows();
174  unsigned columns = grid.columns();
175  T top_left, top, top_right, left, right, bottom_left, bottom, bottom_right;
176  top_left = grid((row - 1 + rows) % rows, (col - 1 + columns) % columns);
177  top = grid((row - 1 + rows) % rows, col);
178  top_right = grid((row - 1 + rows) % rows, (col + 1) % columns);
179  left = grid(row, (col - 1 + columns) % columns);
180  right = grid(row, (col + 1) % columns);
181  bottom_left = grid((row + 1) % rows, (col - 1 + columns) % columns);
182  bottom = grid((row + 1) % rows, col);
183  bottom_right = grid((row + 1) % rows, (col + 1) % columns);
184  return std::make_tuple(top_left, top, top_right, left, right, bottom_left, bottom, bottom_right);
185  };
186 };
187 } // namespace seq
188 } // namespace ca
189 
190 #endif
This header imports all the other headers of the framework.
Grid of the cellular automaton.
Definition: grid.hpp:31
static Grid newWithSameSize(const Grid &other)
Return a grid of the same dimension of the grid passed as argument.
Definition: grid.hpp:95
Sequential Cellular Automaton.
Definition: sequential_automaton.hpp:38
CellularAutomaton(const CellularAutomaton &other)=delete
Deleted copy constructor.
std::function< T(T, T, T, T, T, T, T, T, T)> update_function
Function used to compute the next state of the cell.
Definition: sequential_automaton.hpp:151
virtual void simulate(unsigned steps=1)
Run the simulation for a given number of steps.
Definition: sequential_automaton.hpp:79
virtual size_t get_generation() const
Get the generation of the simulation.
Definition: sequential_automaton.hpp:110
Grid< T > & grid
Grid of the C.A.
Definition: sequential_automaton.hpp:134
size_t generation
Current generation of the grid.
Definition: sequential_automaton.hpp:140
CellularAutomaton(Grid< T > &grid, std::function< T(T, T, T, T, T, T, T, T, T)> update_function)
Construct a new Cellular Automaton object.
Definition: sequential_automaton.hpp:47
friend std::ostream & operator<<(std::ostream &os, const CellularAutomaton &ca)
Overload of the << operator.
Definition: sequential_automaton.hpp:123
virtual std::tuple< T, T, T, T, T, T, T, T > get_neighborhood(int row, int col) const
Get the neighborhood of a cell.
Definition: sequential_automaton.hpp:170
CellularAutomaton(CellularAutomaton &&other)
Construct a new Cellular Automaton object from another one using move semantic.
Definition: sequential_automaton.hpp:56
Definition of the grid of the automaton.
Namespace of the framework.
Definition: barrier.hpp:20