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

@@ -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();