Struct stochasta::CardDeck

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

A deck of cards.

This may contain multiple cards which are equal.

Example: Uneven dice

The following code shows how to construct an uneven dice with a second one instead of a six.

use stochasta::CardDeck;
use stochasta::Probability;

let dice = CardDeck::from(vec!["1", "2", "3", "4", "5", "1"]);

assert_eq!(dice.size(), 6);
assert_eq!(dice.probability(&"1"), Probability::new(1, 3));
assert_eq!(dice.probability(&"2"), Probability::new(1, 6));
assert_eq!(dice.probability(&"5"), Probability::new(1, 6));
assert_eq!(dice.probability(&"6"), Probability::new(0, 6));

Type Parameters

  • C: The type of a single card

Implementations§

source§

impl<C> CardDeck<C>where C: Eq + Hash + Ord,

source

pub fn new() -> Self

Creates a new empty deck.

Example
use stochasta::CardDeck;

let cards: CardDeck<i32> = CardDeck::new();
assert_eq!(cards.is_empty(), true);
source

pub fn add(&mut self, card: C)

Adds the given card once to the deck.

Example
use stochasta::CardDeck;

let card = "demo";
let mut deck = CardDeck::new();
assert_eq!(deck.count(&card), 0);

deck.add(card);
assert_eq!(deck.count(&card), 1);
source

pub fn add_times(&mut self, card: C, n: u64)

Adds the card n times to the deck.

Example
use stochasta::CardDeck;

let card = "demo";
let mut deck = CardDeck::new();
assert_eq!(deck.count(&card), 0);

deck.add_times(card, 5);
assert_eq!(deck.count(&card), 5);
source

pub fn remove_all(&mut self, card: &C)

Removes all appearances of the given card from the deck.

Example
use stochasta::CardDeck;

let mut deck = CardDeck::from(vec![1, 3, 3]);
assert_eq!(deck.count(&3), 2);

deck.remove_all(&3);
assert_eq!(deck.count(&3), 0);
source

pub fn remove_times(&mut self, card: C, n: u64)

Removes the card n times from the deck.

If n is greater than count the amount will simply set to zero.

Example
use stochasta::CardDeck;

let mut deck = CardDeck::from(vec![3, 3, 3]);
assert_eq!(deck.count(&3), 3);

deck.remove_times(3, 2);
assert_eq!(deck.count(&3), 1);
source

pub fn set_card(&mut self, card: C, n: u64)

Sets the amount of cards to n. Will overwrite any pre-existing value.

Example
use stochasta::CardDeck;

let mut deck = CardDeck::new();
assert_eq!(deck.count(&"alpha"), 0);

deck.set_card("alpha", 10);
assert_eq!(deck.count(&"alpha"), 10);
source

pub fn is_empty(&self) -> bool

Returns true, if the deck is empty.

Example
use stochasta::CardDeck;

let cards: CardDeck<i32> = CardDeck::new();
// ...
assert_eq!(cards.is_empty(), cards.size() == 0);
source

pub fn size(&self) -> u64

Returns the number of cards in the deck.

Example
use stochasta::CardDeck;

let weird_dice = CardDeck::from(vec![1, 2, 1]);
assert_eq!(weird_dice.size(), 3);
source

pub fn probability(&self, card: &C) -> Probability

Returns the probability of an equal card to be drawn.

Example
use stochasta::Probability;
use stochasta::CardDeck;

let dice = CardDeck::from(vec!["1", "2", "3", "4", "5", "6"]);
assert_eq!(dice.probability(&"1"), Probability::new(1, 6));
source

pub fn probabilities(&self) -> HashMap<&C, Probability>

Returns the probability of the cards to be drawn.

The probabilities are guaranteed to be > 0.

Example
use std::collections::HashMap;
use stochasta::Probability;
use stochasta::CardDeck;

let coin = CardDeck::from(vec!["heads", "tails"]);
assert_eq!(
    coin.probabilities(),
    HashMap::from([
        (&"heads", Probability::new(1, 2)),
        (&"tails", Probability::new(1, 2)),
    ])
);
source

pub fn contains(&self, card: &C) -> bool

Checks whether the card is contained at least once in the deck.

Example
use stochasta::CardDeck;

let card = "demo";
let mut deck = CardDeck::new();
assert_eq!(deck.contains(&card), false);

deck.add(card);
assert_eq!(deck.contains(&card), true);
source

pub fn count(&self, card: &C) -> u64

Checks the amount of equal cards.

Example
use stochasta::CardDeck;

let deck = CardDeck::from(vec![1, 3, 3]);

assert_eq!(deck.count(&1), 1);
assert_eq!(deck.count(&3), 2);
assert_eq!(deck.count(&5), 0);
source§

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

source

pub fn draw(&self, card: C) -> Self

Draws one exemplar of card from the deck and returns a new deck.

Example
use stochasta::CardDeck;

let original = CardDeck::from(vec![1, 2, 3]);

assert_eq!(original.contains(&3), true);

let new_deck = original.draw(3);

assert_eq!(original.contains(&3), true);
assert_eq!(new_deck.contains(&3), false);

Trait Implementations§

source§

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

source§

fn clone(&self) -> CardDeck<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 CardDeck<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 CardDeck<C>where C: Eq + Hash + Ord + Default,

source§

fn default() -> CardDeck<C>

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

impl<'de, C> Deserialize<'de> for CardDeck<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 CardDeck<C>where C: Eq + Hash + Ord + Display,

source§

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

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

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

source§

fn extend<T: IntoIterator<Item = C>>(&mut self, cards: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<C> From<Vec<C, Global>> for CardDeck<C>where C: Eq + Hash + Ord + Default,

source§

fn from(cards: Vec<C>) -> Self

Converts to this type from the input type.
source§

impl<C> FromIterator<C> for CardDeck<C>where C: Eq + Hash + Ord + Default,

source§

fn from_iter<T>(cards: T) -> Selfwhere T: IntoIterator<Item = C>,

Creates a value from an iterator. Read more
source§

impl<C> Hash for CardDeck<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 CardDeck<C>where C: Eq + Hash + Ord + Ord,

source§

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

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

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

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

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

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

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

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

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

source§

fn eq(&self, other: &CardDeck<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<CardDeck<C>> for CardDeck<C>where C: Eq + Hash + Ord + PartialOrd,

source§

fn partial_cmp(&self, other: &CardDeck<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 CardDeck<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 CardDeck<C>where C: Eq + Hash + Ord + Eq,

source§

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

source§

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

Auto Trait Implementations§

§

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

§

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

§

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

§

impl<C> Unpin for CardDeck<C>

§

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

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere T: for<'de> Deserialize<'de>,