Advertisement
ricco_soares

hash table third variation

Nov 17th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.03 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.    
  12.     def hash_func(self, string):
  13.         if self.hash_function_type == 1:
  14.             aux = 0
  15.             count = 5
  16.             for c in string:
  17.                 aux+= ord(c)*count
  18.                 count+=1
  19.             hax = (aux)%len(self.table)
  20.             #print('nome: ', string,'tamanho da hash table: ', len(self.table), 'hash: ', hax)
  21.             return hax
  22.  
  23.         elif self.hash_function_type == 2:
  24.             hax = 0
  25.             cont = 0
  26.             p = 31
  27.             for c in string:
  28.                 hax = hax + ord(c)*(p**cont)
  29.                 cont+=1
  30.             return (hax%len(self.table))
  31.         else:
  32.             print('funcao hash de tipo invalido')
  33.          
  34.    
  35.     def insert(self, string):
  36.         hash_key = self.hash_func(string)
  37.         if self.table[hash_key][0] == None:
  38.             self.table[hash_key][0] = string
  39.             self.positions_occupied+=1
  40.             self.occupancy_rate= 100*(self.positions_occupied)/self.size
  41.         else:
  42.             if self.collision_treatment_type == 1:
  43.                 self.table[hash_key].append(string)  
  44.                 self.collisions+=1        
  45.  
  46.             elif self.collision_treatment_type == 2:
  47.                 hash_key+= 2
  48.                 while self.table[hash_key][0] != None:
  49.                     hash_key += 2
  50.                     self.collisions+=1
  51.                 self.table[hash_key][0] = string
  52.                 self.positions_occupied+=1
  53.                 self.occupancy_rate= 100*(self.positions_occupied)/self.size
  54.  
  55.             else:
  56.                 print('tipo de tratamento de colisao da hash table invalido')
  57.  
  58.     def search(self, string):
  59.         acessos = 0
  60.         hash_key = self.hash_func(string)
  61.         flag = False
  62.         if self.collision_treatment_type == 1:
  63.             for item in self.table[hash_key]:
  64.                 acessos+=1
  65.                 if item == string:
  66.                     flag = True
  67.                     print('nome', string, 'encontrado no index: ', hash_key, 'e o total de acessos foi: ', acessos)
  68.             if flag == False:
  69.                 print('nome', string, 'nao foi encontrado')
  70.         elif self.collision_treatment_type == 2:
  71.             condition = False
  72.             while (condition == False):
  73.                 if ((self.table[hash_key][0] == string) or (self.table[hash_key][0] == None)):
  74.                     condition = True
  75.                     if self.table[hash_key][0] == string:
  76.                         flag = True
  77.                 else:
  78.                     hash_key+=2
  79.                 acessos+=1
  80.             if flag == True:
  81.                 print('nome', string, 'encontrado no index: ', hash_key, 'e o total de acessos foi: ', acessos)  
  82.             else:
  83.                 print('nome', string, 'nao foi encontrado')
  84.         else:
  85.             print('tipo de tratamento de colisao da hash table invalido')
  86.  
  87.  
  88. table = hash_table(3169 , 10000, 1, 1) #hash table containing modal hash function and chained collision treatment
  89. table2 = hash_table(3169, 10000, 1, 2)  #hash table containing polinomial hash function and chained collision treatment
  90. table3 = hash_table(20123, 10000, 2, 1) #hash table containing modal hash function and linear search (EABL) collision treatment
  91. table4 = hash_table(20123, 10000, 2, 2) #hash table containing polinomial hash function and linear search (EABL) collision treatment
  92. #defining R, a small prime number that will be used in the modular hash function
  93. R = 53
  94. M = len(table.table)
  95.  
  96. #to read the archive building all the 4 possible hash tables containing the names in each line:
  97. f= open("nomes_10000.txt","r")
  98. for line in f:
  99.     aux = line[0:len(line) - 1]
  100.     table.insert(aux)
  101.     table2.insert(aux)
  102.     table3.insert(aux)
  103.     table4.insert(aux)
  104.  
  105. f.close()
  106. print('')
  107. print('TABELA COM HASH MODAL E ENCADEAMENTO FECHADO:')
  108. print('numero de colisoes: ', table.collisions)
  109. print('taxa de ocupacao da tabela:', table.occupancy_rate,'%')
  110. print('')
  111. print('TABELA COM HASH POLINOMIAL E ENCADEAMENTO FECHADO:')
  112. print('numero de colisoes: ', table2.collisions)
  113. print('taxa de ocupacao da tabela:', table2.occupancy_rate,'%')
  114. print('')
  115. print('TABELA COM HASH MODAL E EABL:')
  116. print('numero de colisoes: ', table3.collisions)
  117. print('taxa de ocupacao da tabela:', table3.occupancy_rate,'%')
  118. print('')
  119. print('TABELA COM HASH POLINOMIAL E EABL:')
  120. print('numero de colisoes: ', table4.collisions)
  121. print('taxa de ocupacao da tabela:', table4.occupancy_rate,'%')
  122. print('')
  123. print('consultas:')
  124. #to read the second archive:
  125. f = open("consultas.txt", "r")
  126. for line in f:
  127.    aux = line[0:len(line) - 1]
  128.    table.search(aux)
  129.  
  130. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement