Mercurial > hg-stable
changeset 32340:bd0fd3ff9916
util: rewrite sortdict using Python 2.7's OrderedDict
Pattern copied from
https://docs.python.org/dev/library/collections.html#ordereddict-examples-and-recipes.
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Tue, 16 May 2017 23:40:29 -0700 |
parents | 7040f5131454 |
children | 8a87bfc5bebb |
files | mercurial/util.py |
diffstat | 1 files changed, 5 insertions(+), 45 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/util.py Tue May 16 23:36:38 2017 +0900 +++ b/mercurial/util.py Tue May 16 23:40:29 2017 -0700 @@ -567,54 +567,14 @@ return f -class sortdict(dict): +class sortdict(collections.OrderedDict): '''a simple sorted dictionary''' - def __init__(self, data=None): - self._list = [] - if data: - self.update(data) + def __setitem__(self, key, value): + if key in self: + del self[key] + super(sortdict, self).__setitem__(key, value) def copy(self): return sortdict(self) - def __setitem__(self, key, val): - if key in self: - self._list.remove(key) - self._list.append(key) - dict.__setitem__(self, key, val) - def __iter__(self): - return self._list.__iter__() - def update(self, src): - if isinstance(src, dict): - src = src.iteritems() - for k, v in src: - self[k] = v - def clear(self): - dict.clear(self) - self._list = [] - def items(self): - return [(k, self[k]) for k in self._list] - def __delitem__(self, key): - dict.__delitem__(self, key) - self._list.remove(key) - def pop(self, key, *args, **kwargs): - try: - self._list.remove(key) - except ValueError: - pass - return dict.pop(self, key, *args, **kwargs) - def keys(self): - return self._list[:] - def iterkeys(self): - return self._list.__iter__() - def iteritems(self): - for k in self._list: - yield k, self[k] - def insert(self, index, key, val): - self._list.insert(index, key) - dict.__setitem__(self, key, val) - def __repr__(self): - if not self: - return '%s()' % self.__class__.__name__ - return '%s(%r)' % (self.__class__.__name__, self.items()) class _lrucachenode(object): """A node in a doubly linked list.