Implementato Iterator in NodePositionList e aggiunto solo i prototipi nelle altre classe che implementano Sequence

This commit is contained in:
2014-04-01 15:12:42 +02:00
parent c8254693f9
commit 56672f3b94
5 changed files with 102 additions and 3 deletions

View File

@@ -4,6 +4,8 @@ import arraylist.ArrayIndexList;
import general_utility.test_object;
import position.NodePositionList;
import java.util.Iterator;
/**
* Created with xgiovio.macbookair.
* User: xgiovio
@@ -25,6 +27,18 @@ public class NodePositionListTest {
a.reverse();
System.out.print(a);
Iterator<test_object> 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());

View File

@@ -1,6 +1,10 @@
package position;
import exceptions.*;
import position.utility.DNode;
import sun.org.mozilla.javascript.internal.ObjToIntMap;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Realization of a PositionList using a doubly-linked list of nodes.
@@ -187,4 +191,74 @@ public class NodePositionList<E> implements PositionList<E> {
}
@Override
public Iterator<E> iterator() {
return new MyIterator(this);
}
class MyIterator implements Iterator<E>{
public MyIterator (NodePositionList<E> structure){
new_structure = new NodePositionList<E>();
if (structure.size() != 0){
Position<E> 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() {
}
NodePositionList<E> new_structure;
Position<E> pos = null;
}
}

View File

@@ -9,7 +9,7 @@ import exceptions.*;
//Copyright (c) 2003 Brown University, Providence, RI
//Additional modifications and methods by xgiovio
public interface PositionList<E> {
public interface PositionList<E> extends Iterable<E> {
public int size();

View File

@@ -7,6 +7,8 @@ import exceptions.InvalidPositionException;
import position.Position;
import sequence.utility.ArrayPosition;
import java.util.Iterator;
/**
* Created with xgiovio.macbookair.
* User: xgiovio
@@ -283,4 +285,9 @@ public class ArraySequence<E> implements Sequence<E> {
}
}
@Override
public Iterator<E> iterator() {
return null;
}
}

View File

@@ -8,6 +8,8 @@ import exceptions.InvalidPositionException;
import position.Position;
import sequence.utility.DNodeFake;
import java.util.Iterator;
/**
* Created with MONSTER.
* User: xgiovio
@@ -219,6 +221,8 @@ public class ArraySequenceFake<E> extends ArrayIndexList<E> implements Sequence<
}
}
@Override
public Iterator<E> iterator() {
return null;
}
}