Struct stochasta::CardDrawTree

source ·
pub struct CardDrawTree<C>
where C: Eq + Hash + Ord,
{ /* private fields */ }
Expand description

A representation of a card drawing process.

§Type Parameters

  • C: The type of a single card

Implementations§

source§

impl<C> CardDrawTree<C>
where C: Eq + Hash + Ord + Clone,

source

pub fn new() -> Self

Creates a new empty tree.

§Example
use stochasta::CardDrawTree;

let tree: CardDrawTree<i32> = CardDrawTree::new();
assert!(tree.is_empty());
source

pub fn create_from(card_deck: &CardDeck<C>) -> Self

Creates a tree with one level from the given card deck.

§Example
use stochasta::{CardDeck, CardDrawTree};

let deck = CardDeck::from(vec!["heads", "tails"]);
let tree = CardDrawTree::create_from(&deck);
source

pub fn without_shrinking(card_deck: &CardDeck<C>, draws: u32) -> Self

Creates a new tree with the number of draws with an unshrinking stack.

The stack won’t shrink from drawing cards; instead every drawn card is put back to the stack.

For a shrinking deck, see Self::shrinking().

source

pub fn shrinking(card_deck: &CardDeck<C>, draws: u32) -> Self

Creates a new tree with the number of draws with a shrinking stack.

The stack will shrink from drawing cards; once a card is drawn it is no longer part of the stack.

For a non-shrinking deck, see Self::without_shrinking().

source

pub fn probability_of(&self, sequence: &[C]) -> Probability

Returns the probability of a certain sequence in the tree.

The order is important as well as the position - the first entry will be searched among the root nodes.

§Example
use stochasta::{CardDeck, CardDrawTree, Probability, PROBABILITY_ONE, PROBABILITY_ZERO};

let coin = CardDeck::from(vec!["H", "T"]);
let tree = CardDrawTree::without_shrinking(&coin, 2);

assert_eq!(tree.probability_of(&[]), PROBABILITY_ONE);
assert_eq!(tree.probability_of(&["H"]), Probability::new(1, 2));
assert_eq!(tree.probability_of(&["H", "H"]), Probability::new(1, 4));
// 3x heads is impossible when only throwing 2x
assert_eq!(tree.probability_of(&["H", "H", "H"]), PROBABILITY_ZERO);
source

pub fn is_empty(&self) -> bool

Returns true if the tree has no nodes.

source

pub fn paths(&self) -> Vec<CardDrawSequence<C>>

Returns all paths.

§Example
use stochasta::{CardDeck, CardDrawTree, CardDrawSequence, Probability};

let coin = CardDeck::from(vec!["H", "T"]);
let tree = CardDrawTree::without_shrinking(&coin, 2);

let result = tree.paths();
let one_quarter = Probability::new(1, 4);

assert_eq!(result.len(), 4);
assert!(result.contains(&CardDrawSequence::new(vec!["H", "H"], one_quarter)));
assert!(result.contains(&CardDrawSequence::new(vec!["H", "T"], one_quarter)));
assert!(result.contains(&CardDrawSequence::new(vec!["T", "H"], one_quarter)));
assert!(result.contains(&CardDrawSequence::new(vec!["T", "T"], one_quarter)));
source§

impl<C> CardDrawTree<C>
where C: Eq + Hash + Ord + Display,

source

pub fn to_graphviz(&self) -> String

Creates a Graphviz-graph from the decision tree.

§Example

For a more interesting graph this example covers an oddly weighted coin where heads is twice as likely to be thrown as tails.

use stochasta::{CardDeck, CardDrawTree};

let odd_coin = CardDeck::from(vec!["heads", "heads", "tails"]);
let tree = CardDrawTree::without_shrinking(&odd_coin, 2);
let output = r#"digraph {
_root[label="", shape="circle"];
_root->_heads_2[label="2/3"];
_heads_2[label="heads (2/3)"];
_heads_2->_heads_3[label="2/3"];
_heads_3[label="heads (4/9)"];
_heads_2->_tails_4[label="1/3"];
_tails_4[label="tails (2/9)"];
_root->_tails_5[label="1/3"];
_tails_5[label="tails (1/3)"];
_tails_5->_heads_6[label="2/3"];
_heads_6[label="heads (2/9)"];
_tails_5->_tails_7[label="1/3"];
_tails_7[label="tails (1/9)"];
}"#;
assert_eq!(tree.to_graphviz(), output);
§ASCII Visualisation

This will result in the following graph (here sideways for better visualisation):

                        2/3
                      +-----[ heads (4/9) ]
      2/3            /
    +-----[ heads (2/3) ]
   /                 \  1/3
  /                   +-----[ tails (2/9) ]
 /
O
 \                      2/3
  \                   +-----[ heads (2/9) ]
   \  1/3            /
    +-----[ tails (1/3) ]
                     \  1/3
                      +-----[ tails (1/9) ]
§Output
  • the paths have the probability from their parent node
  • the cards have additionally the total probability to reach it from the root node in brackets

Trait Implementations§

source§

impl<C> Clone for CardDrawTree<C>
where C: Eq + Hash + Ord + Clone,

source§

fn clone(&self) -> CardDrawTree<C>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<C> Debug for CardDrawTree<C>
where C: Eq + Hash + Ord + Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<C> Default for CardDrawTree<C>
where C: Eq + Hash + Ord + Clone,

source§

fn default() -> Self

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

impl<'de, C> Deserialize<'de> for CardDrawTree<C>
where C: Eq + Hash + Ord + Deserialize<'de>,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<C> Display for CardDrawTree<C>
where C: Eq + Hash + Ord + Clone + Display,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<C> Hash for CardDrawTree<C>
where C: Eq + Hash + Ord + Hash,

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<C> Ord for CardDrawTree<C>
where C: Eq + Hash + Ord + Ord,

source§

fn cmp(&self, other: &CardDrawTree<C>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<C> PartialEq for CardDrawTree<C>
where C: Eq + Hash + Ord + PartialEq,

source§

fn eq(&self, other: &CardDrawTree<C>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<C> PartialOrd for CardDrawTree<C>
where C: Eq + Hash + Ord + PartialOrd,

source§

fn partial_cmp(&self, other: &CardDrawTree<C>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<C> Serialize for CardDrawTree<C>
where C: Eq + Hash + Ord + Serialize,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<C> Eq for CardDrawTree<C>
where C: Eq + Hash + Ord + Eq,

source§

impl<C> StructuralPartialEq for CardDrawTree<C>
where C: Eq + Hash + Ord,

Auto Trait Implementations§

§

impl<C> Freeze for CardDrawTree<C>

§

impl<C> RefUnwindSafe for CardDrawTree<C>
where C: RefUnwindSafe,

§

impl<C> Send for CardDrawTree<C>
where C: Send,

§

impl<C> Sync for CardDrawTree<C>
where C: Sync,

§

impl<C> Unpin for CardDrawTree<C>

§

impl<C> UnwindSafe for CardDrawTree<C>
where C: RefUnwindSafe,

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,