|
|
@ -23,6 +23,7 @@
|
|
|
|
package net.yacy.kelondro.util;
|
|
|
|
package net.yacy.kelondro.util;
|
|
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Array;
|
|
|
|
import java.lang.reflect.Array;
|
|
|
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.ConcurrentModificationException;
|
|
|
|
import java.util.ConcurrentModificationException;
|
|
|
|
|
|
|
|
|
|
|
|
import net.yacy.cora.order.CloneableIterator;
|
|
|
|
import net.yacy.cora.order.CloneableIterator;
|
|
|
@ -32,32 +33,37 @@ public class StackIterator<E> implements CloneableIterator<E> {
|
|
|
|
|
|
|
|
|
|
|
|
private final CloneableIterator<E> a, b;
|
|
|
|
private final CloneableIterator<E> a, b;
|
|
|
|
private E na, nb;
|
|
|
|
private E na, nb;
|
|
|
|
|
|
|
|
private final Comparator<E> c;
|
|
|
|
|
|
|
|
|
|
|
|
public StackIterator(
|
|
|
|
public StackIterator(
|
|
|
|
final CloneableIterator<E> a,
|
|
|
|
final CloneableIterator<E> a,
|
|
|
|
final CloneableIterator<E> b) {
|
|
|
|
final CloneableIterator<E> b,
|
|
|
|
|
|
|
|
final Comparator<E> c, final boolean up) {
|
|
|
|
// this works currently only for String-type key iterations
|
|
|
|
// this works currently only for String-type key iterations
|
|
|
|
this.a = a;
|
|
|
|
this.a = a;
|
|
|
|
this.b = b;
|
|
|
|
this.b = b;
|
|
|
|
|
|
|
|
this.c = up ? c : new Comparator<E>() {
|
|
|
|
|
|
|
|
@Override public int compare(E o1, E o2) {return -c.compare(o1, o2);}
|
|
|
|
|
|
|
|
};
|
|
|
|
nexta();
|
|
|
|
nexta();
|
|
|
|
nextb();
|
|
|
|
nextb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public StackIterator<E> clone(final Object modifier) {
|
|
|
|
public StackIterator<E> clone(final Object modifier) {
|
|
|
|
return new StackIterator<E>(this.a.clone(modifier), this.b.clone(modifier));
|
|
|
|
return new StackIterator<E>(this.a.clone(modifier), this.b.clone(modifier), this.c, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void nexta() {
|
|
|
|
private void nexta() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if ((this.a != null) && (this.a.hasNext())) this.na = this.a.next(); else this.na = null;
|
|
|
|
if (this.a != null && this.a.hasNext()) this.na = this.a.next(); else this.na = null;
|
|
|
|
} catch (final ConcurrentModificationException e) {
|
|
|
|
} catch (final ConcurrentModificationException e) {
|
|
|
|
this.na = null;
|
|
|
|
this.na = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private void nextb() {
|
|
|
|
private void nextb() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if ((this.b != null) && (this.b.hasNext())) this.nb = this.b.next(); else this.nb = null;
|
|
|
|
if (this.b != null && this.b.hasNext()) this.nb = this.b.next(); else this.nb = null;
|
|
|
|
} catch (final ConcurrentModificationException e) {
|
|
|
|
} catch (final ConcurrentModificationException e) {
|
|
|
|
this.nb = null;
|
|
|
|
this.nb = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -65,7 +71,7 @@ public class StackIterator<E> implements CloneableIterator<E> {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public boolean hasNext() {
|
|
|
|
public boolean hasNext() {
|
|
|
|
return (this.na != null) || (this.nb != null);
|
|
|
|
return this.na != null || this.nb != null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -81,9 +87,14 @@ public class StackIterator<E> implements CloneableIterator<E> {
|
|
|
|
nexta();
|
|
|
|
nexta();
|
|
|
|
return s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// just stack the Objects
|
|
|
|
// return the object in right order
|
|
|
|
s = this.na;
|
|
|
|
if (this.c == null || this.c.compare(this.na, this.nb) <= 0) {
|
|
|
|
nexta();
|
|
|
|
s = this.na;
|
|
|
|
|
|
|
|
nexta();
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
s = this.nb;
|
|
|
|
|
|
|
|
nextb();
|
|
|
|
return s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -93,7 +104,7 @@ public class StackIterator<E> implements CloneableIterator<E> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static <A> CloneableIterator<A> stack(final CloneableIterator<A>[] iterators) {
|
|
|
|
public static <A> CloneableIterator<A> stack(final CloneableIterator<A>[] iterators, final Comparator<A> c, final boolean up) {
|
|
|
|
// this extends the ability to combine two iterators
|
|
|
|
// this extends the ability to combine two iterators
|
|
|
|
// to the ability of combining a set of iterators
|
|
|
|
// to the ability of combining a set of iterators
|
|
|
|
if (iterators == null || iterators.length == 0) return new CloneableIterator<A>() {
|
|
|
|
if (iterators == null || iterators.length == 0) return new CloneableIterator<A>() {
|
|
|
@ -122,13 +133,13 @@ public class StackIterator<E> implements CloneableIterator<E> {
|
|
|
|
if (iterators.length == 2) {
|
|
|
|
if (iterators.length == 2) {
|
|
|
|
if (iterators[0] == null) return iterators[1];
|
|
|
|
if (iterators[0] == null) return iterators[1];
|
|
|
|
if (iterators[1] == null) return iterators[0];
|
|
|
|
if (iterators[1] == null) return iterators[0];
|
|
|
|
return new StackIterator<A>(iterators[0], iterators[1]);
|
|
|
|
return new StackIterator<A>(iterators[0], iterators[1], c, up);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CloneableIterator<A> a = iterators[0];
|
|
|
|
CloneableIterator<A> a = iterators[0];
|
|
|
|
final CloneableIterator<A>[] iterators0 = (CloneableIterator<A>[]) Array.newInstance(CloneableIterator.class, iterators.length - 1);
|
|
|
|
final CloneableIterator<A>[] iterators0 = (CloneableIterator<A>[]) Array.newInstance(CloneableIterator.class, iterators.length - 1);
|
|
|
|
System.arraycopy(iterators, 1, iterators0, 0, iterators.length - 1);
|
|
|
|
System.arraycopy(iterators, 1, iterators0, 0, iterators.length - 1);
|
|
|
|
if (a == null) return stack(iterators0);
|
|
|
|
if (a == null) return stack(iterators0, c, up);
|
|
|
|
return new StackIterator<A>(a, stack(iterators0));
|
|
|
|
return new StackIterator<A>(a, stack(iterators0, c, up), c, up);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|