orderdict: Use DictMixin and add orderdict to m5.util
authorNathan Binkert <nate@binkert.org>
Sun, 16 Aug 2009 20:40:00 +0000 (13:40 -0700)
committerNathan Binkert <nate@binkert.org>
Sun, 16 Aug 2009 20:40:00 +0000 (13:40 -0700)
src/python/SConscript
src/python/m5/util/__init__.py
src/python/m5/util/orderdict.py

index a767545ec9b84a8fe593ca542a650acc9e88e134..b01b422f5c2f2a4017809155b5c9a893f715a30d 100644 (file)
@@ -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')
index 5c4a066c6824272df769496b736eb9b1efaf5217..48e8111bd2ef214d43e07199155fc32cf521f60e 100644 (file)
@@ -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):
index 3f755d29924eb3a1426b093c0946dcf13578c1f4..1ffbca87a9448259bd4d70a24c7b6b08681413bb 100644 (file)
 
 __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 ]