Struct rlifesrc_lib::World

source ·
pub struct World<R: Rule, A: Algorithm<R>> {
    pub(crate) config: Config,
    pub(crate) rule: R,
    cells: Box<[UnsafeCell<LifeCell<R>>]>,
    pub(crate) cell_count: Vec<u32>,
    pub(crate) front_cell_count: u32,
    pub(crate) conflicts: u64,
    pub(crate) set_stack: Vec<SetCell<R, A>>,
    pub(crate) check_index: u32,
    pub(crate) next_unknown: Option<CellRef<R>>,
    pub(crate) non_empty_front: bool,
    pub(crate) algo_data: A,
}
Expand description

The world.

Fields§

§config: Config

World configuration.

§rule: R

The rule of the cellular automaton.

§cells: Box<[UnsafeCell<LifeCell<R>>]>

A vector that stores all the cells in the search range.

This vector will not be moved after its creation. All the cells will live throughout the lifetime of the world.

Here UnsafeCell is used to (hopefully) avoid the Undefined Behavior caused by Stacked Borrows.

§cell_count: Vec<u32>

Number of known living cells in each generation.

For Generations rules, dying cells are not counted.

§front_cell_count: u32

Number of unknown or living cells on the first row or column.

§conflicts: u64

Number of conflicts during the search.

§set_stack: Vec<SetCell<R, A>>

A stack to record the cells whose values are set during the search.

The cells in this stack always have known states.

It is used in backtracking.

§check_index: u32

The position of the next cell to be examined in the set_stack.

See proceed for details.

§next_unknown: Option<CellRef<R>>

The starting point to look for an unknown cell.

There must be no unknown cell before this cell.

§non_empty_front: bool

Whether to force the first row/column to be nonempty.

Depending on the search order, the ‘front’ means:

  • the first row, when the search order is row first;
  • the first column, when the search order is column first;
  • the first row plus the first column, when the search order is diagonal.
§algo_data: A

Other data used by the algorithm.

Implementations§

source§

impl<R: Rule<IsGen = False>> World<R, Backjump<R>>

source

fn learn_from_confl(&mut self, reason: ConflReason<R>)

Store the cells involved in the conflict reason into self.algo_data.learnt.

source

pub(crate) fn set_cell_impl( &mut self, cell: CellRef<R>, state: State, reason: Reason<R> ) -> Result<(), ConflReason<R>>

Sets the state of a cell, push it to the set_stack, and update the neighborhood descriptor of its neighbors.

The original state of the cell must be unknown.

Return false if the number of living cells exceeds the max_cell_count or the front becomes empty.

source

fn retreat_impl(&mut self) -> bool

Retreats to the last time when a unknown cell is decided by choice, and switch that cell to the other state.

Returns true if successes, false if it goes back to the time before the first cell is set.

source

fn analyze(&mut self) -> bool

Retreats to the last time when a unknown cell is assumed, analyzes the conflict during the backtracking, and backjumps if possible.

The reason of conflict must be stored in self.algo_data.learnt before calling this method.

Returns true if successes, false if it goes back to the time before the first cell is set.

source

fn go(&mut self, step: &mut u64) -> bool

Keeps proceeding and backtracking, until there are no more cells to examine (and returns true), or the backtracking goes back to the time before the first cell is set (and returns false).

It also records the number of steps it has walked in the parameter step. A step consists of a proceed and a analyze.

source§

impl<R: Rule> World<R, LifeSrc>

source

pub(crate) fn set_cell_impl( &mut self, cell: CellRef<R>, state: State, reason: Reason ) -> Result<(), ()>

Sets the state of a cell, push it to the set_stack, and update the neighborhood descriptor of its neighbors.

The original state of the cell must be unknown.

Return false if the number of living cells exceeds the max_cell_count or the front becomes empty.

source

fn retreat_impl(&mut self) -> bool

Retreats to the last time when a unknown cell is decided by choice, and switch that cell to the other state.

Returns true if successes, false if it goes back to the time before the first cell is set.

source

fn go(&mut self, step: &mut u64) -> bool

Keeps proceeding and backtracking, until there are no more cells to examine (and returns true), or the backtracking goes back to the time before the first cell is set (and returns false).

It also records the number of steps it has walked in the parameter step. A step consists of a proceed and a retreat.

