Fixed #11874 -- Optimizations and cleanups for SortedDict. Thanks to Alex Gaynor and Armin Ronacher for the report and patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11815 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
7cb8892fdb
commit
333615accb
|
@ -1,3 +1,6 @@
|
|||
from copy import deepcopy
|
||||
|
||||
|
||||
class MergeDict(object):
|
||||
"""
|
||||
A simple class for creating new "virtual" dictionaries that actually look
|
||||
|
@ -72,22 +75,20 @@ class SortedDict(dict):
|
|||
self.keyOrder.append(key)
|
||||
|
||||
def __deepcopy__(self, memo):
|
||||
from copy import deepcopy
|
||||
return self.__class__([(key, deepcopy(value, memo))
|
||||
for key, value in self.iteritems()])
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
super(SortedDict, self).__setitem__(key, value)
|
||||
if key not in self.keyOrder:
|
||||
if key not in self:
|
||||
self.keyOrder.append(key)
|
||||
super(SortedDict, self).__setitem__(key, value)
|
||||
|
||||
def __delitem__(self, key):
|
||||
super(SortedDict, self).__delitem__(key)
|
||||
self.keyOrder.remove(key)
|
||||
|
||||
def __iter__(self):
|
||||
for k in self.keyOrder:
|
||||
yield k
|
||||
return iter(self.keyOrder)
|
||||
|
||||
def pop(self, k, *args):
|
||||
result = super(SortedDict, self).pop(k, *args)
|
||||
|
@ -108,7 +109,7 @@ class SortedDict(dict):
|
|||
|
||||
def iteritems(self):
|
||||
for key in self.keyOrder:
|
||||
yield key, super(SortedDict, self).__getitem__(key)
|
||||
yield key, self[key]
|
||||
|
||||
def keys(self):
|
||||
return self.keyOrder[:]
|
||||
|
@ -117,18 +118,18 @@ class SortedDict(dict):
|
|||
return iter(self.keyOrder)
|
||||
|
||||
def values(self):
|
||||
return map(super(SortedDict, self).__getitem__, self.keyOrder)
|
||||
return map(self.__getitem__, self.keyOrder)
|
||||
|
||||
def itervalues(self):
|
||||
for key in self.keyOrder:
|
||||
yield super(SortedDict, self).__getitem__(key)
|
||||
yield self[key]
|
||||
|
||||
def update(self, dict_):
|
||||
for k, v in dict_.items():
|
||||
self.__setitem__(k, v)
|
||||
for k, v in dict_.iteritems():
|
||||
self[k] = v
|
||||
|
||||
def setdefault(self, key, default):
|
||||
if key not in self.keyOrder:
|
||||
if key not in self:
|
||||
self.keyOrder.append(key)
|
||||
return super(SortedDict, self).setdefault(key, default)
|
||||
|
||||
|
|
Loading…
Reference in New Issue