MCPcopy Create free account
hub / github.com/Jack-Lee-Hiter/AlgorithmsByPython / put

Method put

Hash.py:32–54  ·  view source on GitHub ↗
(self, key, data)

Source from the content-addressed store, hash-verified

30 self.data = [None] * self.size
31
32 def put(self, key, data):
33 hashvalue = self.hashfunction(key, self.size)
34
35 if self.slots[hashvalue] == None:
36 self.slots[hashvalue] = key
37 self.data[hashvalue] = data
38 else:
39 if self.slots[hashvalue] == key:
40 self.data[hashvalue] = data # 替换相同索引对应的项目
41 else:
42 if None not in self.slots and key not in self.slots:
43 # 判断hash是否已经满了, 必须添加key not in self.slots, 否则修改已有hash值会直接返回-1
44 print('sorry, there is not enough slots for you!')
45 return -1
46 nextslot = self.rehash(hashvalue, len(self.slots))
47 while self.slots[nextslot] != None and self.slots[nextslot] != key:
48 nextslot = self.rehash(nextslot, len(self.slots))
49
50 if self.slots[nextslot] == None:
51 self.slots[nextslot] = key
52 self.data[nextslot] = data
53 else:
54 self.data[nextslot] = data
55
56 def hashfunction(self, key, size):
57 return key%size

Callers 1

__setitem__Method · 0.95

Calls 2

hashfunctionMethod · 0.95
rehashMethod · 0.95

Tested by

no test coverage detected