changeset 207:71624cf77dbf

change complete list
author tatsuki
date Tue, 19 May 2015 14:41:06 +0900
parents f06dc60bd70f
children be08961d4c84
files src/main/java/jp/ac/u_ryukyu/ie/cr/list/List.java src/main/java/jp/ac/u_ryukyu/ie/cr/list/TailNode.java src/main/java/jp/ac/u_ryukyu/ie/cr/list/headNode.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTreeEditor.java src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/list/deleteTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/list/listAdd.java src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/list/replaceTest.java
diffstat 7 files changed, 93 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/list/List.java	Tue May 19 13:09:47 2015 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/list/List.java	Tue May 19 14:41:06 2015 +0900
@@ -12,7 +12,7 @@
     final private int listLength;
 
     public List() {
-        this.head = new headNode(null);
+        this.head = new headNode();
         this.listLength = 0;
     }
 
@@ -22,12 +22,12 @@
             list = list.addLast(attribute);
         }
         this.head = list.getHead();
-        this.listLength = head.getNext().getNum();
+        this.listLength = head.getNext().getNum() + 1;
     }
 
     private List(Node<T> head) {
         this.head = head;
-        this.listLength = head.getNext().getNum();
+        this.listLength = head.getNext().getNum() + 1;
     }
 
     public Node<T> getHead() {
@@ -35,15 +35,14 @@
     }
 
     public List<T> add(int num, T attribute) {
-        Node<T> newNode = head.getNext().add(num, attribute);
-        if (newNode == null)
+        Node<T> newHead = head.add(num, attribute);
+        if (newHead == null)
             return this;
-        Node<T> newHead = new headNode<>(newNode);
         return new List<T>(newHead);
     }
 
     public List<T> addLast(T attribute) {
-        Node newNode = new DefaultNode(attribute, listLength + 1, head.getNext());
+        Node newNode = new DefaultNode(attribute, listLength, head.getNext());
         Node newHead = new headNode(newNode);
         return new List(newHead);
     }
@@ -107,10 +106,9 @@
     }
 
     public List<T> replace(int num, T attribute) {
-        Node<T> newNode = head.getNext().replaceNode(num, attribute);
-        if (newNode == null)
+        Node<T> newHead = head.replaceNode(num, attribute);
+        if (newHead == null)
             return this;
-        Node<T> newHead = new headNode<>(newNode);
         return new List<T>(newHead);
     }
 
