pub struct EnvironmentRule {
    pub row_range: usize,
    pub col_range: usize,
    pub boundaries: (BoundaryBehaviour, BoundaryBehaviour),
    pub cell_transform: fn(_: &CellGrid) -> char,
}
Expand description

An environment rule uses the neighborhood (up to a certain range as specified) of a cell and applies a function to it. The result of this function is the next value of that cell. Applying this to each cell yields the entire transformation.

Note that each application of the cell_transform function will read from the entire untransformed array. Also, the environment will wrap around the grid edges.

use cellumina::rule::Rule;
let rule = cellumina::rule::EnvironmentRule {
    row_range: 1,
    col_range: 1,
    boundaries: (cellumina::rule::BoundaryBehaviour::Periodic, cellumina::rule::BoundaryBehaviour::Periodic),
    cell_transform: |env: &cellumina::CellGrid| match env
    // Iterate over neighbors.
        .iter()
        .enumerate()
        .map(|val| match val {
            // The cell we are transforming does not get counted.
            (4, 'X') => 0,
            // Any cell containing an 'X' counts for 1 (alive).
            (_, 'X') => 1,
            // Any cell containing any other entry (only ' ' in our initial configuration) counts as 0 (dead).
            _ => 0,
        })
        // Sum over these 9 values...
        .sum()
        // ... and map the sum to the new enty of our cell:
    {
        // 2 neighbors: The cell keeps its state.
        2 => env[1][1],
        // 3 neighbors: The cell gets born.
        3 => 'X',
        // 0, 1 or more than 3 neighbors: The cell dies.
        _ => ' ',
    },
};
let mut grid = grid::grid![[' ', ' ', 'X', ' ', ' '][' ', ' ', 'X',' ', ' '][' ', ' ', ' ', ' ', ' '][' ', ' ', 'X', ' ', ' '][' ', ' ', 'X', ' ', ' ']];
rule.transform(&mut grid);
assert_eq!(
    grid,
    grid::grid![[' ', 'X', 'X', 'X', ' '][' ', ' ', ' ',' ', ' '][' ', ' ', ' ', ' ', ' '][' ', ' ', ' ', ' ', ' '][' ', 'X', 'X', 'X', ' ']]
);
rule.transform(&mut grid);
rule.transform(&mut grid);
rule.transform(&mut grid);
assert_eq!(
    grid,
    grid::grid![[' ', 'X', ' ', 'X', ' '][' ', ' ', 'X',' ', ' '][' ', ' ', ' ', ' ', ' '][' ', ' ', 'X', ' ', ' '][' ', 'X', ' ', 'X', ' ']]
);

Fields§

§row_range: usize

The vertical range of an environment, extending in both direction from the cell to be transformed.

Your cell_transform function will receive a grid of height 2 * row_range + 1, centered on the cell that will be replaced by the output.

§col_range: usize

The horizontal range of an environment, extending in both direction from the cell to be transformed.

Your cell_transform function will receive a grid of width 2 * col_range + 1, centered on the cell that will be replaced by the output.

§boundaries: (BoundaryBehaviour, BoundaryBehaviour)

How the rule is supposed to handle cells at the edges of the state space. The first item describes how to handle trying to access rows out of range, the second columns out of range.

§cell_transform: fn(_: &CellGrid) -> char

The function that calculates the next state of a single cell based on its environment.

Receives a grid of size 2 * row_range + 1 x 2 * col_range + 1. Must return a character. In the next iteration after applying this rule, the cell in the center of the received grid will contain the return value of this function.

Trait Implementations§

source§

impl Clone for EnvironmentRule

source§

fn clone(&self) -> EnvironmentRule

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for EnvironmentRule

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for EnvironmentRule

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Rule for EnvironmentRule

source§

fn transform(&self, grid: &mut CellGrid)

Transforms the passed cell grid according to this transformation rule. Transformation happens in-place.
source§

impl Copy for EnvironmentRule

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<R, P> ReadPrimitive<R> for Pwhere R: Read + ReadEndian<P>, P: Default,

source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V