comparison src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/index/IndexCreater.java @ 0:44465893e8b8

first Commit
author Kazuma
date Wed, 30 Nov 2016 01:47:55 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:44465893e8b8
1 package jp.ac.u_ryukyu.ie.cr.jungle.store.index;
2
3
4 import jp.ac.u_ryukyu.ie.cr.jungle.data.list.List;
5 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode;
6 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNodeChildren;
7 import jp.ac.u_ryukyu.ie.cr.jungle.data.treemap.TreeMap;
8
9 import java.util.Iterator;
10 import java.util.Optional;
11 import java.util.Stack;
12
13 public class IndexCreater {
14
15 TreeNode node;
16 int childNumber;
17 private TreeNodeChildren children;
18 ParentIndex parentIndex = new ParentIndex();
19 TreeMap<String, TreeMap<String, List<TreeNode>>> indexList = new TreeMap<>();
20
21 public IndexCreater(TreeNode rootNode) {
22 Stack<TreeNode> nodeStack = new Stack<>();
23 Stack<Integer> searchStack = new Stack<>();
24 this.node = rootNode;
25 while (node != null) {
26 TreeNode targetNode = node;
27 Iterator<String> keys = targetNode.getAttributes().getKeys();
28 for (; keys.hasNext(); ) {
29 String key = keys.next();
30 String value = targetNode.getAttributes().getString(key);
31 if (value != null)
32 indexList = set(key, value, targetNode);
33 }
34 if (node.getChildren().size() > 0) {
35 nodeStack.push(node);
36 TreeNode parent = node;
37 children = node.getChildren();
38 node = children.at(0).b();
39 parentIndex.set(parent, node);
40 childNumber = 1;
41 searchStack.push(childNumber);
42 } else if (node == rootNode) {
43 node = null; // no more node
44 children = null;
45 return;
46 } else if (children != null && children.size() > childNumber) {
47 childNumber = searchStack.pop();
48 TreeNode parent = nodeStack.pop();
49 nodeStack.push(parent);
50 node = children.at(childNumber).b();
51 parentIndex.set(parent, node);
52 searchStack.push(++childNumber);
53 } else {
54 node = nodeStack.pop();
55 children = node.getChildren();
56 childNumber = searchStack.pop();
57 for (; children.size() == childNumber; ) {
58 if (node == rootNode) {
59 node = null; // no more node
60 children = null;
61 return;
62 }
63 node = nodeStack.pop();
64 children = node.getChildren();
65 childNumber = searchStack.pop();
66 }
67 if (node != null && childNumber < children.size()) {
68 nodeStack.push(node);
69 TreeNode parent = node;
70 node = children.at(childNumber).b();
71 parentIndex.set(parent, node);
72 searchStack.push(++childNumber);
73 }
74 }
75 }
76 }
77
78 public TreeMap<String, TreeMap<String, List<TreeNode>>> set(String key, String value, TreeNode node) {
79 if (key == null)
80 System.out.println("");
81 Optional<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key);
82 if (!indexOp.isPresent()) {
83 TreeMap<String, List<TreeNode>> index = new TreeMap<>();
84 List<TreeNode> nodeList = new List<>();
85 nodeList = nodeList.addLast(node);
86 TreeMap newIndex = index.put(value, nodeList);
87 indexList = indexList.put(key, newIndex);
88 return indexList;
89 }
90
91 TreeMap<String, List<TreeNode>> index = indexOp.get();
92 Optional<List<TreeNode>> nodeListOp = index.get(value);
93
94 List<TreeNode> newNodeList;
95
96 if (nodeListOp.isPresent()) {
97 newNodeList = nodeListOp.get().addLast(node);
98
99 } else {
100 List<TreeNode> nodeList = new List<>();
101 newNodeList = nodeList.addLast(node);
102
103 }
104 TreeMap newIndex = index.put(value, newNodeList);
105 indexList = indexList.put(key, newIndex);
106
107 return indexList;
108 }
109
110 public TreeMap<String, TreeMap<String, List<TreeNode>>> getIndex() {
111 return indexList;
112 }
113
114 public ParentIndex getParentIndex() {
115 return parentIndex;
116 }
117 }