diff --git a/src/org/oscim/utils/pool/Inlist.java b/src/org/oscim/utils/pool/Inlist.java index 71fc2d77..fc491dd9 100644 --- a/src/org/oscim/utils/pool/Inlist.java +++ b/src/org/oscim/utils/pool/Inlist.java @@ -14,6 +14,8 @@ */ package org.oscim.utils.pool; + + public class Inlist> { private final static boolean debug = false; @@ -44,7 +46,34 @@ public class Inlist> { } - public static > T prepend(T list, T item) { + public static > T get(T list, int i){ + if (i < 0) + return null; + + while (--i > 0 && list != null) + list = list.next; + + if (i == 0) + return list; + + return null; + } + +// public static > T insertAt(T list, T item, int i){ +// if (i < 0) +// return null; +// +// while (--i > 0 && list != null) +// list = list.next; +// +// if (i == 0) +// return list; +// +// return null; +// } + + + public static > T push(T list, T item) { item.next = list; return item; } @@ -70,4 +99,36 @@ public class Inlist> { return list; } + + public static > T prependRelative(T list, T item, T other) { + + if (debug) { + if (item.next != null) { + // warn + item.next = null; + } + } + + if (list == null) + throw new IllegalArgumentException("Inlist.prependRelative 'list' is null"); + + + if (list == other){ + item.next = list; + return item; + } + + Inlist it = list; + + while (it != null && it.next != other) + it = it.next; + + if (it == null) + throw new IllegalArgumentException("Inlist.prependRelative 'other' not in 'list'"); + + item.next = it.next; + it.next = item; + + return list; + } }