float/double ranges

This commit is contained in:
Alex Tkachman
2012-01-06 15:55:27 +02:00
parent eba343dd84
commit dba41c2e88
12 changed files with 453 additions and 47 deletions
@@ -30,15 +30,15 @@ public class RangeTo implements IntrinsicMethod {
JetBinaryExpression expression = (JetBinaryExpression) element;
final Type leftType = codegen.expressionType(expression.getLeft());
final Type rightType = codegen.expressionType(expression.getRight());
if (JetTypeMapper.isIntPrimitive(leftType)) {
// if (JetTypeMapper.isIntPrimitive(leftType)) {
codegen.gen(expression.getLeft(), leftType);
codegen.gen(expression.getRight(), rightType);
v.invokestatic("jet/runtime/Ranges", "rangeTo", "(" + leftType.getDescriptor() + rightType.getDescriptor() + ")" + expectedType.getDescriptor());
return StackValue.onStack(expectedType);
}
else {
throw new UnsupportedOperationException("ranges are only supported for int objects");
}
// }
// else {
// throw new UnsupportedOperationException("ranges are only supported for int objects");
// }
}
}
}
+48 -24
View File
@@ -326,6 +326,30 @@ class CharRange(val start : Char, val size : Int) : Range<Char>, CharIterable {
val isReversed : Boolean
}
class FloatRange(val start : Float, val size : Float) : Range<Float> {
fun contains (elem: Float) : Boolean
val end : Float
fun minus() : FloatRange
fun step(step: Float) : FloatIterator
val isReversed : Boolean
}
class DoubleRange(val start : Double, val size : Double) : Range<Double> {
fun contains (elem: Double) : Boolean
val end : Double
fun minus() : DoubleRange
fun step(step: Double) : DoubleIterator
val isReversed : Boolean
}
abstract class Number : Hashable {
abstract val dbl : Double
abstract val flt : Float
@@ -391,13 +415,13 @@ class Double : Number, Comparable<Double> {
fun mod(other : Short) : Double
fun mod(other : Byte) : Double
fun rangeTo(other : Double) : Range<Double>
fun rangeTo(other : Float) : Range<Double>
fun rangeTo(other : Long) : Range<Double>
fun rangeTo(other : Int) : Range<Double>
fun rangeTo(other : Short) : Range<Double>
fun rangeTo(other : Byte) : Range<Double>
fun rangeTo(other : Char) : Range<Double>
fun rangeTo(other : Double) : DoubleRange
fun rangeTo(other : Float) : DoubleRange
fun rangeTo(other : Long) : DoubleRange
fun rangeTo(other : Int) : DoubleRange
fun rangeTo(other : Short) : DoubleRange
fun rangeTo(other : Byte) : DoubleRange
fun rangeTo(other : Char) : DoubleRange
fun inc() : Double
fun dec() : Double
@@ -454,13 +478,13 @@ class Float : Number, Comparable<Float> {
fun mod(other : Byte) : Float
fun mod(other : Char) : Float
fun rangeTo(other : Double) : Range<Double>
fun rangeTo(other : Float) : Range<Float>
fun rangeTo(other : Long) : Range<Double>
fun rangeTo(other : Int) : Range<Double>
fun rangeTo(other : Short) : Range<Float>
fun rangeTo(other : Byte) : Range<Float>
fun rangeTo(other : Char) : Range<Float>
fun rangeTo(other : Double) : DoubleRange
fun rangeTo(other : Float) : FloatRange
fun rangeTo(other : Long) : DoubleRange
fun rangeTo(other : Int) : DoubleRange
fun rangeTo(other : Short) : FloatRange
fun rangeTo(other : Byte) : FloatRange
fun rangeTo(other : Char) : FloatRange
fun inc() : Float
fun dec() : Float
@@ -517,8 +541,8 @@ class Long : Number, Comparable<Long> {
fun mod(other : Byte) : Long
fun mod(other : Char) : Long
fun rangeTo(other : Double) : Range<Double>
fun rangeTo(other : Float) : Range<Double>
fun rangeTo(other : Double) : DoubleRange
fun rangeTo(other : Float) : FloatRange
fun rangeTo(other : Long) : LongRange
fun rangeTo(other : Int) : LongRange
fun rangeTo(other : Short) : LongRange
@@ -588,8 +612,8 @@ class Int : Number, Comparable<Int> {
fun mod(other : Byte) : Int
fun mod(other : Char) : Int
fun rangeTo(other : Double) : Range<Double>
fun rangeTo(other : Float) : Range<Double>
fun rangeTo(other : Double) : DoubleRange
fun rangeTo(other : Float) : FloatRange
fun rangeTo(other : Long) : LongRange
fun rangeTo(other : Int) : IntRange
fun rangeTo(other : Short) : IntRange
@@ -659,8 +683,8 @@ class Char : Number, Comparable<Char> {
fun mod(other : Byte) : Int
// fun mod(other : Char) : Int
fun rangeTo(other : Double) : Range<Double>
fun rangeTo(other : Float) : Range<Float>
fun rangeTo(other : Double) : DoubleRange
fun rangeTo(other : Float) : FloatRange
fun rangeTo(other : Long) : LongRange
fun rangeTo(other : Int) : IntRange
fun rangeTo(other : Short) : ShortRange
@@ -722,8 +746,8 @@ class Short : Number, Comparable<Short> {
fun mod(other : Byte) : Int
fun mod(other : Char) : Int
fun rangeTo(other : Double) : Range<Double>
fun rangeTo(other : Float) : Range<Float>
fun rangeTo(other : Double) : DoubleRange
fun rangeTo(other : Float) : FloatRange
fun rangeTo(other : Long) : LongRange
fun rangeTo(other : Int) : IntRange
fun rangeTo(other : Short) : ShortRange
@@ -785,8 +809,8 @@ class Byte : Number, Comparable<Byte> {
fun mod(other : Byte) : Int
fun mod(other : Char) : Int
fun rangeTo(other : Double) : Range<Double>
fun rangeTo(other : Float) : Range<Float>
fun rangeTo(other : Double) : DoubleRange
fun rangeTo(other : Float) : FloatRange
fun rangeTo(other : Long) : LongRange
fun rangeTo(other : Int) : IntRange
fun rangeTo(other : Short) : ShortRange
@@ -0,0 +1,13 @@
fun box() : String {
System.out?.println(System.out?.println(10.flt..11.flt))
for(f in 10.flt..11.flt step 0.3.flt) {
System.out?.println(f)
}
for(f in 10.dbl..11.dbl step 0.3.dbl) {
System.out?.println(f)
}
return "OK"
}
@@ -371,4 +371,8 @@ public class PrimitiveTypesTest extends CodegenTestCase {
public void testKt925 () {
blackBoxFile("regressions/kt925.kt");
}
public void testKt765 () {
blackBoxFile("regressions/kt765.kt");
}
}
+9 -2
View File
@@ -78,8 +78,15 @@ public final class ByteRange implements Range<Byte>, ByteIterable, JetObject {
public MyIterator(byte startValue, int count, int step) {
cur = startValue;
this.step = step;
reversed = count < 0;
this.count = reversed ? -count : count;
if(count < 0) {
reversed = true;
count = -count;
startValue += count;
}
else {
reversed = false;
}
this.count = count;
}
@Override
+9 -2
View File
@@ -78,8 +78,15 @@ public final class CharRange implements Range<Character>, CharIterable, JetObjec
public MyIterator(char startValue, int count, int step) {
cur = startValue;
this.step = step;
reversed = count < 0;
this.count = reversed ? -count : count;
if(count < 0) {
reversed = true;
count = -count;
startValue += count;
}
else {
reversed = false;
}
this.count = count;
}
@Override
+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;
}
}
}
+1 -1
View File
@@ -79,7 +79,7 @@ public final class IntRange implements Range<Integer>, IntIterable, JetObject {
cur = startValue;
this.step = step;
if(count < 0) {
reversed = count < 0;
reversed = true;
count = -count;
startValue += count;
}
+9 -2
View File
@@ -78,8 +78,15 @@ public final class LongRange implements Range<Long>, LongIterable, JetObject {
public MyIterator(long startValue, long count, long step) {
cur = startValue;
this.step = step;
reversed = count < 0;
this.count = reversed ? -count : count;
if(count < 0) {
reversed = true;
count = -count;
startValue += count;
}
else {
reversed = false;
}
this.count = count;
}
@Override
+9 -2
View File
@@ -78,8 +78,15 @@ public final class ShortRange implements Range<Short>, ShortIterable, JetObject
public MyIterator(short startValue, int count, int step) {
cur = startValue;
this.step = step;
reversed = count < 0;
this.count = reversed ? -count : count;
if(count < 0) {
reversed = true;
count = -count;
startValue += count;
}
else {
reversed = false;
}
this.count = count;
}
@Override
+112 -9
View File
@@ -48,6 +48,14 @@ public class Ranges {
}
}
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);
@@ -93,6 +101,14 @@ public class Ranges {
}
}
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);
@@ -138,6 +154,14 @@ public class Ranges {
}
}
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);
@@ -183,6 +207,14 @@ public class Ranges {
}
}
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);
@@ -192,6 +224,62 @@ public class Ranges {
}
}
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);
@@ -228,6 +316,14 @@ public class Ranges {
}
}
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);
@@ -238,7 +334,7 @@ public class Ranges {
}
public static void main(String[] args) {
List<String> strings = Arrays.asList("byte", "short", "int", "long", /*"float", "double",*/ "char");
List<String> strings = Arrays.asList("byte", "short", "int", "long", "float", "double", "char");
for(String t1 : strings)
for(String t2 : strings) {
String resType;
@@ -264,14 +360,21 @@ public class Ranges {
resType = "ByteRange";
}
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" +
"}");
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" +
"}");
}
}
}
}