Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 71:5dbae471a3b3
Deleting DefaultNode(change to DefaultTreeNodeChildren)
(on error)
author | one |
---|---|
date | Tue, 26 Aug 2014 07:32:15 +0900 |
parents | 053c9b52a5b2 |
children | 5969848b1bb8 |
files | src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/TreeNode.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultTreeNode.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultTreeNodeChildren.java |
diffstat | 3 files changed, 53 insertions(+), 64 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/TreeNode.java Wed Aug 20 18:10:30 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/TreeNode.java Tue Aug 26 07:32:15 2014 +0900 @@ -14,5 +14,4 @@ public Node getAsNode(); - public T createNewNode(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultTreeNode.java Wed Aug 20 18:10:30 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultTreeNode.java Tue Aug 26 07:32:15 2014 +0900 @@ -1,27 +1,38 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction; +import java.nio.ByteBuffer; + +import fj.Ord; +import fj.data.List; +import fj.data.TreeMap; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNode; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; public class DefaultTreeNode implements TreeNode<DefaultTreeNode> { - private final DefaultNode wrap; + //private final DefaultNode wrap; + public List<DefaultTreeNode> children; + public TreeMap<String,ByteBuffer> attrs; + + private static final List<DefaultTreeNode> NIL_LIST = List.nil(); + private static final TreeMap<String,ByteBuffer> NIL_MAP = TreeMap.empty(Ord.stringOrd); public DefaultTreeNode() { - this(new DefaultNode()); + this(NIL_LIST,NIL_MAP); } - public DefaultTreeNode(DefaultNode _wrap) + public DefaultTreeNode(List<DefaultTreeNode> _children,TreeMap<String,ByteBuffer> _attrs) { - wrap = _wrap; + attrs = _attrs; + children = _children; } @Override public DefaultTreeNodeChildren getChildren() { - return new DefaultTreeNodeChildren(wrap); + return new DefaultTreeNodeChildren(children, attrs); } @Override @@ -30,12 +41,6 @@ return new DefaultTreeNodeAttribute(wrap); } - @Override - public DefaultTreeNode createNewNode() - { - return new DefaultTreeNode(new DefaultNode()); - } - public DefaultNode getWrap() { return wrap; @@ -46,4 +51,9 @@ { return getWrap(); } + + public DefaultNode clone() + { + return new DefaultNode(children,attrs); + } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultTreeNodeChildren.java Wed Aug 20 18:10:30 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultTreeNodeChildren.java Tue Aug 26 07:32:15 2014 +0900 @@ -2,6 +2,7 @@ import java.nio.ByteBuffer; import java.util.Iterator; + import fj.P2; import fj.data.List; import fj.data.TreeMap; @@ -26,16 +27,18 @@ } - private final DefaultNode node; + public List<DefaultTreeNode> children; + public TreeMap<String,ByteBuffer> attrs; - public DefaultTreeNodeChildren(DefaultNode _node) + public DefaultTreeNodeChildren(List<DefaultTreeNode> _children,TreeMap<String,ByteBuffer> _attrs) { - node = _node; + children = _children; + attrs = _attrs; } private boolean boundaryCheck(int _pos) { - int size = node.getChildren().size(); + int size = children.length(); if(size < _pos){ return false; } @@ -50,16 +53,13 @@ return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); } - DefaultChildren children = node.getChildren(); - DefaultAttributes attrs = node.getAttributes(); - List<DefaultNode> raw = children.getChildrenAsRawList(); - P2<List<DefaultNode>,List<DefaultNode>> split = raw.splitAt(_pos); - List<DefaultNode> newChildren = split._1().snoc(new DefaultNode()).append(split._2()); - DefaultNode newNode = new DefaultNode(newChildren,attrs.getAttributesAsRawMap()); + P2<List<DefaultTreeNode>,List<DefaultTreeNode>> split = children.splitAt(_pos); + List<DefaultTreeNode> newChildren = split._1().snoc(new DefaultTreeNode()).append(split._2()); + DefaultTreeNode newNode = new DefaultTreeNode(newChildren,attrs); - return DefaultEither.newB(new DefaultTreeNode(newNode)); + return DefaultEither.newB(newNode); } @Override @@ -68,71 +68,54 @@ if(!boundaryCheck(_pos) || _pos < 0 || size() == 0){ return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); } - - DefaultChildren chs = node.getChildren(); - List<DefaultNode> raw = chs.getChildrenAsRawList(); - - P2<List<DefaultNode>,List<DefaultNode>> split = raw.splitAt(_pos); - List<DefaultNode> newChildren = split._1().append(split._2().tail()); + + P2<List<DefaultTreeNode>,List<DefaultTreeNode>> split = children.splitAt(_pos); + List<DefaultTreeNode> newChildren = split._1().append(split._2().tail()); - DefaultAttributes attrs = node.getAttributes(); - TreeMap<String, ByteBuffer> map = attrs.getAttributesAsRawMap(); - DefaultNode newNode = new DefaultNode(newChildren,map); + DefaultTreeNode newNode = new DefaultTreeNode(newChildren,attrs); - return DefaultEither.newB(new DefaultTreeNode(newNode)); + return DefaultEither.newB(newNode); } @Override public int size() { - return node.getChildren().size(); + return children.length(); } @Override public Iterator<DefaultTreeNode> iterator() { - IterableConverter.Converter<DefaultTreeNode,DefaultNode> converter = new IterableConverter.Converter<DefaultTreeNode,DefaultNode>(){ - @Override - public DefaultTreeNode conv(DefaultNode _b) - { - return new DefaultTreeNode(_b); - } - }; - - List<DefaultNode> raw = node.getChildren().getChildrenAsRawList(); - return new IterableConverter<DefaultTreeNode,DefaultNode>(raw,converter).iterator(); + return children.iterator(); } @Override public Either<Error,DefaultTreeNode> replaceNode(int _pos,DefaultTreeNode _replacement) { - int size = node.getChildren().size(); + int size = children.length(); if(!(0 <= _pos && _pos < size)){ return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); } - DefaultNode replacement = _replacement.getWrap(); + DefaultTreeNode replacement = _replacement; - List<DefaultNode> rawList = node.getChildren().getChildrenAsRawList(); - P2<List<DefaultNode>,List<DefaultNode>> split = rawList.splitAt(_pos + 1); - List<DefaultNode> init = split._1().reverse().tail().reverse(); - List<DefaultNode> newInit = init.snoc(replacement); - List<DefaultNode> newList = newInit.append(split._2()); - TreeMap<String, ByteBuffer> rawMap = node.getAttributes().getAttributesAsRawMap(); + P2<List<DefaultTreeNode>,List<DefaultTreeNode>> split = children.splitAt(_pos + 1); + List<DefaultTreeNode> init = split._1().reverse().tail().reverse(); + List<DefaultTreeNode> newInit = init.snoc(replacement); + List<DefaultTreeNode> newList = newInit.append(split._2()); - return DefaultEither.newB(new DefaultTreeNode(new DefaultNode(newList,rawMap))); + return DefaultEither.newB(new DefaultTreeNode(newList,attrs)); } @Override public Either<Error,DefaultTreeNode> at(int _pos) { - List<DefaultNode> rawList = node.getChildren().getChildrenAsRawList(); - DefaultNode ch = rawList.index(_pos); - if(ch == null){ + DefaultTreeNode Node = children.index(_pos); + if(Node == null){ return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); } - return DefaultEither.newB(new DefaultTreeNode(ch)); + return DefaultEither.newB(Node); } @Override @@ -141,15 +124,12 @@ if(!boundaryCheck(_pos) || _pos < 0){ return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); } - DefaultNode newChild = _newChild.getWrap(); - List<DefaultNode> raw = node.getChildren().getChildrenAsRawList(); - TreeMap<String, ByteBuffer> rawMap = node.getAttributes().getAttributesAsRawMap(); - P2<List<DefaultNode>,List<DefaultNode>> split = raw.splitAt(_pos); - List<DefaultNode> newChildren = split._1().snoc(newChild).append(split._2()); - DefaultNode newNode = new DefaultNode(newChildren,rawMap); + P2<List<DefaultTreeNode>,List<DefaultTreeNode>> split = children.splitAt(_pos); + List<DefaultTreeNode> newChildren = split._1().snoc(_newChild).append(split._2()); + DefaultTreeNode newNode = new DefaultTreeNode(newChildren,attrs); - return DefaultEither.newB(new DefaultTreeNode(newNode)); + return DefaultEither.newB(newNode); } }