Skip to content

Commit

Permalink
Merge pull request #3332 from GreenLightning/enhancement
Browse files Browse the repository at this point in the history
Add size() method to PooledLinkedList.
  • Loading branch information
xoppa committed Aug 18, 2015
2 parents d681cff + b1a6b85 commit 9b7d1ad
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 31 deletions.
39 changes: 8 additions & 31 deletions gdx/src/com/badlogic/gdx/utils/PooledLinkedList.java
Expand Up @@ -35,12 +35,14 @@ static final class Item<T> {

public PooledLinkedList (int maxPoolSize) {
this.pool = new Pool<Item<T>>(16, maxPoolSize) {
@Override
protected Item<T> newObject () {
return new Item<T>();
}
};
}

/** Adds the specified object to the end of the list regardless of iteration status */
public void add (T object) {
Item<T> item = pool.obtain();
item.payload = object;
Expand All @@ -60,6 +62,11 @@ public void add (T object) {
size++;
}

/** Returns the number of items in the list */
public int size () {
return size;
}

/** Starts iterating over the list's items from the head of the list */
public void iter () {
iter = head;
Expand Down Expand Up @@ -128,41 +135,11 @@ public void remove () {
n.prev = p;
}

// public static void main (String[] argv) {
// PooledLinkedList<Integer> list = new PooledLinkedList<Integer>(10);
//
// list.add(1);
// list.add(2);
// list.add(3);
// list.add(4);
// list.iter();
// list.next();
// list.next();
// list.remove();
// list.next();
// list.next();
// list.remove();
//
// list.iter();
// Integer v = null;
// while ((v = list.next()) != null)
// System.out.println(v);
//
// list.iter();
// list.next();
// list.next();
// list.remove();
//
// list.iter();
// list.next();
// list.remove();
// }

public void clear () {
iter();
T v = null;
while ((v = next()) != null)
remove();

}

}
68 changes: 68 additions & 0 deletions gdx/test/com/badlogic/gdx/utils/PooledLinkedListTest.java
@@ -0,0 +1,68 @@
package com.badlogic.gdx.utils;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class PooledLinkedListTest {

private PooledLinkedList<Integer> list;

@Before
public void setUp () {
list = new PooledLinkedList<Integer>(10);
list.add(1);
list.add(2);
list.add(3);
}

@Test
public void size () {
assertEquals(3, list.size());
list.iter();
list.next();
list.remove();
assertEquals(2, list.size());
}

@Test
public void iteration () {
list.iter();
assertEquals(Integer.valueOf(1), list.next());
assertEquals(Integer.valueOf(2), list.next());
assertEquals(Integer.valueOf(3), list.next());
assertNull(list.next());
}

@Test
public void reverseIteration () {
list.iterReverse();
assertEquals(Integer.valueOf(3), list.previous());
assertEquals(Integer.valueOf(2), list.previous());
assertEquals(Integer.valueOf(1), list.previous());
assertNull(list.previous());
}

@Test
public void remove () {
list.iter();
list.next(); // 1
list.remove();
list.next(); // 2
list.next(); // 3
list.remove();
list.iter();
assertEquals(Integer.valueOf(2), list.next());
assertNull(list.next());
}

@Test
public void clear () {
list.clear();
assertEquals(0, list.size());
list.iter();
assertNull(list.next());
}

}

0 comments on commit 9b7d1ad

Please sign in to comment.