Struct rlifesrc_lib::world::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
§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>>
impl<R: Rule<IsGen = False>> World<R, Backjump<R>>
sourcefn learn_from_confl(&mut self, reason: ConflReason<R>)
fn learn_from_confl(&mut self, reason: ConflReason<R>)
Store the cells involved in the conflict reason into self.algo_data.learnt
.
sourcepub(crate) fn set_cell_impl(
&mut self,
cell: CellRef<R>,
state: State,
reason: Reason<R>
) -> Result<(), ConflReason<R>>
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.
sourcefn retreat_impl(&mut self) -> bool
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.
sourcefn analyze(&mut self) -> bool
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.
sourcefn go(&mut self, step: &mut u64) -> bool
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>
impl<R: Rule> World<R, LifeSrc>
sourcepub(crate) fn set_cell_impl(
&mut self,
cell: CellRef<R>,
state: State,
reason: Reason
) -> Result<(), ()>
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.
sourcefn retreat_impl(&mut self) -> bool
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.
sourcefn go(&mut self, step: &mut u64) -> bool
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>
impl<R: Rule, A: Algorithm<R>> World<R, A>
sourcefn consistify(&mut self, cell: CellRef<R>) -> Result<(), A::ConflReason>
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.
sourcefn consistify10(&mut self, cell: CellRef<R>) -> Result<(), A::ConflReason>
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.
sourcepub(crate) fn proceed(&mut self) -> Result<(), A::ConflReason>
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.
sourcepub(crate) fn retreat(&mut self) -> bool
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.
sourcefn decide(&mut self) -> Option<bool>
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.
sourcepub(crate) fn presearch(self) -> Self
pub(crate) fn presearch(self) -> Self
Deduces all cells that could be deduced before the first decision.
sourcepub fn search(&mut self, max_step: Option<u64>) -> Status
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>
impl<R: Rule> World<R, LifeSrc>
sourcepub fn new_with_rule<A: Algorithm<R>>(config: &Config, rule: R) -> World<R, A>
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.
sourcepub fn new_lifesrc(config: &Config, rule: R) -> Self
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>>
impl<R: Rule<IsGen = False>> World<R, Backjump<R>>
sourcepub fn new_backjump(config: &Config, rule: R) -> Self
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>
impl<R: Rule, A: Algorithm<R>> World<R, A>
sourcefn init_front(self) -> Self
fn init_front(self) -> Self
Initialize the list of cells in the front.
sourcefn init_border(self) -> Self
fn init_border(self) -> Self
Initialize the cells at the borders.
sourcefn init_nbhd(self) -> Self
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
.
sourcefn init_pred_succ(self) -> Self
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
.
sourcefn init_sym(self) -> Self
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.
sourcefn init_state(self) -> Self
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
.
sourcefn init_known_cells(self, known_cells: &[KnownCell]) -> Self
fn init_known_cells(self, known_cells: &[KnownCell]) -> Self
Sets the known cells.
sourcefn set_next(&mut self, coord: Coord)
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.
sourcefn init_search_order(self, search_order: &SearchOrder) -> Self
fn init_search_order(self, search_order: &SearchOrder) -> Self
Sets the search order.
sourcepub(crate) fn find_cell(&self, coord: Coord) -> Option<CellRef<R>>
pub(crate) fn find_cell(&self, coord: Coord) -> Option<CellRef<R>>
Finds a cell by its coordinates. Returns a CellRef
.
sourcefn find_cell_mut(&mut self, coord: Coord) -> Option<&mut LifeCell<R>>
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
.
sourcepub(crate) fn set_cell(
&mut self,
cell: CellRef<R>,
state: State,
reason: A::Reason
) -> Result<(), A::ConflReason>
pub(crate) fn set_cell( &mut self, cell: CellRef<R>, state: State, reason: A::Reason ) -> Result<(), A::ConflReason>
sourcepub(crate) fn clear_cell(&mut self, cell: CellRef<R>)
pub(crate) fn clear_cell(&mut self, cell: CellRef<R>)
Clears the state
of a cell,
and update the neighborhood descriptor of its neighbors.
sourcepub(crate) fn get_unknown(&mut self) -> Option<CellRef<R>>
pub(crate) fn get_unknown(&mut self) -> Option<CellRef<R>>
Gets a references to the first unknown cell since next_unknown
.
sourcefn is_trivial(&self) -> bool
fn is_trivial(&self) -> bool
Tests if the result is trivial.
sourcefn is_subperiodic(&self) -> bool
fn is_subperiodic(&self) -> bool
Tests if the fundamental period of the result is smaller than the given period.
sourcefn is_subsymmetric(&self) -> bool
fn is_subsymmetric(&self) -> bool
Tests if the result is invariant under more transformations than required by the given symmetry.
sourcepub fn get_cell_state(&self, coord: Coord) -> Option<State>
pub fn get_cell_state(&self, coord: Coord) -> Option<State>
Gets the state of a cell. Returns Err(())
if there is no such cell.
sourcepub const fn is_gen_rule(&self) -> bool
pub const fn is_gen_rule(&self) -> bool
Whether the rule is a Generations rule.
sourcepub fn is_b0_rule(&self) -> bool
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.
sourcepub fn cell_count_gen(&self, t: i32) -> u32
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.
sourcepub fn cell_count(&self) -> u32
pub fn cell_count(&self) -> u32
Minimum number of known living cells in all generation.
For Generations rules, dying cells are not counted.
sourcepub fn set_max_cell_count(&mut self, max_cell_count: Option<u32>)
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.
sourcepub fn rle_gen(&self, t: i32) -> String
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
!
.
sourcepub fn plaintext_gen(&self, t: i32) -> String
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
?
.