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,
impl<C> CardDeck<C>where C: Eq + Hash + Ord,
sourcepub fn new() -> Self
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);
sourcepub fn add(&mut self, card: C)
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);
sourcepub fn add_times(&mut self, card: C, n: u64)
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);
sourcepub fn remove_all(&mut self, card: &C)
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);
sourcepub fn remove_times(&mut self, card: C, n: u64)
pub fn remove_times(&mut self, card: C, n: u64)
sourcepub fn set_card(&mut self, card: C, n: u64)
pub fn set_card(&mut self, card: C, n: u64)
Sets the amount of card
s 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);
sourcepub fn is_empty(&self) -> bool
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);
sourcepub fn size(&self) -> u64
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);
sourcepub fn probability(&self, card: &C) -> Probability
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));
sourcepub fn probabilities(&self) -> HashMap<&C, Probability>
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§impl<C> CardDeck<C>where
C: Eq + Hash + Ord + Clone,
impl<C> CardDeck<C>where C: Eq + Hash + Ord + Clone,
sourcepub fn draw(&self, card: C) -> Self
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<'de, C> Deserialize<'de> for CardDeck<C>where
C: Eq + Hash + Ord + Deserialize<'de>,
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>,
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> Extend<C> for CardDeck<C>where
C: Eq + Hash + Ord,
impl<C> Extend<C> for CardDeck<C>where C: Eq + Hash + Ord,
source§fn extend<T: IntoIterator<Item = C>>(&mut self, cards: T)
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)
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)
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> FromIterator<C> for CardDeck<C>where
C: Eq + Hash + Ord + Default,
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>,
fn from_iter<T>(cards: T) -> Selfwhere T: IntoIterator<Item = C>,
Creates a value from an iterator. Read more
source§impl<C> Ord for CardDeck<C>where
C: Eq + Hash + Ord + Ord,
impl<C> Ord for CardDeck<C>where C: Eq + Hash + Ord + Ord,
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
Compares and returns the maximum of two values. Read more
source§impl<C> PartialEq<CardDeck<C>> for CardDeck<C>where
C: Eq + Hash + Ord + PartialEq,
impl<C> PartialEq<CardDeck<C>> for CardDeck<C>where C: Eq + Hash + Ord + PartialEq,
source§impl<C> PartialOrd<CardDeck<C>> for CardDeck<C>where
C: Eq + Hash + Ord + PartialOrd,
impl<C> PartialOrd<CardDeck<C>> for CardDeck<C>where C: Eq + Hash + Ord + PartialOrd,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
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 moreimpl<C> Eq for CardDeck<C>where C: Eq + Hash + Ord + Eq,
impl<C> StructuralEq for CardDeck<C>where C: Eq + Hash + Ord,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more