概述
<div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?postid=10350475">
<h1 id="-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #aaaaaa;">映射与字典
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">字典dict是Python中重要的数据结构,在字典中,每一个键都对应一个值,其中键与值的关系就叫做映射,也可以说是每一个键都映射到一个值上。
collections MutableMapping
<span class="hljs-class"><span class="hljs-keyword" style="font-weight: bold;">class <span class="hljs-title" style="font-weight: bold; color: #0048ab;">MyMap<span class="hljs-params">(MutableMapping):
<span class="hljs-class"><span class="hljs-keyword" style="font-weight: bold;">class</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">item</span><span class="hljs-params">()</span>:</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"><a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params">(self,key,value)</span>:</span>
self.key = key
self.value = value
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__eq__</span><span class="hljs-params">(self,other)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> self.key == other.key
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__ne__</span><span class="hljs-params">(self,other)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> self.key != other.key
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"><a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params">(self)</span>:</span>
self.table = []
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__getitem__</span><span class="hljs-params">(self,item)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> i <span class="hljs-keyword" style="font-weight: bold;">in</span> self.table:
<span class="hljs-keyword" style="font-weight: bold;">if</span> i.key == item:
<span class="hljs-keyword" style="font-weight: bold;">return</span> i.value
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span>+ repr(item))
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__setitem__</span><span class="hljs-params">(self,value)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> i <span class="hljs-keyword" style="font-weight: bold;">in</span> self.table:
<span class="hljs-keyword" style="font-weight: bold;">if</span> i.key == key:
i.value = value
<span class="hljs-keyword" style="font-weight: bold;">return</span>
self.table.append(self.item(key,value))
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__delitem__</span><span class="hljs-params">(self,key)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> n,i <span class="hljs-keyword" style="font-weight: bold;">in</span> enumerate(self.table):
<span class="hljs-keyword" style="font-weight: bold;">if</span> i.key == key:
self.pop(n)
<span class="hljs-keyword" style="font-weight: bold;">return</span>
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span>+ repr(key))
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__len__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> len(self.table)
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__iter__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> i <span class="hljs-keyword" style="font-weight: bold;">in</span> self.table:
<span class="hljs-keyword" style="font-weight: bold;">yield</span> i.key
<span class="hljs-class"><span class="hljs-keyword" style="font-weight: bold;">class</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">item</span><span class="hljs-params">()</span>:</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"><a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params">(self,key,value)</span>:</span>
self.key = key
self.value = value
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__eq__</span><span class="hljs-params">(self,other)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> self.key == other.key
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__ne__</span><span class="hljs-params">(self,other)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> self.key != other.key
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"><a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params">(self)</span>:</span>
self.table = []
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__getitem__</span><span class="hljs-params">(self,item)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> i <span class="hljs-keyword" style="font-weight: bold;">in</span> self.table:
<span class="hljs-keyword" style="font-weight: bold;">if</span> i.key == item:
<span class="hljs-keyword" style="font-weight: bold;">return</span> i.value
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span>+ repr(item))
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__setitem__</span><span class="hljs-params">(self,value)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> i <span class="hljs-keyword" style="font-weight: bold;">in</span> self.table:
<span class="hljs-keyword" style="font-weight: bold;">if</span> i.key == key:
i.value = value
<span class="hljs-keyword" style="font-weight: bold;">return</span>
self.table.append(self.item(key,value))
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__delitem__</span><span class="hljs-params">(self,key)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> n,i <span class="hljs-keyword" style="font-weight: bold;">in</span> enumerate(self.table):
<span class="hljs-keyword" style="font-weight: bold;">if</span> i.key == key:
self.pop(n)
<span class="hljs-keyword" style="font-weight: bold;">return</span>
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span>+ repr(key))
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__len__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> len(self.table)
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__iter__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> i <span class="hljs-keyword" style="font-weight: bold;">in</span> self.table:
<span class="hljs-keyword" style="font-weight: bold;">yield</span> i.key
<span class="hljs-class"><span class="hljs-keyword" style="font-weight: bold;">class <span class="hljs-title" style="font-weight: bold; color: #0048ab;">MyMap<span class="hljs-params">(MutableMapping):
<span class="hljs-class"><span class="hljs-keyword" style="font-weight: bold;">class <span class="hljs-title" style="font-weight: bold; color: #0048ab;">MyMap<span class="hljs-params">(MutableMapping):
<span class="hljs-class"><span class="hljs-keyword" style="font-weight: bold;">class</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">item</span><span class="hljs-params">()</span>:</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"><a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params">(self,key,value)</span>:</span>
self.key = key
self.value = value
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__eq__</span><span class="hljs-params">(self,other)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> self.key == other.key
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__ne__</span><span class="hljs-params">(self,other)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> self.key != other.key
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"><a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params">(self)</span>:</span>
self.table = []
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__getitem__</span><span class="hljs-params">(self,item)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> i <span class="hljs-keyword" style="font-weight: bold;">in</span> self.table:
<span class="hljs-keyword" style="font-weight: bold;">if</span> i.key == item:
<span class="hljs-keyword" style="font-weight: bold;">return</span> i.value
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span>+ repr(item))
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__setitem__</span><span class="hljs-params">(self,value)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> i <span class="hljs-keyword" style="font-weight: bold;">in</span> self.table:
<span class="hljs-keyword" style="font-weight: bold;">if</span> i.key == key:
i.value = value
<span class="hljs-keyword" style="font-weight: bold;">return</span>
self.table.append(self.item(key,value))
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__delitem__</span><span class="hljs-params">(self,key)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> n,i <span class="hljs-keyword" style="font-weight: bold;">in</span> enumerate(self.table):
<span class="hljs-keyword" style="font-weight: bold;">if</span> i.key == key:
self.pop(n)
<span class="hljs-keyword" style="font-weight: bold;">return</span>
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span>+ repr(key))
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__len__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> len(self.table)
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__iter__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> i <span class="hljs-keyword" style="font-weight: bold;">in</span> self.table:
<span class="hljs-keyword" style="font-weight: bold;">yield</span> i.key
:
mask = ( << ) -
h =
character s:
h = (h << & mask) | (h >> )
h += ord(character)
h
>是右移,&是按位与,|是按位或,ord()函数返回一个字符的ascii码或unicode值
函数,传入对象x,返回一个整型值作为它的哈希码
自定义的对象作为参数传入则会报错。
自定义的对象能够使用,可以在类中实现一个叫做的特殊方法,在该函数中调用hash函数,并传入该对象的一些不可变属性组合,将值再返回,例如:
:
<span class="hljs-keyword" style="font-weight: bold;">return hash((self.red,self.green,self.blue))
<span class="hljs-keyword" style="font-weight: bold;">return hash((self.red,self.green,self.blue))
<span class="hljs-keyword" style="font-weight: bold;">return hash((self.red,self.green,self.blue))
:
<span class="hljs-class"><span class="hljs-keyword" style="font-weight: bold;">class</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">item</span><span class="hljs-params">()</span>:</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"><a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params">(self,other)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> self.key != other.key
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"><a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params">(self,cap=<span class="hljs-number">11</span>,p=<span class="hljs-number">109345121</span>)</span>:</span>
self._table = ca<a href="https://www.jb51.cc/tag/P/" target="_blank" class="keywords">P*</a>[<span class="hljs-keyword" style="font-weight: bold;">None</span>]
self._n = <span class="hljs-number">0</span> <span class="hljs-comment" style="color: #738191;"># 元素<a href="https://www.jb51.cc/tag/shuliang/" target="_blank" class="keywords">数量</a></span>
self._prime = p <span class="hljs-comment" style="color: #738191;"># MAD中的参数</span>
self._scale = <span class="hljs-number">1</span> + random.randrange(p+<span class="hljs-number">1</span>) <span class="hljs-comment" style="color: #738191;"># MAD中的参数</span>
self._shift = random.randrange(p) <span class="hljs-comment" style="color: #738191;"># MAD中的参数</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_hash_func</span><span class="hljs-params">(self,key)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> (hash(key)*self._scale+self._shift)<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>elf._prime%len(self._table)
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__len__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> self._n
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__getitem__</span><span class="hljs-params">(self,k)</span>:</span>
j = self._hash_func(k)
<span class="hljs-keyword" style="font-weight: bold;">return</span> self._inner_getitem(j,k)
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__setitem__</span><span class="hljs-params">(self,value)</span>:</span>
j = self._hash_func(key)
self._inner_setitem(j,value)
<span class="hljs-keyword" style="font-weight: bold;">if</span> self._n>len(self._table)//<span class="hljs-number">2</span>: <span class="hljs-comment" style="color: #738191;">#调整大小,使<a href="https://www.jb51.cc/tag/fuzai/" target="_blank" class="keywords">负载</a>因子小于等于0.5</span>
self._resize(<span class="hljs-number">2</span>*len(self._table)-<span class="hljs-number">1</span>)
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__delitem__</span><span class="hljs-params">(self,key)</span>:</span>
j = self._hash_func(key)
self._inner_delitem(j,key)
self._n -= <span class="hljs-number">1</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_resize</span><span class="hljs-params">(self,cap)</span>:</span>
old = list(self.items())
self._table = ca<a href="https://www.jb51.cc/tag/P/" target="_blank" class="keywords">P*</a>[<span class="hljs-keyword" style="font-weight: bold;">None</span>]
self._n = <span class="hljs-number">0</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> (k,v) <span class="hljs-keyword" style="font-weight: bold;">in</span> old:
self[k] = v
<span class="hljs-class"><span class="hljs-keyword" style="font-weight: bold;">class</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">item</span><span class="hljs-params">()</span>:</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"><a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params">(self,other)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> self.key != other.key
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"><a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params">(self,cap=<span class="hljs-number">11</span>,p=<span class="hljs-number">109345121</span>)</span>:</span>
self._table = ca<a href="https://www.jb51.cc/tag/P/" target="_blank" class="keywords">P*</a>[<span class="hljs-keyword" style="font-weight: bold;">None</span>]
self._n = <span class="hljs-number">0</span> <span class="hljs-comment" style="color: #738191;"># 元素<a href="https://www.jb51.cc/tag/shuliang/" target="_blank" class="keywords">数量</a></span>
self._prime = p <span class="hljs-comment" style="color: #738191;"># MAD中的参数</span>
self._scale = <span class="hljs-number">1</span> + random.randrange(p+<span class="hljs-number">1</span>) <span class="hljs-comment" style="color: #738191;"># MAD中的参数</span>
self._shift = random.randrange(p) <span class="hljs-comment" style="color: #738191;"># MAD中的参数</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_hash_func</span><span class="hljs-params">(self,key)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> (hash(key)*self._scale+self._shift)<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>elf._prime%len(self._table)
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__len__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> self._n
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__getitem__</span><span class="hljs-params">(self,k)</span>:</span>
j = self._hash_func(k)
<span class="hljs-keyword" style="font-weight: bold;">return</span> self._inner_getitem(j,k)
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__setitem__</span><span class="hljs-params">(self,value)</span>:</span>
j = self._hash_func(key)
self._inner_setitem(j,value)
<span class="hljs-keyword" style="font-weight: bold;">if</span> self._n>len(self._table)//<span class="hljs-number">2</span>: <span class="hljs-comment" style="color: #738191;">#调整大小,使<a href="https://www.jb51.cc/tag/fuzai/" target="_blank" class="keywords">负载</a>因子小于等于0.5</span>
self._resize(<span class="hljs-number">2</span>*len(self._table)-<span class="hljs-number">1</span>)
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__delitem__</span><span class="hljs-params">(self,key)</span>:</span>
j = self._hash_func(key)
self._inner_delitem(j,key)
self._n -= <span class="hljs-number">1</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_resize</span><span class="hljs-params">(self,cap)</span>:</span>
old = list(self.items())
self._table = ca<a href="https://www.jb51.cc/tag/P/" target="_blank" class="keywords">P*</a>[<span class="hljs-keyword" style="font-weight: bold;">None</span>]
self._n = <span class="hljs-number">0</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> (k,v) <span class="hljs-keyword" style="font-weight: bold;">in</span> old:
self[k] = v
<span class="hljs-class"><span class="hljs-keyword" style="font-weight: bold;">class</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">item</span><span class="hljs-params">()</span>:</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"><a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params">(self,other)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> self.key != other.key
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;"><a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a></span><span class="hljs-params">(self,cap=<span class="hljs-number">11</span>,p=<span class="hljs-number">109345121</span>)</span>:</span>
self._table = ca<a href="https://www.jb51.cc/tag/P/" target="_blank" class="keywords">P*</a>[<span class="hljs-keyword" style="font-weight: bold;">None</span>]
self._n = <span class="hljs-number">0</span> <span class="hljs-comment" style="color: #738191;"># 元素<a href="https://www.jb51.cc/tag/shuliang/" target="_blank" class="keywords">数量</a></span>
self._prime = p <span class="hljs-comment" style="color: #738191;"># MAD中的参数</span>
self._scale = <span class="hljs-number">1</span> + random.randrange(p+<span class="hljs-number">1</span>) <span class="hljs-comment" style="color: #738191;"># MAD中的参数</span>
self._shift = random.randrange(p) <span class="hljs-comment" style="color: #738191;"># MAD中的参数</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_hash_func</span><span class="hljs-params">(self,key)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> (hash(key)*self._scale+self._shift)<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>elf._prime%len(self._table)
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__len__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> self._n
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__getitem__</span><span class="hljs-params">(self,k)</span>:</span>
j = self._hash_func(k)
<span class="hljs-keyword" style="font-weight: bold;">return</span> self._inner_getitem(j,k)
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__setitem__</span><span class="hljs-params">(self,value)</span>:</span>
j = self._hash_func(key)
self._inner_setitem(j,value)
<span class="hljs-keyword" style="font-weight: bold;">if</span> self._n>len(self._table)//<span class="hljs-number">2</span>: <span class="hljs-comment" style="color: #738191;">#调整大小,使<a href="https://www.jb51.cc/tag/fuzai/" target="_blank" class="keywords">负载</a>因子小于等于0.5</span>
self._resize(<span class="hljs-number">2</span>*len(self._table)-<span class="hljs-number">1</span>)
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__delitem__</span><span class="hljs-params">(self,key)</span>:</span>
j = self._hash_func(key)
self._inner_delitem(j,key)
self._n -= <span class="hljs-number">1</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_resize</span><span class="hljs-params">(self,cap)</span>:</span>
old = list(self.items())
self._table = ca<a href="https://www.jb51.cc/tag/P/" target="_blank" class="keywords">P*</a>[<span class="hljs-keyword" style="font-weight: bold;">None</span>]
self._n = <span class="hljs-number">0</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> (k,v) <span class="hljs-keyword" style="font-weight: bold;">in</span> old:
self[k] = v
:
解决冲突的方式实现的哈希表"""
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_getitem</span><span class="hljs-params">(self,j,k)</span>:</span>
bucket = self._table[j] <span class="hljs-comment" style="color: #738191;">#把二级容器叫做桶</span>
<span class="hljs-keyword" style="font-weight: bold;">if</span> bucket <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>:
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span>+ repr(k))
<span class="hljs-keyword" style="font-weight: bold;">return</span> bucket[k]
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_setitem</span><span class="hljs-params">(self,k,v)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">if</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>:
self._table[j] = MyMap()
oldsize = len(self._table[j])
self._table[j][k] = v
<span class="hljs-keyword" style="font-weight: bold;">if</span> len(self._table[j])>oldsize:
self._n += <span class="hljs-number">1</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_delitem</span><span class="hljs-params">(self,k)</span>:</span>
bucket = self._table[j]
<span class="hljs-keyword" style="font-weight: bold;">if</span> bucket <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>:
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span> + repr(k))
<span class="hljs-keyword" style="font-weight: bold;">del</span> bucket[k]
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__iter__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> bucket <span class="hljs-keyword" style="font-weight: bold;">in</span> self._table:
<span class="hljs-keyword" style="font-weight: bold;">if</span> bucket <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">not</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>:
<span class="hljs-keyword" style="font-weight: bold;">for</span> key <span class="hljs-keyword" style="font-weight: bold;">in</span> bucket:
<span class="hljs-keyword" style="font-weight: bold;">yield</span> key
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_getitem</span><span class="hljs-params">(self,j,k)</span>:</span>
bucket = self._table[j] <span class="hljs-comment" style="color: #738191;">#把二级容器叫做桶</span>
<span class="hljs-keyword" style="font-weight: bold;">if</span> bucket <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>:
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span>+ repr(k))
<span class="hljs-keyword" style="font-weight: bold;">return</span> bucket[k]
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_setitem</span><span class="hljs-params">(self,k,v)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">if</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>:
self._table[j] = MyMap()
oldsize = len(self._table[j])
self._table[j][k] = v
<span class="hljs-keyword" style="font-weight: bold;">if</span> len(self._table[j])>oldsize:
self._n += <span class="hljs-number">1</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_delitem</span><span class="hljs-params">(self,k)</span>:</span>
bucket = self._table[j]
<span class="hljs-keyword" style="font-weight: bold;">if</span> bucket <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>:
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span> + repr(k))
<span class="hljs-keyword" style="font-weight: bold;">del</span> bucket[k]
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__iter__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> bucket <span class="hljs-keyword" style="font-weight: bold;">in</span> self._table:
<span class="hljs-keyword" style="font-weight: bold;">if</span> bucket <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">not</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>:
<span class="hljs-keyword" style="font-weight: bold;">for</span> key <span class="hljs-keyword" style="font-weight: bold;">in</span> bucket:
<span class="hljs-keyword" style="font-weight: bold;">yield</span> key
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_getitem</span><span class="hljs-params">(self,j,k)</span>:</span>
bucket = self._table[j] <span class="hljs-comment" style="color: #738191;">#把二级容器叫做桶</span>
<span class="hljs-keyword" style="font-weight: bold;">if</span> bucket <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>:
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span>+ repr(k))
<span class="hljs-keyword" style="font-weight: bold;">return</span> bucket[k]
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_setitem</span><span class="hljs-params">(self,k,v)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">if</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>:
self._table[j] = MyMap()
oldsize = len(self._table[j])
self._table[j][k] = v
<span class="hljs-keyword" style="font-weight: bold;">if</span> len(self._table[j])>oldsize:
self._n += <span class="hljs-number">1</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_delitem</span><span class="hljs-params">(self,k)</span>:</span>
bucket = self._table[j]
<span class="hljs-keyword" style="font-weight: bold;">if</span> bucket <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>:
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span> + repr(k))
<span class="hljs-keyword" style="font-weight: bold;">del</span> bucket[k]
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__iter__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> bucket <span class="hljs-keyword" style="font-weight: bold;">in</span> self._table:
<span class="hljs-keyword" style="font-weight: bold;">if</span> bucket <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">not</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>:
<span class="hljs-keyword" style="font-weight: bold;">for</span> key <span class="hljs-keyword" style="font-weight: bold;">in</span> bucket:
<span class="hljs-keyword" style="font-weight: bold;">yield</span> key
:
解决冲突实现的哈希表"""
_AVAIL = object() 标记删除位置
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_is_available</span><span class="hljs-params">(self,j)</span>:</span>
<span class="hljs-string" style="color: #0048ab;">"""判断该位置是否可用"""</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span> <span class="hljs-keyword" style="font-weight: bold;">or</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;">is</span> HashMapTwo._AVAIL
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_find_slot</span><span class="hljs-params">(self,k)</span>:</span>
<span class="hljs-string" style="color: #0048ab;">"""寻找键k所在的索引
如果找到了,返回(True,索引)
如果没找到,返回(False,第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的索引位置)"""</span>
firstAvail = <span class="hljs-keyword" style="font-weight: bold;">None</span>
<span class="hljs-keyword" style="font-weight: bold;">while</span> <span class="hljs-keyword" style="font-weight: bold;">True</span>:
<span class="hljs-keyword" style="font-weight: bold;">if</span> self._is_available(j):
<span class="hljs-keyword" style="font-weight: bold;">if</span> firstAvail <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>: <span class="hljs-comment" style="color: #738191;"># _AVAIL<a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a>可以是第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的位置</span>
firstAvail = j
<span class="hljs-keyword" style="font-weight: bold;">if</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>: <span class="hljs-comment" style="color: #738191;"># 跳过_AVAIL<a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a></span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> (<span class="hljs-keyword" style="font-weight: bold;">False</span>,firstAvail)
<span class="hljs-keyword" style="font-weight: bold;">elif</span> k == self._table[j].key:
<span class="hljs-keyword" style="font-weight: bold;">return</span> (<span class="hljs-keyword" style="font-weight: bold;">True</span>,j)
j = (j + <span class="hljs-number">1</span>) % len(self._table) <span class="hljs-comment" style="color: #738191;"># 向下<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>查找</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_getitem</span><span class="hljs-params">(self,k)</span>:</span>
found,s = self._find_slot(j,k)
<span class="hljs-keyword" style="font-weight: bold;">if</span> <span class="hljs-keyword" style="font-weight: bold;">not</span> found:
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span> + repr(k))
<span class="hljs-keyword" style="font-weight: bold;">return</span> self._table[s].value
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_setitem</span><span class="hljs-params">(self,v)</span>:</span>
found,k)
<span class="hljs-keyword" style="font-weight: bold;">if</span> <span class="hljs-keyword" style="font-weight: bold;">not</span> found: <span class="hljs-comment" style="color: #738191;"># 使用第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的位置</span>
self._table[s] = self.Item(k,v)
self._n += <span class="hljs-number">1</span>
<span class="hljs-keyword" style="font-weight: bold;">else</span>:
self._table[s].value = v
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_delitem</span><span class="hljs-params">(self,k)
<span class="hljs-keyword" style="font-weight: bold;">if</span> <span class="hljs-keyword" style="font-weight: bold;">not</span> found:
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span> + repr(k))
self._table[s] = HashMapTwo._AVAIL <span class="hljs-comment" style="color: #738191;"># <a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a><a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a></span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__iter__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> j <span class="hljs-keyword" style="font-weight: bold;">in</span> range(len(self._table)):
<span class="hljs-keyword" style="font-weight: bold;">if</span> <span class="hljs-keyword" style="font-weight: bold;">not</span> self._is_available(j):
<span class="hljs-keyword" style="font-weight: bold;">yield</span> self._table[j].key
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_is_available</span><span class="hljs-params">(self,j)</span>:</span>
<span class="hljs-string" style="color: #0048ab;">"""判断该位置是否可用"""</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span> <span class="hljs-keyword" style="font-weight: bold;">or</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;">is</span> HashMapTwo._AVAIL
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_find_slot</span><span class="hljs-params">(self,k)</span>:</span>
<span class="hljs-string" style="color: #0048ab;">"""寻找键k所在的索引
如果找到了,返回(True,索引)
如果没找到,返回(False,第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的索引位置)"""</span>
firstAvail = <span class="hljs-keyword" style="font-weight: bold;">None</span>
<span class="hljs-keyword" style="font-weight: bold;">while</span> <span class="hljs-keyword" style="font-weight: bold;">True</span>:
<span class="hljs-keyword" style="font-weight: bold;">if</span> self._is_available(j):
<span class="hljs-keyword" style="font-weight: bold;">if</span> firstAvail <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>: <span class="hljs-comment" style="color: #738191;"># _AVAIL<a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a>可以是第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的位置</span>
firstAvail = j
<span class="hljs-keyword" style="font-weight: bold;">if</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>: <span class="hljs-comment" style="color: #738191;"># 跳过_AVAIL<a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a></span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> (<span class="hljs-keyword" style="font-weight: bold;">False</span>,firstAvail)
<span class="hljs-keyword" style="font-weight: bold;">elif</span> k == self._table[j].key:
<span class="hljs-keyword" style="font-weight: bold;">return</span> (<span class="hljs-keyword" style="font-weight: bold;">True</span>,j)
j = (j + <span class="hljs-number">1</span>) % len(self._table) <span class="hljs-comment" style="color: #738191;"># 向下<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>查找</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_getitem</span><span class="hljs-params">(self,k)</span>:</span>
found,s = self._find_slot(j,k)
<span class="hljs-keyword" style="font-weight: bold;">if</span> <span class="hljs-keyword" style="font-weight: bold;">not</span> found:
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span> + repr(k))
<span class="hljs-keyword" style="font-weight: bold;">return</span> self._table[s].value
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_setitem</span><span class="hljs-params">(self,v)</span>:</span>
found,k)
<span class="hljs-keyword" style="font-weight: bold;">if</span> <span class="hljs-keyword" style="font-weight: bold;">not</span> found: <span class="hljs-comment" style="color: #738191;"># 使用第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的位置</span>
self._table[s] = self.Item(k,v)
self._n += <span class="hljs-number">1</span>
<span class="hljs-keyword" style="font-weight: bold;">else</span>:
self._table[s].value = v
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_delitem</span><span class="hljs-params">(self,k)
<span class="hljs-keyword" style="font-weight: bold;">if</span> <span class="hljs-keyword" style="font-weight: bold;">not</span> found:
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span> + repr(k))
self._table[s] = HashMapTwo._AVAIL <span class="hljs-comment" style="color: #738191;"># <a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a><a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a></span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__iter__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> j <span class="hljs-keyword" style="font-weight: bold;">in</span> range(len(self._table)):
<span class="hljs-keyword" style="font-weight: bold;">if</span> <span class="hljs-keyword" style="font-weight: bold;">not</span> self._is_available(j):
<span class="hljs-keyword" style="font-weight: bold;">yield</span> self._table[j].key
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_is_available</span><span class="hljs-params">(self,j)</span>:</span>
<span class="hljs-string" style="color: #0048ab;">"""判断该位置是否可用"""</span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span> <span class="hljs-keyword" style="font-weight: bold;">or</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;">is</span> HashMapTwo._AVAIL
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_find_slot</span><span class="hljs-params">(self,k)</span>:</span>
<span class="hljs-string" style="color: #0048ab;">"""寻找键k所在的索引
如果找到了,返回(True,索引)
如果没找到,返回(False,第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的索引位置)"""</span>
firstAvail = <span class="hljs-keyword" style="font-weight: bold;">None</span>
<span class="hljs-keyword" style="font-weight: bold;">while</span> <span class="hljs-keyword" style="font-weight: bold;">True</span>:
<span class="hljs-keyword" style="font-weight: bold;">if</span> self._is_available(j):
<span class="hljs-keyword" style="font-weight: bold;">if</span> firstAvail <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>: <span class="hljs-comment" style="color: #738191;"># _AVAIL<a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a>可以是第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的位置</span>
firstAvail = j
<span class="hljs-keyword" style="font-weight: bold;">if</span> self._table[j] <span class="hljs-keyword" style="font-weight: bold;">is</span> <span class="hljs-keyword" style="font-weight: bold;">None</span>: <span class="hljs-comment" style="color: #738191;"># 跳过_AVAIL<a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a></span>
<span class="hljs-keyword" style="font-weight: bold;">return</span> (<span class="hljs-keyword" style="font-weight: bold;">False</span>,firstAvail)
<span class="hljs-keyword" style="font-weight: bold;">elif</span> k == self._table[j].key:
<span class="hljs-keyword" style="font-weight: bold;">return</span> (<span class="hljs-keyword" style="font-weight: bold;">True</span>,j)
j = (j + <span class="hljs-number">1</span>) % len(self._table) <span class="hljs-comment" style="color: #738191;"># 向下<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>查找</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_getitem</span><span class="hljs-params">(self,k)</span>:</span>
found,s = self._find_slot(j,k)
<span class="hljs-keyword" style="font-weight: bold;">if</span> <span class="hljs-keyword" style="font-weight: bold;">not</span> found:
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span> + repr(k))
<span class="hljs-keyword" style="font-weight: bold;">return</span> self._table[s].value
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_setitem</span><span class="hljs-params">(self,v)</span>:</span>
found,k)
<span class="hljs-keyword" style="font-weight: bold;">if</span> <span class="hljs-keyword" style="font-weight: bold;">not</span> found: <span class="hljs-comment" style="color: #738191;"># 使用第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>可提供的位置</span>
self._table[s] = self.Item(k,v)
self._n += <span class="hljs-number">1</span>
<span class="hljs-keyword" style="font-weight: bold;">else</span>:
self._table[s].value = v
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">_inner_delitem</span><span class="hljs-params">(self,k)
<span class="hljs-keyword" style="font-weight: bold;">if</span> <span class="hljs-keyword" style="font-weight: bold;">not</span> found:
<span class="hljs-keyword" style="font-weight: bold;">raise</span> KeyError(<span class="hljs-string" style="color: #0048ab;">'Key Error: '</span> + repr(k))
self._table[s] = HashMapTwo._AVAIL <span class="hljs-comment" style="color: #738191;"># <a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a><a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a></span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">def</span> <span class="hljs-title" style="font-weight: bold; color: #0048ab;">__iter__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword" style="font-weight: bold;">for</span> j <span class="hljs-keyword" style="font-weight: bold;">in</span> range(len(self._table)):
<span class="hljs-keyword" style="font-weight: bold;">if</span> <span class="hljs-keyword" style="font-weight: bold;">not</span> self._is_available(j):
<span class="hljs-keyword" style="font-weight: bold;">yield</span> self._table[j].key
如果您也喜欢它,动动您的小指点个赞吧