Improving performance with an ordered collection
When simulating card games, the player's hand can be modeled as a set of cards or a list of cards. With most conventional single-deck games, a set collection works out nicely because there's only one instance of any given card, and the set class can do very fast operations to confirm whether a given card is or is not in the set.
When modeling Pinochle, however, we have a challenging problem. The Pinochle deck is 48 cards; it has two each of the 9, 10, Jack, Queen, King, and ace cards. A simple set
object won't work well when duplicates are possible; we would need a multiset or bag. A multiset is a set-like collection that permits duplicate items.
In a multiset, a membership test becomes a bit more complex. For example, we can add the Card(9,'
')
object to a bag collection more than once, and then also remove it more than one time.
We have a number of ways to create a multiset:
- We can...