Merge branch 'master' of git+ssh://git.labs.intellij.net/jet

This commit is contained in:
James Strachan
2012-01-07 07:02:05 +00:00
34 changed files with 1092 additions and 208 deletions
+25 -24
View File
@@ -11,14 +11,6 @@ public final class ByteRange implements Range<Byte>, ByteIterable, JetObject {
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;
@@ -44,13 +36,20 @@ public final class ByteRange implements Range<Byte>, ByteIterable, JetObject {
return count < 0 ? -count : count;
}
public ByteIterator step(int step) {
if(step < 0)
return new MyIterator(getEnd(), -count, -step);
else
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
@@ -67,27 +66,27 @@ public final class ByteRange implements Range<Byte>, ByteIterable, JetObject {
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 step;
private int count;
private final boolean reversed;
public MyIterator(byte startValue, int count) {
public MyIterator(byte startValue, int count, int step) {
cur = startValue;
reversed = count < 0;
this.count = reversed ? -count : count;
this.step = step;
if(count < 0) {
reversed = true;
count = -count;
startValue += count;
}
else {
reversed = false;
}
this.count = count;
}
@Override
@@ -97,12 +96,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);
}
}
+25 -24
View File
@@ -11,14 +11,6 @@ public final class CharRange implements Range<Character>, CharIterable, JetObjec
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;
@@ -48,9 +40,16 @@ public final class CharRange implements Range<Character>, CharIterable, JetObjec
return new CharRange(getEnd(), -count);
}
public CharIterator step(int step) {
if(step < 0)
return new MyIterator(getEnd(), -count, -step);
else
return new MyIterator(start, count, step);
}
@Override
public CharIterator iterator() {
return new MyIterator(start, count);
return new MyIterator(start, count, 1);
}
@Override
@@ -67,27 +66,27 @@ public final class CharRange implements Range<Character>, CharIterable, JetObjec
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 step;
private int count;
private final boolean reversed;
public MyIterator(char startValue, int count) {
public MyIterator(char startValue, int count, int step) {
cur = startValue;
reversed = count < 0;
this.count = reversed ? -count : count;
this.step = step;
if(count < 0) {
reversed = true;
count = -count;
startValue += count;
}
else {
reversed = false;
}
this.count = count;
}
@Override
@@ -97,12 +96,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);
}
}
+117
View File
@@ -0,0 +1,117 @@
package jet;
public final class DoubleRange implements Range<Double>, JetObject {
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(DoubleRange.class, false);
private final double start;
private final double size;
public DoubleRange(double startValue, double size) {
this.start = startValue;
this.size = size;
}
@Override
public boolean contains(Double item) {
if (item == null) return false;
if (size >= 0) {
return item >= start && item < start + size;
}
return item <= start && item > start + size;
}
public DoubleIterator step(double step) {
if(step < 0)
return new MyIterator(getEnd(), -size, -step);
else
return new MyIterator(start, size, step);
}
public boolean getIsReversed() {
return size < 0;
}
public double getStart() {
return start;
}
public double getEnd() {
return size < 0 ? start + size: start + size;
}
public double getSize() {
return size < 0 ? -size : size;
}
public DoubleRange minus() {
return new DoubleRange(getEnd(), -size);
}
@Override
public TypeInfo<?> getTypeInfo() {
return typeInfo;
}
@Override
public JetObject getOuterObject() {
return null;
}
public static DoubleRange count(int length) {
return new DoubleRange(0, length);
}
private static class MyIterator extends DoubleIterator {
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(MyIterator.class, false);
private double cur;
private double step;
private final double end;
private final boolean reversed;
public MyIterator(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;
}
}
@Override
public boolean getHasNext() {
if(reversed)
return cur >= end;
else
return cur <= end;
}
@Override
public double nextDouble() {
if(reversed) {
cur -= step;
return cur + step;
}
else {
cur += step;
return cur - step;
}
}
@Override
public TypeInfo<?> getTypeInfo() {
return typeInfo;
}
@Override
public JetObject getOuterObject() {
return null;
}
}
}
+117
View File
@@ -0,0 +1,117 @@
package jet;
public final class FloatRange implements Range<Float>, JetObject {
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(FloatRange.class, false);
private final float start;
private final float size;
public FloatRange(float startValue, float size) {
this.start = startValue;
this.size = size;
}
@Override
public boolean contains(Float item) {
if (item == null) return false;
if (size >= 0) {
return item >= start && item < start + size;
}
return item <= start && item > start + size;
}
public FloatIterator step(float step) {
if(step < 0)
return new MyIterator(getEnd(), -size, -step);
else
return new MyIterator(start, size, step);
}
public boolean getIsReversed() {
return size < 0;
}
public float getStart() {
return start;
}
public float getEnd() {
return size < 0 ? start + size: start + size;
}
public float getSize() {
return size < 0 ? -size : size;
}
public FloatRange minus() {
return new FloatRange(getEnd(), -size);
}
@Override
public TypeInfo<?> getTypeInfo() {
return typeInfo;
}
@Override
public JetObject getOuterObject() {
return null;
}
public static FloatRange count(int length) {
return new FloatRange(0, length);
}
private static class MyIterator extends FloatIterator {
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(MyIterator.class, false);
private float cur;
private float step;
private final float end;
private final boolean reversed;
public MyIterator(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;
}
}
@Override
public boolean getHasNext() {
if(reversed)
return cur >= end;
else
return cur <= end;
}
@Override
public float nextFloat() {
if(reversed) {
cur -= step;
return cur + step;
}
else {
cur += step;
return cur - step;
}
}
@Override
public TypeInfo<?> getTypeInfo() {
return typeInfo;
}
@Override
public JetObject getOuterObject() {
return null;
}
}
}
+25 -24
View File
@@ -11,14 +11,6 @@ public final class IntRange implements Range<Integer>, IntIterable, JetObject {
this.count = count;
}
public IntRange(int startValue, int count, boolean reversed) {
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;
@@ -28,6 +20,13 @@ public final class IntRange implements Range<Integer>, IntIterable, JetObject {
return item <= start && item > start + count;
}
public IntIterator step(int step) {
if(step < 0)
return new MyIterator(getEnd(), -count, -step);
else
return new MyIterator(start, count, step);
}
public boolean getIsReversed() {
return count < 0;
}
@@ -50,7 +49,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
@@ -67,27 +66,27 @@ public final class IntRange implements Range<Integer>, IntIterable, JetObject {
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);
}
}
private static class MyIterator extends IntIterator {
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;
reversed = count < 0;
this.count = reversed ? -count : count;
this.step = step;
if(count < 0) {
reversed = true;
count = -count;
startValue += count;
}
else {
reversed = false;
}
this.count = count;
}
@Override
@@ -97,12 +96,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;
}
}
+25 -24
View File
@@ -11,12 +11,11 @@ public final class LongRange implements Range<Long>, LongIterable, JetObject {
this.count = count;
}
public LongRange(long startValue, long count, boolean reversed) {
this(startValue, reversed ? -count : count);
}
public LongRange(int startValue, int count, boolean reversed, int defaultMask) {
this(startValue, reversed ? -count : count, (defaultMask & 4) == 0);
public LongIterator step(long step) {
if(step < 0)
return new MyIterator(getEnd(), -count, -step);
else
return new MyIterator(start, count, step);
}
@Override
@@ -50,7 +49,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,31 +62,31 @@ 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;
reversed = count < 0;
this.count = reversed ? -count : count;
this.step = step;
if(count < 0) {
reversed = true;
count = -count;
startValue += count;
}
else {
reversed = false;
}
this.count = count;
}
@Override
@@ -97,12 +96,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);
}
}
+23 -22
View File
@@ -11,12 +11,11 @@ public final class ShortRange implements Range<Short>, ShortIterable, JetObject
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);
public ShortIterator step(int step) {
if(step < 0)
return new MyIterator(getEnd(), -count, -step);
else
return new MyIterator(start, count, step);
}
@Override
@@ -50,7 +49,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
@@ -67,27 +66,27 @@ public final class ShortRange implements Range<Short>, ShortIterable, JetObject
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 step;
private int count;
private final boolean reversed;
public MyIterator(short startValue, int count) {
public MyIterator(short startValue, int count, int step) {
cur = startValue;
reversed = count < 0;
this.count = reversed ? -count : count;
this.step = step;
if(count < 0) {
reversed = true;
count = -count;
startValue += count;
}
else {
reversed = false;
}
this.count = count;
}
@Override
@@ -97,12 +96,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);
}
}
+380
View File
@@ -0,0 +1,380 @@
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(from, to-from-1);
}
else {
return new ByteRange(from, to-from+1);
}
}
public static ShortRange rangeTo(byte from, short to) {
if(from > to) {
return new ShortRange(from, to-from-1);
}
else {
return new ShortRange(from, to-from+1);
}
}
public static IntRange rangeTo(byte from, int to) {
if(from > to) {
return new IntRange(from, to-from-1);
}
else {
return new IntRange(from, to-from+1);
}
}
public static LongRange rangeTo(byte from, long to) {
if(from > to) {
return new LongRange(from, to-from-1);
}
else {
return new LongRange(from, to-from+1);
}
}
public static FloatRange rangeTo(byte from, float to) {
return new FloatRange(from, to-from);
}
public static DoubleRange rangeTo(byte from, double to) {
return new DoubleRange(from, to-from);
}
public static CharRange rangeTo(byte from, char to) {
if(from > to) {
return new CharRange((char) from, to-from-1);
}
else {
return new CharRange((char) from, to-from+1);
}
}
public static ShortRange rangeTo(short from, byte to) {
if(from > to) {
return new ShortRange(from, to-from-1);
}
else {
return new ShortRange(from, to-from+1);
}
}
public static ShortRange rangeTo(short from, short to) {
if(from > to) {
return new ShortRange(from, to-from-1);
}
else {
return new ShortRange(from, to-from+1);
}
}
public static IntRange rangeTo(short from, int to) {
if(from > to) {
return new IntRange(from, to-from-1);
}
else {
return new IntRange(from, to-from+1);
}
}
public static LongRange rangeTo(short from, long to) {
if(from > to) {
return new LongRange(from, to-from-1);
}
else {
return new LongRange(from, to-from+1);
}
}
public static FloatRange rangeTo(short from, float to) {
return new FloatRange(from, to-from);
}
public static DoubleRange rangeTo(short from, double to) {
return new DoubleRange(from, to-from);
}
public static ShortRange rangeTo(short from, char to) {
if(from > to) {
return new ShortRange(from, to-from-1);
}
else {
return new ShortRange(from, to-from+1);
}
}
public static IntRange rangeTo(int from, byte to) {
if(from > to) {
return new IntRange(from, to-from-1);
}
else {
return new IntRange(from, to-from+1);
}
}
public static IntRange rangeTo(int from, short to) {
if(from > to) {
return new IntRange(from, to-from-1);
}
else {
return new IntRange(from, to-from+1);
}
}
public static IntRange rangeTo(int from, int to) {
if(from > to) {
return new IntRange(from, to-from-1);
}
else {
return new IntRange(from, to-from+1);
}
}
public static LongRange rangeTo(int from, long to) {
if(from > to) {
return new LongRange(from, to-from-1);
}
else {
return new LongRange(from, to-from+1);
}
}
public static FloatRange rangeTo(int from, float to) {
return new FloatRange(from, to-from);
}
public static DoubleRange rangeTo(int from, double to) {
return new DoubleRange(from, to-from);
}
public static IntRange rangeTo(int from, char to) {
if(from > to) {
return new IntRange(from, to-from-1);
}
else {
return new IntRange(from, to-from+1);
}
}
public static LongRange rangeTo(long from, byte to) {
if(from > to) {
return new LongRange(from, to-from-1);
}
else {
return new LongRange(from, to-from+1);
}
}
public static LongRange rangeTo(long from, short to) {
if(from > to) {
return new LongRange(from, to-from-1);
}
else {
return new LongRange(from, to-from+1);
}
}
public static LongRange rangeTo(long from, int to) {
if(from > to) {
return new LongRange(from, to-from-1);
}
else {
return new LongRange(from, to-from+1);
}
}
public static LongRange rangeTo(long from, long to) {
if(from > to) {
return new LongRange(from, to-from-1);
}
else {
return new LongRange(from, to-from+1);
}
}
public static FloatRange rangeTo(long from, float to) {
return new FloatRange(from, to-from);
}
public static DoubleRange rangeTo(long from, double to) {
return new DoubleRange(from, to-from);
}
public static LongRange rangeTo(long from, char to) {
if(from > to) {
return new LongRange(from, to-from-1);
}
else {
return new LongRange(from, to-from+1);
}
}
public static FloatRange rangeTo(float from, byte to) {
return new FloatRange(from, to-from);
}
public static FloatRange rangeTo(float from, short to) {
return new FloatRange(from, to-from);
}
public static FloatRange rangeTo(float from, int to) {
return new FloatRange(from, to-from);
}
public static FloatRange rangeTo(float from, long to) {
return new FloatRange(from, to-from);
}
public static FloatRange rangeTo(float from, float to) {
return new FloatRange(from, to-from);
}
public static DoubleRange rangeTo(float from, double to) {
return new DoubleRange(from, to-from);
}
public static FloatRange rangeTo(float from, char to) {
return new FloatRange(from, to-from);
}
public static DoubleRange rangeTo(double from, byte to) {
return new DoubleRange(from, to-from);
}
public static DoubleRange rangeTo(double from, short to) {
return new DoubleRange(from, to-from);
}
public static DoubleRange rangeTo(double from, int to) {
return new DoubleRange(from, to-from);
}
public static DoubleRange rangeTo(double from, long to) {
return new DoubleRange(from, to-from);
}
public static DoubleRange rangeTo(double from, float to) {
return new DoubleRange(from, to-from);
}
public static DoubleRange rangeTo(double from, double to) {
return new DoubleRange(from, to-from);
}
public static DoubleRange rangeTo(double from, char to) {
return new DoubleRange(from, to-from);
}
public static CharRange rangeTo(char from, byte to) {
if(from > to) {
return new CharRange(from, to-from-1);
}
else {
return new CharRange(from, to-from+1);
}
}
public static ShortRange rangeTo(char from, short to) {
if(from > to) {
return new ShortRange((short) from, to-from-1);
}
else {
return new ShortRange((short) from, to-from+1);
}
}
public static IntRange rangeTo(char from, int to) {
if(from > to) {
return new IntRange(from, to-from-1);
}
else {
return new IntRange(from, to-from+1);
}
}
public static LongRange rangeTo(char from, long to) {
if(from > to) {
return new LongRange(from, to-from-1);
}
else {
return new LongRange(from, to-from+1);
}
}
public static FloatRange rangeTo(char from, float to) {
return new FloatRange(from, to-from);
}
public static DoubleRange rangeTo(char from, double to) {
return new DoubleRange(from, to-from);
}
public static CharRange rangeTo(char from, char to) {
if(from > to) {
return new CharRange(from, to-from-1);
}
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";
}
if(resType.equals("FloatRange") || resType.equals("DoubleRange")) {
System.out.println("\npublic static " + resType + " rangeTo(" + t1 + " from, " + t2 + " to) {\n" +
" return new " + resType + "(from, to-from);\n" +
"}");
}
else {
System.out.println("\npublic static " + resType + " rangeTo(" + t1 + " from, " + t2 + " to) {" +
"\n if(from > to) {\n" +
" return new " + resType + "(from, to-from-1);\n" +
" }\n" +
" else {\n" +
" return new " + resType + "(from, to-from+1);\n" +
" }\n" +
"}");
}
}
}
}