File tree Expand file tree Collapse file tree 2 files changed +4
-3
lines changed Expand file tree Collapse file tree 2 files changed +4
-3
lines changed Original file line number Diff line number Diff line change @@ -178,3 +178,4 @@ class HashTable(object):
178
178
- 《Data Structures and Algorithms in Python》11 章 Hash Tables
179
179
- 《算法导论》第三版 11 章散列表,了解几种哈希冲突的解决方式,以及为什么我们选择二次探查而不是线性探查法?
180
180
- 介绍 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 )
Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ class Slot(object):
31
31
"""定义一个 hash 表 数组的槽
32
32
注意,一个槽有三种状态,看你能否想明白。相比链接法解决冲突,二次探查法删除一个 key 的操作稍微复杂。
33
33
1.从未使用 HashMap.UNUSED。此槽没有被使用和冲突过,查找时只要找到 UNUSED 就不用再继续探查了
34
- 2.使用过但是 remove 了,此时是 HashMap.EMPTY,该探查点后边的元素扔可能是有key
34
+ 2.使用过但是 remove 了,此时是 HashMap.EMPTY,该探查点后边的元素仍然可能是有key的,需要继续查找
35
35
3.槽正在使用 Slot 节点
36
36
"""
37
37
@@ -82,12 +82,12 @@ def _find_slot_for_insert(self, key):
82
82
def _slot_can_insert (self , index ):
83
83
return (self ._table [index ] is HashTable .EMPTY or self ._table [index ] is HashTable .UNUSED )
84
84
85
- def __contains__ (self , key ): # in operator
85
+ def __contains__ (self , key ): # in operator,实现之后可以使用 in 操作符判断
86
86
index = self ._find_key (key )
87
87
return index is not None
88
88
89
89
def add (self , key , value ):
90
- if key in self :
90
+ if key in self : # update
91
91
index = self ._find_key (key )
92
92
self ._table [index ].value = value
93
93
return False
You can’t perform that action at this time.
0 commit comments