diff --git a/com/xgiovio/ArraySequenceFakeTest.java b/com/xgiovio/ArraySequenceFakeTest.java deleted file mode 100644 index 0cba260..0000000 --- a/com/xgiovio/ArraySequenceFakeTest.java +++ /dev/null @@ -1,25 +0,0 @@ -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/NodePositionListTest.java b/com/xgiovio/NodePositionListTest.java index 5a2938e..6296648 100644 --- a/com/xgiovio/NodePositionListTest.java +++ b/com/xgiovio/NodePositionListTest.java @@ -3,6 +3,7 @@ package com.xgiovio; import arraylist.ArrayIndexList; import general_utility.test_object; import position.NodePositionList; +import position.Position; import java.util.Iterator; @@ -38,6 +39,18 @@ public class NodePositionListTest { System.out.print(it.next()); System.out.print(it.hasNext()); + Iterator> itp = a.positions(); + System.out.print(itp.hasNext()); + System.out.print(itp.next().element()); + System.out.print(itp.hasNext()); + System.out.print(itp.next().element()); + System.out.print(itp.hasNext()); + System.out.print(itp.next().element()); + System.out.print(itp.hasNext()); + System.out.print(itp.next().element()); + System.out.print(itp.hasNext()); + + diff --git a/position/NodePositionList.java b/position/NodePositionList.java index e85c286..4f9dc55 100644 --- a/position/NodePositionList.java +++ b/position/NodePositionList.java @@ -251,7 +251,7 @@ public class NodePositionList implements PositionList { @Override public void remove() { - + throw new UnsupportedOperationException (); } NodePositionList new_structure; @@ -260,5 +260,73 @@ public class NodePositionList implements PositionList { } + @Override + public Iterator> positions() { + return new MyPositionsIterator(this); + } + class MyPositionsIterator implements Iterator>{ + + public MyPositionsIterator (NodePositionList structure){ + + new_structure = new NodePositionList>(); + if (structure.size() != 0){ + Position temp; + for (temp = structure.first() ; temp!= structure.last() ; temp = structure.next(temp)){ + new_structure.addLast(temp); + } + new_structure.addLast(temp); + } + + } + + @Override + public boolean hasNext() { + if (pos == null){ + if (new_structure.size() <= 0){ + return false; + } else { + return true; + } + + } else { + + try { + new_structure.next(pos); + return true; + } + catch (BoundaryViolationException err){ + return false; + + } + + + } + } + + @Override + public Position next() throws NoSuchElementException { + if (hasNext()){ + if (pos == null){ + pos = new_structure.first(); + }else { + pos = new_structure.next(pos); + } + return pos.element(); + } else{ + throw new NoSuchElementException(); + } + } + + @Override + public void remove() { + throw new UnsupportedOperationException (); + + } + + NodePositionList> new_structure; + Position> pos = null; + + + } } diff --git a/position/PositionList.java b/position/PositionList.java index c9ccb65..e8886d3 100644 --- a/position/PositionList.java +++ b/position/PositionList.java @@ -37,5 +37,8 @@ public interface PositionList extends Iterable { public E set(Position p, E e) throws InvalidPositionException; + public Iterator> positions(); + + } diff --git a/priorityqueue/MyEntry.java b/priorityqueue/MyEntry.java new file mode 100644 index 0000000..fe29598 --- /dev/null +++ b/priorityqueue/MyEntry.java @@ -0,0 +1,37 @@ +package priorityqueue; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 01/04/2014 + * Time: 21:00 + */ +public class MyEntry implements Entry { + + public MyEntry(K a, V b){ + key = a; + value = b; + } + + + + @Override + public K getKey() { + return key; + } + + @Override + public V getValue() { + return value; + } + + + @Override + public String toString() { + return ("" + key + " - " + value); + } + + private K key; + private V value; + +} diff --git a/priorityqueue/SortedListPriorityQueue.java b/priorityqueue/SortedListPriorityQueue.java new file mode 100644 index 0000000..addbda7 --- /dev/null +++ b/priorityqueue/SortedListPriorityQueue.java @@ -0,0 +1,88 @@ +package priorityqueue; + +import exceptions.EmptyPriorityQueueException; +import position.NodePositionList; +import position.Position; +import utility.DefaultComparator; + +import java.security.InvalidKeyException; +import java.util.Comparator; +import java.util.Iterator; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 01/04/2014 + * Time: 20:57 + */ +public class SortedListPriorityQueue implements PriorityQueue { + + private NodePositionList> data = new NodePositionList>(); + private Comparator c; + + public SortedListPriorityQueue(){ + c = new DefaultComparator(); + } + public SortedListPriorityQueue(Comparator comp){ + c = comp; + } + + + @Override + public int size() { + return data.size(); + } + + @Override + public boolean isEmpty() { + return data.isEmpty(); + } + + @Override + public Entry min() throws EmptyPriorityQueueException { + if (isEmpty()){ + throw new EmptyPriorityQueueException(); + } else { + return data.first().element(); + } + } + + @Override + public Entry insert(K key, V value) throws InvalidKeyException { + try { + MyEntry t = new MyEntry(key, value); + if (data.size() == 0) { + data.addFirst(t); + return t; + } else { + Iterator>> itp = data.positions(); + int status; + Position> temp_pos = null; + for (; itp.hasNext(); ) { + temp_pos = itp.next(); + status = c.compare(temp_pos.element().getKey(), key); + if (status > 0) + data.addBefore(temp_pos, t); + return t; + } + data.addLast(t); + return t; + + } + } + catch (ClassCastException err){ + throw new InvalidKeyException(); + } + } + + @Override + public Entry removeMin() throws EmptyPriorityQueueException { + + if (isEmpty()){ + throw new EmptyPriorityQueueException(); + } else { + return data.remove(data.first()); + } + } + +} diff --git a/sequence/ArraySequence.java b/sequence/ArraySequence.java index c485fc7..1838616 100644 --- a/sequence/ArraySequence.java +++ b/sequence/ArraySequence.java @@ -290,4 +290,9 @@ public class ArraySequence implements Sequence { public Iterator iterator() { return null; } + + @Override + public Iterator> positions() { + return null; + } } diff --git a/sequence/ArraySequenceFake.java b/sequence/ArraySequenceFake.java deleted file mode 100644 index 728663c..0000000 --- a/sequence/ArraySequenceFake.java +++ /dev/null @@ -1,228 +0,0 @@ -package sequence; - -import arraylist.ArrayIndexList; -import exceptions.BoundaryViolationException; -import exceptions.EmptyListException; -import exceptions.EmptySequenceException; -import exceptions.InvalidPositionException; -import position.Position; -import sequence.utility.DNodeFake; - -import java.util.Iterator; - -/** - * 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"); - } - } - - @Override - public Iterator iterator() { - return null; - } -} diff --git a/sequence/utility/DNodeFake.java b/sequence/utility/DNodeFake.java deleted file mode 100644 index 6de351d..0000000 --- a/sequence/utility/DNodeFake.java +++ /dev/null @@ -1,51 +0,0 @@ -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 ArraySequenceFake - - -public class DNodeFake implements Position { - private E element = null; - private int index = -1; - - - public DNodeFake(E elem, int in_idex) { - - element = elem; - index = in_idex; - } - public DNodeFake(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/utility/DefaultComparator.java b/utility/DefaultComparator.java new file mode 100644 index 0000000..b51857e --- /dev/null +++ b/utility/DefaultComparator.java @@ -0,0 +1,15 @@ +package utility; + +import java.util.Comparator; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 01/04/2014 + * Time: 20:56 + */ +public class DefaultComparator implements Comparator { + public int compare(E a, E b)throws ClassCastException { + return ((Comparable)a).compareTo(b); + } +} \ No newline at end of file