Defining 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 works out nicely because there's only one instance of any given card, and the set class can do very fast operations to confirm that 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 of 9, 10, Jack, Queen, King, and Ace. A simple set won't work well for this; we would need a multiset or bag. This is a set that permits duplicate items.
The operations are still limited to membership tests. For example, we can add the object Card(9,'♢')
object more than once, and then also remove it more than one time.
We have a number of ways to create a multiset:
- We can use a list. Appending an item has a nearly fixed cost, characterized as O(1). Searching for an item has a bad performance problem. The complexity of testing for membership tends...