(self, key, data)
| 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 |
no test coverage detected