哈希表:也叫散列表。
给每个元素一种逻辑下标,直接可以找到。
哈希函数计算逻辑下表
h(key) = key%x
哈希冲突
链接法:缺点哈希函数选的不好,冲突太多链会变长。
开放寻址法。当一个槽被占用,采用一种方式寻找下一个可用的槽:
1.线性探查:当一个槽被占用,找下一个可用的槽
2.二次探查,当一个槽被占用,以二次方作为偏移量。
3.双重散列:重新计算hash结果。
哈希函数
装载因子load factor,一直插入数据,根据装载因子开辟新的空间进行扩容。
重哈希,rehashing。
class Array(object):
def __init__(self, size=32, init=None):
self._size = size
self._items = [None] * size
def __getitem__(self, index):
return self._items[index]
def __setitem__(self, index, value):
self._items[index] = value
def __len__(self):
return self._size
def clear(self, value=None):
for i in range(len(self._items)):
self._items[i] = value
def __iter__(self):
for item in self._items:
yield item
class Slot(object):
def __init__(self, key, value):
self.key, self.value = key, value
class HashTable(object):
UNUSED = None # 表示slot 没有被使用过
EMPTY = Slot(None, None)
def __init__(self):
self._table = Array(8, init=HashTable.UNUSED)
self.length = 0
@property
def _load_factor(self): # 负载因子
return self.length / float(len(self._table))
def __len__(self):
return self.length
def _hash(self, key):
return abs(hash(key)) % len(self._table) # abs()返回绝对值
def _find_key(self, key):
index = self._hash(key)
_len = len(self._table)
while self._table[index] is not HashTable.UNUSED:
if self._table[index] is HashTable.EMPTY:
index = (index * 5 + 1) % _len # cpython解决hash冲突的的一种方式。
continue
elif self._table[index].key == key:
return index
else:
index = (index * 5 + 1) % _len
return None
def _slot_can_insert(self, index):
return (self._table[index] is HashTable.EMPTY or self._table[index] is HashTable.UNUSED)
def _find_slot_for_insert(self, key):
index = self._hash(key)
_len = len(self._table)
while not self._slot_can_insert(index):
index = (index * 5 + 1) % _len
return index
def __contains__(self, key):
index = self._find_key(key)
return index is not None
def add(self, key, value): # 增加
if key in self:
index = self._find_key(key)
self._table[index].value = value
return False
else:
index = self._find_slot_for_insert(key)
self._table[index] = Slot(key, value)
self.length += 1
if self._load_factor >= 0.8:
self._rehash()
return True
def _rehash(self):
old_table = self._table
newsize = len(self._table) * 2 # 扩容是乘以2
self._table = Array(newsize, HashTable.UNUSED)
self.length = 0
for slot in old_table:
if slot is not HashTable.UNUSED and slot is not HashTable.EMPTY:
index = self._find_slot_for_insert(slot.key)
self._table[index] = slot
self.length += 1
def get(self, key, default=None):
index = self._find_key(key)
if index is None:
return default
else:
return self._table[index].value
def remove(self, key):
index = self._find_key(key)
if index is None:
raise KeyError()
value = self._table[index].value
self.length -= 1
self._table[index] = HashTable.EMPTY
return value
def __iter__(self):
for slot in self._table:
if slot not in (HashTable.EMPTY, HashTable.UNUSED):
yield slot.key
def test_hash_table():
h = HashTable()
h.add('a', 0)
h.add('b', 1)
h.add('c', 2)
assert len(h) == 3
assert h.get("a") == 0
assert h.get("b") == 1
assert h.get("asdf") == None
h.remove('a')
assert h.get('a') is None
assert sorted(list(h)) == ["b", "c"]
n = 50
for i in range(n):
h.add(i, i)
for i in range(n):
assert h.get(i) == i