diff options
-rw-r--r-- | src/python/SConscript | 1 | ||||
-rw-r--r-- | src/python/m5/util/__init__.py | 1 | ||||
-rw-r--r-- | src/python/m5/util/orderdict.py | 59 |
3 files changed, 28 insertions, 33 deletions
diff --git a/src/python/SConscript b/src/python/SConscript index a767545ec..b01b422f5 100644 --- a/src/python/SConscript +++ b/src/python/SConscript @@ -56,6 +56,7 @@ PySource('m5.util', 'm5/util/attrdict.py') PySource('m5.util', 'm5/util/jobfile.py') PySource('m5.util', 'm5/util/misc.py') PySource('m5.util', 'm5/util/multidict.py') +PySource('m5.util', 'm5/util/orderdict.py') SwigSource('m5.internal', 'swig/core.i') SwigSource('m5.internal', 'swig/debug.i') diff --git a/src/python/m5/util/__init__.py b/src/python/m5/util/__init__.py index 5c4a066c6..48e8111bd 100644 --- a/src/python/m5/util/__init__.py +++ b/src/python/m5/util/__init__.py @@ -29,6 +29,7 @@ from attrdict import attrdict, optiondict from misc import * from multidict import multidict +from orderdict import orderdict import jobfile def print_list(items, indent=4): diff --git a/src/python/m5/util/orderdict.py b/src/python/m5/util/orderdict.py index 3f755d299..1ffbca87a 100644 --- a/src/python/m5/util/orderdict.py +++ b/src/python/m5/util/orderdict.py @@ -28,17 +28,20 @@ __all__ = [ 'orderdict' ] -class orderdict(dict): - def __init__(self, d = {}): - self._keys = d.keys() - super(orderdict, self).__init__(d) +from UserDict import DictMixin + +class orderdict(dict, DictMixin): + def __init__(self, *args, **kwargs): + if len(args) > 1: + raise TypeError("expected at most one argument, got %d" % \ + len(args)) + self._keys = [] + self.update(*args, **kwargs) def __setitem__(self, key, item): - super(orderdict, self).__setitem__(key, item) - if not hasattr(self, '_keys'): - self._keys = [key,] - if key not in self._keys: + if key not in self: self._keys.append(key) + super(orderdict, self).__setitem__(key, item) def __delitem__(self, key): super(orderdict, self).__delitem__(key) @@ -48,33 +51,23 @@ class orderdict(dict): super(orderdict, self).clear() self._keys = [] - def items(self): - for i in self._keys: - yield i, self[i] + def iterkeys(self): + for key in self._keys: + yield key - def keys(self): - return self._keys + def itervalues(self): + for key in self._keys: + yield self[key] - def popitem(self): - if len(self._keys) == 0: - raise KeyError('dictionary is empty') - else: - key = self._keys[-1] - val = self[key] - del self[key] - return key, val + def iteritems(self): + for key in self._keys: + yield key, self[key] - def setdefault(self, key, failobj = None): - super(orderdict, self).setdefault(key, failobj) - if key not in self._keys: - self._keys.append(key) - - def update(self, d): - for key in d.keys(): - if not self.has_key(key): - self._keys.append(key) - super(orderdict, self).update(d) + def keys(self): + return self._keys[:] def values(self): - for i in self._keys: - yield self[i] + return [ self[key] for key in self._keys ] + + def items(self): + return [ (self[key],key) for key in self._keys ] |