annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
127
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
1 package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
2
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
3 import java.util.Iterator;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
4 import java.util.Stack;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
5
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
6 import fj.data.List;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
7 import fj.data.TreeMap;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNodeChildren;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
13
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
14 public class PathNodeIndexIterator implements Iterator<Pair<TreeNode, NodePath>> {
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
15
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
16 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
17 NodePath path;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
18 TreeNode root;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
19 TreeNode node;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
20 int childNumber;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
21 private TreeNodeChildren children;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
22 private Stack<TreeNode> nodeStack = new Stack<TreeNode>();
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
23 private Stack<Integer> searchStack = new Stack<Integer>();
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
24
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
25 /*
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
26 * get queryIndexCondition from query
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
27 * if already index exists, use index
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
28 * otherwise traverse tree and create index
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
29 *
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
30 * */
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
31 public PathNodeIndexIterator(TreeNode root, TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) {
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
32 this.root = root;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
33 this.index = index;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
34 path = new DefaultNodePath();
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
35 node = root;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
36 }
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
37
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
38 @Override
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
39 public boolean hasNext() {
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
40 return node != null;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
41 }
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
42
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
43 @Override
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
44 public Pair<TreeNode, NodePath> next() {
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
45 TreeNode now = node;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
46 NodePath currentPath = path;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
47 if (node.getChildren().size() > 0) { //
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
48 nodeStack.push(node);
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
49 path = path.add(0);
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
50 children = node.getChildren();
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
51 node = children.at(0).b();
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
52 childNumber = 1;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
53 searchStack.push(childNumber);
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
54 } else if (node == root) {
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
55 node = null; // no more node
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
56 children = null;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
57 return new Pair<TreeNode, NodePath>(now, currentPath);
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
58 }else if (children != null && children.size() > childNumber) {
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
59 childNumber = searchStack.pop();
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
60 node = children.at(childNumber).b();
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
61 path = path.tail().add(childNumber);
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
62 searchStack.push(++childNumber);
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
63 } else {
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
64 path = path.tail();
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
65 node = nodeStack.pop();
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
66 children = node.getChildren();
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
67 childNumber = searchStack.pop();
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
68 for (; children.size() == childNumber;) {
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
69 if (node == root) {
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
70 node = null; // no more node
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
71 children = null;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
72 return new Pair<TreeNode, NodePath>(now, currentPath);
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
73 }
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
74 path = path.tail();
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
75 node = nodeStack.pop();
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
76 children = node.getChildren();
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
77 childNumber = searchStack.pop();
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
78 }
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
79 if (node != null && childNumber < children.size()) {
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
80 path = path.add(childNumber);
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
81 nodeStack.push(node);
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
82 node = children.at(childNumber).b();
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
83 searchStack.push(++childNumber);
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
84 }
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
85 }
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
86 System.out.println("path = " + path.toString());
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
87 return new Pair<TreeNode, NodePath>(now, currentPath);
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
88 }
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
89
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
90 @Override
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
91 public void remove() {
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
92 // TODO Auto-generated method stub
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
93
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
94 }
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
95
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
96 public TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> getIndex() {
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
97 return index;
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
98 }
b2c1fd513feb push index thread add read log
one
parents:
diff changeset
99 }