Sie sind auf Seite 1von 2

class IListIterator<T> implements Iterator<T> {

IList<T> items;
IListIterator(IList<T> items) {
this.items = items;
}
public boolean hasNext() {
...
}
public T next() {
...
}
public void remove() {
throw new UnsupportedOperationException("Don't do this!");
}
}
As with the previous iterator, we have a next item when we are currently pointing at aCons node:

// In IListIterator
public boolean hasNext() {
return this.items.isCons();
}
And likewise we can return the item in the current Cons for our next value. To update the iterator, we advance items to refer to the current Cons’s rest:

// In IListIterator
public T next() {
ConsList<T> itemsAsCons = this.items.asCons();
T answer = itemsAsCons.first;
this.items = itemsAsCons.rest;
return answer;
}

We can make our ILists be Iterable, too:

// Declare that every IList is an Iterable:


interface IList<T> extends Iterable<T> {
... everything as before ...
}
class ConsList<T> implements IList<T> {
... everything as before ...
public Iterator<T> iterator() {
return new IListIterator<T>(this);
}
}
class MtList<T> implements IList<T> {
... everything as before ...
public Iterator<T> iterator() {
return new IListIterator<T>(this);
}
}
And with those last few definitions, we can now use ILists in for-each loops, exactly as we could with ArrayLists.

for (T item : myList) {


// iterates forward through myList
...
}

for (int idxVar = start; idxVar < end; idxVar = idxVar + 1) {


...body...
}
// An IShapeVisitor is a function over IShapes
interface IShapeVisitor<R> extends IFunc<IShape, R> {
R visitCircle(Circle c);
R visitSquare(Square s);
R visitRect(Rect r);
}

// ShapeArea is a function object over IShapes that computes their area


class ShapeArea implements IShapeVisitor<Double> {
// Everything from the IShapeVisitor interface:
public Double visitCircle(Circle c) { return Math.PI * c.radius * c.radius; }
public Double visitSquare(Square s) { return s.side * s.side; }
public Double visitRect(Rect r) { return r.w * r.h; }

// Everything from the IFunc interface:


public Double apply(IShape s) { return s.accept(this); }
}
interface IAT {
<R> R accept(IATVisitor<R> visitor);

class Unknown implements IAT {


public <R> R accept(IATVisitor<R> visitor) {
return visitor.visitUnknown(this);
}
}

class Person implements IAT {

String name;
IAT mom;
IAT dad;

Person(String name, IAT mom, IAT dad) {


this.name = name;
this.mom = mom;
this.dad = dad;
}

public <R> R accept(IATVisitor<R> visitor) {


return visitor.visitPerson(this);
}
}

interface IATVisitor<R> {
R visitUnknown(Unknown unknown);
R visitPerson(Person person);
}

class NameVisitor implements IATVisitor<IList<String>> {

public IList<String> visitUnknown(Unknown unknown) {


return new Empty<String>();
}

public IList<String> visitPerson(Person person) {


return new Cons<String>(person.name,
person.dad.accept(new NameVisitor()).append(person.mom.accept(new NameVisitor())));
}

// In Graph
boolean bfs(Vertex from, Vertex to) {
return searchHelp(from, to, new Queue<Vertex>());
}
boolean dfs(Vertex from, Vertex to) {
return searchHelp(from, to, new Stack<Vertex>());
}
boolean searchHelp(Vertex from, Vertex to, ICollection<Vertex> worklist) {
Deque<Vertex> alreadySeen = new Deque<Vertex>();

// Initialize the worklist with the from vertex


worklist.add(from);
// As long as the worklist isn't empty...
while (!worklist.isEmpty()) {
Vertex next = worklist.remove();
if (next.equals(to)) {
return true; // Success!
}
else if (alreadySeen.contains(next)) {
// do nothing: we've already seen this one
}
else {
// add all the neighbors of next to the worklist for further processing
for (Edge e : next.outEdges) {
worklist.add(e.to);
}
// add next to alreadySeen, since we're done with it
alreadySeen.addAtHead(next);
}
}
// We haven't found the to vertex, and there are no more to try
return false;
}

Das könnte Ihnen auch gefallen