Make OrderedDict.popitem() a bit smarter and faster
This commit is contained in:
parent
19e5a6fb4a
commit
331722d411
|
@ -108,6 +108,29 @@ class OrderedDict(dict, MutableMapping):
|
||||||
pass
|
pass
|
||||||
dict.clear(self)
|
dict.clear(self)
|
||||||
|
|
||||||
|
def popitem(self, last=True, PREV=0, NEXT=1, KEY=2, dict_pop=dict.pop):
|
||||||
|
'''od.popitem() -> (k, v), return and remove a (key, value) pair.
|
||||||
|
Pairs are returned in LIFO order if last is true or FIFO order if false.
|
||||||
|
|
||||||
|
'''
|
||||||
|
if not self:
|
||||||
|
raise KeyError('dictionary is empty')
|
||||||
|
root = self.__root
|
||||||
|
if last: # link_prev <--> link <--> root
|
||||||
|
link = root[PREV]
|
||||||
|
link_prev = link[PREV]
|
||||||
|
link_prev[NEXT] = root
|
||||||
|
root[PREV] = link_prev
|
||||||
|
else: # root <--> link <--> link_next
|
||||||
|
link = root[NEXT]
|
||||||
|
link_next = link[NEXT]
|
||||||
|
root[NEXT] = link_next
|
||||||
|
link_next[PREV] = root
|
||||||
|
key = link[KEY]
|
||||||
|
del self.__map[key]
|
||||||
|
value = dict_pop(self, key)
|
||||||
|
return key, value
|
||||||
|
|
||||||
setdefault = MutableMapping.setdefault
|
setdefault = MutableMapping.setdefault
|
||||||
update = MutableMapping.update
|
update = MutableMapping.update
|
||||||
pop = MutableMapping.pop
|
pop = MutableMapping.pop
|
||||||
|
@ -116,17 +139,6 @@ class OrderedDict(dict, MutableMapping):
|
||||||
items = MutableMapping.items
|
items = MutableMapping.items
|
||||||
__ne__ = MutableMapping.__ne__
|
__ne__ = MutableMapping.__ne__
|
||||||
|
|
||||||
def popitem(self, last=True):
|
|
||||||
'''od.popitem() -> (k, v), return and remove a (key, value) pair.
|
|
||||||
Pairs are returned in LIFO order if last is true or FIFO order if false.
|
|
||||||
|
|
||||||
'''
|
|
||||||
if not self:
|
|
||||||
raise KeyError('dictionary is empty')
|
|
||||||
key = next(reversed(self) if last else iter(self))
|
|
||||||
value = self.pop(key)
|
|
||||||
return key, value
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
'od.__repr__() <==> repr(od)'
|
'od.__repr__() <==> repr(od)'
|
||||||
if not self:
|
if not self:
|
||||||
|
|
Loading…
Reference in New Issue