add Inlist iterator + LList
This commit is contained in:
parent
8aa9cafa5e
commit
6dddf614dd
@ -16,6 +16,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.oscim.utils.pool;
|
package org.oscim.utils.pool;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
import javax.annotation.CheckReturnValue;
|
import javax.annotation.CheckReturnValue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,6 +29,58 @@ import javax.annotation.CheckReturnValue;
|
|||||||
* are *REALLY* sure about it. Better do not use it! :)
|
* are *REALLY* sure about it. Better do not use it! :)
|
||||||
*/
|
*/
|
||||||
public class Inlist<T extends Inlist<T>> {
|
public class Inlist<T extends Inlist<T>> {
|
||||||
|
/** UNTESTED */
|
||||||
|
public static class List<T extends Inlist<T>> implements Iterable<T> {
|
||||||
|
T head;
|
||||||
|
T cur;
|
||||||
|
Iterator<T> mIterator = new Iterator<T>() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return cur != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T next() {
|
||||||
|
if (cur == null)
|
||||||
|
throw new IllegalStateException();
|
||||||
|
|
||||||
|
T tmp = cur;
|
||||||
|
cur = cur.next;
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove() {
|
||||||
|
T tmp = cur.next;
|
||||||
|
head = Inlist.remove(head, cur);
|
||||||
|
cur = tmp;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<T> iterator() {
|
||||||
|
cur = head;
|
||||||
|
return mIterator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void push(T it) {
|
||||||
|
it.next = head;
|
||||||
|
head = it;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove(T it) {
|
||||||
|
cur = null;
|
||||||
|
head = Inlist.remove(head, it);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T clear() {
|
||||||
|
T ret = head;
|
||||||
|
head = null;
|
||||||
|
cur = null;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public T next;
|
public T next;
|
||||||
|
|
||||||
|
|||||||
@ -17,5 +17,37 @@
|
|||||||
package org.oscim.utils.pool;
|
package org.oscim.utils.pool;
|
||||||
|
|
||||||
public class LList<T> extends Inlist<LList<T>> {
|
public class LList<T> extends Inlist<LList<T>> {
|
||||||
T data;
|
public LList(T l) {
|
||||||
|
data = l;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final T data;
|
||||||
|
|
||||||
|
public static <E extends LList<T>, T> LList<T> find(LList<T> list, T item) {
|
||||||
|
for (LList<T> l = list; l != null; l = l.next)
|
||||||
|
if (l.data == item)
|
||||||
|
return l;
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <E extends LList<T>, T> LList<T> remove(LList<T> list, T item) {
|
||||||
|
if (list.data == item)
|
||||||
|
return list.next;
|
||||||
|
|
||||||
|
LList<T> prev = list;
|
||||||
|
for (LList<T> l = list.next; l != null; l = l.next)
|
||||||
|
if (l.data == item) {
|
||||||
|
prev.next = l.next;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T extends LList<T>> LList<T> push(LList<T> list, T item) {
|
||||||
|
item.next = list;
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user