view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/IndexCreater.java @ 188:868a746996ad

minner change
author tatsuki
date Fri, 17 Apr 2015 22:12:44 +0900
parents 209df7faa37c
children
line wrap: on
line source

package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index;

import java.util.Optional;
import java.util.Stack;

import fj.data.List;
import jp.ac.u_ryukyu.ie.cr.tatsuki.TreeMap.TreeMap;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNodeChildren;

public class IndexCreater {

    TreeNode root;
    TreeNode node;
    int childNumber;
    private TreeNodeChildren children;
    private Stack<TreeNode> nodeStack = new Stack<TreeNode>();
    private Stack<Integer> searchStack = new Stack<Integer>();
    ParentIndex parentIndex = new ParentIndex();
    TreeMap<String, TreeMap<String, List<TreeNode>>> indexList = new TreeMap();

    public IndexCreater(TreeNode rootNode) {
        this.root = rootNode;
        this.node = rootNode;
        while (node != null) {
            TreeNode targetNode = node;
            List<String> keys = targetNode.getAttributes().getKeys();
            for (String key : keys) {
                String value = targetNode.getAttributes().getString(key);
                if (value != null)
                    indexList = set(key, value, targetNode);
            }
            if (node.getChildren().size() > 0) {
                nodeStack.push(node);
                TreeNode parent = node;
                children = node.getChildren();
                node = children.at(0).b();
                parentIndex.set(parent, node);
                childNumber = 1;
                searchStack.push(childNumber);
            } else if (node == root) {
                node = null; // no more node
                children = null;
                return;
            } else if (children != null && children.size() > childNumber) {
                childNumber = searchStack.pop();
                TreeNode parent = nodeStack.pop();
                nodeStack.push(parent);
                node = children.at(childNumber).b();
                parentIndex.set(parent, node);
                searchStack.push(++childNumber);
            } else {
                node = nodeStack.pop();
                children = node.getChildren();
                childNumber = searchStack.pop();
                for (; children.size() == childNumber; ) {
                    if (node == root) {
                        node = null; // no more node
                        children = null;
                        return;
                    }
                    node = nodeStack.pop();
                    children = node.getChildren();
                    childNumber = searchStack.pop();
                }
                if (node != null && childNumber < children.size()) {
                    nodeStack.push(node);
                    TreeNode parent = node;
                    node = children.at(childNumber).b();
                    parentIndex.set(parent, node);
                    searchStack.push(++childNumber);
                }
            }
        }
    }

    public TreeMap<String, TreeMap<String, List<TreeNode>>> set(String key, String value, TreeNode node) {
        if (key == null )
            System.out.println("");
        Optional<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key);
        if (!indexOp.isPresent()) {
            TreeMap<String, List<TreeNode>> index = new TreeMap();
            List<TreeNode> nodeList = List.nil();
            nodeList = nodeList.cons(node);
            TreeMap<String, List<TreeNode>> newIndex = index.put(value, nodeList);
            indexList = indexList.put(key, newIndex);
            return indexList;
        }

        TreeMap<String, List<TreeNode>> index = indexOp.get();
        Optional<List<TreeNode>> nodeListOp = index.get(value);

        List<TreeNode> newNodeList;

        if (nodeListOp.isPresent()) {
            newNodeList = nodeListOp.get().cons(node);

        } else {
            List<TreeNode> nodeList =  List.nil();
            newNodeList = nodeList.cons(node);

        }
        TreeMap<String, List<TreeNode>> newIndex = index.put(value, newNodeList);
        indexList = indexList.put(key, newIndex);

        return indexList;
    }

    public TreeMap<String, TreeMap<String, List<TreeNode>>> getIndex() {
        return indexList;
    }

    public ParentIndex getParentIndex() {
        return parentIndex;
    }
}