2017-03-04 09:53:41 +07:00
|
|
|
class Item(object):
|
|
|
|
|
|
|
|
def __init__(self, key, value):
|
|
|
|
self.key = key
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
|
|
|
|
class HashTable(object):
|
|
|
|
|
|
|
|
def __init__(self, size):
|
|
|
|
self.size = size
|
|
|
|
self.table = [[] for _ in range(self.size)]
|
|
|
|
|
|
|
|
def _hash_function(self, key):
|
|
|
|
return key % self.size
|
|
|
|
|
|
|
|
def set(self, key, value):
|
|
|
|
hash_index = self._hash_function(key)
|
|
|
|
for item in self.table[hash_index]:
|
|
|
|
if item.key == key:
|
|
|
|
item.value = value
|
|
|
|
return
|
|
|
|
self.table[hash_index].append(Item(key, value))
|
|
|
|
|
|
|
|
def get(self, key):
|
|
|
|
hash_index = self._hash_function(key)
|
|
|
|
for item in self.table[hash_index]:
|
|
|
|
if item.key == key:
|
|
|
|
return item.value
|
|
|
|
raise KeyError('Key not found')
|
|
|
|
|
|
|
|
def remove(self, key):
|
|
|
|
hash_index = self._hash_function(key)
|
|
|
|
for index, item in enumerate(self.table[hash_index]):
|
|
|
|
if item.key == key:
|
|
|
|
del self.table[hash_index][index]
|
|
|
|
return
|
2018-07-19 13:09:09 +07:00
|
|
|
raise KeyError('Key not found')
|