Skip to content

Commit f636089

Browse files
committed
Merge branch 'master' of github.com:PegasusWang/python_data_structures_and_algorithms
2 parents 517461c + 3a2f4d8 commit f636089

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

docs/07_哈希表/hashtable.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,4 @@ class HashTable(object):
178178
- 《Data Structures and Algorithms in Python》11 章 Hash Tables
179179
- 《算法导论》第三版 11 章散列表,了解几种哈希冲突的解决方式,以及为什么我们选择二次探查而不是线性探查法?
180180
- 介绍 c 解释器如何实现的 python dict对象:[Python dictionary implementation](http://www.laurentluce.com/posts/python-dictionary-implementation/)
181+
- [Python hash function implement](https://stackoverflow.com/questions/2070276/where-can-i-find-source-or-algorithm-of-pythons-hash-function)

docs/07_哈希表/hashtable.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Slot(object):
3131
"""定义一个 hash 表数组的槽(slot 这里指的就是数组的一个位置)
3232
注意,一个槽有三种状态,看你能否想明白。相比链接法解决冲突,二次探查法删除一个 key 的操作稍微复杂。
3333
1.从未使用 HashMap.UNUSED。此槽没有被使用和冲突过,查找时只要找到 UNUSED 就不用再继续探查了
34-
2.使用过但是 remove 了,此时是 HashMap.EMPTY,该探查点后边的元素扔可能是有key
34+
2.使用过但是 remove 了,此时是 HashMap.EMPTY,该探查点后边的元素仍然可能是有key的,需要继续查找
3535
3.槽正在使用 Slot 节点
3636
"""
3737

@@ -87,12 +87,12 @@ def _find_slot_for_insert(self, key):
8787
def _slot_can_insert(self, index):
8888
return (self._table[index] is HashTable.EMPTY or self._table[index] is HashTable.UNUSED)
8989

90-
def __contains__(self, key): # in operator
90+
def __contains__(self, key): # in operator,实现之后可以使用 in 操作符判断
9191
index = self._find_key(key)
9292
return index is not None
9393

9494
def add(self, key, value):
95-
if key in self:
95+
if key in self: # update
9696
index = self._find_key(key)
9797
self._table[index].value = value
9898
return False

0 commit comments

Comments
 (0)