KT-821 and other improvements for ranges

This commit is contained in:
Alex Tkachman
2012-01-05 18:34:32 +02:00
parent 89933439f4
commit 9fbf27eb11
9 changed files with 489 additions and 17 deletions
+119
View File
@@ -0,0 +1,119 @@
package jet;
public final class ByteRange implements Range<Byte>, ByteIterable, JetObject {
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(ByteRange.class, false);
private final byte start;
private final int count;
public ByteRange(byte startValue, int count) {
this.start = startValue;
this.count = count;
}
public ByteRange(byte startValue, int count, boolean reversed) {
this(startValue, reversed ? -count : count);
}
public ByteRange(byte startValue, int count, boolean reversed, int defaultMask) {
this(startValue, reversed ? -count : count, (defaultMask & 4) == 0);
}
@Override
public boolean contains(Byte item) {
if (item == null) return false;
if (count >= 0) {
return item >= start && item < start + count;
}
return item <= start && item > start + count;
}
public boolean getIsReversed() {
return count < 0;
}
public byte getStart() {
return start;
}
public byte getEnd() {
return (byte) (count < 0 ? start + count + 1: start+count-1);
}
public int getSize() {
return count < 0 ? -count : count;
}
public ByteRange minus() {
return new ByteRange(getEnd(), -count);
}
@Override
public ByteIterator iterator() {
return new MyIterator(start, count);
}
@Override
public TypeInfo<?> getTypeInfo() {
return typeInfo;
}
@Override
public JetObject getOuterObject() {
return null;
}
public static ByteRange count(int length) {
return new ByteRange((byte) 0, length);
}
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);
}
}
private static class MyIterator extends ByteIterator {
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(MyIterator.class, false);
private byte cur;
private int count;
private final boolean reversed;
public MyIterator(byte startValue, int count) {
cur = startValue;
reversed = count < 0;
this.count = reversed ? -count : count;
}
@Override
public boolean getHasNext() {
return count > 0;
}
@Override
public byte nextByte() {
count--;
if(reversed) {
return cur--;
}
else {
return cur++;
}
}
@Override
public TypeInfo<?> getTypeInfo() {
return typeInfo;
}
@Override
public JetObject getOuterObject() {
return null;
}
}
}
+119
View File
@@ -0,0 +1,119 @@
package jet;
public final class CharRange implements Range<Character>, CharIterable, JetObject {
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(CharRange.class, false);
private final char start;
private final int count;
public CharRange(char startValue, int count) {
this.start = startValue;
this.count = count;
}
public CharRange(char startValue, int count, boolean reversed) {
this(startValue, reversed ? -count : count);
}
public CharRange(char startValue, int count, boolean reversed, int defaultMask) {
this(startValue, reversed ? -count : count, (defaultMask & 4) == 0);
}
@Override
public boolean contains(Character item) {
if (item == null) return false;
if (count >= 0) {
return item >= start && item < start + count;
}
return item <= start && item > start + count;
}
public boolean getIsReversed() {
return count < 0;
}
public char getStart() {
return start;
}
public char getEnd() {
return (char) (count < 0 ? start + count + 1: start+count-1);
}
public int getSize() {
return count < 0 ? -count : count;
}
public CharRange minus() {
return new CharRange(getEnd(), -count);
}
@Override
public CharIterator iterator() {
return new MyIterator(start, count);
}
@Override
public TypeInfo<?> getTypeInfo() {
return typeInfo;
}
@Override
public JetObject getOuterObject() {
return null;
}
public static CharRange count(int length) {
return new CharRange((char) 0, length);
}
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);
}
}
private static class MyIterator extends CharIterator {
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(MyIterator.class, false);
private char cur;
private int count;
private final boolean reversed;
public MyIterator(char startValue, int count) {
cur = startValue;
reversed = count < 0;
this.count = reversed ? -count : count;
}
@Override
public boolean getHasNext() {
return count > 0;
}
@Override
public char nextChar() {
count--;
if(reversed) {
return cur--;
}
else {
return cur++;
}
}
@Override
public TypeInfo<?> getTypeInfo() {
return typeInfo;
}
@Override
public JetObject getOuterObject() {
return null;
}
}
}
+13 -1
View File
@@ -15,6 +15,10 @@ public final class IntRange implements Range<Integer>, IntIterable, JetObject {
this(startValue, reversed ? -count : count);
}
public IntRange(int startValue, int count, boolean reversed, int defaultMask) {
this(startValue, reversed ? -count : count, (defaultMask & 4) == 0);
}
@Override
public boolean contains(Integer item) {
if (item == null) return false;
@@ -24,18 +28,26 @@ public final class IntRange implements Range<Integer>, IntIterable, JetObject {
return item <= start && item > start + count;
}
public boolean getIsReversed() {
return count < 0;
}
public int getStart() {
return start;
}
public int getEnd() {
return start+count-1;
return count < 0 ? start + count + 1: start+count-1;
}
public int getSize() {
return count < 0 ? -count : count;
}
public IntRange minus() {
return new IntRange(getEnd(), -count);
}
@Override
public IntIterator iterator() {
return new MyIterator(start, count);
+12
View File
@@ -15,6 +15,10 @@ public final class LongRange implements Range<Long>, LongIterable, JetObject {
this(startValue, reversed ? -count : count);
}
public LongRange(int startValue, int count, boolean reversed, int defaultMask) {
this(startValue, reversed ? -count : count, (defaultMask & 4) == 0);
}
@Override
public boolean contains(Long item) {
if (item == null) return false;
@@ -24,6 +28,10 @@ public final class LongRange implements Range<Long>, LongIterable, JetObject {
return item <= start && item > start + count;
}
public boolean getIsReversed() {
return count < 0;
}
public long getStart() {
return start;
}
@@ -36,6 +44,10 @@ public final class LongRange implements Range<Long>, LongIterable, JetObject {
return count < 0 ? -count : count;
}
public LongRange minus() {
return new LongRange(getEnd(), -count);
}
@Override
public LongIterator iterator() {
return new MyIterator(start, count);
+119
View File
@@ -0,0 +1,119 @@
package jet;
public final class ShortRange implements Range<Short>, ShortIterable, JetObject {
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(ShortRange.class, false);
private final short start;
private final int count;
public ShortRange(short startValue, int count) {
this.start = startValue;
this.count = count;
}
public ShortRange(short startValue, int count, boolean reversed) {
this(startValue, reversed ? -count : count);
}
public ShortRange(short startValue, int count, boolean reversed, int defaultMask) {
this(startValue, reversed ? -count : count, (defaultMask & 4) == 0);
}
@Override
public boolean contains(Short item) {
if (item == null) return false;
if (count >= 0) {
return item >= start && item < start + count;
}
return item <= start && item > start + count;
}
public boolean getIsReversed() {
return count < 0;
}
public short getStart() {
return start;
}
public short getEnd() {
return (short) (count < 0 ? start + count + 1: start+count-1);
}
public int getSize() {
return count < 0 ? -count : count;
}
public ShortRange minus() {
return new ShortRange(getEnd(), -count);
}
@Override
public ShortIterator iterator() {
return new MyIterator(start, count);
}
@Override
public TypeInfo<?> getTypeInfo() {
return typeInfo;
}
@Override
public JetObject getOuterObject() {
return null;
}
public static ShortRange count(int length) {
return new ShortRange((byte) 0, length);
}
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);
}
}
private static class MyIterator extends ShortIterator {
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(MyIterator.class, false);
private short cur;
private int count;
private final boolean reversed;
public MyIterator(short startValue, int count) {
cur = startValue;
reversed = count < 0;
this.count = reversed ? -count : count;
}
@Override
public boolean getHasNext() {
return count > 0;
}
@Override
public short nextShort() {
count--;
if(reversed) {
return cur--;
}
else {
return cur++;
}
}
@Override
public TypeInfo<?> getTypeInfo() {
return typeInfo;
}
@Override
public JetObject getOuterObject() {
return null;
}
}
}