Struct markov_generator::Chain
source · pub struct Chain<T> { /* private fields */ }
Expand description
A Markov chain.
This type implements Serialize
and Deserialize
when the
serde
feature is enabled (which it is by default).
Implementations§
source§impl<T: Item> Chain<T>
impl<T: Item> Chain<T>
sourcepub fn add_all<I>(&mut self, items: I, edges: AddEdges)where
I: Iterator<Item = T>,
T: Clone,
pub fn add_all<I>(&mut self, items: I, edges: AddEdges)where
I: Iterator<Item = T>,
T: Clone,
Adds all items in an iterator to the chain.
This essentially calls Self::add_next
on every overlapping window
of self.depth() + 1
elements.
edges
controls whether the start or end of items
can be used as
start or end data for the chain. See the documentation for AddEdges
for more information.
sourcepub fn add<I: IntoIterator<Item = Option<T>>>(&mut self, items: I)
pub fn add<I: IntoIterator<Item = Option<T>>>(&mut self, items: I)
Adds items to the chain.
The first self.depth() + 1
items are added,
increasing the chance that the first self.depth()
items will be followed by the remaining item.
If items.into_iter()
yields fewer than
self.depth()
items, this function is a no-op. If it yields exactly
self.depth()
items, the remaining item is treated as None
.
sourcepub fn add_start<I>(&mut self, items: I)where
I: IntoIterator<Item = T>,
I::IntoIter: Clone,
pub fn add_start<I>(&mut self, items: I)where
I: IntoIterator<Item = T>,
I::IntoIter: Clone,
Adds items preceded by various amounts of None
s so that
Self::get_start
has a chance of returning those items.
Specifically, this function calls Self::add
with i
None
s
followed by the items in items
for every i
from 1 to
self.depth()
(inclusive). This increases the chance that the
first self.depth()
items of items
will be returned by
Self::get_start
.
Note that this function does not increase the chance that the first
self.depth()
items of items
will be followed by the
self.depth() + 1
st item;
Self::add_next
or Self::add
must be called.
sourcepub fn add_next<I: IntoIterator<Item = T>>(&mut self, items: I)
pub fn add_next<I: IntoIterator<Item = T>>(&mut self, items: I)
Convenience function that wraps each item in Some
and calls
Self::add
.
Note that this function alone does not increase the chance that
items
will be returned by Self::get_start
; Self::add_start
(or manually passing None
-prefixed sequences to Self::add
) must
be used.
sourcepub fn generate(&self) -> Generator<'_, T, ThreadRng> ⓘ
Available on crate feature std
only.
pub fn generate(&self) -> Generator<'_, T, ThreadRng> ⓘ
std
only.Generates random Markov chain data.
Returns an iterator that yields the elements by reference. If you want
them by value, simply use Iterator::cloned
(as long as T
is
Clone
).
sourcepub fn generate_with_rng<R: Rng>(&self, rng: R) -> Generator<'_, T, R> ⓘ
pub fn generate_with_rng<R: Rng>(&self, rng: R) -> Generator<'_, T, R> ⓘ
Like Self::generate
, but takes a custom random number generator.
sourcepub fn get<'a, I>(&'a self, items: I) -> Option<&'a T>where
I: IntoIterator<Item = Option<&'a T>>,
Available on crate feature std
only.
pub fn get<'a, I>(&'a self, items: I) -> Option<&'a T>where
I: IntoIterator<Item = Option<&'a T>>,
std
only.Gets a random item that has followed items
in the added data.
Only the first self.depth()
items are considered.
A return value of None
either means those items were never followed
by anything in the data passed to Self::add
, or that None
sometimes followed those items and that possibility happened to be
picked by the random number generator.
Given iter
as items.into_iter()
, if
iter.next()
returns None
, it is treated as if
it returned Some(None)
.
sourcepub fn get_with_rng<'a, I, R>(&'a self, items: I, rng: R) -> Option<&'a T>where
I: IntoIterator<Item = Option<&'a T>>,
R: Rng,
pub fn get_with_rng<'a, I, R>(&'a self, items: I, rng: R) -> Option<&'a T>where
I: IntoIterator<Item = Option<&'a T>>,
R: Rng,
Like Self::get
, but takes a custom random number generator.
sourcepub fn get_start(&self) -> impl Iterator<Item = &T>
Available on crate feature std
only.
pub fn get_start(&self) -> impl Iterator<Item = &T>
std
only.Gets some initial items that have appeared at the start of a sequence
(see Self::add_start
).
sourcepub fn get_start_with_rng<R: Rng>(&self, rng: R) -> impl Iterator<Item = &T>
pub fn get_start_with_rng<R: Rng>(&self, rng: R) -> impl Iterator<Item = &T>
Like Self::get_start
, but takes a custom random number generator.
sourcepub fn get_next<'a, I>(&'a self, items: I) -> Option<&'a T>where
I: IntoIterator<Item = &'a T>,
Available on crate feature std
only.
pub fn get_next<'a, I>(&'a self, items: I) -> Option<&'a T>where
I: IntoIterator<Item = &'a T>,
std
only.sourcepub fn get_next_with_rng<'a, I, R>(&'a self, items: I, rng: R) -> Option<&'a T>where
I: IntoIterator<Item = &'a T>,
R: Rng,
pub fn get_next_with_rng<'a, I, R>(&'a self, items: I, rng: R) -> Option<&'a T>where
I: IntoIterator<Item = &'a T>,
R: Rng,
Like Self::get_next
, but takes a custom random number generator.