source§

impl<R: Rule, A: Algorithm<R>> World<R, A>

source

fn consistify(&mut self, cell: CellRef<R>) -> Result<(), A::ConflReason>

Consistifies a cell.

Examines the state and the neighborhood descriptor of the cell, and makes sure that it can validly produce the cell in the next generation. If possible, determines the states of some of the cells involved.

If there is a conflict, returns its reason.

source

fn consistify10(&mut self, cell: CellRef<R>) -> Result<(), A::ConflReason>

Consistifies a cell, its neighbors, and its predecessor.

If there is a conflict, returns its reason.

source

pub(crate) fn proceed(&mut self) -> Result<(), A::ConflReason>

Deduces all the consequences by consistify and symmetry.

If there is a conflict, returns its reason.

source

pub(crate) fn retreat(&mut self) -> bool

Retreats to the last time when a unknown cell is decided by choice, and switch that cell to the other state.

Returns true if successes, false if it goes back to the time before the first cell is set.

source

fn decide(&mut self) -> Option<bool>

Makes a decision.

Chooses an unknown cell, assigns a state for it, and push a reference to it to the set_stack.

Returns None is there is no unknown cell, Some(false) if the new state leads to an immediate conflict.

source

pub(crate) fn presearch(self) -> Self

Deduces all cells that could be deduced before the first decision.

source

pub fn search(&mut self, max_step: Option<u64>) -> Status

The search function.

Returns Status::Found if a result is found, Status::None if such pattern does not exist, Status::Searching if the number of steps exceeds max_step and no results are found.

source§

impl<R: Rule> World<R, LifeSrc>

source

pub fn new_with_rule<A: Algorithm<R>>(config: &Config, rule: R) -> World<R, A>

Creates a new world from the configuration and the rule.

source

pub fn new_lifesrc(config: &Config, rule: R) -> Self

Creates a new world from the configuration and the rule, using the LifeSrc algorithm.

source§

impl<R: Rule<IsGen = False>> World<R, Backjump<R>>

source

pub fn new_backjump(config: &Config, rule: R) -> Self

Creates a new world from the configuration and the rule, using the Backjump algorithm.

source§

impl<R: Rule, A: Algorithm<R>> World<R, A>

source

fn init_front(self) -> Self

Initialize the list of cells in the front.

source

fn init_border(self) -> Self

Initialize the cells at the borders.

source

fn init_nbhd(self) -> Self

Links the cells to their neighbors.

Note that for cells on the edges of the search range, some neighbors might point to None.

source

fn init_pred_succ(self) -> Self

Links a cell to its predecessor and successor.

If the predecessor is out of the search range, then marks the current cell as known.

If the successor is out of the search range, then sets it to None.

source

fn init_sym(self) -> Self

Links a cell to the symmetric cells.

If some symmetric cell is out of the search range, then marks the current cell as known.

source

fn init_state(self) -> Self

Sets states for the cells.

All cells are set to unknown unless they are at the border, or are marked as known in init_pred_succ or init_sym.

source

fn init_known_cells(self, known_cells: &[KnownCell]) -> Self

Sets the known cells.

source

fn set_next(&mut self, coord: Coord)

Set the next of a cell to be next_unknown and set next_unknown to be this cell.

source

fn init_search_order(self, search_order: &SearchOrder) -> Self

Sets the search order.

source

pub(crate) fn find_cell(&self, coord: Coord) -> Option<CellRef<R>>

Finds a cell by its coordinates. Returns a CellRef.

source

fn find_cell_mut(&mut self, coord: Coord) -> Option<&mut LifeCell<R>>

Finds a cell by its coordinates.

Unlike find_cell, it returns None when the cell is out of the diagonal_width.

source

pub(crate) fn set_cell( &mut self, cell: CellRef<R>, state: State, reason: A::Reason ) -> Result<(), A::ConflReason>

Sets the state of a cell, push it to the set_stack, and update the neighborhood descriptor of its neighbors.

The original state of the cell must be unknown.

source

pub(crate) fn clear_cell(&mut self, cell: CellRef<R>)

Clears the state of a cell, and update the neighborhood descriptor of its neighbors.

source

pub(crate) fn get_unknown(&mut self) -> Option<CellRef<R>>

