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
+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" +
"}");
}
}
}
}