Simplified primitive iterators.
This commit is contained in:
@@ -81,9 +81,9 @@ public final class ByteRange implements Range<Byte>, ByteIterable {
|
||||
|
||||
public ByteIterator step(int step) {
|
||||
if (step < 0)
|
||||
return new ByteIteratorImpl(getEnd(), -count, -step);
|
||||
return new ByteIteratorImpl(getEnd(), getStart(), step);
|
||||
else
|
||||
return new ByteIteratorImpl(start, count, step);
|
||||
return new ByteIteratorImpl(getStart(), getEnd(), step);
|
||||
}
|
||||
|
||||
public byte getStart() {
|
||||
@@ -100,46 +100,30 @@ public final class ByteRange implements Range<Byte>, ByteIterable {
|
||||
|
||||
@Override
|
||||
public ByteIterator iterator() {
|
||||
return new ByteIteratorImpl(start, count, 1);
|
||||
return new ByteIteratorImpl(getStart(), getEnd(), 1);
|
||||
}
|
||||
|
||||
private static class ByteIteratorImpl extends ByteIterator {
|
||||
private final int step;
|
||||
private byte cur;
|
||||
private int count;
|
||||
private byte next;
|
||||
private final byte end;
|
||||
private final int increment;
|
||||
|
||||
private final boolean reversed;
|
||||
|
||||
public ByteIteratorImpl(byte startValue, int count, int step) {
|
||||
cur = startValue;
|
||||
this.step = step;
|
||||
if (count < 0) {
|
||||
reversed = true;
|
||||
count = -count;
|
||||
startValue += count;
|
||||
}
|
||||
else {
|
||||
reversed = false;
|
||||
}
|
||||
this.count = count;
|
||||
public ByteIteratorImpl(byte start, byte end, int increment) {
|
||||
this.next = start;
|
||||
this.end = end;
|
||||
this.increment = increment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return count > 0;
|
||||
return increment > 0 ? next <= end : next >= end;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte nextByte() {
|
||||
count -= step;
|
||||
if (reversed) {
|
||||
cur -= step;
|
||||
return (byte) (cur + step);
|
||||
}
|
||||
else {
|
||||
cur += step;
|
||||
return (byte) (cur - step);
|
||||
}
|
||||
byte value = next;
|
||||
next += increment;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,9 +81,9 @@ public final class CharRange implements Range<Character>, CharIterable {
|
||||
|
||||
public CharIterator step(int step) {
|
||||
if (step < 0)
|
||||
return new CharIteratorImpl(getEnd(), -count, -step);
|
||||
return new CharIteratorImpl(getEnd(), getStart(), step);
|
||||
else
|
||||
return new CharIteratorImpl(start, count, step);
|
||||
return new CharIteratorImpl(getStart(), getEnd(), step);
|
||||
}
|
||||
|
||||
public char getStart() {
|
||||
@@ -100,46 +100,30 @@ public final class CharRange implements Range<Character>, CharIterable {
|
||||
|
||||
@Override
|
||||
public CharIterator iterator() {
|
||||
return new CharIteratorImpl(start, count, 1);
|
||||
return new CharIteratorImpl(getStart(), getEnd(), 1);
|
||||
}
|
||||
|
||||
private static class CharIteratorImpl extends CharIterator {
|
||||
private final int step;
|
||||
private char cur;
|
||||
private int count;
|
||||
private char next;
|
||||
private final char end;
|
||||
private final int increment;
|
||||
|
||||
private final boolean reversed;
|
||||
|
||||
public CharIteratorImpl(char startValue, int count, int step) {
|
||||
cur = startValue;
|
||||
this.step = step;
|
||||
if (count < 0) {
|
||||
reversed = true;
|
||||
count = -count;
|
||||
startValue += count;
|
||||
}
|
||||
else {
|
||||
reversed = false;
|
||||
}
|
||||
this.count = count;
|
||||
public CharIteratorImpl(char start, char end, int increment) {
|
||||
this.next = start;
|
||||
this.end = end;
|
||||
this.increment = increment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return count > 0;
|
||||
return increment > 0 ? next <= end : next >= end;
|
||||
}
|
||||
|
||||
@Override
|
||||
public char nextChar() {
|
||||
count -= step;
|
||||
if (reversed) {
|
||||
cur -= step;
|
||||
return (char) (cur + step);
|
||||
}
|
||||
else {
|
||||
cur += step;
|
||||
return (char) (cur - step);
|
||||
}
|
||||
char value = next;
|
||||
next += increment;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,9 +79,9 @@ public final class DoubleRange implements Range<Double> {
|
||||
|
||||
public DoubleIterator step(double step) {
|
||||
if (step < 0)
|
||||
return new DoubleIteratorImpl(getEnd(), -size, -step);
|
||||
return new DoubleIteratorImpl(getEnd(), getStart(), step);
|
||||
else
|
||||
return new DoubleIteratorImpl(start, size, step);
|
||||
return new DoubleIteratorImpl(getStart(), getEnd(), step);
|
||||
}
|
||||
|
||||
public double getStart() {
|
||||
@@ -97,44 +97,26 @@ public final class DoubleRange implements Range<Double> {
|
||||
}
|
||||
|
||||
private static class DoubleIteratorImpl extends DoubleIterator {
|
||||
private final double step;
|
||||
private double next;
|
||||
private final double end;
|
||||
private double cur;
|
||||
private final double increment;
|
||||
|
||||
private final boolean reversed;
|
||||
|
||||
public DoubleIteratorImpl(double startValue, double size, double step) {
|
||||
cur = startValue;
|
||||
this.step = step;
|
||||
if (size < 0) {
|
||||
reversed = true;
|
||||
end = startValue-size;
|
||||
startValue -= size;
|
||||
}
|
||||
else {
|
||||
reversed = false;
|
||||
this.end = startValue + size;
|
||||
}
|
||||
public DoubleIteratorImpl(double start, double end, double increment) {
|
||||
this.next = start;
|
||||
this.end = end;
|
||||
this.increment = increment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (reversed)
|
||||
return cur >= end;
|
||||
else
|
||||
return cur <= end;
|
||||
return increment > 0 ? next <= end : next >= end;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double nextDouble() {
|
||||
if (reversed) {
|
||||
cur -= step;
|
||||
return cur + step;
|
||||
}
|
||||
else {
|
||||
cur += step;
|
||||
return cur - step;
|
||||
}
|
||||
double value = next;
|
||||
next += increment;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,9 +75,9 @@ public final class FloatRange implements Range<Float> {
|
||||
|
||||
public FloatIterator step(float step) {
|
||||
if (step < 0)
|
||||
return new FloatIteratorImpl(getEnd(), -size, -step);
|
||||
return new FloatIteratorImpl(getEnd(), getStart(), step);
|
||||
else
|
||||
return new FloatIteratorImpl(start, size, step);
|
||||
return new FloatIteratorImpl(getStart(), getEnd(), step);
|
||||
}
|
||||
|
||||
public float getStart() {
|
||||
@@ -93,44 +93,26 @@ public final class FloatRange implements Range<Float> {
|
||||
}
|
||||
|
||||
private static class FloatIteratorImpl extends FloatIterator {
|
||||
private final float step;
|
||||
private float next;
|
||||
private final float end;
|
||||
private float cur;
|
||||
private final float increment;
|
||||
|
||||
private final boolean reversed;
|
||||
|
||||
public FloatIteratorImpl(float startValue, float size, float step) {
|
||||
cur = startValue;
|
||||
this.step = step;
|
||||
if (size < 0) {
|
||||
reversed = true;
|
||||
end = startValue-size;
|
||||
startValue -= size;
|
||||
}
|
||||
else {
|
||||
reversed = false;
|
||||
this.end = startValue + size;
|
||||
}
|
||||
public FloatIteratorImpl(float start, float end, float increment) {
|
||||
this.next = start;
|
||||
this.end = end;
|
||||
this.increment = increment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (reversed)
|
||||
return cur >= end;
|
||||
else
|
||||
return cur <= end;
|
||||
return increment > 0 ? next <= end : next >= end;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float nextFloat() {
|
||||
if (reversed) {
|
||||
cur -= step;
|
||||
return cur + step;
|
||||
}
|
||||
else {
|
||||
cur += step;
|
||||
return cur - step;
|
||||
}
|
||||
float value = next;
|
||||
next += increment;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,9 +81,9 @@ public final class IntRange implements Range<Integer>, IntIterable {
|
||||
|
||||
public IntIterator step(int step) {
|
||||
if (step < 0)
|
||||
return new IntIteratorImpl(getEnd(), -count, -step);
|
||||
return new IntIteratorImpl(getEnd(), getStart(), step);
|
||||
else
|
||||
return new IntIteratorImpl(start, count, step);
|
||||
return new IntIteratorImpl(getStart(), getEnd(), step);
|
||||
}
|
||||
|
||||
public int getStart() {
|
||||
@@ -100,46 +100,30 @@ public final class IntRange implements Range<Integer>, IntIterable {
|
||||
|
||||
@Override
|
||||
public IntIterator iterator() {
|
||||
return new IntIteratorImpl(start, count, 1);
|
||||
return new IntIteratorImpl(getStart(), getEnd(), 1);
|
||||
}
|
||||
|
||||
private static class IntIteratorImpl extends IntIterator {
|
||||
private int cur;
|
||||
private int step;
|
||||
private int count;
|
||||
private int next;
|
||||
private final int end;
|
||||
private final int increment;
|
||||
|
||||
private final boolean reversed;
|
||||
|
||||
public IntIteratorImpl(int startValue, int count, int step) {
|
||||
cur = startValue;
|
||||
this.step = step;
|
||||
if (count < 0) {
|
||||
reversed = true;
|
||||
count = -count;
|
||||
startValue += count;
|
||||
}
|
||||
else {
|
||||
reversed = false;
|
||||
}
|
||||
this.count = count;
|
||||
public IntIteratorImpl(int start, int end, int increment) {
|
||||
this.next = start;
|
||||
this.end = end;
|
||||
this.increment = increment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return count > 0;
|
||||
return increment > 0 ? next <= end : next >= end;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextInt() {
|
||||
count -= step;
|
||||
if (reversed) {
|
||||
cur -= step;
|
||||
return cur + step;
|
||||
}
|
||||
else {
|
||||
cur += step;
|
||||
return cur - step;
|
||||
}
|
||||
int value = next;
|
||||
next += increment;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,9 +82,9 @@ public final class LongRange implements Range<Long>, LongIterable {
|
||||
|
||||
public LongIterator step(long step) {
|
||||
if (step < 0)
|
||||
return new LongIteratorImpl(getEnd(), -count, -step);
|
||||
return new LongIteratorImpl(getEnd(), getStart(), step);
|
||||
else
|
||||
return new LongIteratorImpl(start, count, step);
|
||||
return new LongIteratorImpl(getStart(), getEnd(), step);
|
||||
}
|
||||
|
||||
public long getStart() {
|
||||
@@ -101,46 +101,30 @@ public final class LongRange implements Range<Long>, LongIterable {
|
||||
|
||||
@Override
|
||||
public LongIterator iterator() {
|
||||
return new LongIteratorImpl(start, count, 1);
|
||||
return new LongIteratorImpl(getStart(), getEnd(), 1);
|
||||
}
|
||||
|
||||
private static class LongIteratorImpl extends LongIterator {
|
||||
private final long step;
|
||||
private long cur;
|
||||
private long count;
|
||||
private long next;
|
||||
private final long end;
|
||||
private final long increment;
|
||||
|
||||
private final boolean reversed;
|
||||
|
||||
public LongIteratorImpl(long startValue, long count, long step) {
|
||||
cur = startValue;
|
||||
this.step = step;
|
||||
if (count < 0) {
|
||||
reversed = true;
|
||||
count = -count;
|
||||
startValue += count;
|
||||
}
|
||||
else {
|
||||
reversed = false;
|
||||
}
|
||||
this.count = count;
|
||||
public LongIteratorImpl(long start, long end, long increment) {
|
||||
this.next = start;
|
||||
this.end = end;
|
||||
this.increment = increment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return count > 0;
|
||||
return increment > 0 ? next <= end : next >= end;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long nextLong() {
|
||||
count -= step;
|
||||
if (reversed) {
|
||||
cur -= step;
|
||||
return (cur + step);
|
||||
}
|
||||
else {
|
||||
cur += step;
|
||||
return (cur - step);
|
||||
}
|
||||
long value = next;
|
||||
next += increment;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,9 +81,9 @@ public final class ShortRange implements Range<Short>, ShortIterable {
|
||||
|
||||
public ShortIterator step(int step) {
|
||||
if (step < 0)
|
||||
return new ShortIteratorImpl(getEnd(), -count, -step);
|
||||
return new ShortIteratorImpl(getEnd(), getStart(), step);
|
||||
else
|
||||
return new ShortIteratorImpl(start, count, step);
|
||||
return new ShortIteratorImpl(getStart(), getEnd(), step);
|
||||
}
|
||||
|
||||
public short getStart() {
|
||||
@@ -100,46 +100,30 @@ public final class ShortRange implements Range<Short>, ShortIterable {
|
||||
|
||||
@Override
|
||||
public ShortIterator iterator() {
|
||||
return new ShortIteratorImpl(start, count, 1);
|
||||
return new ShortIteratorImpl(getStart(), getEnd(), 1);
|
||||
}
|
||||
|
||||
private static class ShortIteratorImpl extends ShortIterator {
|
||||
private final int step;
|
||||
private short cur;
|
||||
private int count;
|
||||
private short next;
|
||||
private final short end;
|
||||
private final int increment;
|
||||
|
||||
private final boolean reversed;
|
||||
|
||||
public ShortIteratorImpl(short startValue, int count, int step) {
|
||||
cur = startValue;
|
||||
this.step = step;
|
||||
if (count < 0) {
|
||||
reversed = true;
|
||||
count = -count;
|
||||
startValue += count;
|
||||
}
|
||||
else {
|
||||
reversed = false;
|
||||
}
|
||||
this.count = count;
|
||||
public ShortIteratorImpl(short start, short end, int increment) {
|
||||
this.next = start;
|
||||
this.end = end;
|
||||
this.increment = increment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return count > 0;
|
||||
return increment > 0 ? next <= end : next >= end;
|
||||
}
|
||||
|
||||
@Override
|
||||
public short nextShort() {
|
||||
count -= step;
|
||||
if (reversed) {
|
||||
cur -= step;
|
||||
return (short) (cur + step);
|
||||
}
|
||||
else {
|
||||
cur += step;
|
||||
return (short) (cur - step);
|
||||
}
|
||||
short value = next;
|
||||
next += increment;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user