Advertisement
Guest User

hashset_sorted

a guest
Mar 22nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1.  
  2. # Imports
  3. # Use any appropriate data structure here.
  4. from copy import deepcopy
  5.  
  6. from sorted_list_linked import SortedList
  7. # Define the new_slot slot creation function.
  8. new_slot = SortedList
  9.  
  10. # Constants
  11. SEP = '-' * 40
  12.  
  13.  
  14. class HashSet:
  15. """
  16. -------------------------------------------------------
  17. Constants.
  18. -------------------------------------------------------
  19. """
  20. _LOAD_FACTOR = 20
  21.  
  22. def __init__(self, slots):
  23. """
  24. -------------------------------------------------------
  25. Initializes an empty HashSet of size slots.
  26. Use: hs = HashSet(slots)
  27. -------------------------------------------------------
  28. Precondition:
  29. size - number of initial slots in hashset (int > 0)
  30. Postconditions:
  31. Initializes an empty HashSet.
  32. -------------------------------------------------------
  33. """
  34. self._slots = slots
  35. self._table = []
  36.  
  37. for i in range(self._slots):
  38. self._table.append(new_slot())
  39. self._count = 0
  40. return
  41.  
  42. def __len__(self):
  43. """
  44. -------------------------------------------------------
  45. Returns the number of values in the hashset.
  46. Use: n = len( hs )
  47. -------------------------------------------------------
  48. Postconditions:
  49. Returns the number of values in the hashset.
  50. -------------------------------------------------------
  51. """
  52. return self._count
  53.  
  54. def is_empty(self):
  55. """
  56. -------------------------------------------------------
  57. Determines if the hashset is empty.
  58. Use: b = hs.is_empty()
  59. -------------------------------------------------------
  60. Postconditions:
  61. Returns True if the hashset is empty, False otherwise.
  62. -------------------------------------------------------
  63. """
  64. return self._count == 0
  65.  
  66. def _find_slot(self, key):
  67. """
  68. -------------------------------------------------------
  69. Returns the slot for a key value.
  70. Use: list = hs._find_slot( key )
  71. -------------------------------------------------------
  72. Postconditions:
  73. returns:
  74. slot - list at the position of hash key in self._slots
  75. -------------------------------------------------------
  76. """
  77. hashkey = hash(key) % self._slots
  78. slot = self._table[hashkey]
  79. return slot
  80.  
  81. def __contains__(self, key):
  82. """
  83. ---------------------------------------------------------
  84. Determines if the hashset contains key.
  85. Use: b = key in hs
  86. -------------------------------------------------------
  87. Preconditions:
  88. key - a comparable data element (?)
  89. Postconditions:
  90. Returns True if the hashset contains key, False otherwise.
  91. -------------------------------------------------------
  92. """
  93. slot = self._find_slot(key)
  94. return key in slot
  95.  
  96. def insert(self, value):
  97. """
  98. ---------------------------------------------------------
  99. Inserts value into the hashset, allows only one copy of value.
  100. Calls _rehash if the hashset _LOAD_FACTOR is exceeded.
  101. Use: inserted = hs.insert( value )
  102. -------------------------------------------------------
  103. Preconditions:
  104. value - a comparable data element (?)
  105. Postconditions:
  106. returns
  107. inserted - True if value is inserted, False otherwise.
  108. -------------------------------------------------------
  109. """
  110. inserted = False
  111. val_slot = self._find_slot(value)
  112. if not value in val_slot:
  113. inserted = True
  114. val_slot.insert(value)
  115. self._count += 1
  116. if self._count > self._LOAD_FACTOR * self._slots:
  117. self._rehash()
  118.  
  119. return inserted
  120.  
  121. def find(self, key):
  122. """
  123. ---------------------------------------------------------
  124. Returns the value identified by key.
  125. Use: value = hs.find( key )
  126. -------------------------------------------------------
  127. Preconditions:
  128. key - a comparable data element (?)
  129. Postconditions:
  130. returns:
  131. value - if it exists in the hashset, None otherwise.
  132. -------------------------------------------------------
  133. """
  134. slot = self._find_slot(key)
  135. value = slot.find(key)
  136. return value
  137.  
  138. def remove(self, key):
  139. """
  140. ---------------------------------------------------------
  141. Removes the value matching key from the hashset, if it exists.
  142. Use: value = hs.remove( key )
  143. -------------------------------------------------------
  144. Preconditions:
  145. key - a comparable data element (?)
  146. Postconditions:
  147. returns:
  148. value - if it exists in the hashset, None otherwise.
  149. -------------------------------------------------------
  150. """
  151. value = None
  152. s = self._find_slot(key)
  153. if key in s:
  154. value = s.pop(s.index(key))
  155.  
  156. self._count -= 1
  157. return value
  158.  
  159. def _rehash(self):
  160. """
  161. ---------------------------------------------------------
  162. Increases the number of slots in the hashset and reallocates the
  163. existing data within the hashset to the new table.
  164. Use: hs._rehash()
  165. -------------------------------------------------------
  166. Postconditions:
  167. Existing data is reallocated amongst the hashset table.
  168. -------------------------------------------------------
  169. """
  170. old_table = deepcopy(self._table) # Inefficient af
  171. old_slots = self._slots
  172.  
  173. self._slots = self._slots * 2 + 1
  174.  
  175. for i in range(old_slots):
  176. self._table[i] = new_slot()
  177. for i in range(old_slots, self._slots):
  178. self._table.append(new_slot())
  179.  
  180. for i in old_table:
  181. for j in i:
  182. s = self._find_slot(j)
  183. s.insert(j)
  184.  
  185. return
  186.  
  187. def __iter__(self):
  188. for i in range(self._slots):
  189. for j in self._table[i]:
  190. yield j
  191.  
  192. def debug(self):
  193. """
  194. ---------------------------------------------------------
  195. Prints the contents of the hashset starting at slot 0,
  196. showing the slot currently being printed. Used for
  197. debugging purposes.
  198. Use: hs.debug()
  199. -------------------------------------------------------
  200. Postconditions:
  201. The contents of the hashset are printed and the slots identified.
  202. -------------------------------------------------------
  203. """
  204. for i in range(self._slots):
  205. print("\nSlot {}\n".format(i))
  206. for j in self._table[i]:
  207. print(j)
  208. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement