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