Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. class hash_table:
  2. def __init__(self, size, n_elements, collision_treatment_type, hash_function_type):
  3. self.size = size
  4. self.n_elements = n_elements
  5. self.collision_treatment_type = collision_treatment_type
  6. self.hash_function_type = hash_function_type
  7. self.table = [[None] for i in range(size)]
  8. self.collisions = 0
  9. self.positions_occupied = 0
  10. self.occupancy_rate = 0
  11. self.least = 0
  12. self.most = 0
  13. self.least_name = []
  14. self.most_name = []
  15.  
  16. def hash_func(self, string):
  17. if self.hash_function_type == 1:
  18. aux = 0
  19. cont = 0
  20. for c in string:
  21. aux+= ord(c)**2
  22. hax = (aux)%len(self.table)
  23. #print('nome: ', string,'tamanho da hash table: ', len(self.table), 'hash: ', hax)
  24. return hax
  25.  
  26. elif self.hash_function_type == 2:
  27. hax = 0
  28. cont = 0
  29. p = 31
  30. for c in string:
  31. hax = hax + ord(c)*(p**cont)
  32. cont+=1
  33. return (hax%len(self.table))
  34. else:
  35. print('funcao hash de tipo invalido')
  36.  
  37.  
  38. def insert(self, string):
  39. hash_key = self.hash_func(string)
  40. if self.table[hash_key][0] == None:
  41. self.table[hash_key][0] = string
  42. self.positions_occupied+=1
  43. self.occupancy_rate= 100*(self.positions_occupied)/self.size
  44. else:
  45. if self.collision_treatment_type == 1:
  46. self.table[hash_key].append(string)
  47. self.collisions+=1
  48.  
  49. elif self.collision_treatment_type == 2:
  50. hash_key+= 1
  51. while self.table[hash_key][0] != None:
  52. hash_key += 1
  53. self.collisions+=1
  54. self.table[hash_key][0] = string
  55. self.positions_occupied+=1
  56. self.occupancy_rate= 100*(self.positions_occupied)/self.size
  57.  
  58. else:
  59. print('tipo de tratamento de colisao da hash table invalido')
  60.  
  61. def search(self, string):
  62. acessos = 0
  63. hash_key = self.hash_func(string)
  64. flag = False
  65. if self.collision_treatment_type == 1:
  66. for item in self.table[hash_key]:
  67. acessos+=1
  68. if item == string:
  69. flag = True
  70. print('nome', string, 'encontrado no index: ', hash_key, 'e o total de acessos foi: ', acessos)
  71.  
  72. if self.least == 0:
  73. self.least = acessos
  74. self.least_name.append(string)
  75. elif acessos < self.least:
  76. self.least = acessos
  77. self.least_name.clear()
  78. self.least_name.append(string)
  79. elif acessos == self.least:
  80. self.least_name.append(string)
  81.  
  82. if self.most == 0:
  83. self.most = acessos
  84. self.most_name.append(string)
  85. elif acessos > self.most:
  86. self.most = acessos
  87. self.most_name.clear()
  88. self.most_name.append(string)
  89. elif acessos == self.most:
  90. self.most_name.append(string)
  91.  
  92. if flag == False:
  93. print('nome', string, 'nao foi encontrado')
  94. elif self.collision_treatment_type == 2:
  95. condition = False
  96. while (condition == False):
  97. if ((self.table[hash_key][0] == string) or (self.table[hash_key][0] == None)):
  98. condition = True
  99. if self.table[hash_key][0] == string:
  100. flag = True
  101. else:
  102. hash_key+=1
  103. acessos+=1
  104. if flag == True:
  105. print('nome', string, 'encontrado no index: ', hash_key, 'e o total de acessos foi: ', acessos)
  106.  
  107. if self.least == 0:
  108. self.least = acessos
  109. self.least_name.append(string)
  110. elif acessos < self.least:
  111. self.least = acessos
  112. self.least_name.clear()
  113. self.least_name.append(string)
  114. elif acessos == self.least:
  115. self.least_name.append(string)
  116.  
  117. if self.most == 0:
  118. self.most = acessos
  119. self.most_name.append(string)
  120. elif acessos > self.most:
  121. self.most = acessos
  122. self.most_name.clear()
  123. self.most_name.append(string)
  124. elif acessos == self.most:
  125. self.most_name.append(string)
  126.  
  127. else:
  128. print('nome', string, 'nao foi encontrado')
  129. else:
  130. print('tipo de tratamento de colisao da hash table invalido')
  131.  
  132.  
  133. table = hash_table(3169 , 10000, 1, 1) #hash table containing modal hash function and chained collision treatment
  134. table2 = hash_table(3169, 10000, 1, 2) #hash table containing polinomial hash function and chained collision treatment
  135. table3 = hash_table(20123, 10000, 2, 1) #hash table containing modal hash function and linear search (EABL) collision treatment
  136. table4 = hash_table(20123, 10000, 2, 2) #hash table containing polinomial hash function and linear search (EABL) collision treatment
  137. #defining R, a small prime number that will be used in the modular hash function
  138. R = 53
  139. M = len(table.table)
  140.  
  141. #to read the archive building all the 4 possible hash tables containing the names in each line:
  142. f= open("nomes_10000.txt","r")
  143. for line in f:
  144. aux = line[0:len(line) - 1]
  145. table.insert(aux)
  146. table2.insert(aux)
  147. table3.insert(aux)
  148. table4.insert(aux)
  149.  
  150. f.close()
  151. print('')
  152. print('TABELA COM HASH MODAL E ENCADEAMENTO FECHADO:')
  153. print('numero de colisoes: ', table.collisions)
  154. print('taxa de ocupacao da tabela:', table.occupancy_rate,'%')
  155. print('')
  156. print('TABELA COM HASH POLINOMIAL E ENCADEAMENTO FECHADO:')
  157. print('numero de colisoes: ', table2.collisions)
  158. print('taxa de ocupacao da tabela:', table2.occupancy_rate,'%')
  159. print('')
  160. print('TABELA COM HASH MODAL E EABL:')
  161. print('numero de colisoes: ', table3.collisions)
  162. print('taxa de ocupacao da tabela:', table3.occupancy_rate,'%')
  163. print('')
  164. print('TABELA COM HASH POLINOMIAL E EABL:')
  165. print('numero de colisoes: ', table4.collisions)
  166. print('taxa de ocupacao da tabela:', table4.occupancy_rate,'%')
  167. print('')
  168. print('consultas:')
  169. #to read the second archive:
  170. f = open("consultas.txt", "r")
  171. for line in f:
  172. aux = line[0:len(line) - 1]
  173. table.search(aux)
  174. print('')
  175. print('nome(s) com menor numero de acessos:',table.least_name)
  176. print('com ', table.least, ' acesso(s)')
  177. print('')
  178. print('nome(s) com maior numero de acessos:',table.most_name)
  179. print('com ', table.most, ' acesso(s)')
  180.  
  181. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement