KT-925 step methods for integral ranges
This commit is contained in:
@@ -44,13 +44,17 @@ public final class ByteRange implements Range<Byte>, ByteIterable, JetObject {
|
||||
return count < 0 ? -count : count;
|
||||
}
|
||||
|
||||
public ByteIterator step(int step) {
|
||||
return new MyIterator(start, count, step);
|
||||
}
|
||||
|
||||
public ByteRange minus() {
|
||||
return new ByteRange(getEnd(), -count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteIterator iterator() {
|
||||
return new MyIterator(start, count);
|
||||
return new MyIterator(start, count, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -80,12 +84,14 @@ public final class ByteRange implements Range<Byte>, ByteIterable, JetObject {
|
||||
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(MyIterator.class, false);
|
||||
|
||||
private byte cur;
|
||||
private int step;
|
||||
private int count;
|
||||
|
||||
private final boolean reversed;
|
||||
|
||||
public MyIterator(byte startValue, int count) {
|
||||
public MyIterator(byte startValue, int count, int step) {
|
||||
cur = startValue;
|
||||
this.step = step;
|
||||
reversed = count < 0;
|
||||
this.count = reversed ? -count : count;
|
||||
}
|
||||
@@ -97,12 +103,14 @@ public final class ByteRange implements Range<Byte>, ByteIterable, JetObject {
|
||||
|
||||
@Override
|
||||
public byte nextByte() {
|
||||
count--;
|
||||
count -= step;
|
||||
if(reversed) {
|
||||
return cur--;
|
||||
cur -= step;
|
||||
return (byte) (cur + step);
|
||||
}
|
||||
else {
|
||||
return cur++;
|
||||
cur += step;
|
||||
return (byte) (cur - step);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,9 +48,13 @@ public final class CharRange implements Range<Character>, CharIterable, JetObjec
|
||||
return new CharRange(getEnd(), -count);
|
||||
}
|
||||
|
||||
public CharIterator step(int step) {
|
||||
return new MyIterator(start, count, step);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharIterator iterator() {
|
||||
return new MyIterator(start, count);
|
||||
return new MyIterator(start, count, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -80,12 +84,14 @@ public final class CharRange implements Range<Character>, CharIterable, JetObjec
|
||||
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(MyIterator.class, false);
|
||||
|
||||
private char cur;
|
||||
private int step;
|
||||
private int count;
|
||||
|
||||
private final boolean reversed;
|
||||
|
||||
public MyIterator(char startValue, int count) {
|
||||
public MyIterator(char startValue, int count, int step) {
|
||||
cur = startValue;
|
||||
this.step = step;
|
||||
reversed = count < 0;
|
||||
this.count = reversed ? -count : count;
|
||||
}
|
||||
@@ -97,12 +103,14 @@ public final class CharRange implements Range<Character>, CharIterable, JetObjec
|
||||
|
||||
@Override
|
||||
public char nextChar() {
|
||||
count--;
|
||||
count -= step;
|
||||
if(reversed) {
|
||||
return cur--;
|
||||
cur -= step;
|
||||
return (char) (cur + step);
|
||||
}
|
||||
else {
|
||||
return cur++;
|
||||
cur += step;
|
||||
return (char) (cur - step);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,10 @@ public final class IntRange implements Range<Integer>, IntIterable, JetObject {
|
||||
return item <= start && item > start + count;
|
||||
}
|
||||
|
||||
public IntIterator step(int step) {
|
||||
return new MyIterator(start, count, step);
|
||||
}
|
||||
|
||||
public boolean getIsReversed() {
|
||||
return count < 0;
|
||||
}
|
||||
@@ -50,7 +54,7 @@ public final class IntRange implements Range<Integer>, IntIterable, JetObject {
|
||||
|
||||
@Override
|
||||
public IntIterator iterator() {
|
||||
return new MyIterator(start, count);
|
||||
return new MyIterator(start, count, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -80,12 +84,14 @@ public final class IntRange implements Range<Integer>, IntIterable, JetObject {
|
||||
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(MyIterator.class, false);
|
||||
|
||||
private int cur;
|
||||
private int step;
|
||||
private int count;
|
||||
|
||||
private final boolean reversed;
|
||||
|
||||
public MyIterator(int startValue, int count) {
|
||||
public MyIterator(int startValue, int count, int step) {
|
||||
cur = startValue;
|
||||
this.step = step;
|
||||
reversed = count < 0;
|
||||
this.count = reversed ? -count : count;
|
||||
}
|
||||
@@ -97,12 +103,14 @@ public final class IntRange implements Range<Integer>, IntIterable, JetObject {
|
||||
|
||||
@Override
|
||||
public int nextInt() {
|
||||
count--;
|
||||
count -= step;
|
||||
if(reversed) {
|
||||
return cur--;
|
||||
cur -= step;
|
||||
return cur + step;
|
||||
}
|
||||
else {
|
||||
return cur++;
|
||||
cur += step;
|
||||
return cur - step;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,10 @@ public final class LongRange implements Range<Long>, LongIterable, JetObject {
|
||||
this(startValue, reversed ? -count : count, (defaultMask & 4) == 0);
|
||||
}
|
||||
|
||||
public LongIterator step(long step) {
|
||||
return new MyIterator(start, count, step);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Long item) {
|
||||
if (item == null) return false;
|
||||
@@ -50,7 +54,7 @@ public final class LongRange implements Range<Long>, LongIterable, JetObject {
|
||||
|
||||
@Override
|
||||
public LongIterator iterator() {
|
||||
return new MyIterator(start, count);
|
||||
return new MyIterator(start, count, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -63,29 +67,22 @@ public final class LongRange implements Range<Long>, LongIterable, JetObject {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IntRange count(int length) {
|
||||
return new IntRange(0, length);
|
||||
}
|
||||
|
||||
public static IntRange rangeTo(int from, int to) {
|
||||
if(from > to) {
|
||||
return new IntRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new IntRange(from, to-from+1);
|
||||
}
|
||||
public static LongRange count(int length) {
|
||||
return new LongRange(0, length);
|
||||
}
|
||||
|
||||
private static class MyIterator extends LongIterator {
|
||||
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(MyIterator.class, false);
|
||||
|
||||
private long cur;
|
||||
private long step;
|
||||
private long count;
|
||||
|
||||
private final boolean reversed;
|
||||
|
||||
public MyIterator(long startValue, long count) {
|
||||
public MyIterator(long startValue, long count, long step) {
|
||||
cur = startValue;
|
||||
this.step = step;
|
||||
reversed = count < 0;
|
||||
this.count = reversed ? -count : count;
|
||||
}
|
||||
@@ -97,12 +94,14 @@ public final class LongRange implements Range<Long>, LongIterable, JetObject {
|
||||
|
||||
@Override
|
||||
public long nextLong() {
|
||||
count--;
|
||||
count -= step;
|
||||
if(reversed) {
|
||||
return cur--;
|
||||
cur -= step;
|
||||
return (cur + step);
|
||||
}
|
||||
else {
|
||||
return cur++;
|
||||
cur += step;
|
||||
return (cur - step);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,10 @@ public final class ShortRange implements Range<Short>, ShortIterable, JetObject
|
||||
this(startValue, reversed ? -count : count, (defaultMask & 4) == 0);
|
||||
}
|
||||
|
||||
public ShortIterator step(int step) {
|
||||
return new MyIterator(start, count, step);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Short item) {
|
||||
if (item == null) return false;
|
||||
@@ -50,7 +54,7 @@ public final class ShortRange implements Range<Short>, ShortIterable, JetObject
|
||||
|
||||
@Override
|
||||
public ShortIterator iterator() {
|
||||
return new MyIterator(start, count);
|
||||
return new MyIterator(start, count, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -80,12 +84,14 @@ public final class ShortRange implements Range<Short>, ShortIterable, JetObject
|
||||
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(MyIterator.class, false);
|
||||
|
||||
private short cur;
|
||||
private int step;
|
||||
private int count;
|
||||
|
||||
private final boolean reversed;
|
||||
|
||||
public MyIterator(short startValue, int count) {
|
||||
public MyIterator(short startValue, int count, int step) {
|
||||
cur = startValue;
|
||||
this.step = step;
|
||||
reversed = count < 0;
|
||||
this.count = reversed ? -count : count;
|
||||
}
|
||||
@@ -97,12 +103,14 @@ public final class ShortRange implements Range<Short>, ShortIterable, JetObject
|
||||
|
||||
@Override
|
||||
public short nextShort() {
|
||||
count--;
|
||||
count -= step;
|
||||
if(reversed) {
|
||||
return cur--;
|
||||
cur -= step;
|
||||
return (short) (cur + step);
|
||||
}
|
||||
else {
|
||||
return cur++;
|
||||
cur += step;
|
||||
return (short) (cur - step);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,277 @@
|
||||
package jet.runtime;
|
||||
|
||||
import jet.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author alex.tkachman
|
||||
*/
|
||||
public class Ranges {
|
||||
private Ranges() {
|
||||
}
|
||||
|
||||
public static ByteRange rangeTo(byte from, byte to) {
|
||||
if(from > to) {
|
||||
return new ByteRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new ByteRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static ShortRange rangeTo(byte from, short to) {
|
||||
if(from > to) {
|
||||
return new ShortRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new ShortRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static IntRange rangeTo(byte from, int to) {
|
||||
if(from > to) {
|
||||
return new IntRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new IntRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static LongRange rangeTo(byte from, long to) {
|
||||
if(from > to) {
|
||||
return new LongRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new LongRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static CharRange rangeTo(byte from, char to) {
|
||||
if(from > to) {
|
||||
return new CharRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new CharRange((char) from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static ShortRange rangeTo(short from, byte to) {
|
||||
if(from > to) {
|
||||
return new ShortRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new ShortRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static ShortRange rangeTo(short from, short to) {
|
||||
if(from > to) {
|
||||
return new ShortRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new ShortRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static IntRange rangeTo(short from, int to) {
|
||||
if(from > to) {
|
||||
return new IntRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new IntRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static LongRange rangeTo(short from, long to) {
|
||||
if(from > to) {
|
||||
return new LongRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new LongRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static ShortRange rangeTo(short from, char to) {
|
||||
if(from > to) {
|
||||
return new ShortRange((short) to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new ShortRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static IntRange rangeTo(int from, byte to) {
|
||||
if(from > to) {
|
||||
return new IntRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new IntRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static IntRange rangeTo(int from, short to) {
|
||||
if(from > to) {
|
||||
return new IntRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new IntRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static IntRange rangeTo(int from, int to) {
|
||||
if(from > to) {
|
||||
return new IntRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new IntRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static LongRange rangeTo(int from, long to) {
|
||||
if(from > to) {
|
||||
return new LongRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new LongRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static IntRange rangeTo(int from, char to) {
|
||||
if(from > to) {
|
||||
return new IntRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new IntRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static LongRange rangeTo(long from, byte to) {
|
||||
if(from > to) {
|
||||
return new LongRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new LongRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static LongRange rangeTo(long from, short to) {
|
||||
if(from > to) {
|
||||
return new LongRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new LongRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static LongRange rangeTo(long from, int to) {
|
||||
if(from > to) {
|
||||
return new LongRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new LongRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static LongRange rangeTo(long from, long to) {
|
||||
if(from > to) {
|
||||
return new LongRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new LongRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static LongRange rangeTo(long from, char to) {
|
||||
if(from > to) {
|
||||
return new LongRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new LongRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static CharRange rangeTo(char from, byte to) {
|
||||
if(from > to) {
|
||||
return new CharRange((char) to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new CharRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static ShortRange rangeTo(char from, short to) {
|
||||
if(from > to) {
|
||||
return new ShortRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new ShortRange((short) from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static IntRange rangeTo(char from, int to) {
|
||||
if(from > to) {
|
||||
return new IntRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new IntRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static LongRange rangeTo(char from, long to) {
|
||||
if(from > to) {
|
||||
return new LongRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new LongRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static CharRange rangeTo(char from, char to) {
|
||||
if(from > to) {
|
||||
return new CharRange(to, from-to+1, true);
|
||||
}
|
||||
else {
|
||||
return new CharRange(from, to-from+1);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<String> strings = Arrays.asList("byte", "short", "int", "long", /*"float", "double",*/ "char");
|
||||
for(String t1 : strings)
|
||||
for(String t2 : strings) {
|
||||
String resType;
|
||||
if(t1.equals("double") || t2.equals("double")) {
|
||||
resType = "DoubleRange";
|
||||
}
|
||||
else if(t1.equals("float") || t2.equals("float")) {
|
||||
resType = "FloatRange";
|
||||
}
|
||||
else if(t1.equals("long") || t2.equals("long")) {
|
||||
resType = "LongRange";
|
||||
}
|
||||
else if(t1.equals("int") || t2.equals("int")) {
|
||||
resType = "IntRange";
|
||||
}
|
||||
else if(t1.equals("short") || t2.equals("short")) {
|
||||
resType = "ShortRange";
|
||||
}
|
||||
else if(t1.equals("char") || t2.equals("char")) {
|
||||
resType = "CharRange";
|
||||
}
|
||||
else {
|
||||
resType = "ByteRange";
|
||||
}
|
||||
|
||||
System.out.println("\npublic static " + resType + " rangeTo(" + t1 + " from, " + t2 + " to) {" +
|
||||
"\n if(from > to) {\n" +
|
||||
" return new " + resType + "(to, from-to+1, true);\n" +
|
||||
" }\n" +
|
||||
" else {\n" +
|
||||
" return new " + resType + "(from, to-from+1);\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user