DrawSet
: A set with an efficient random draw
- class epydemic.DrawSet(including=None, excluding=None)
A set re-implementation.
This class provides a re-implementation of sets that also provides an efficient (\(O(\log N)\)) method for drawing an element at random.
We implement only those parts of the set API that we need: perhaps ought to add the rest, for future-proofing.
- Parameters:
including (
Optional
[Iterator
[Union
[Any
,Tuple
[Any
,Any
]]]]) – (optional) iterator of elements to addexcluding (
Optional
[Iterator
[Union
[Any
,Tuple
[Any
,Any
]]]]) – (optional) elements to exclude from the initialisation
The set interface
- DrawSet.add(e)
Add an element to the set. This is a no-op if the element is already in the set.
- Parameters:
e (
Union
[Any
,Tuple
[Any
,Any
]]) – the element to add
- DrawSet.__contains__(e)
Check whether the given element is a member of the set.
- Parameters:
e (
Union
[Any
,Tuple
[Any
,Any
]]) – the element- Return type:
bool
- Returns:
True if the element is in the set
- DrawSet.empty()
Test if the set is empty.
- Return type:
bool
- Returns:
True if the set is empty
- DrawSet.__len__()
Return the size of the set.
- Return type:
int
- Returns:
the size of the set
- DrawSet.__iter__()
Return an iterator that returns elements in order.
- Return type:
Iterator
[Union
[Any
,Tuple
[Any
,Any
]]]- Returns:
an iterator
Random drawing
The point of this class is to provide a way of drawing a random element efficiently, which isn’t possible using the standard Python set interface.
- DrawSet.draw()
Draw an element from the set at random.
- Return type:
Union
[Any
,Tuple
[Any
,Any
]]- Returns:
a random element, or none if the set is empty