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.
This commit is contained in:
@@ -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<E> iterator() {
|
||||
|
||||
E[] temp = (E[])new Object[this.size()];
|
||||
for (int i = 0 ;i < this.size(); i++){
|
||||
temp[i] = this.get(i);
|
||||
}
|
||||
return new IndexListIterator<E>(temp);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ package arraylist;
|
||||
*/
|
||||
//Copyright (c) 2003 Brown University, Providence, RI
|
||||
//Additional modifications and methods by xgiovio
|
||||
public interface IndexList<E> {
|
||||
public interface IndexList<E> extends Iterable<E> {
|
||||
|
||||
|
||||
public E remove(int i) throws IndexOutOfBoundsException;
|
||||
|
||||
@@ -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<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());
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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<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());
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -32,11 +32,38 @@ public class NodePositionListTest {
|
||||
a.addLast(new Integer(4));
|
||||
|
||||
|
||||
global.cancellaDuplicati(a);
|
||||
|
||||
|
||||
System.out.print(a);
|
||||
|
||||
Iterator<Integer> 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());
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
74
iterator/ElementIterator.java
Normal file
74
iterator/ElementIterator.java
Normal file
@@ -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<E> implements Iterator<E> {
|
||||
|
||||
public ElementIterator (PositionList<E> 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<E> new_structure = null;
|
||||
Position<E> pos = null;
|
||||
|
||||
|
||||
}
|
||||
55
iterator/IndexListIterator.java
Normal file
55
iterator/IndexListIterator.java
Normal file
@@ -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<E> implements Iterator<E> {
|
||||
|
||||
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<E> data = new ArrayIndexList<E>();
|
||||
private int pos = -1;
|
||||
|
||||
|
||||
}
|
||||
@@ -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<E> implements PositionList<E> {
|
||||
|
||||
|
||||
@Override
|
||||
// iterator created using specific class for PositionList data structures : ElementIterator
|
||||
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;
|
||||
|
||||
return new ElementIterator<E>(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@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<E> new_structure;
|
||||
Position<E> pos = null;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Position<E>> positions() {
|
||||
return new MyPositionsIterator(this);
|
||||
}
|
||||
|
||||
@@ -37,8 +37,5 @@ public interface PositionList<E> extends Iterable<E> {
|
||||
public E set(Position<E> p, E e) throws InvalidPositionException;
|
||||
|
||||
|
||||
public Iterator<Position<E>> positions();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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<E> implements Sequence<E> {
|
||||
|
||||
|
||||
@Override
|
||||
///////////////// implemented used a generic IndexListIterator
|
||||
public Iterator<E> iterator() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Position<E>> 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<E>(temp);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user