Improve code clarity a bit.

This commit is contained in:
Raymond Hettinger 2010-03-09 11:29:10 +00:00
parent aba2293862
commit e30bc38ce9
1 changed files with 10 additions and 5 deletions

View File

@ -41,7 +41,8 @@ class OrderedDict(dict, MutableMapping):
self.__root self.__root
except AttributeError: except AttributeError:
self.__root = root = [None, None, None] # sentinel node self.__root = root = [None, None, None] # sentinel node
PREV, NEXT = 0, 1 PREV = 0
NEXT = 1
root[PREV] = root[NEXT] = root root[PREV] = root[NEXT] = root
self.__map = {} self.__map = {}
self.update(*args, **kwds) self.update(*args, **kwds)
@ -51,7 +52,8 @@ class OrderedDict(dict, MutableMapping):
# 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:
PREV, NEXT = 0, 1 PREV = 0
NEXT = 1
root = self.__root root = self.__root
last = root[PREV] last = root[PREV]
last[NEXT] = root[PREV] = self.__map[key] = [last, root, key] last[NEXT] = root[PREV] = self.__map[key] = [last, root, key]
@ -62,7 +64,8 @@ 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 PREV = 0
NEXT = 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]
@ -70,7 +73,8 @@ class OrderedDict(dict, MutableMapping):
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 NEXT = 1
KEY = 2
root = self.__root root = self.__root
curr = root[NEXT] curr = root[NEXT]
while curr is not root: while curr is not root:
@ -80,7 +84,8 @@ class OrderedDict(dict, MutableMapping):
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 PREV = 0
KEY = 2
root = self.__root root = self.__root
curr = root[PREV] curr = root[PREV]
while curr is not root: while curr is not root: