From 9e2382834282da169e9a120993ae142904ce1dbb Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Sun, 6 Apr 2014 21:55:57 +0200 Subject: [PATCH] IndexListIterator che implementa Iterator mediante un array list. Implementazione di Iterator mediante cursore : ElementIterator per PositionList. Aggiunti alcuni metodi e spostato le classi IndexListIterator,ElementIterator nel package Iterator. --- arraylist/ArrayIndexList.java | 16 ++++++ arraylist/IndexList.java | 2 +- com/xgiovio/ArrayIndexListTest.java | 10 ++++ com/xgiovio/ArraySequenceTest.java | 10 ++++ com/xgiovio/NodePositionListTest.java | 29 ++++++++++- iterator/ElementIterator.java | 74 +++++++++++++++++++++++++++ iterator/IndexListIterator.java | 55 ++++++++++++++++++++ position/NodePositionList.java | 68 ++---------------------- position/PositionList.java | 3 -- sequence/ArraySequence.java | 13 +++-- 10 files changed, 205 insertions(+), 75 deletions(-) create mode 100644 iterator/ElementIterator.java create mode 100644 iterator/IndexListIterator.java diff --git a/arraylist/ArrayIndexList.java b/arraylist/ArrayIndexList.java index 0b83aa8..9e94aaf 100644 --- a/arraylist/ArrayIndexList.java +++ b/arraylist/ArrayIndexList.java @@ -1,5 +1,9 @@ package arraylist; +import iterator.IndexListIterator; + +import java.util.Iterator; + /** * Created with xgiovio.macbookair. * User: xgiovio @@ -93,4 +97,16 @@ package arraylist; return to_return; } + + @Override + ///////////////// implemented used a generic IndexListIterator + public Iterator iterator() { + + E[] temp = (E[])new Object[this.size()]; + for (int i = 0 ;i < this.size(); i++){ + temp[i] = this.get(i); + } + return new IndexListIterator(temp); + + } } diff --git a/arraylist/IndexList.java b/arraylist/IndexList.java index bf08f34..7525bed 100644 --- a/arraylist/IndexList.java +++ b/arraylist/IndexList.java @@ -8,7 +8,7 @@ package arraylist; */ //Copyright (c) 2003 Brown University, Providence, RI //Additional modifications and methods by xgiovio -public interface IndexList { +public interface IndexList extends Iterable { public E remove(int i) throws IndexOutOfBoundsException; diff --git a/com/xgiovio/ArrayIndexListTest.java b/com/xgiovio/ArrayIndexListTest.java index 5223fb6..6af9441 100644 --- a/com/xgiovio/ArrayIndexListTest.java +++ b/com/xgiovio/ArrayIndexListTest.java @@ -6,6 +6,8 @@ import sequence.NodeSequence; import sequence.Sequence; import stack.NodeStack; +import java.util.Iterator; + /** * Created with xgiovio.macbookair. * User: xgiovio @@ -27,6 +29,14 @@ public class ArrayIndexListTest { System.out.print(a); + Iterator it = a.iterator(); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); diff --git a/com/xgiovio/ArraySequenceTest.java b/com/xgiovio/ArraySequenceTest.java index 0499158..b187b53 100644 --- a/com/xgiovio/ArraySequenceTest.java +++ b/com/xgiovio/ArraySequenceTest.java @@ -4,6 +4,8 @@ import general_utility.test_object; import position.Position; import sequence.ArraySequence; +import java.util.Iterator; + /** * Created with xgiovio.macbookair. * User: xgiovio @@ -22,6 +24,14 @@ public class ArraySequenceTest { System.out.print (a.indexOf( a.next( a.first()))); + Iterator it = a.iterator(); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); + + } diff --git a/com/xgiovio/NodePositionListTest.java b/com/xgiovio/NodePositionListTest.java index 57ee315..80dd6c5 100644 --- a/com/xgiovio/NodePositionListTest.java +++ b/com/xgiovio/NodePositionListTest.java @@ -32,11 +32,38 @@ public class NodePositionListTest { a.addLast(new Integer(4)); - global.cancellaDuplicati(a); System.out.print(a); + Iterator it = a.iterator(); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); + System.out.print(it.next()); + System.out.print(it.hasNext()); + + diff --git a/iterator/ElementIterator.java b/iterator/ElementIterator.java new file mode 100644 index 0000000..bafcf06 --- /dev/null +++ b/iterator/ElementIterator.java @@ -0,0 +1,74 @@ +package iterator; + +import exceptions.BoundaryViolationException; +import position.Position; +import position.PositionList; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 06/04/2014 + * Time: 21:06 + */ + +//iterator implementation with cursor for PositionList data structures. + +public class ElementIterator implements Iterator { + + public ElementIterator (PositionList structure){ + + new_structure = structure; + + } + + @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 E 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 (); + } + + PositionList new_structure = null; + Position pos = null; + + +} \ No newline at end of file diff --git a/iterator/IndexListIterator.java b/iterator/IndexListIterator.java new file mode 100644 index 0000000..d01e124 --- /dev/null +++ b/iterator/IndexListIterator.java @@ -0,0 +1,55 @@ +package iterator; + +import arraylist.ArrayIndexList; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 06/04/2014 + * Time: 21:13 + */ + +// generic iterator list creation using an arraylist. the class calling this metod should pass an array of E + +public class IndexListIterator implements Iterator { + + public IndexListIterator (E[] in_elements){ + for (int i = 0;i< in_elements.length;i++){ + data.add(i,in_elements[i]); + } + } + + @Override + public boolean hasNext() { + + if (pos <= data.size() -2){ + return true; + }else { + return false; + } + + } + + @Override + public E next() throws NoSuchElementException { + if (hasNext()){ + pos++; + return data.get(pos); + }else { + throw new NoSuchElementException(); + } + } + + @Override + public void remove() { + throw new UnsupportedOperationException (); + } + + private ArrayIndexList data = new ArrayIndexList(); + private int pos = -1; + + +} diff --git a/position/NodePositionList.java b/position/NodePositionList.java index 4f9dc55..a508f67 100644 --- a/position/NodePositionList.java +++ b/position/NodePositionList.java @@ -1,7 +1,7 @@ package position; import exceptions.*; +import iterator.ElementIterator; import position.utility.DNode; -import sun.org.mozilla.javascript.internal.ObjToIntMap; import java.util.Iterator; import java.util.NoSuchElementException; @@ -192,75 +192,13 @@ public class NodePositionList implements PositionList { @Override + // iterator created using specific class for PositionList data structures : ElementIterator public Iterator iterator() { - return new MyIterator(this); + return new ElementIterator(this); } - class MyIterator implements Iterator{ - - public MyIterator (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.element()); - } - new_structure.addLast(temp.element()); - } - - } - - @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 E 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 Iterator> positions() { return new MyPositionsIterator(this); } diff --git a/position/PositionList.java b/position/PositionList.java index e8886d3..c9ccb65 100644 --- a/position/PositionList.java +++ b/position/PositionList.java @@ -37,8 +37,5 @@ public interface PositionList extends Iterable { public E set(Position p, E e) throws InvalidPositionException; - public Iterator> positions(); - - } diff --git a/sequence/ArraySequence.java b/sequence/ArraySequence.java index 1838616..f390a10 100644 --- a/sequence/ArraySequence.java +++ b/sequence/ArraySequence.java @@ -4,6 +4,7 @@ import exceptions.BoundaryViolationException; import exceptions.EmptyListException; import exceptions.EmptySequenceException; import exceptions.InvalidPositionException; +import iterator.IndexListIterator; import position.Position; import sequence.utility.ArrayPosition; @@ -287,12 +288,14 @@ public class ArraySequence implements Sequence { @Override + ///////////////// implemented used a generic IndexListIterator public Iterator iterator() { - return null; - } - @Override - public Iterator> positions() { - return null; + E[] temp = (E[])new Object[this.size()]; + for (int i = 0 ;i < this.size(); i++){ + temp[i] = this.get(i); + } + return new IndexListIterator(temp); + } }