view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/IndexCreater.java @ 183:066d9c5758dc

change TreeContext
author tatsuki
date Mon, 23 Mar 2015 15:44:28 +0900
parents a2598139df64
children d2b710337eaa
line wrap: on
line source

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

import java.util.Stack;

import fj.Ord;
import fj.data.List;
import fj.data.Option;
import fj.data.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;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.TreeMapOrd;

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 = TreeMap.empty(Ord.stringOrd);

    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) {

        TreeMap<String, List<TreeNode>> index = indexList.getLoop(key);
        if (index == null) {
            index = TreeMap.empty(Ord.stringOrd);
            List<TreeNode> nodeList = List.nil();
            nodeList = nodeList.cons(node);
            TreeMap<String, List<TreeNode>> newIndex = index.set(value, nodeList);
            indexList = indexList.set(key, newIndex);
            return indexList;
        }

        List<TreeNode> nodeList = index.getLoop(value);

        List<TreeNode> newNodeList;
        if (nodeList != null) {
            newNodeList = nodeList.cons(node);
        } else {
            nodeList = List.nil();
            newNodeList = nodeList.cons(node);
        }
        TreeMap<String, List<TreeNode>> newIndex = index.set(value, newNodeList);
        indexList = indexList.set(key, newIndex);

        return indexList;
    }

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

    public ParentIndex getParentIndex() {
        return parentIndex;
    }
}