From eb15d532080766856fa43caa943e0c58b17b0474 Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Mon, 31 Mar 2014 22:43:48 +0200 Subject: [PATCH] modificato arraysequence in fake e aggiunti una nuova versione. fidato alcuni metodi che restituivano void invece di position. sono necessari ulteriori test --- com/xgiovio/ArraySequenceFakeTest.java | 25 ++ com/xgiovio/ArraySequenceTest.java | 41 +--- position/NodePositionList.java | 7 +- position/PositionList.java | 4 +- sequence/ArraySequence.java | 128 +++++++--- sequence/ArraySequenceFake.java | 224 ++++++++++++++++++ sequence/utility/ArrayPosition.java | 51 ++++ .../{DNodeIndex.java => DNodeFake.java} | 10 +- 8 files changed, 414 insertions(+), 76 deletions(-) create mode 100644 com/xgiovio/ArraySequenceFakeTest.java create mode 100644 sequence/ArraySequenceFake.java create mode 100644 sequence/utility/ArrayPosition.java rename sequence/utility/{DNodeIndex.java => DNodeFake.java} (85%) diff --git a/com/xgiovio/ArraySequenceFakeTest.java b/com/xgiovio/ArraySequenceFakeTest.java new file mode 100644 index 0000000..0cba260 --- /dev/null +++ b/com/xgiovio/ArraySequenceFakeTest.java @@ -0,0 +1,25 @@ +package com.xgiovio; + +import general_utility.test_object; +import position.Position; +import sequence.ArraySequenceFake; + +/** + * Created with xgiovio.macbookair. + * User: xgiovio + * Date: 23/03/14 + * Time: 20:37 + */ +public class ArraySequenceFakeTest { + + public static void main(String[] args) { + ArraySequenceFake a = new ArraySequenceFake(10); + + a.addFirst(new test_object(3)); + Position p = a.addAfter(a.first(), new test_object(6)); + System.out.print(a); + + + } + +} diff --git a/com/xgiovio/ArraySequenceTest.java b/com/xgiovio/ArraySequenceTest.java index 54c0965..a9a9e83 100644 --- a/com/xgiovio/ArraySequenceTest.java +++ b/com/xgiovio/ArraySequenceTest.java @@ -1,10 +1,9 @@ package com.xgiovio; -import arraylist.ArrayIndexList; import general_utility.test_object; import position.Position; import sequence.ArraySequence; -import sequence.NodeSequence; +import sequence.ArraySequenceFake; /** * Created with xgiovio.macbookair. @@ -17,43 +16,9 @@ public class ArraySequenceTest { public static void main(String[] args) { ArraySequence a = new ArraySequence(10); - a.add(0,new test_object(1)); - a.add(1,new test_object(2)); - a.add(2,new test_object(3)); - a.add(3,new test_object(4)); - + a.addFirst(new test_object(3)); + Position p = a.addAfter(a.first(), new test_object(6)); System.out.print(a); - a.remove(2); - System.out.print(a); - System.out.print(a.first().element()); - System.out.print(a.last().element()); - a.addLast(new test_object(1000)); - System.out.print(a); - a.addFirst(new test_object(2000)); - System.out.print(a); - a.add(0,new test_object(1)); - System.out.print(a); - a.add(5,new test_object(5)); - System.out.print(a); - a.add(6,new test_object(125)); - System.out.print(a); - a.add(8,new test_object(211)); - System.out.print(a); - a.set(0, new test_object(211)); - System.out.print(a); - a.set(1,new test_object(211)); - System.out.print(a); - - - global.reverse(a); - - System.out.print(a); - - - - - - } diff --git a/position/NodePositionList.java b/position/NodePositionList.java index 54b8eaa..9f2d5f4 100644 --- a/position/NodePositionList.java +++ b/position/NodePositionList.java @@ -79,21 +79,22 @@ public class NodePositionList implements PositionList { return next; } - public void addBefore(Position p, E element) throws InvalidPositionException { + public Position addBefore(Position p, E element) throws InvalidPositionException { DNode v = checkPosition(p); numElts++; DNode newNode = new DNode(v.getPrev(), v, element); v.getPrev().setNext(newNode); v.setPrev(newNode); + return newNode; } - public void addAfter(Position p, E element) - throws InvalidPositionException { + public Position addAfter(Position p, E element) throws InvalidPositionException { DNode v = checkPosition(p); numElts++; DNode newNode = new DNode(v, v.getNext(), element); v.getNext().setPrev(newNode); v.setNext(newNode); + return newNode; } public void addFirst(E element) { diff --git a/position/PositionList.java b/position/PositionList.java index fd16f47..2b28e1f 100644 --- a/position/PositionList.java +++ b/position/PositionList.java @@ -27,9 +27,9 @@ public interface PositionList { public void addLast(E e); - public void addAfter(Position p, E e) throws InvalidPositionException; + public Position addAfter(Position p, E e) throws InvalidPositionException; - public void addBefore(Position p, E e) throws InvalidPositionException; + public Position addBefore(Position p, E e) throws InvalidPositionException; public E remove(Position p) throws InvalidPositionException; diff --git a/sequence/ArraySequence.java b/sequence/ArraySequence.java index e2f70cb..05a38ca 100644 --- a/sequence/ArraySequence.java +++ b/sequence/ArraySequence.java @@ -1,27 +1,82 @@ package sequence; -import arraylist.ArrayIndexList; import exceptions.BoundaryViolationException; import exceptions.EmptyListException; import exceptions.EmptySequenceException; import exceptions.InvalidPositionException; import position.Position; -import sequence.utility.DNodeIndex; +import sequence.utility.ArrayPosition; /** - * Created with MONSTER. + * Created with xgiovio.macbookair. * User: xgiovio - * Date: 30/03/2014 - * Time: 17:19 + * Date: 31/03/14 + * Time: 16:12 */ -public class ArraySequence extends ArrayIndexList implements Sequence { +public class ArraySequence implements Sequence { + private Position[] array; + private int capacity = 100; + private int size = 0; - public ArraySequence (int in){ - super(in); + public ArraySequence() { + array = (Position[]) new Object[capacity]; } - @Override + public ArraySequence(int in_size) { + array = (Position[]) new Object[in_size]; + capacity = in_size; + } + + + public int size() { + return size; + } + + public boolean isEmpty() { + return size() == 0; + } + + public E get(int r) throws IndexOutOfBoundsException { + checkIndex(r, size()); + return array[r].element(); + } + + public E set(int r, E e) throws IndexOutOfBoundsException { + checkIndex(r, size()); + E temp = array[r].element(); + ((ArrayPosition)array[r]).setElement(e); + return temp; + } + + + + public void add(int r, E e) throws IndexOutOfBoundsException { + checkIndex(r, size() + 1); + if (size == capacity) { + capacity *= 2; + Position[] B =(Position[]) new Object[capacity]; + for (int i=0; i=r; i--) + array[i+1] = array[i]; + ((ArrayPosition)array[r]).setElement(e); + size++; + } + + + public E remove(int r) throws IndexOutOfBoundsException { + checkIndex(r, size()); + E temp = array[r].element(); + for (int i=r; i extends ArrayIndexList implements Sequence { } } + @Override public E getLast() throws EmptySequenceException { try { @@ -65,11 +121,12 @@ public class ArraySequence extends ArrayIndexList implements Sequence { } } + @Override public Position atIndex(int in_index) throws BoundaryViolationException { try { checkIndex(in_index, size()); - return new DNodeIndex(get(in_index),in_index); + return array[in_index]; } catch (IndexOutOfBoundsException err){ throw new BoundaryViolationException(); @@ -79,13 +136,13 @@ public class ArraySequence extends ArrayIndexList implements Sequence { @Override public int indexOf(Position position) throws InvalidPositionException { checkPosition(position); - return ((DNodeIndex) position).getIndex(); + return ((ArrayPosition) position).getIndex(); } @Override public Position first() throws EmptyListException { try{ - return new DNodeIndex(get(0),0); + return array[0]; } catch (IndexOutOfBoundsException err){ throw new EmptyListException("Empty Array"); @@ -95,18 +152,19 @@ public class ArraySequence extends ArrayIndexList implements Sequence { @Override public Position last() throws EmptyListException { try{ - return new DNodeIndex(get(size() - 1),size() - 1); + return array[size()-1]; } catch (IndexOutOfBoundsException err){ throw new EmptyListException("Empty Array"); } } + @Override public Position next(Position p) throws InvalidPositionException, BoundaryViolationException { - DNodeIndex t = checkPosition(p); + ArrayPosition t = checkPosition(p); if (t.getIndex()< size() -1){ - return new DNodeIndex(get(t.getIndex() + 1),t.getIndex() + 1); + return array[t.getIndex() + 1]; }else{ throw new BoundaryViolationException(); } @@ -114,9 +172,9 @@ public class ArraySequence extends ArrayIndexList implements Sequence { @Override public Position prev(Position p) throws InvalidPositionException, BoundaryViolationException { - DNodeIndex t = checkPosition(p); + ArrayPosition t = checkPosition(p); if (t.getIndex()> 0){ - return new DNodeIndex(get(t.getIndex() - 1),t.getIndex() - 1); + return array[t.getIndex() - 1]; }else{ throw new BoundaryViolationException(); } @@ -124,19 +182,21 @@ public class ArraySequence extends ArrayIndexList implements Sequence { @Override public void addFirst(E e) { - add (0,e); + add (0,e); } @Override public void addLast(E e) { - add (size(),e); + add (size(),e); } + @Override - public void addAfter(Position p, E e) throws InvalidPositionException { + public Position addAfter(Position p, E e) throws InvalidPositionException { try { - DNodeIndex t = checkPosition(p); + ArrayPosition t = checkPosition(p); add(t.getIndex() + 1, e); + return array[t.getIndex() + 1]; } catch (IndexOutOfBoundsException err){ throw new InvalidPositionException(); @@ -144,21 +204,23 @@ public class ArraySequence extends ArrayIndexList implements Sequence { } @Override - public void addBefore(Position p, E e) throws InvalidPositionException { + public Position addBefore(Position p, E e) throws InvalidPositionException { try { - DNodeIndex t = checkPosition(p); + ArrayPosition t = checkPosition(p); add(t.getIndex() - 1,e); + return array[t.getIndex() - 1]; } catch (IndexOutOfBoundsException err){ throw new InvalidPositionException(); } } + @Override public E remove(Position p) throws InvalidPositionException { try { - DNodeIndex t = checkPosition(p); - return remove(t.getIndex()); + ArrayPosition t = checkPosition(p); + return remove(t.getIndex()); } catch (IndexOutOfBoundsException err){ throw new InvalidPositionException(); @@ -168,7 +230,7 @@ public class ArraySequence extends ArrayIndexList implements Sequence { @Override public E set(Position p, E e) throws InvalidPositionException { try { - DNodeIndex t = checkPosition(p); + ArrayPosition t = checkPosition(p); return set (t.getIndex(),e); } catch (IndexOutOfBoundsException err){ @@ -178,8 +240,10 @@ public class ArraySequence extends ArrayIndexList implements Sequence { } + @Override public String toString() { + String to_return = ""; to_return = to_return + "["; @@ -196,13 +260,19 @@ public class ArraySequence extends ArrayIndexList implements Sequence { return to_return; } - protected DNodeIndex checkPosition(Position p) throws InvalidPositionException { + protected void checkIndex(int r, int n) // + throws IndexOutOfBoundsException { // + if (r < 0 || r >= n) + throw new IndexOutOfBoundsException("Illegal index: " + r); + } + + protected ArrayPosition checkPosition(Position p) throws InvalidPositionException { if (p == null) throw new InvalidPositionException ("Null position passed"); try { - DNodeIndex temp = (DNodeIndex) p; + ArrayPosition temp = (ArrayPosition) p; if ( temp.getIndex() >= size() || temp.getIndex() < 0 ) throw new InvalidPositionException ("Position does not belong to a valid value"); @@ -213,6 +283,4 @@ public class ArraySequence extends ArrayIndexList implements Sequence { } } - - } diff --git a/sequence/ArraySequenceFake.java b/sequence/ArraySequenceFake.java new file mode 100644 index 0000000..e1de7b1 --- /dev/null +++ b/sequence/ArraySequenceFake.java @@ -0,0 +1,224 @@ +package sequence; + +import arraylist.ArrayIndexList; +import exceptions.BoundaryViolationException; +import exceptions.EmptyListException; +import exceptions.EmptySequenceException; +import exceptions.InvalidPositionException; +import position.Position; +import sequence.utility.DNodeFake; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 30/03/2014 + * Time: 17:19 + */ + + // fake positions created on the fly. if the element in the array change, it doesn't change on the position retrieved before. + + +public class ArraySequenceFake extends ArrayIndexList implements Sequence { + + + public ArraySequenceFake(int in){ + super(in); + } + + @Override + public E getFirst() throws EmptySequenceException { + try { + E ele = get(0); + return ele; + } + catch (IndexOutOfBoundsException err){ + throw new EmptySequenceException(); + } + } + + @Override + public E getLast() throws EmptySequenceException { + try { + E ele = get(size()-1); + return ele; + } + catch (IndexOutOfBoundsException err){ + throw new EmptySequenceException(); + } + } + + @Override + public E removeFirst() throws EmptySequenceException { + try { + E ele = remove(0); + return ele; + } + catch (IndexOutOfBoundsException err){ + throw new EmptySequenceException(); + } + } + + @Override + public E removeLast() throws EmptySequenceException { + try { + E ele = remove(size() - 1); + return ele; + } + catch (IndexOutOfBoundsException err){ + throw new EmptySequenceException(); + } + } + + @Override + public Position atIndex(int in_index) throws BoundaryViolationException { + try { + checkIndex(in_index, size()); + return new DNodeFake(get(in_index),in_index); + } + catch (IndexOutOfBoundsException err){ + throw new BoundaryViolationException(); + } + } + + @Override + public int indexOf(Position position) throws InvalidPositionException { + checkPosition(position); + return ((DNodeFake) position).getIndex(); + } + + @Override + public Position first() throws EmptyListException { + try{ + return new DNodeFake(get(0),0); + } + catch (IndexOutOfBoundsException err){ + throw new EmptyListException("Empty Array"); + } + } + + @Override + public Position last() throws EmptyListException { + try{ + return new DNodeFake(get(size() - 1),size() - 1); + } + catch (IndexOutOfBoundsException err){ + throw new EmptyListException("Empty Array"); + } + } + + @Override + public Position next(Position p) throws InvalidPositionException, BoundaryViolationException { + DNodeFake t = checkPosition(p); + if (t.getIndex()< size() -1){ + return new DNodeFake(get(t.getIndex() + 1),t.getIndex() + 1); + }else{ + throw new BoundaryViolationException(); + } + } + + @Override + public Position prev(Position p) throws InvalidPositionException, BoundaryViolationException { + DNodeFake t = checkPosition(p); + if (t.getIndex()> 0){ + return new DNodeFake(get(t.getIndex() - 1),t.getIndex() - 1); + }else{ + throw new BoundaryViolationException(); + } + } + + @Override + public void addFirst(E e) { + add (0,e); + } + + @Override + public void addLast(E e) { + add (size(),e); + } + + @Override + public Position addAfter(Position p, E e) throws InvalidPositionException { + try { + DNodeFake t = checkPosition(p); + add(t.getIndex() + 1, e); + return new DNodeFake(get(t.getIndex() + 1),t.getIndex() + 1); + } + catch (IndexOutOfBoundsException err){ + throw new InvalidPositionException(); + } + } + + @Override + public Position addBefore(Position p, E e) throws InvalidPositionException { + try { + DNodeFake t = checkPosition(p); + add(t.getIndex() - 1,e); + return new DNodeFake(get(t.getIndex() - 1),t.getIndex() - 1); + } + catch (IndexOutOfBoundsException err){ + throw new InvalidPositionException(); + } + } + + @Override + public E remove(Position p) throws InvalidPositionException { + try { + DNodeFake t = checkPosition(p); + return remove(t.getIndex()); + } + catch (IndexOutOfBoundsException err){ + throw new InvalidPositionException(); + } + } + + @Override + public E set(Position p, E e) throws InvalidPositionException { + try { + DNodeFake t = checkPosition(p); + return set (t.getIndex(),e); + } + catch (IndexOutOfBoundsException err){ + throw new InvalidPositionException(); + } + + + } + + @Override + public String toString() { + String to_return = ""; + to_return = to_return + "["; + + for ( int i = 0; i< size() ; i++){ + + if ( i== size() -1){ + to_return+=(get(i).toString()); + }else{ + to_return+=(get(i).toString() + ","); + } + } + to_return = to_return + "]"; + + return to_return; + } + + protected DNodeFake checkPosition(Position p) throws InvalidPositionException { + if (p == null) + throw new InvalidPositionException + ("Null position passed"); + + try { + DNodeFake temp = (DNodeFake) p; + if ( temp.getIndex() >= size() || temp.getIndex() < 0 ) + throw new InvalidPositionException + ("Position does not belong to a valid value"); + return temp; + } catch (ClassCastException e) { + throw new InvalidPositionException + ("Position is of wrong type for this array"); + } + } + + + +} diff --git a/sequence/utility/ArrayPosition.java b/sequence/utility/ArrayPosition.java new file mode 100644 index 0000000..a9fb132 --- /dev/null +++ b/sequence/utility/ArrayPosition.java @@ -0,0 +1,51 @@ +package sequence.utility; + +import exceptions.InvalidPositionException; +import position.Position; + + +/** + * A simple node class for a doubly-linked list. Each DNode has a + * reference to a stored element, a previous node, and a next node. + * + * @author Roberto Tamassia + */ +//Copyright (c) 2003 Brown University, Providence, RI +//Additional modifications and methods by xgiovio + +// used on ArraySequence + + +public class ArrayPosition implements Position { + private E element = null; + private int index = -1; + + + public ArrayPosition(E elem, int in_idex) { + + element = elem; + index = in_idex; + } + public ArrayPosition(E elem) { + + element = elem; + } + + public E element() throws InvalidPositionException { + if (index == -1) + throw new InvalidPositionException("Position is not in a list!"); + return element; + } + + + public void setElement(E newElement) { element = newElement; } + + public int getIndex() { + return index; + } + + public void setIndex(int index) { + this.index = index; + } +} + diff --git a/sequence/utility/DNodeIndex.java b/sequence/utility/DNodeFake.java similarity index 85% rename from sequence/utility/DNodeIndex.java rename to sequence/utility/DNodeFake.java index c2e01a2..6de351d 100644 --- a/sequence/utility/DNodeIndex.java +++ b/sequence/utility/DNodeFake.java @@ -3,6 +3,7 @@ package sequence.utility; import exceptions.InvalidPositionException; import position.Position; + /** * A simple node class for a doubly-linked list. Each DNode has a * reference to a stored element, a previous node, and a next node. @@ -12,17 +13,20 @@ import position.Position; //Copyright (c) 2003 Brown University, Providence, RI //Additional modifications and methods by xgiovio -public class DNodeIndex implements Position { +// used on ArraySequenceFake + + +public class DNodeFake implements Position { private E element = null; private int index = -1; - public DNodeIndex( E elem, int in_idex) { + public DNodeFake(E elem, int in_idex) { element = elem; index = in_idex; } - public DNodeIndex( E elem) { + public DNodeFake(E elem) { element = elem; }