fenfire-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ff-cvs] fenfire/org/fenfire swamp/CachedNodeFunction.ja...


From: Tuomas J. Lukka
Subject: [ff-cvs] fenfire/org/fenfire swamp/CachedNodeFunction.ja...
Date: Sun, 17 Aug 2003 10:34:20 -0400

CVSROOT:        /cvsroot/fenfire
Module name:    fenfire
Branch:         
Changes by:     Tuomas J. Lukka <address@hidden>        03/08/17 10:34:20

Modified files:
        org/fenfire/swamp: CachedNodeFunction.java 
        org/fenfire/view/buoy: TransclusionConnector.java 
Added files:
        org/fenfire/swamp: CachedPureNodeFunction.java 
Removed files:
        org/fenfire/swamp: FlushableCachedNodeFunction.java 

Log message:
        Impure cache, move first function to pure caste

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/fenfire/fenfire/org/fenfire/swamp/CachedPureNodeFunction.java?rev=1.1
http://savannah.gnu.org/cgi-bin/viewcvs/fenfire/fenfire/org/fenfire/swamp/CachedNodeFunction.java.diff?tr1=1.8&tr2=1.9&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/fenfire/fenfire/org/fenfire/view/buoy/TransclusionConnector.java.diff?tr1=1.22&tr2=1.23&r1=text&r2=text

Patches:
Index: fenfire/org/fenfire/swamp/CachedNodeFunction.java
diff -u fenfire/org/fenfire/swamp/CachedNodeFunction.java:1.8 
fenfire/org/fenfire/swamp/CachedNodeFunction.java:1.9
--- fenfire/org/fenfire/swamp/CachedNodeFunction.java:1.8       Wed Jun 11 
13:00:24 2003
+++ fenfire/org/fenfire/swamp/CachedNodeFunction.java   Sun Aug 17 10:34:20 2003
@@ -31,16 +31,13 @@
 import org.fenfire.util.*;
 
 /** A function which caches its values.
- * In order to work right, the operations
- * performed in the original function
- * are very restricted.
- * First of all, no side effects are allowed.
- * Second, no function whose return value depends
- * on anything except the passed parameters
- * (including the Graph) may be called.
+ * This is a less restricted class than CachedPureNodeFunction,
+ * allowing impure node functions and flushing.
  * <p>
- * As an aside, this class works completely also
- * without a graph, for caching constant functions.
+ * This class is, of course, far more dangerous and may introduce
+ * subtle caching bugs - using it is slightly deprecated,
+ * PureNodeFunctions 
+ * and CachedPureNodeFunction should be used whenever possible.
  */
 public class CachedNodeFunction implements NodeFunction {
 
@@ -70,11 +67,24 @@
        }
     }
 
-    public CachedNodeFunction(int n, ConstGraph g, NodeFunction f) {
+
+    /** Constructor for default set.
+     */
+    public CachedNodeFunction(int n, ConstGraph graph, NodeFunction f) {
+       this(n,graph,f, DEFAULT);
+    }
+
+    /** Constructor for different sets.
+     * @param set The set to be flushed when flushing.
+     */
+    public CachedNodeFunction(int n, ConstGraph graph, NodeFunction f, Object 
set) {
        cache = new CachingMap(n);
-       this.ourGraph = g;
+       this.ourGraph = graph;
        this.f = f;
+       this.n = n;
+        this.add(set, this);
     }
+        
 
     public Object f(ConstGraph g, Object node) {
        Obs o = null;
@@ -101,4 +111,52 @@
            cac.addObs(o);
        return cac.value;
     }
+
+    static private HashMap funcsMap = new HashMap();
+    static private void add(Object set, Object func) {
+       if (funcsMap.get(set) == null)
+           funcsMap.put(set, new ArrayList());
+       List l = (List)funcsMap.get(set);
+       l.add(func);
+    }
+
+    /** Flush all different sets.
+     */
+    static public void flushAll() {
+       for (Iterator it = funcsMap.keySet().iterator(); it.hasNext(); ) {
+           Object set = it.next();
+           flush(set);
+       }
+    }
+
+    /** Flush the default set.
+     */
+    static public void flush() {
+       flush(DEFAULT);
+    }
+
+    /** Flush the set which is given.
+     * Throws an error if set doesn't exist.
+     */
+    static public void flush(Object set) {
+       if (funcsMap.get(set) == null)
+           throw new Error("No flush set found: '"+set+"'");
+
+       List l = (List)funcsMap.get(set);
+        for (int i=0; i<l.size(); i++) {
+            CachedNodeFunction one = 
+                (CachedNodeFunction)l.get(i);
+            one.flushItAndSmile();
+        }
+    }
+
+
+    static private final Object DEFAULT = new Object();
+
+    private final int n;
+    private void flushItAndSmile() {
+        cache = new CachingMap(n);
+    }
+
+
 }
Index: fenfire/org/fenfire/view/buoy/TransclusionConnector.java
diff -u fenfire/org/fenfire/view/buoy/TransclusionConnector.java:1.22 
fenfire/org/fenfire/view/buoy/TransclusionConnector.java:1.23
--- fenfire/org/fenfire/view/buoy/TransclusionConnector.java:1.22       Fri Aug 
15 05:35:07 2003
+++ fenfire/org/fenfire/view/buoy/TransclusionConnector.java    Sun Aug 17 
10:34:20 2003
@@ -44,7 +44,7 @@
 /** Hang transclusion buoys.
  */
 public class TransclusionConnector implements BuoyViewConnector {
-public static final String rcsid = "$Id: TransclusionConnector.java,v 1.22 
2003/08/15 09:35:07 tjl Exp $";
+public static final String rcsid = "$Id: TransclusionConnector.java,v 1.23 
2003/08/17 14:34:20 tjl Exp $";
     public static boolean dbg = false;
     private static void p(String s) { 
System.out.println("TransclusionConnector:: "+s); }
 
@@ -57,7 +57,7 @@
     protected NodeFunction nodeView;
 
     // Function: node -> pair(scrollblock, View2D.Anchor)
-    private class SB4Node implements NodeFunction {
+    private class SB4Node implements PureNodeFunction {
        public Object f(ConstGraph g, Object node) {
            Enfilade1D enf = (Enfilade1D)fen.txtfunc.f(g, node);
            List l = enf.getList();
@@ -117,8 +117,8 @@
 
     public TransclusionConnector(Fen fen, NodeFunction nodeView) {
        this.fen = fen;
-       this.scrollBlockForNode = new FlushableCachedNodeFunction(1000,
-                   fen, new SB4Node());
+       this.scrollBlockForNode = new CachedPureNodeFunction(1000,
+                   fen.constgraph, new SB4Node());
        this.nodeView = nodeView;
     }
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]