Have links in OrderedDicts be native Python lists instead

of a custom class with __slots__.  This simplifies the
code a bit, reduces memory consumption, improves speed,
and eliminates the need for weak reference proxies.
This commit is contained in:
Raymond Hettinger 2010-03-09 09:58:53 +00:00
parent 9bd3508530
commit aba2293862
1 changed files with 19 additions and 28 deletions

View File

@ -10,7 +10,6 @@ from operator import itemgetter as _itemgetter, eq as _eq
from keyword import iskeyword as _iskeyword from keyword import iskeyword as _iskeyword
import sys as _sys import sys as _sys
import heapq as _heapq import heapq as _heapq
from weakref import proxy as _proxy
from itertools import repeat as _repeat, chain as _chain, starmap as _starmap, \ from itertools import repeat as _repeat, chain as _chain, starmap as _starmap, \
ifilter as _ifilter, imap as _imap ifilter as _ifilter, imap as _imap
@ -18,9 +17,6 @@ from itertools import repeat as _repeat, chain as _chain, starmap as _starmap, \
### OrderedDict ### OrderedDict
################################################################################ ################################################################################
class _Link(object):
__slots__ = 'prev', 'next', 'key', '__weakref__'
class OrderedDict(dict, MutableMapping): class OrderedDict(dict, MutableMapping):
'Dictionary that remembers insertion order' 'Dictionary that remembers insertion order'
# An inherited dict maps keys to values. # An inherited dict maps keys to values.
@ -31,9 +27,7 @@ class OrderedDict(dict, MutableMapping):
# The internal self.__map dictionary maps keys to links in a doubly linked list. # The internal self.__map dictionary maps keys to links in a doubly linked list.
# The circular doubly linked list starts and ends with a sentinel element. # The circular doubly linked list starts and ends with a sentinel element.
# The sentinel element never gets deleted (this simplifies the algorithm). # The sentinel element never gets deleted (this simplifies the algorithm).
# The prev/next links are weakref proxies (to prevent circular references). # Each link is stored as a list of length three: [PREV, NEXT, KEY].
# Individual links are kept alive by the hard reference in self.__map.
# Those hard references disappear when a key is deleted from an OrderedDict.
def __init__(self, *args, **kwds): def __init__(self, *args, **kwds):
'''Initialize an ordered dictionary. Signature is the same as for '''Initialize an ordered dictionary. Signature is the same as for
@ -46,28 +40,21 @@ class OrderedDict(dict, MutableMapping):
try: try:
self.__root self.__root
except AttributeError: except AttributeError:
self.__root = root = _Link() # sentinel node for the doubly linked list self.__root = root = [None, None, None] # sentinel node
root.prev = root.next = root PREV, NEXT = 0, 1
root[PREV] = root[NEXT] = root
self.__map = {} self.__map = {}
self.update(*args, **kwds) self.update(*args, **kwds)
def clear(self):
'od.clear() -> None. Remove all items from od.'
root = self.__root
root.prev = root.next = root
self.__map.clear()
dict.clear(self)
def __setitem__(self, key, value): def __setitem__(self, key, value):
'od.__setitem__(i, y) <==> od[i]=y' 'od.__setitem__(i, y) <==> od[i]=y'
# Setting a new item creates a new link which goes at the end of the linked # Setting a new item creates a new link which goes at the end of the linked
# list, and the inherited dictionary is updated with the new key/value pair. # list, and the inherited dictionary is updated with the new key/value pair.
if key not in self: if key not in self:
self.__map[key] = link = _Link() PREV, NEXT = 0, 1
root = self.__root root = self.__root
last = root.prev last = root[PREV]
link.prev, link.next, link.key = last, root, key last[NEXT] = root[PREV] = self.__map[key] = [last, root, key]
last.next = root.prev = _proxy(link)
dict.__setitem__(self, key, value) dict.__setitem__(self, key, value)
def __delitem__(self, key): def __delitem__(self, key):
@ -75,27 +62,30 @@ class OrderedDict(dict, MutableMapping):
# Deleting an existing item uses self.__map to find the link which is # Deleting an existing item uses self.__map to find the link which is
# then removed by updating the links in the predecessor and successor nodes. # then removed by updating the links in the predecessor and successor nodes.
dict.__delitem__(self, key) dict.__delitem__(self, key)
PREV, NEXT = 0, 1
link = self.__map.pop(key) link = self.__map.pop(key)
link.prev.next = link.next link[PREV][NEXT] = link[NEXT]
link.next.prev = link.prev link[NEXT][PREV] = link[PREV]
def __iter__(self): def __iter__(self):
'od.__iter__() <==> iter(od)' 'od.__iter__() <==> iter(od)'
# Traverse the linked list in order. # Traverse the linked list in order.
NEXT, KEY = 1, 2
root = self.__root root = self.__root
curr = root.next curr = root[NEXT]
while curr is not root: while curr is not root:
yield curr.key yield curr[KEY]
curr = curr.next curr = curr[NEXT]
def __reversed__(self): def __reversed__(self):
'od.__reversed__() <==> reversed(od)' 'od.__reversed__() <==> reversed(od)'
# Traverse the linked list in reverse order. # Traverse the linked list in reverse order.
PREV, KEY = 0, 2
root = self.__root root = self.__root
curr = root.prev curr = root[PREV]
while curr is not root: while curr is not root:
yield curr.key yield curr[KEY]
curr = curr.prev curr = curr[PREV]
def __reduce__(self): def __reduce__(self):
'Return state information for pickling' 'Return state information for pickling'
@ -108,6 +98,7 @@ class OrderedDict(dict, MutableMapping):
return (self.__class__, (items,), inst_dict) return (self.__class__, (items,), inst_dict)
return self.__class__, (items,) return self.__class__, (items,)
clear = MutableMapping.clear
setdefault = MutableMapping.setdefault setdefault = MutableMapping.setdefault
update = MutableMapping.update update = MutableMapping.update
pop = MutableMapping.pop pop = MutableMapping.pop