Skip to main content

Rules

Struct Rules 

Source
pub struct Rules<D> { /* private fields */ }
Expand description

A set of rules, kept in ascending order of cost.

Propagators make search fast; rules explain. Rules::step applies the cheapest rule that makes progress, so a solve reads like a person’s and each step can serve as a hint. Rules::solve repeats that until no rule fires; the most expensive rule it needed grades the puzzle (Trace::grade).

flowchart LR
    start([domains]) --> cheapest[try rules, cheapest first]
    cheapest -->|a rule removed values| record[record the step] --> cheapest
    cheapest -->|no rule fires| done([trace: steps, solved?])

§Examples

use constraint_search::{Domain, Domains, ValueSubset, VariableSubset, Rules};

// One row of four cells over the values 0..4, with two cells already given.
let mut domains = Domains::<u16>::new();
let row: Vec<_> = (0..4).map(|_| domains.add(u16::range(4))).collect();
domains.assign(row[0], 0)?;
domains.assign(row[1], 1)?;

let mut rules = Rules::new();
rules.add(VariableSubset::new(1, 1, [row.clone()]));
rules.add(ValueSubset::new(1, 2, [row.clone()]));

let trace = rules.solve(&mut domains, false)?;
assert!(!trace.solved); // cells 2 and 3 can still swap values 2 and 3
assert_eq!(rules.get(trace.steps[0].rule).name(), "variable single");

Implementations§

Source§

impl<D: Domain> Rules<D>

Source

pub const fn new() -> Self

An empty rule set.

Source

pub fn add(&mut self, rule: impl Rule<D> + 'static) -> &mut Self

Adds rule after every rule that costs no more than it.

Source

pub fn get(&self, index: usize) -> &dyn Rule<D>

The rule at index, as referenced by Step::rule.

§Panics

Panics if index is not below Rules::len.

Source

pub fn len(&self) -> usize

The number of rules.

Source

pub fn is_empty(&self) -> bool

Returns true if the set holds no rule.

Source

pub fn step( &self, domains: &mut Domains<D>, assume_unique: bool, ) -> Result<Option<Step<D>>, Inconsistent>

Applies the cheapest rule that removes a value, once; None if no rule does.

This is the next hint for a person solving the puzzle. Rules with Rule::assumes_unique take part only if assume_unique is true.

§Errors

Returns Inconsistent if a rule finds the puzzle has no solution. Values removed by that rule before it failed stay removed.

Source

pub fn solve( &self, domains: &mut Domains<D>, assume_unique: bool, ) -> Result<Trace<D>, Inconsistent>

Applies Rules::step until no rule makes progress.

§Errors

Returns Inconsistent if a rule finds the puzzle has no solution.

Trait Implementations§

Source§

impl<D: Domain> Default for Rules<D>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<D> !RefUnwindSafe for Rules<D>

§

impl<D> !UnwindSafe for Rules<D>

§

impl<D> Forget for Rules<D>

§

impl<D> Freeze for Rules<D>
where Vec<Box<dyn Rule<D>>>: Freeze,

§

impl<D> Move for Rules<D>

§

impl<D> Send for Rules<D>
where Vec<Box<dyn Rule<D>>>: Send,

§

impl<D> Sync for Rules<D>
where Vec<Box<dyn Rule<D>>>: Sync,

§

impl<D> Unpin for Rules<D>
where Vec<Box<dyn Rule<D>>>: Unpin,

§

impl<D> UnsafeUnpin for Rules<D>
where Vec<Box<dyn Rule<D>>>: UnsafeUnpin,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.