Fix NumberIterator
nice. thanks
package tech.tablesaw.columns.numbers;
+import java.util.Iterator;
+import java.util.ListIterator;
+
import it.unimi.dsi.fastutil.doubles.DoubleArrayList;
import it.unimi.dsi.fastutil.doubles.DoubleIterator;
import it.unimi.dsi.fastutil.floats.FloatArrayList;
@@ -7,15 +10,13 @@ import it.unimi.dsi.fastutil.floats.FloatIterator;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntIterator;
-import java.util.Iterator;
-
public class NumberIterator implements Iterable<Double> {
private DoubleArrayList dList;
private IntArrayList iList;
private FloatArrayList fList;
- private final Object iterator;
+ private final ListIterator<?> iterator;
public NumberIterator(DoubleArrayList list) {
this.dList = list;
@@ -32,45 +33,52 @@ public class NumberIterator implements Iterable<Double> {
this.iterator = list.iterator();
}
+ public int nextIndex() {
+ return iterator.nextIndex();
+ }
+
+ public int previousIndex() {
+ return iterator.previousIndex();
+ }
+
public double next() {
if (dList != null) {
return ((DoubleIterator) iterator).nextDouble();
- } else {
+ } else if (iList != null){
return ((IntIterator) iterator).nextInt();
+ } else {
+ return ((FloatIterator) iterator).nextFloat();
}
}
public int nextInt() {
if (dList != null) {
return (int) ((DoubleIterator) iterator).nextDouble();
- } else {
+ } else if (iList != null){
return ((IntIterator) iterator).nextInt();
+ } else {
+ return (int) ((FloatIterator) iterator).nextFloat();
}
}
public float nextFloat() {
- if (fList != null) {
- return ((FloatIterator) iterator).nextFloat();
- }
- else if (dList != null) { // TODO (check for cast exception)
- return ((FloatIterator) iterator).nextFloat();
- } else {
+ if (dList != null) {
+ return (float) ((DoubleIterator) iterator).nextDouble();
+ } else if (iList != null){
return ((IntIterator) iterator).nextInt();
+ } else {
+ return ((FloatIterator) iterator).nextFloat();
}
}
public boolean hasNext() {
- if (dList != null) {
- return ((DoubleIterator) iterator).hasNext();
- } else {
- return ((IntIterator) iterator).hasNext();
- }
+ return iterator.hasNext();
}
public Iterator<Double> iterator() {
if (dList != null) {
return dList.iterator();
- } else {
+ } else if (iList != null) {
return new Iterator<Double>() {
IntIterator intIterator = iList.iterator();
@Override
@@ -83,6 +91,19 @@ public class NumberIterator implements Iterable<Double> {
return (double) intIterator.nextInt();
}
};
+ } else {
+ return new Iterator<Double>() {
+ FloatIterator floatIterator = fList.iterator();
+ @Override
+ public boolean hasNext() {
+ return floatIterator.hasNext();
+ }
+
+ @Override
+ public Double next() {
+ return (double) floatIterator.nextFloat();
+ }
+ };
}
}
}
