From 0956f6b92f9e7dc72ba3cdbd687a2a51b288cb61 Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Tue, 8 Apr 2014 18:18:58 +0200 Subject: [PATCH] fixes --- com/xgiovio/NodePositionListTest.java | 5 + position/NodePositionList.java | 120 +++++++++++-------- priorityqueue/SortedListPriorityQueue.java | 2 +- priorityqueue/UnsortedListPriorityQueue.java | 4 +- 4 files changed, 77 insertions(+), 54 deletions(-) diff --git a/com/xgiovio/NodePositionListTest.java b/com/xgiovio/NodePositionListTest.java index 80dd6c5..7c5c140 100644 --- a/com/xgiovio/NodePositionListTest.java +++ b/com/xgiovio/NodePositionListTest.java @@ -63,6 +63,11 @@ public class NodePositionListTest { System.out.print(it.next()); System.out.print(it.hasNext()); + System.out.print(a.positions().iterator()); + System.out.print(it.hasNext()); + System.out.print(((Position)(it.next().); + + diff --git a/position/NodePositionList.java b/position/NodePositionList.java index a508f67..826c58c 100644 --- a/position/NodePositionList.java +++ b/position/NodePositionList.java @@ -198,73 +198,91 @@ public class NodePositionList implements PositionList { } + // inception style. a dream in a dream in a dream - public Iterator> positions() { - return new MyPositionsIterator(this); + public Iterable> positions() { + return new MyPositionsIterable(this); } - class MyPositionsIterator implements Iterator>{ + class MyPositionsIterable implements Iterable>{ - public MyPositionsIterator (NodePositionList structure){ + private NodePositionList base = null; - new_structure = new NodePositionList>(); - if (structure.size() != 0){ - Position temp; - for (temp = structure.first() ; temp!= structure.last() ; temp = structure.next(temp)){ + public MyPositionsIterable(NodePositionList in){ + base = in; + } + + @Override + public Iterator> iterator() { + return new MyPositionsIterator(base); + } + + 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); } - 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()){ + @Override + public boolean hasNext() { if (pos == null){ - pos = new_structure.first(); - }else { - pos = new_structure.next(pos); + if (new_structure.size() <= 0){ + return false; + } else { + return true; + } + + } else { + + try { + new_structure.next(pos); + return true; + } + catch (BoundaryViolationException err){ + return false; + + } + + } - return pos.element(); - } else{ - throw new NoSuchElementException(); } + + @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; } - @Override - public void remove() { - throw new UnsupportedOperationException (); - - } - - NodePositionList> new_structure; - Position> pos = null; } + + // end inception style + } diff --git a/priorityqueue/SortedListPriorityQueue.java b/priorityqueue/SortedListPriorityQueue.java index 693363b..12dae67 100644 --- a/priorityqueue/SortedListPriorityQueue.java +++ b/priorityqueue/SortedListPriorityQueue.java @@ -55,7 +55,7 @@ public class SortedListPriorityQueue implements PriorityQueue { data.addFirst(t); return t; } else { - Iterator>> itp = data.positions(); + Iterator>> itp = data.positions().iterator(); int status; Position> temp_pos = null; for (; itp.hasNext(); ) { diff --git a/priorityqueue/UnsortedListPriorityQueue.java b/priorityqueue/UnsortedListPriorityQueue.java index 1ff38f1..318c0e5 100644 --- a/priorityqueue/UnsortedListPriorityQueue.java +++ b/priorityqueue/UnsortedListPriorityQueue.java @@ -43,7 +43,7 @@ public class UnsortedListPriorityQueue implements PriorityQueue { if (isEmpty()){ throw new EmptyPriorityQueueException(); } else { - Iterator>> itp = data.positions(); + Iterator>> itp = data.positions().iterator(); int status; Position> min = null; Position> temp_pos = null; @@ -83,7 +83,7 @@ public class UnsortedListPriorityQueue implements PriorityQueue { if (isEmpty()){ throw new EmptyPriorityQueueException(); } else { - Iterator>> itp = data.positions(); + Iterator>> itp = data.positions().iterator(); int status; Position> min = null; Position> temp_pos = null;