|
Data Structures and Algorithms
with Object-Oriented Design Patterns in Python |
Program
shows the insert, __contains__,
and withdraw methods of the SetAsArray class.
The insert method is used to put an item into the set.
The method takes an int argument that specifies the item to be inserted.
Then the corresponding element of _array is set to True
to indicate that the item has been added to the set.
The running time of the insert operation is O(1).

Program: SetAsArray class insert, __contains__ and withdraw methods.
The __contains__ method is used to test whether a given item is an element of the set. The semantics are somewhat subtle. Since a set does not actually keep track of the specific object instances that are inserted, the membership test is based on the value of the argument. The method simply returns the value of the appropriate element of the _array. The running time of the __contains__ operation is O(1).
The withdraw method is used to take an item out of a set. The withdrawal operation is the opposite of insertion. Instead of setting the appropriate array element to True, it is set to False. The running time of the withdraw is identical to that of insert, viz., is O(1).