Skip to content

Commit 3a2f4d8

Browse files
committed
hashtable 代码注释
1 parent dfffaf4 commit 3a2f4d8

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 表 数组的槽
3232
注意,一个槽有三种状态,看你能否想明白。相比链接法解决冲突,二次探查法删除一个 key 的操作稍微复杂。
3333
1.从未使用 HashMap.UNUSED。此槽没有被使用和冲突过,查找时只要找到 UNUSED 就不用再继续探查了
34-
2.使用过但是 remove 了,此时是 HashMap.EMPTY,该探查点后边的元素扔可能是有key
34+
2.使用过但是 remove 了,此时是 HashMap.EMPTY,该探查点后边的元素仍然可能是有key的,需要继续查找
3535
3.槽正在使用 Slot 节点
3636
"""
3737

@@ -82,12 +82,12 @@ def _find_slot_for_insert(self, key):
8282
def _slot_can_insert(self, index):
8383
return (self._table[index] is HashTable.EMPTY or self._table[index] is HashTable.UNUSED)
8484

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

8989
def add(self, key, value):
90-
if key in self:
90+
if key in self: # update
9191
index = self._find_key(key)
9292
self._table[index].value = value
9393
return False

0 commit comments

Comments
 (0)