Mercurial > hg > Members > tatsuki > bench > jungle-core
annotate src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultTreeNodeChildren.java @ 97:a1e20a440ddd
add BruteForceTraverser
author | one |
---|---|
date | Mon, 08 Sep 2014 17:03:08 +0900 |
parents | 4c6b915302a8 |
children | ef183969bf31 |
rev | line source |
---|---|
37 | 1 package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction; |
2 | |
3 import java.nio.ByteBuffer; | |
4 import java.util.Iterator; | |
71 | 5 |
37 | 6 import fj.P2; |
7 import fj.data.List; | |
8 import fj.data.TreeMap; | |
81 | 9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; |
52 | 10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNodeChildren; |
87 | 11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultOperationLog; |
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.OperationLog; | |
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.AppendChildAtOperation; | |
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.DeleteChildAtOperation; | |
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation; | |
37 | 16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditorError; |
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; | |
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; | |
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; | |
20 | |
86 | 21 public class DefaultTreeNodeChildren implements TreeNodeChildren |
37 | 22 { |
39 | 23 public static void main(String _args[]) |
24 { | |
25 List<Integer> list = List.range(0,5); | |
41 | 26 P2<List<Integer>, List<Integer>> split = list.splitAt(0); |
39 | 27 System.out.println(split._1().length()); |
28 System.out.println(split._2().length()); | |
29 | |
30 } | |
31 | |
81 | 32 public List<TreeNode> children; |
71 | 33 public TreeMap<String,ByteBuffer> attrs; |
87 | 34 |
81 | 35 public DefaultTreeNodeChildren(List<TreeNode> _children,TreeMap<String,ByteBuffer> _attrs) |
37 | 36 { |
71 | 37 children = _children; |
38 attrs = _attrs; | |
37 | 39 } |
40 | |
41 private boolean boundaryCheck(int _pos) | |
42 { | |
71 | 43 int size = children.length(); |
39 | 44 if(size < _pos){ |
37 | 45 return false; |
46 } | |
47 | |
48 return true; | |
49 } | |
50 | |
51 @Override | |
81 | 52 public List<TreeNode> getChildrenAsRawList(){ |
75
26dfa90016d1
error delete but 2 test program commentout and not action BulletinBoard
one
parents:
74
diff
changeset
|
53 return children; |
26dfa90016d1
error delete but 2 test program commentout and not action BulletinBoard
one
parents:
74
diff
changeset
|
54 } |
26dfa90016d1
error delete but 2 test program commentout and not action BulletinBoard
one
parents:
74
diff
changeset
|
55 @Override |
81 | 56 public Either<Error,TreeNode> addNewChildAt(int _pos) |
37 | 57 { |
39 | 58 if(!boundaryCheck(_pos) || _pos < 0){ |
37 | 59 return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); |
60 } | |
61 | |
81 | 62 P2<List<TreeNode>,List<TreeNode>> split = children.splitAt(_pos); |
63 List<TreeNode> newChildren = split._1().snoc(new DefaultTreeNode()).append(split._2()); | |
97 | 64 TreeNode newNode = new DefaultTreeNode(newChildren,attrs); |
71 | 65 return DefaultEither.newB(newNode); |
37 | 66 } |
67 | |
68 @Override | |
81 | 69 public Either<Error,TreeNode> deleteChildAt(int _pos) |
37 | 70 { |
39 | 71 if(!boundaryCheck(_pos) || _pos < 0 || size() == 0){ |
37 | 72 return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); |
73 } | |
71 | 74 |
81 | 75 P2<List<TreeNode>,List<TreeNode>> split = children.splitAt(_pos); |
76 List<TreeNode> newChildren = split._1().append(split._2().tail()); | |
97 | 77 TreeNode newNode = new DefaultTreeNode(newChildren,attrs); |
37 | 78 |
71 | 79 return DefaultEither.newB(newNode); |
37 | 80 } |
81 | |
82 @Override | |
83 public int size() | |
84 { | |
71 | 85 return children.length(); |
37 | 86 } |
87 | |
88 @Override | |
81 | 89 public Iterator<TreeNode> iterator() |
37 | 90 { |
71 | 91 return children.iterator(); |
37 | 92 } |
93 | |
94 @Override | |
81 | 95 public Either<Error,TreeNode> replaceNode(int _pos,TreeNode _replacement) |
37 | 96 { |
71 | 97 int size = children.length(); |
41 | 98 if(!(0 <= _pos && _pos < size)){ |
37 | 99 return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); |
100 } | |
81 | 101 TreeNode replacement = _replacement; |
37 | 102 |
81 | 103 P2<List<TreeNode>,List<TreeNode>> split = children.splitAt(_pos + 1); |
104 List<TreeNode> init = split._1().reverse().tail().reverse(); | |
105 List<TreeNode> newInit = init.snoc(replacement); | |
106 List<TreeNode> newList = newInit.append(split._2()); | |
107 TreeNode node = new DefaultTreeNode(newList,attrs); | |
108 return DefaultEither.newB(node); | |
37 | 109 } |
41 | 110 |
111 @Override | |
81 | 112 public Either<Error,TreeNode> at(int _pos) |
41 | 113 { |
81 | 114 TreeNode Node = children.index(_pos); |
71 | 115 if(Node == null){ |
41 | 116 return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); |
117 } | |
118 | |
71 | 119 return DefaultEither.newB(Node); |
41 | 120 } |
121 | |
122 @Override | |
81 | 123 public Either<Error,TreeNode> addNewChildAt(int _pos,TreeNode _newChild) |
41 | 124 { |
125 if(!boundaryCheck(_pos) || _pos < 0){ | |
126 return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); | |
127 } | |
128 | |
129 | |
81 | 130 P2<List<TreeNode>,List<TreeNode>> split = children.splitAt(_pos); |
131 List<TreeNode> newChildren = split._1().snoc(_newChild).append(split._2()); | |
132 TreeNode newNode = new DefaultTreeNode(newChildren,attrs); | |
41 | 133 |
71 | 134 return DefaultEither.newB(newNode); |
41 | 135 } |
37 | 136 } |