classlib: implement general reversed ListIterator

This commit is contained in:
ihromant 2023-09-25 15:58:45 +03:00 committed by Alexey Andreev
parent e383b94e1b
commit c858542605
2 changed files with 67 additions and 94 deletions

View File

@ -529,53 +529,7 @@ public class TLinkedList<E> extends TAbstractSequentialList<E> implements TDeque
@Override @Override
public TListIterator<E> listIterator() { public TListIterator<E> listIterator() {
TListIterator<E> lit = list.listIterator(list.size()); return reversed.listIterator();
return new TListIterator<>() {
@Override
public boolean hasPrevious() {
return lit.hasNext();
}
@Override
public E previous() {
return lit.next();
}
@Override
public int nextIndex() {
return list.size() - lit.previousIndex() - 1;
}
@Override
public int previousIndex() {
return list.size() - lit.nextIndex() - 1;
}
@Override
public void set(E e) {
lit.set(e);
}
@Override
public void add(E e) {
lit.add(e);
}
@Override
public boolean hasNext() {
return lit.hasPrevious();
}
@Override
public E next() {
return lit.previous();
}
@Override
public void remove() {
lit.remove();
}
};
} }
@Override @Override
@ -615,53 +569,7 @@ public class TLinkedList<E> extends TAbstractSequentialList<E> implements TDeque
@Override @Override
public TListIterator<E> listIterator(int index) { public TListIterator<E> listIterator(int index) {
TListIterator<E> lit = list.listIterator(list.size() - index); return reversed.listIterator(index);
return new TListIterator<>() {
@Override
public boolean hasPrevious() {
return lit.hasNext();
}
@Override
public E previous() {
return lit.next();
}
@Override
public int nextIndex() {
return list.size() - lit.previousIndex() - 1;
}
@Override
public int previousIndex() {
return list.size() - lit.nextIndex() - 1;
}
@Override
public void set(E e) {
lit.set(e);
}
@Override
public void add(E e) {
lit.add(e);
}
@Override
public boolean hasNext() {
return lit.hasPrevious();
}
@Override
public E next() {
return lit.previous();
}
@Override
public void remove() {
lit.remove();
}
};
} }
@Override @Override

View File

@ -98,6 +98,16 @@ class TReversedList<E> extends TAbstractList<E> {
}; };
} }
@Override
public TListIterator<E> listIterator() {
return new ReversedListIterator<>(this, base.listIterator(base.size()));
}
@Override
public TListIterator<E> listIterator(int idx) {
return new ReversedListIterator<>(this, base.listIterator(base.size() - idx));
}
@Override @Override
public void clear() { public void clear() {
base.clear(); base.clear();
@ -118,4 +128,59 @@ class TReversedList<E> extends TAbstractList<E> {
super(base); super(base);
} }
} }
private static class ReversedListIterator<E> implements TListIterator<E> {
private final TList<E> list;
private final TListIterator<E> lit;
private ReversedListIterator(TList<E> list, TListIterator<E> lit) {
this.list = list;
this.lit = lit;
}
@Override
public boolean hasPrevious() {
return lit.hasNext();
}
@Override
public E previous() {
return lit.next();
}
@Override
public int nextIndex() {
return list.size() - lit.previousIndex() - 1;
}
@Override
public int previousIndex() {
return list.size() - lit.nextIndex() - 1;
}
@Override
public void set(E e) {
lit.set(e);
}
@Override
public void add(E e) {
lit.add(e);
}
@Override
public boolean hasNext() {
return lit.hasPrevious();
}
@Override
public E next() {
return lit.previous();
}
@Override
public void remove() {
lit.remove();
}
}
} }