From fb628f4e257b42b174eb50fd3e912d1170109581 Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Mon, 2 Jun 2014 12:51:47 +0200 Subject: [PATCH] Inlist: - add Inlist.List size() - make Inlist work items indirectly extending Inlist - check Inlist.push() argument to not push lists --- .../test/org/oscim/utils/pool/InlistTest.java | 68 +++++++++++++++ vtm/src/org/oscim/utils/pool/Inlist.java | 84 ++++++++++++------- 2 files changed, 121 insertions(+), 31 deletions(-) diff --git a/vtm-tests/test/org/oscim/utils/pool/InlistTest.java b/vtm-tests/test/org/oscim/utils/pool/InlistTest.java index 0e3ac8ef..4634635a 100644 --- a/vtm-tests/test/org/oscim/utils/pool/InlistTest.java +++ b/vtm-tests/test/org/oscim/utils/pool/InlistTest.java @@ -1,5 +1,6 @@ package org.oscim.utils.pool; +import static java.lang.System.out; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -75,6 +76,73 @@ public class InlistTest { assertEquals(i, 7); } + @Test + public void shouldRemoveFirstInIterator() { + List list = new List(); + list.push(new Thing(1)); + list.push(new Thing(2)); + list.push(new Thing(3)); + list.push(new Thing(4)); + list.push(new Thing(5)); + + out.println("\n shouldRemoveFirstInIterator"); + + int cnt = 5; + for (Thing t : list) { + list.remove(); + cnt--; + assertEquals(cnt, list.size()); + } + } + + @Test + public void shouldRemoveSomeInIterator() { + List list = new List(); + list.push(new Thing(1)); + list.push(new Thing(2)); + list.push(new Thing(3)); + list.push(new Thing(4)); + list.push(new Thing(5)); + list.push(new Thing(6)); + out.println("\n shouldRemoveSomeInIterator"); + + int pos = 0; + for (Thing t : list) { + if (pos++ % 2 == 0) { + out.println(pos + " val:" + t.value); + list.remove(); + } + } + + assertEquals(3, list.size()); + + for (Thing t : list) { + out.println(t.value); + } + } + + @Test + public void shouldRemoveLastInIterator() { + List list = new List(); + list.append(new Thing(1)); + list.append(new Thing(2)); + out.println("\n shouldRemoveLastInIterator"); + + int pos = 0; + for (Thing t : list) { + if (pos++ == 1) { + out.println(pos + " val:" + t.value); + list.remove(); + } + } + + assertEquals(1, list.size()); + + for (Thing t : list) { + out.println(t.value); + } + } + static class Thing extends Inlist { final int value; diff --git a/vtm/src/org/oscim/utils/pool/Inlist.java b/vtm/src/org/oscim/utils/pool/Inlist.java index 03b17ecd..cc7ad07a 100644 --- a/vtm/src/org/oscim/utils/pool/Inlist.java +++ b/vtm/src/org/oscim/utils/pool/Inlist.java @@ -140,9 +140,17 @@ public class Inlist> { /** Iterator: Remove current item */ @Override public void remove() { - T tmp = (T) cur.next; - head = Inlist.remove(head, cur); - cur = tmp; + /* iterator is at first position */ + if (head.next == cur) { + head = head.next; + return; + } + + Inlist prev = head; + while (prev.next.next != cur) + prev = prev.next; + + prev.next = cur; } /** NB: Only one iterator at a time possible! */ @@ -151,10 +159,18 @@ public class Inlist> { cur = head; return this; } + + public int size() { + return Inlist.size(head); + } } public T next; + public T next() { + return next; + } + /** * Push 'item' onto 'list'. * @@ -165,6 +181,9 @@ public class Inlist> { @SuppressWarnings({ "unchecked", "rawtypes" }) @CheckReturnValue public static > T push(T list, T item) { + if (item.next != null) + throw new IllegalArgumentException("'item' is a list"); + ((Inlist) (item)).next = list; return item; } @@ -175,9 +194,9 @@ public class Inlist> { * @param list the list * @return the number of items in 'list' */ - static > int size(T list) { + public static > int size(T list) { int count = 0; - for (T l = list; l != null; l = l.next) + for (Inlist l = list; l != null; l = l.next) count++; return count; } @@ -189,15 +208,16 @@ public class Inlist> { * @param item the item * @return the new head of 'list' */ + @SuppressWarnings({ "unchecked", "rawtypes" }) @CheckReturnValue - public static > T remove(T list, T item) { + public static > T remove(T list, T item) { if (item == list) { - T head = item.next; + Inlist head = item.next; item.next = null; - return head; + return (T) head; } - for (T prev = list, it = list.next; it != null; it = it.next) { + for (Inlist prev = list, it = list.next; it != null; it = it.next) { if (it == item) { prev.next = item.next; item.next = null; @@ -216,13 +236,14 @@ public class Inlist> { * @param i the index * @return the item or null */ + @SuppressWarnings({ "unchecked" }) @CheckReturnValue - public static > T get(T list, int i) { + public static > T get(T list, int i) { if (i < 0) return null; while (--i > 0 && list != null) - list = list.next; + list = (T) list.next; if (i == 0) return list; @@ -238,8 +259,9 @@ public class Inlist> { * @param item the item * @return the new head of 'list' */ + @SuppressWarnings({ "unchecked", "rawtypes" }) @CheckReturnValue - public static > T appendItem(T list, T item) { + public static > T appendItem(T list, T item) { if (item.next != null) throw new IllegalArgumentException("'item' is list"); @@ -247,11 +269,11 @@ public class Inlist> { if (list == null) return item; - T it = list; + Inlist it = list; while (it.next != null) it = it.next; - it.next = item; + ((Inlist) it).next = item; return list; } @@ -263,22 +285,24 @@ public class Inlist> { * @param other the other * @return the head of 'list' */ + @SuppressWarnings({ "rawtypes", "unchecked" }) @CheckReturnValue - public static > T appendList(T list, T other) { + public static T appendList(T list, T other) { if (list == null) return other; - if (list == other) + if (other == null) return list; - for (T it = list;; it = it.next) { + for (Inlist it = list;; it = it.next) { if (it.next == null) { - it.next = other; + ((Inlist) it).next = other; break; - } else if (it.next == other) { - throw new IllegalArgumentException("'other' already in 'list'"); } + //else if (it.next == other) { + // throw new IllegalArgumentException("'other' already in 'list'"); + //} } return list; @@ -290,12 +314,13 @@ public class Inlist> { * @param list the list * @return the last item */ + @SuppressWarnings("unchecked") @CheckReturnValue - public static > T last(T list) { + public static > T last(T list) { while (list != null) { if (list.next == null) return list; - list = list.next; + list = (T) list.next; } return null; } @@ -308,8 +333,9 @@ public class Inlist> { * @param other the other list * @return the new head of list */ + @SuppressWarnings({ "unchecked", "rawtypes" }) @CheckReturnValue - public static > T prependRelative(T list, T item, T other) { + public static > T prependRelative(T list, T item, T other) { if (item.next != null) throw new IllegalArgumentException("'item' is list"); @@ -318,25 +344,21 @@ public class Inlist> { throw new IllegalArgumentException("'list' is null"); if (list == other) { - item.next = list; + ((Inlist) item).next = list; return item; } T it = list; while (it != null && it.next != other) - it = it.next; + it = (T) it.next; if (it == null) throw new IllegalArgumentException("'other' not in 'list'"); - item.next = it.next; - it.next = item; + ((Inlist) item).next = it.next; + ((Inlist) it).next = item; return list; } - - public T next() { - return next; - } }