@@ -119,15 +117,15 @@
     }
 
     public T head() {
-        return index(1);
+        return index(0);
     }
 
     public List<T> deleteLast() {
-        return delete(listLength);
+        return delete(listLength - 1);
     }
 
     public List<T> deleteHead() {
-        return delete(1);
+        return delete(0);
     }
 
     public int length() {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/list/TailNode.java	Tue May 19 14:41:06 2015 +0900
@@ -0,0 +1,38 @@
+package jp.ac.u_ryukyu.ie.cr.list;
+
+/**
+ * Created by e115731 on 15/05/19.
+ */
+public class TailNode<T> implements Node<T> {
+
+    @Override
+    public int getNum() {
+        return -1;
+    }
+
+    @Override
+    public Node<T> getNext() {
+        return null;
+    }
+
+    @Override
+    public T getAttribute() {
+        return null;
+    }
+
+    @Override
+    public Node<T> add(int num, T attribute) {
+        return null;
+    }
+
+    @Override
+    public Node<T> delete(int num) {
+        return null;
+    }
+
+    @Override
+    public Node<T> replaceNode(int num, T attribute) {
+        return null;
+    }
+
+}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/list/headNode.java	Tue May 19 13:09:47 2015 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/list/headNode.java	Tue May 19 14:41:06 2015 +0900
@@ -6,6 +6,11 @@
 public class headNode<T> implements Node<T> {
     private final Node next;
 
+
+    public headNode() {
+        this.next = new TailNode<T>();
+    }
+
     public headNode(Node next) {
         this.next = next;
     }
@@ -15,8 +20,6 @@
     }
 
     public Node getNext() {
-        if (next == null)
-            return this;
         return next;
     }
 
@@ -25,11 +28,21 @@
     }
 
     public Node<T> add(int num, T attribute) {
-        if (num == 0) {
-            return new DefaultNode<>(attribute, num, this);
+        if (next.getNum() == num - 1) {
+            Node<T> newNode = new DefaultNode<>(attribute, num, next);
+            return new headNode<T>(newNode);
         }
 
-        return null;
+        if (next.getNum() == num) {
+            Node<T> newNode = new DefaultNode(attribute, num, next.getNext());
+            Node<T> newNext = new DefaultNode(next.getAttribute(), num++, newNode);
+            return new headNode(newNext);
+        }
+
+        Node<T> newNode = next.add(num, attribute);
+        if (newNode == null)
+            return this;
+        return new headNode(newNode);
     }
 
     public Node<T> delete(int deleteNum) {
@@ -45,6 +58,15 @@
 
     @Override
     public Node<T> replaceNode(int num, T attribute) {
-        return null;
+        Node<T> nextNode = getNext();
+        if (nextNode.getNum() == num) {
+            Node<T> newNode = new DefaultNode(attribute, num, nextNode.getNext());
+            return new headNode(newNode);
+        }
+
+        Node<T> newNode = next.replaceNode(num, attribute);
+        if (newNode == null)
+            return this;
+        return new headNode(newNode);
     }
 }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTreeEditor.java	Tue May 19 13:09:47 2015 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTreeEditor.java	Tue May 19 14:41:06 2015 +0900
@@ -48,7 +48,7 @@
 		}
 		
 		// target
-		Direction<TreeNode> targetDirection = path.head();
+		Direction<TreeNode> targetDirection = path.last();
 		TreeNode target = targetDirection.getTarget();
 		Either<Error,LoggingNode> either = editor.edit(target);
 		if(either.isA()){
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/list/deleteTest.java	Tue May 19 13:09:47 2015 +0900
+++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/list/deleteTest.java	Tue May 19 14:41:06 2015 +0900
@@ -12,7 +12,7 @@
     public void deleteTest() {
         List<Integer> list = new List<Integer>();
 
-        for (int count = 1; count <= 10; count++) {
+        for (int count = 0; count < 10; count++) {
             list = list.addLast(count);
         }
 
@@ -23,8 +23,8 @@
         Assert.assertEquals(attribute,2);
         Assert.assertEquals(newList.length(),8);
         newList = newList.deleteLast();
-        attribute = newList.index(7);
-        Assert.assertEquals(attribute,9);
+        attribute = newList.index(6);
+        Assert.assertEquals(attribute,8);
         Assert.assertEquals(list.length(),10);
     }
 }
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/list/listAdd.java	Tue May 19 13:09:47 2015 +0900
+++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/list/listAdd.java	Tue May 19 14:41:06 2015 +0900
@@ -13,8 +13,18 @@
     public void listAddTest() {
 
         List<Integer> list = new List<Integer>();
-        for (int count = 1; count <= 10; count++) {
+        List<Integer> list2 = new List<Integer>();
+
+        for (int count = 0; count < 10; count++) {
             list = list.addLast(count);
+            list2 = list2.add(count, count);
+        }
+
+        for (int count = 0; count < 10; count++) {
+            int num = list.index(count);
+            int num2 = list2.index(count);
+            Assert.assertEquals(num,count);
+            Assert.assertEquals(num2,count);
         }
         Assert.assertEquals(list.length(), 10);
         int num = list.index(5);
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/list/replaceTest.java	Tue May 19 13:09:47 2015 +0900
+++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/list/replaceTest.java	Tue May 19 14:41:06 2015 +0900
@@ -11,7 +11,7 @@
     @Test
     public void replaceTest() {
         List<Integer> list = new List<Integer>();
-        for (int count = 1; count <= 10; count++) {
+        for (int count = 0; count < 10; count++) {
             list = list.addLast(count);
         }
         List<Integer> newList = list.replace(5, 15);