diff src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/query/PathNodeIndexIterator.java @ 127:b2c1fd513feb

push index thread add read log
author one
date Mon, 13 Oct 2014 03:22:16 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/query/PathNodeIndexIterator.java	Mon Oct 13 03:22:16 2014 +0900
@@ -0,0 +1,99 @@
+package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query;
+
+import java.util.Iterator;
+import java.util.Stack;
+
+import fj.data.List;
+import fj.data.TreeMap;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNodeChildren;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair;
+
+public class PathNodeIndexIterator implements Iterator<Pair<TreeNode, NodePath>> {
+
+	TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index;	
+	NodePath path;
+	TreeNode root;
+	TreeNode node;
+	int childNumber;
+	private TreeNodeChildren children;
+	private Stack<TreeNode> nodeStack = new Stack<TreeNode>();
+	private Stack<Integer> searchStack = new Stack<Integer>();
+	
+	/*
+	 * get queryIndexCondition from query 
+	 * if already index exists, use index 
+	 * otherwise traverse tree and create index  
+	 *
+	 * */
+	public PathNodeIndexIterator(TreeNode root, TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) {
+		this.root = root;
+		this.index = index;
+		path = new DefaultNodePath();
+		node = root;
+	}
+
+	@Override
+	public boolean hasNext() {
+		return node != null;
+	}
+
+	@Override
+	public Pair<TreeNode, NodePath> next() {
+		TreeNode now = node;
+		NodePath currentPath = path;
+		if (node.getChildren().size() > 0) { // 
+			nodeStack.push(node);
+			path = path.add(0);
+			children = node.getChildren();
+			node = children.at(0).b();
+			childNumber = 1;
+			searchStack.push(childNumber);
+		} else if (node == root) {
+			node = null; // no more node
+			children = null;
+			return new Pair<TreeNode, NodePath>(now, currentPath);
+		}else if (children != null && children.size() > childNumber) {
+			childNumber = searchStack.pop();
+			node = children.at(childNumber).b();
+			path = path.tail().add(childNumber);
+			searchStack.push(++childNumber);
+		} else {
+			path = path.tail();
+			node = nodeStack.pop();
+			children = node.getChildren();
+			childNumber = searchStack.pop();
+			for (; children.size() == childNumber;) {
+				if (node == root) {
+					node = null; // no more node
+					children = null;
+					return new Pair<TreeNode, NodePath>(now, currentPath);
+				}
+				path = path.tail();
+				node = nodeStack.pop();
+				children = node.getChildren();
+				childNumber = searchStack.pop();
+			}
+			if (node != null && childNumber < children.size()) {
+				path = path.add(childNumber);
+				nodeStack.push(node);
+				node = children.at(childNumber).b();
+				searchStack.push(++childNumber);
+			}
+		}
+		System.out.println("path = " + path.toString());
+		return new Pair<TreeNode, NodePath>(now, currentPath);
+	}
+
+	@Override
+	public void remove() {
+		// TODO Auto-generated method stub
+
+	}
+
+	public 	TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> getIndex() {
+		return index;
+	}
+}