diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/QRangeTree.java @ 4797:43e69af28b3c

A naive algorithm to figure out the "Umhuellende" of a set of WQKms.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sun, 13 Jan 2013 14:18:04 +0100
parents 729a5edb0313
children bcf25d8c183e
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/QRangeTree.java	Sat Jan 12 11:30:46 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/QRangeTree.java	Sun Jan 13 14:18:04 2013 +0100
@@ -101,8 +101,52 @@
                 return current.q;
             }
         }
+
+        public Node findNode(double pos) {
+            Node current = this;
+            while (current != null) {
+                if (pos < current.a) {
+                    current = current.left;
+                }
+                else if (pos > current.b) {
+                    current = current.right;
+                }
+                return current;
+            }
+            return null;
+        }
+
+        public boolean contains(double c) {
+            return c >= a && c <= b;
+        }
     } // class Node
 
+    /** Class to cache the last found tree leaf in a search for Q.
+     *  Its likely that a neighbored pos search
+     *  results in using the same leaf node. So
+     *  caching this leaf will minimize expensive
+     *  tree traversals.
+     *  Modeled as inner class because the QRangeTree
+     *  itself is a shared data structure.
+     *  Using this class omits interpolation between
+     *  leaves.
+     */
+    public final class QuickQFinder {
+
+        private Node last;
+
+        public QuickQFinder() {
+        }
+
+        public double findQ(double pos) {
+            if (last != null && last.contains(pos)) {
+                return last.q;
+            }
+            last = QRangeTree.this.findNode(pos);
+            return last != null ? last.q : Double.NaN;
+        }
+    } // class QuickQFinder
+
     protected Node root;
 
     public QRangeTree() {
@@ -212,6 +256,10 @@
         return root != null ? root.findQ(pos) : Double.NaN;
     }
 
+    public Node findNode(double pos) {
+        return root != null ? root.findNode(pos) : null;
+    }
+
     protected Node head() {
         Node head = root;
         while (head.left != null) {

http://dive4elements.wald.intevation.org