[][src]Struct generations::Generations

pub struct Generations<Model: Clearable> { /* fields omitted */ }

This struct manages transitions between generations. It stores two models, one of which is considered "current" and the other "scratch". The step method advances the simulation by calling a function with a reference to the current generation and a mutable reference to the scratch generation; the function uses the current generation to write the next generation out to the scratch generation, after which it becomes the new current generation (and the previous generation becomes the new scratch generation).

Methods

impl<Model: Clearable> Generations<Model>[src]

pub fn new(seed: Model, scratch: Model) -> Self[src]

Create a new Generations instance with a seed model, which will become the initial current generation, and a scratch generation.

Example

Create a simulation using a vector for the seed generation and a pre-allocated vector for the scratch generation

use generations::Generations;

let mut gen = Generations::new(
    vec![1, 2, 3, 4, 5],
    Vec::with_capacity(5)
);

gen.step(|current_gen, next_gen| {
    assert_eq!(current_gen, &[1, 2, 3, 4, 5]);
    assert_eq!(next_gen, &[]);
    assert_eq!(next_gen.capacity(), 5);
});

#[must_use] pub fn current(&self) -> &Model[src]

Get a reference to the current generation. This is the result of the most recent step or reset, or the seed generation if no steps have been run.

Example

use generations::Generations;

let gen = Generations::new_defaulted(vec![1, 2, 3, 4]);
assert_eq!(gen.current(), &[1, 2, 3, 4]);

pub fn step(&mut self, stepper: impl FnOnce(&Model, &mut Model)) -> &Model[src]

Advance the simulation 1 step using a stepping function. The stepping function takes a reference to the current generation and a mutable reference to the new generation, which is cleared before the stepping function is called. It is expected to advance the simulation by reading the current genration and writing the next generation. After the stepping function writes the new generation, it is marked as current.

This function returns a reference to the previously current generation, so that it can be compared if desired with the current generation.

Example

// Simple example that rotates a vector 1 step
use generations::Generations;

let mut gen = Generations::new_cloned(vec![1, 2, 3, 4]);

let prev = gen.step(|current_gen, next_gen| {
    assert!(next_gen.is_empty());
    next_gen.extend(current_gen.iter().skip(1));
    next_gen.extend(current_gen.first());
});

assert_eq!(prev, &[1, 2, 3, 4]);
assert_eq!(gen.current(), &[2, 3, 4, 1]);

pub fn reset_with(&mut self, seeder: impl FnOnce(&mut Model))[src]

Replace the current generation with a new seed generation using a function. Has no effect on the existing scratch generation. The current generation is cleared before the seed function is called.

use generations::Generations;

let mut gen = Generations::new_defaulted(vec![1, 2, 3, 4]);
gen.reset_with(|seed_gen| {
    assert!(seed_gen.is_empty());
    seed_gen.extend(&[5, 5, 5, 5]);
});
assert_eq!(gen.current(), &[5, 5, 5, 5]);

pub fn reset(&mut self, seed: Model)[src]

Replace the current generation with a new seed generation. Has no effect on the existing scratch generation.

See also reset_with for a reset method that reuses the existing storage of the current generation.

use generations::Generations;

let mut gen = Generations::new_defaulted(vec![1, 2, 3, 4]);
gen.reset(vec![4, 3, 2, 1]);
assert_eq!(gen.current(), &[4, 3, 2, 1]);

pub fn with_rule<F: FnMut(&Model, &mut Model)>(
    self,
    stepper: F
) -> Simulation<Model, F>
[src]

Combine a Generations struct with a repeatable stepping function, to create a simulation that can be stepped with the same logic each generation. See step for an explaination of the stepping function.

impl<Model: Clone + Clearable> Generations<Model>[src]

pub fn new_cloned(seed_generation: Model) -> Self[src]

Create a new Generations instance with a seed model. Clone the seed model to create an initial scratch model.

impl<Model: Default + Clearable> Generations<Model>[src]

pub fn new_defaulted(seed_generation: Model) -> Self[src]

Create a new Generations instance with a seed model. The Model type's default value is used as the initial scratch model.

Trait Implementations

impl<Model: Clone + Clearable> Clone for Generations<Model>[src]

impl<Model: Clearable, Step: FnMut(&Model, &mut Model)> AsRef<Generations<Model>> for Simulation<Model, Step>[src]

impl<Model: Debug + Clearable> Debug for Generations<Model>[src]

Auto Trait Implementations

impl<Model> Send for Generations<Model> where
    Model: Send

impl<Model> Sync for Generations<Model> where
    Model: Sync

impl<Model> Unpin for Generations<Model> where
    Model: Unpin

impl<Model> RefUnwindSafe for Generations<Model> where
    Model: RefUnwindSafe

impl<Model> UnwindSafe for Generations<Model> where
    Model: UnwindSafe

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]