forgot to add p2pool/util/memoize.py in commit ed68fe40
authorForrest Voight <forrest.voight@gmail.com>
Tue, 14 Aug 2012 19:18:39 +0000 (15:18 -0400)
committerForrest Voight <forrest.voight@gmail.com>
Tue, 14 Aug 2012 19:18:39 +0000 (15:18 -0400)
p2pool/util/memoize.py [new file with mode: 0644]

diff --git a/p2pool/util/memoize.py b/p2pool/util/memoize.py
new file mode 100644 (file)
index 0000000..b5efab2
--- /dev/null
@@ -0,0 +1,21 @@
+_nothing = object()
+
+def memoize_with_backing(backing, has_inverses=set()):
+    def a(f):
+        def b(*args):
+            res = backing.get((f, args), _nothing)
+            if res is not _nothing:
+                return res
+            
+            res = f(*args)
+            
+            backing[(f, args)] = res
+            for inverse in has_inverses:
+                backing[(inverse, args[:-1] + (res,))] = args[-1]
+            
+            return res
+        return b
+    return a
+
+def memoize(f):
+    return memoize_with_backing({})(f)