array.indices

This commit is contained in:
Alex Tkachman
2011-10-08 16:26:49 +02:00
parent 78e913762f
commit e94087f41e
8 changed files with 246 additions and 47 deletions
+56 -9
View File
@@ -1,28 +1,75 @@
package jet;
public class IntRange implements Range<Integer> {
import jet.typeinfo.TypeInfo;
public final class IntRange implements Range<Integer>, Iterable<Integer>, JetObject {
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(IntRange.class, false);
private final int startValue;
private final int endValue;
private final int excludedEndValue;
public IntRange(int startValue, int endValue) {
this.startValue = startValue;
this.endValue = endValue;
this.excludedEndValue = endValue;
}
@Override
public boolean contains(Integer item) {
if (item == null) return false;
if (startValue <= endValue) {
return item >= startValue && item <= endValue;
if (startValue < excludedEndValue) {
return item >= startValue && item < excludedEndValue;
}
return item <= startValue && item >= endValue;
return item <= startValue && item > excludedEndValue;
}
public int getStartValue() {
public int getStart() {
return startValue;
}
public int getEndValue() {
return endValue;
public int getEnd() {
return excludedEndValue;
}
@Override
public Iterator<Integer> iterator() {
return new MyIterator(startValue, excludedEndValue);
}
@Override
public TypeInfo<?> getTypeInfo() {
return typeInfo;
}
public static IntRange count(int length) {
return new IntRange(0, length);
}
private static class MyIterator implements Iterator<Integer> {
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(MyIterator.class, false);
private final int lastValue;
private int cur;
private boolean reversed;
public MyIterator(int startValue, int endValue) {
reversed = endValue <= startValue;
this.lastValue = reversed ? startValue : endValue-1;
cur = reversed ? endValue-1 : startValue;
}
@Override
public boolean hasNext() {
return reversed ? cur >= lastValue : cur <= lastValue;
}
@Override
public Integer next() {
return reversed ? cur-- : cur++;
}
@Override
public TypeInfo<?> getTypeInfo() {
return typeInfo;
}
}
}
+75
View File
@@ -0,0 +1,75 @@
package jet;
import jet.typeinfo.TypeInfo;
public final class LongRange implements Range<Long>, Iterable<Long>, JetObject {
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(IntRange.class, false);
private final long startValue;
private final long excludedEndValue;
public LongRange(long startValue, long endValue) {
this.startValue = startValue;
this.excludedEndValue = endValue;
}
@Override
public boolean contains(Long item) {
if (item == null) return false;
if (startValue < excludedEndValue) {
return item >= startValue && item < excludedEndValue;
}
return item <= startValue && item > excludedEndValue;
}
public long getStart() {
return startValue;
}
public long getEnd() {
return excludedEndValue;
}
@Override
public Iterator<Long> iterator() {
return new MyIterator(startValue, excludedEndValue);
}
@Override
public TypeInfo<?> getTypeInfo() {
return typeInfo;
}
public static IntRange count(int length) {
return new IntRange(0, length);
}
private static class MyIterator implements Iterator<Long> {
private final static TypeInfo typeInfo = TypeInfo.getTypeInfo(MyIterator.class, false);
private final long lastValue;
private long cur;
private boolean reversed;
public MyIterator(long startValue, long endValue) {
reversed = endValue <= startValue;
this.lastValue = reversed ? startValue : endValue-1;
cur = reversed ? endValue-1 : startValue;
}
@Override
public boolean hasNext() {
return reversed ? cur >= lastValue : cur <= lastValue;
}
@Override
public Long next() {
return reversed ? cur-- : cur++;
}
@Override
public TypeInfo<?> getTypeInfo() {
return typeInfo;
}
}
}