Gets a references to the first unknown cell since next_unknown.

source

pub(crate) fn is_boring(&self) -> bool

Tests if the result is borling.

source

fn is_trivial(&self) -> bool

Tests if the result is trivial.

source

fn is_stable(&self) -> bool

Tests if the result is stable.

source

fn is_subperiodic(&self) -> bool

Tests if the fundamental period of the result is smaller than the given period.

source

fn is_subsymmetric(&self) -> bool

Tests if the result is invariant under more transformations than required by the given symmetry.

source

pub fn get_cell_state(&self, coord: Coord) -> Option<State>

Gets the state of a cell. Returns Err(()) if there is no such cell.

source

pub const fn config(&self) -> &Config

World configuration.

source

pub const fn is_gen_rule(&self) -> bool

Whether the rule is a Generations rule.

source

pub fn is_b0_rule(&self) -> bool

Whether the rule contains B0.

In other words, whether a cell would become ALIVE in the next generation, if all its neighbors in this generation are dead.

source

pub fn cell_count_gen(&self, t: i32) -> u32

Number of known living cells in some generation.

For Generations rules, dying cells are not counted.

source

pub fn cell_count(&self) -> u32

Minimum number of known living cells in all generation.

For Generations rules, dying cells are not counted.

source

pub const fn conflicts(&self) -> u64

Number of conflicts during the search.

source

pub fn set_max_cell_count(&mut self, max_cell_count: Option<u32>)

Set the max cell counts.

Currently this is the only parameter that you can change during the search.

source

pub fn rle_gen(&self, t: i32) -> String

Displays the whole world in some generation, in a mix of Plaintext and RLE format.

  • Dead cells are represented by .;
  • Living cells are represented by o for rules with 2 states, A for rules with more states;
  • Dying cells are represented by uppercase letters starting from B;
  • Unknown cells are represented by ?;
  • Each line is ended with $;
  • The whole pattern is ended with !.
source

pub fn plaintext_gen(&self, t: i32) -> String

Displays the whole world in some generation in Plaintext format.

Do not use this for Generations rules.

  • Dead cells are represented by .;
  • Living and Dying cells are represented by o;
  • Unknown cells are represented by ?.
source§

impl<R: Rule, A: Algorithm<R>> World<R, A>

source

pub fn ser(&self) -> WorldSer

Available on crate feature serde only.

Saves the world as a WorldSer.

source

pub fn deser(&mut self, ser: &WorldSer) -> Result<(), Error>

Available on crate feature serde only.

Restores the world from the WorldSer.

Trait Implementations§

source§

impl From<World<Life, Backjump<Life>>> for PolyWorld

Convert into LifeBackjump variant.

source§

fn from(v: World<Life, Backjump<Life>>) -> Self

Converts to this type from the input type.
source§

impl From<World<Life, LifeSrc>> for PolyWorld

Convert into Life variant.

source§

fn from(v: World<Life, LifeSrc>) -> Self

Converts to this type from the input type.
source§

impl From<World<LifeGen, LifeSrc>> for PolyWorld

Convert into LifeGen variant.

source§

fn from(v: World<LifeGen, LifeSrc>) -> Self

Converts to this type from the input type.
source§

impl From<World<NtLife, Backjump<NtLife>>> for PolyWorld

Convert into NtLifeBackjump variant.

source§

fn from(v: World<NtLife, Backjump<NtLife>>) -> Self

Converts to this type from the input type.
source§

impl From<World<NtLife, LifeSrc>> for PolyWorld

Convert into NtLife variant.

source§

fn from(v: World<NtLife, LifeSrc>) -> Self

Converts to this type from the input type.
source§

impl From<World<NtLifeGen, LifeSrc>> for PolyWorld

Convert into NtLifeGen variant.

source§

fn from(v: World<NtLifeGen, LifeSrc>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<R, A> !RefUnwindSafe for World<R, A>

§

impl<R, A> !Send for World<R, A>

§

impl<R, A> !Sync for World<R, A>

§

impl<R, A> Unpin for World<R, A>where A: Unpin, R: Unpin, <A as Algorithm<R>>::Reason: Unpin,

§

impl<R, A> !UnwindSafe for World<R, A>

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.

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.
source§

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

source§

fn vzip(self) -> V