def main(): size = 1000003 # this is a prime number # generate random data to be inserted data = Array(size) for i in range(size): data[i] = random.randint(1, 1000000000) load = [.25, .5, .67, .8, .95] for load_percent in load: # Create new Hash Table h_lp = HashTable_LinearProbing(size) h_dh = HashTable_DoubleHashing(size) num_load_before = int(load_percent * size - 1) num_load_after = int((load_percent + 0.01) * size) for i in range(1, num_load_before): h_lp.insert(data[i], 'Item') h_dh.insert(data[i], 'Item') slots_accessed_before = h_lp.slotsAccessed slots_dh_accessed_before = h_dh.slotsAccessed for i in range(num_load_before, num_load_after): h_lp.insert(data[i], 'Item') h_dh.insert(data[i], 'Item') slots_accessed_after = h_lp.slotsAccessed slots_dh_accessed_after = h_dh.slotsAccessed print 'Load = {}'.format(load_percent) print 'Linear probing {}'.format( round( float((slots_accessed_after - slots_accessed_before) / (0.01 * size)), 2)) print 'Double hashing {}'.format( round( float((slots_dh_accessed_after - slots_dh_accessed_before) / (0.01 * size)), 2)) print
def __init__(self, nrows, ncols): self._array1 = Array(nrows) for i in range(nrows): self._array1[i] = Array(ncols) self._array1[i].clear(None)
# %% # 2.实现一个长度为10的数组,填充数据并遍历显示 from my_array import Array a = Array(10) for i in range(len(a)): a[i] = i + 1 print(a) # %% # 3.修改数组长度为12,并在索引第5和第6位置插入数据 logicalSize = 10 if logicalSize == len(a): temp = Array(len(a) + 2) for i in range(logicalSize): temp[i] = a[i] # 相当于重新创建了一个数组 a = temp print(a) # 验证修改是否成功 targetIndex = 5 for i in range(logicalSize + 1, targetIndex, -1): a[i] = a[i - 1] a[5] = 11 print(a)
def __init__( self, size ): self._table = Array( size ) self._table.clear( None ) self._size = size self._count = 0 self.slotsAccessed = 0
class HashTable_DoubleHashing : def __init__( self, size ): self._table = Array( size ) self._table.clear( None ) self._size = size self._count = 0 self.slotsAccessed = 0 def __len__( self ): return self._count def __contains__( self, key ): ( found, slot ) = self._findSlot( key) return found # insert (if not already in table) # return True/False is key inserted/not def insert( self, key, value ): ( found, slot ) = self._findSlot( key ) if not found : self._table[slot] = _MapEntry( key, value ) self._count += 1 return not found # remove (key, value) (if in the table) # return True/False is key removed/not def remove( self, key ): ( found, slot ) = self._findSlot( key ) if found : self._table[slot] = EMPTY self._count -= 1 return found # find the slot where a key is or should be inserted # return (True/False, slot) if key was found/not def _findSlot(self, key): home = self._hash1(key) i = 0 slot = (home + i) % self._size # IF WE FOUND IT WITHOUT COLLISION, NO NEED TO DOUBLE HASH self.slotsAccessed += 1 if (self._table[slot] == UNUSED or \ (self._table[slot] != EMPTY and \ self._table[slot].key == key)): if self._table[slot] == UNUSED: return (False, slot) elif self._table[slot] != EMPTY and self._table[slot].key == key: return (True, slot) else: i = 1 # Iterate through it once while (i <= self._size): slot = (home + i * self._hash2(key)) % self._size i += 1 self.slotsAccessed += 1 # If we come across an unused slot, it means that its # Never been added before, so we should add it here if self._table[slot] == UNUSED: return (False, slot) elif self._table[slot] != EMPTY and self._table[slot].key == key: return (True, slot) # compute first slot attempted def _hash1( self, key ): return abs(hash(key)) % self._size # compute step for double hashing def _hash2( self, key ): return 1 + abs(hash(key)) % (self._size - 2)
def AddLength(logicalSize, a=[]): tmp = Array(logicalSize) # 创建一个临时数组 for i in range(logicalSize): tmp[i] = a[i] # 相当于重新创建了一个数组 a = tmp
from my_array import Array def AddLength(logicalSize, a=[]): tmp = Array(logicalSize) # 创建一个临时数组 for i in range(logicalSize): tmp[i] = a[i] # 相当于重新创建了一个数组 a = tmp a = Array(10) for i in range(len(a)): a[i] = i + 1 print(a) AddLength(12, a) print(a)
from my_array import Array if __name__ == '__main__': capacity = 4 array = Array(capacity) print('-- Beginning of testing --') print('') # Tests with initial values and capacity of 4 print('Array size: {}'.format(array.get_size())) # Should return 0 print('Array capacity: {}'.format(array.get_capacity())) # Should return 4 print('Array is empty: {}'.format(array.is_empty())) # Should return True print('') # Push items into the array array.push(7) array.push(30) print('Array value at index 0: {}'.format(array.at(0))) # Should return 7 print('Find index of item 30: {}'.format(array.find(30))) # Should return 1 print('') # Reach max capacity and resize the array array.push(3) array.push(10) array.push(2) print('Array size after pushing values: {}'.format(array.get_size())) # Should return 5 print('Capacity after resizing Array: {}'.format(array.get_capacity())) # Should return 8 print('Array is empty, after pushing values: {}'.format(array.is_empty())) # Should return False print('Array value at index 4, after resizing: {}'.format(array.at(4))) # Should return 2 print('')
def test_count(): b = Array('i', 1, 1, 2) assert b.count(1) == 2
def int_startup(): Array('i', 1) return True
def str_startup(): Array('str')
def test_int_startup(): assert int_startup() def str_startup(): Array('str') def test_str_startup(): with pytest.raises(TypeError): str_startup() a = Array('i', 1, 2, 3, 4, 5) def iterate(): b = [_ for _ in a] return b def test_iter(): assert iterate() == [1, 2, 3, 4, 5] def test_len(): assert len(a) == 5
def __init__(self, max_size=100): self._front = 0 self._back = 0 self._cnt = 0 self._arr = Array(max_size)
class HashTable_LinearProbing : def __init__(self, size): self._table = Array(size) self._table.clear(UNUSED) self._size = size self._count = 0 self.slotsAccessed = 0 def __len__(self): return self._count def __contains__(self, key): (found, slot) = self._findSlot(key) return found # insert (if not already in table) # return True/False is key inserted/not def insert(self, key, value): (found, slot) = self._findSlot(key) if not found : self._table[slot] = _MapEntry(key, value) self._count += 1 return not found # remove (key, value) (if in the table) # return True/False is key removed/not def remove(self, key): (found, slot) = self._findSlot(key) if found : self._table[slot] = EMPTY self._count -= 1 return found # find the slot where a key is or should be inserted # return (True/False, slot) if key was found/not def _findSlot(self, key): home = self._hash1(key) i = 0 # Iterate through it once while (i <= self._size): slot = (home + i) % self._size self.slotsAccessed += 1 # If we come across an unused slot, it means that its # Never been added before, so we should add it here if self._table[slot] == UNUSED: return (False, slot) elif self._table[slot] != EMPTY and self._table[slot].key == key: return (True, slot) i += 1 # Iterate once more i = 0 if (self._table[home] == EMPTY): self.slotsAccessed += 1 return (False, slot) while (i < self._size): slot = (home + i) % self._size self.slotsAccessed += 1 if self._table[slot] == EMPTY: return (False, slot) i += 1 # compute first slot attempted def _hash1(self, key): return abs(hash(key)) % self._size # ht = HashTable_LinearProbing(98) # print ht._hash1(55) # ht.insert(26, 'Jam') # ht.insert(26, 'Jammy') # ht.insert(26, 'POO') # ht.insert(27, 'Jelly') # ht.insert(28, 'Marmalade') # for index, item in enumerate(range(0, ht._size)): # print ht._table[index] # ht.remove(26) # print '============' # for item in ht._table: # if item: # print '{} {}'.format(item.value, item.key) # print '============' # ht.insert(26, 'PBJ') # for item in ht._table: # if item: # print '{} {}'.format(item.value, item.key) # print "======HELLO WORLD=======" # print ht._table[27].value # print ht._table[26].value