Remove unneeded dependency on ListIterator in ConsPStack

This commit is contained in:
Alexander Udalov
2014-06-10 19:13:51 +04:00
parent 862a785c11
commit 41aa12d249
2 changed files with 10 additions and 44 deletions
@@ -1,7 +1,6 @@
package kotlin.reflect.jvm.internal.pcollections; package kotlin.reflect.jvm.internal.pcollections;
import java.util.Iterator; import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
/** /**
@@ -11,7 +10,7 @@ import java.util.NoSuchElementException;
* *
* @author harold * @author harold
*/ */
public final class ConsPStack<E> implements Iterable<E> { final class ConsPStack<E> implements Iterable<E> {
private static final ConsPStack<Object> EMPTY = new ConsPStack<Object>(); private static final ConsPStack<Object> EMPTY = new ConsPStack<Object>();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -36,8 +35,10 @@ public final class ConsPStack<E> implements Iterable<E> {
} }
public E get(int index) { public E get(int index) {
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
try { try {
return listIterator(index).next(); return iterator(index).next();
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
throw new IndexOutOfBoundsException("Index: " + index); throw new IndexOutOfBoundsException("Index: " + index);
} }
@@ -45,18 +46,15 @@ public final class ConsPStack<E> implements Iterable<E> {
@Override @Override
public Iterator<E> iterator() { public Iterator<E> iterator() {
return listIterator(0); return iterator(0);
} }
public int size() { public int size() {
return size; return size;
} }
public ListIterator<E> listIterator(final int index) { private Iterator<E> iterator(final int index) {
if (index < 0 || index > size) throw new IndexOutOfBoundsException(); return new Iterator<E>() {
return new ListIterator<E>() {
int i = index;
ConsPStack<E> next = subList(index); ConsPStack<E> next = subList(index);
@Override @Override
@@ -64,21 +62,6 @@ public final class ConsPStack<E> implements Iterable<E> {
return next.size > 0; return next.size > 0;
} }
@Override
public boolean hasPrevious() {
return i > 0;
}
@Override
public int nextIndex() {
return index;
}
@Override
public int previousIndex() {
return index - 1;
}
@Override @Override
public E next() { public E next() {
E e = next.first; E e = next.first;
@@ -86,27 +69,10 @@ public final class ConsPStack<E> implements Iterable<E> {
return e; return e;
} }
@Override
public E previous() {
System.err.println("ConsPStack.listIterator().previous() is inefficient, don't use it!");
next = subList(index - 1); // go from beginning...
return next.first;
}
@Override
public void add(E o) {
throw new UnsupportedOperationException();
}
@Override @Override
public void remove() { public void remove() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void set(E o) {
throw new UnsupportedOperationException();
}
}; };
} }
@@ -114,7 +80,7 @@ public final class ConsPStack<E> implements Iterable<E> {
return new ConsPStack<E>(e, this); return new ConsPStack<E>(e, this);
} }
public ConsPStack<E> minus(Object e) { private ConsPStack<E> minus(Object e) {
if (size == 0) return this; if (size == 0) return this;
if (first.equals(e)) // found it if (first.equals(e)) // found it
return rest; // don't recurse (only remove one) return rest; // don't recurse (only remove one)
@@ -128,7 +94,7 @@ public final class ConsPStack<E> implements Iterable<E> {
return minus(get(i)); return minus(get(i));
} }
public ConsPStack<E> subList(int start) { private ConsPStack<E> subList(int start) {
if (start < 0 || start > size) if (start < 0 || start > size)
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
if (start == 0) if (start == 0)
@@ -19,7 +19,7 @@ package kotlin.reflect.jvm.internal.pcollections;
* *
* @author harold * @author harold
*/ */
public final class IntTreePMap<V> { final class IntTreePMap<V> {
private static final IntTreePMap<Object> EMPTY = new IntTreePMap<Object>(IntTree.EMPTYNODE); private static final IntTreePMap<Object> EMPTY = new IntTreePMap<Object>(IntTree.EMPTYNODE);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")