def resize(self, des_len): """ Sets self.__set to a prime-length list, up to a max length of 104683. The prime selected is the first prime number larger than the desired length, where there also exists a pair prime number exactly 2 smaller than the set size (for rehashing). If it's the first creation of the set, fills the list with Nones; if it's a resizing of the set, rehashes every single entry and reinserts them. """ two_off_primes = massage_primes() largest_prime = two_off_primes[-1] # If this is the first time making the table, populate with Nones if self.occupancy == 0: if des_len > largest_prime: print "Largest available prime is %d, setting as max table size." % largest_prime self.size = largest_prime self.__set = [None] * largest_prime else: i = 0 while des_len > two_off_primes[i]: i += 1 self.size = two_off_primes[i] self.__set = [None] * two_off_primes[i] # Otherwise, this is a resizing up, and we need to rehash every member # Makes a new set, fills with old set's items, then moves pointer to the new set else: if des_len > largest_prime: self.size = largest_prime new_table = [None] * largest_prime else: i = 0 while des_len > two_off_primes[i]: i += 1 self.size = two_off_primes[i] new_table = [None] * two_off_primes[i] for entry in self.__set: if entry == 'USED' or entry == None: pass else: hashedval = self.compute_hash(entry) new_table[hashedval] = entry self.__set = new_table return
def resize_table(self, des_len): """ Sets self.htable to a prime-length list, up to a max length of 104683. The prime selected is the first prime number larger than the desired length, where there also exists a pair prime number exactly 2 smaller than the table size (for rehashing). If it's the first creation of the table, fills the list with Nones; if it's a resizing of the table, rehashes every entry and reinserts them. """ two_off_primes = massage_primes() largest_prime = two_off_primes[-1] # Makes a new table, fills with old tables items, then moves pointer to the new table, # Unless it's the first time making a table, then set to all Nones. if des_len > largest_prime: self.size = largest_prime # If this is the first time making the table, populate with Nones if self.occupancy == 0: print "Largest available prime is %d, setting as max table size." % largest_prime self.__htable = [None] * largest_prime return else: new_table = [None] * largest_prime else: i = 0 while des_len > two_off_primes[i]: i += 1 self.size = two_off_primes[i] # If this is the first time making the table, populate with Nones if self.occupancy == 0: self.__htable = [None] * two_off_primes[i] return else: new_table = [None] * two_off_primes[i] for entry in self.__htable: if entry == 'USED' or entry is None: pass else: hashedval = self.compute_hash(entry[0]) new_table[hashedval] = (entry[0], entry[1]) self.__htable = new_table