dsa.hashset
Module containing a HashSet class implemented using HashTable.
1""" Module containing a HashSet class implemented using HashTable. """ 2from dsa.hashtable import HashTable 3 4class HashSet: 5 """ 6 A set implementation using a hash table for storage. 7 """ 8 def __init__(self, iterable=None): 9 """ 10 Initialize a hash set. 11 12 Args: 13 iterable: An optional iterable of initial elements. 14 """ 15 self._table = HashTable() 16 if iterable: 17 for item in iterable: 18 self.add(item) 19 20 def add(self, item): 21 """ 22 Add an item to the set. 23 24 args: 25 item: The item to add. 26 """ 27 self._table[item] = True 28 29 def remove(self, item): 30 """ 31 Remove an item from the set. 32 33 Args: 34 item: The item to remove. 35 """ 36 del self._table[item] 37 38 def contains(self, item): 39 """ 40 Check if an item is in the set. 41 42 Args: 43 item: The item to check. 44 """ 45 return item in self._table 46 47 def __contains__(self, item): 48 """ 49 Check if an item is in the set. 50 51 Args: 52 item: The item to check. 53 54 Returns: 55 bool: True if the item is in the set, False otherwise. 56 """ 57 return self.contains(item) 58 59 def __len__(self): 60 """ 61 Get the number of items in the set. 62 63 Returns: 64 int: The number of items in the set. 65 """ 66 return len(self._table) 67 68 def __iter__(self): 69 """ 70 Iterate over the items in the set. 71 72 Yields: 73 The items in the set. 74 """ 75 for key in self._table: 76 yield key 77 78 def __repr__(self): 79 """ 80 Get a string representation of the set. 81 82 Returns: 83 str: A string representation of the set. 84 """ 85 return f"HashSet({list(self)})" 86 87 def __eq__(self, other): 88 """ 89 Check if two sets are equal. 90 91 Args: 92 other: The other set to compare. 93 94 Returns: 95 bool: True if the sets are equal, False otherwise. 96 """ 97 if not isinstance(other, HashSet): 98 return False 99 return set(self) == set(other)
class
HashSet:
5class HashSet: 6 """ 7 A set implementation using a hash table for storage. 8 """ 9 def __init__(self, iterable=None): 10 """ 11 Initialize a hash set. 12 13 Args: 14 iterable: An optional iterable of initial elements. 15 """ 16 self._table = HashTable() 17 if iterable: 18 for item in iterable: 19 self.add(item) 20 21 def add(self, item): 22 """ 23 Add an item to the set. 24 25 args: 26 item: The item to add. 27 """ 28 self._table[item] = True 29 30 def remove(self, item): 31 """ 32 Remove an item from the set. 33 34 Args: 35 item: The item to remove. 36 """ 37 del self._table[item] 38 39 def contains(self, item): 40 """ 41 Check if an item is in the set. 42 43 Args: 44 item: The item to check. 45 """ 46 return item in self._table 47 48 def __contains__(self, item): 49 """ 50 Check if an item is in the set. 51 52 Args: 53 item: The item to check. 54 55 Returns: 56 bool: True if the item is in the set, False otherwise. 57 """ 58 return self.contains(item) 59 60 def __len__(self): 61 """ 62 Get the number of items in the set. 63 64 Returns: 65 int: The number of items in the set. 66 """ 67 return len(self._table) 68 69 def __iter__(self): 70 """ 71 Iterate over the items in the set. 72 73 Yields: 74 The items in the set. 75 """ 76 for key in self._table: 77 yield key 78 79 def __repr__(self): 80 """ 81 Get a string representation of the set. 82 83 Returns: 84 str: A string representation of the set. 85 """ 86 return f"HashSet({list(self)})" 87 88 def __eq__(self, other): 89 """ 90 Check if two sets are equal. 91 92 Args: 93 other: The other set to compare. 94 95 Returns: 96 bool: True if the sets are equal, False otherwise. 97 """ 98 if not isinstance(other, HashSet): 99 return False 100 return set(self) == set(other)
A set implementation using a hash table for storage.
HashSet(iterable=None)
9 def __init__(self, iterable=None): 10 """ 11 Initialize a hash set. 12 13 Args: 14 iterable: An optional iterable of initial elements. 15 """ 16 self._table = HashTable() 17 if iterable: 18 for item in iterable: 19 self.add(item)
Initialize a hash set.
Args: iterable: An optional iterable of initial elements.
def
add(self, item):
21 def add(self, item): 22 """ 23 Add an item to the set. 24 25 args: 26 item: The item to add. 27 """ 28 self._table[item] = True
Add an item to the set.
args: item: The item to add.