Skip to content

Commit

Permalink
Added/enhanced the primitive List implementations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Gramlich committed May 7, 2012
1 parent 1e682be commit 1e59a3f
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/org/andengine/util/adt/list/FloatArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public void clear() {
this.mSize = 0;
}

@Override
public float[] toArray() {
final float[] array = new float[this.mSize];
System.arraycopy(this.mItems, 0, array, 0, this.mSize);
return array ;
}

// ===========================================================
// Methods
// ===========================================================
Expand Down
1 change: 1 addition & 0 deletions src/org/andengine/util/adt/list/IFloatList.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ public interface IFloatList {
public float remove(final int pIndex) throws ArrayIndexOutOfBoundsException;
public int size();
public void clear();
public float[] toArray();
}
26 changes: 26 additions & 0 deletions src/org/andengine/util/adt/list/IIntList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.andengine.util.adt.list;

/**
* (c) Zynga 2012
*
* @author Nicolas Gramlich <[email protected]>
* @since 19:21:53 - 03.05.2012
*/
public interface IIntList {
// ===========================================================
// Constants
// ===========================================================

// ===========================================================
// Methods
// ===========================================================

public boolean isEmpty();
public float get(final int pIndex) throws ArrayIndexOutOfBoundsException;
public void add(final int pItem);
public void add(final int pIndex, final int pItem) throws ArrayIndexOutOfBoundsException;
public float remove(final int pIndex) throws ArrayIndexOutOfBoundsException;
public int size();
public void clear();
public int[] toArray();
}
26 changes: 26 additions & 0 deletions src/org/andengine/util/adt/list/ILongList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.andengine.util.adt.list;

/**
* (c) Zynga 2012
*
* @author Nicolas Gramlich <[email protected]>
* @since 19:36:57 - 03.05.2012
*/
public interface ILongList {
// ===========================================================
// Constants
// ===========================================================

// ===========================================================
// Methods
// ===========================================================

public boolean isEmpty();
public float get(final int pIndex) throws ArrayIndexOutOfBoundsException;
public void add(final long pItem);
public void add(final int pIndex, final long pItem) throws ArrayIndexOutOfBoundsException;
public float remove(final int pIndex) throws ArrayIndexOutOfBoundsException;
public int size();
public void clear();
public long[] toArray();
}
122 changes: 122 additions & 0 deletions src/org/andengine/util/adt/list/IntArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package org.andengine.util.adt.list;

/**
* TODO This class could take some kind of AllocationStrategy object.
*
* (c) Zynga 2012
*
* @author Nicolas Gramlich <[email protected]>
* @since 19:22:29 - 03.05.2012
*/
public class IntArrayList implements IIntList {
// ===========================================================
// Constants
// ===========================================================

private static final int CAPACITY_INITIAL_DEFAULT = 0;

// ===========================================================
// Fields
// ===========================================================

private int[] mItems;
private int mSize;

// ===========================================================
// Constructors
// ===========================================================

public IntArrayList() {
this(IntArrayList.CAPACITY_INITIAL_DEFAULT);
}

public IntArrayList(final int pInitialCapacity) {
this.mItems = new int[pInitialCapacity];
}

// ===========================================================
// Getter & Setter
// ===========================================================

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

@Override
public boolean isEmpty() {
return this.mSize == 0;
}

@Override
public float get(final int pIndex) throws ArrayIndexOutOfBoundsException {
return this.mItems[pIndex];
}

@Override
public void add(final int pItem) {
this.ensureCapacity(this.mSize + 1);

this.mItems[this.mSize] = pItem;
this.mSize++;
}

@Override
public void add(final int pIndex, final int pItem) throws ArrayIndexOutOfBoundsException {
this.ensureCapacity(this.mSize + 1);

System.arraycopy(this.mItems, pIndex, this.mItems, pIndex + 1, this.mSize - pIndex);

this.mItems[pIndex] = pItem;
this.mSize++;
}

@Override
public float remove(final int pIndex) throws ArrayIndexOutOfBoundsException {
final float oldValue = this.mItems[pIndex];

final int numMoved = this.mSize - pIndex - 1;
if(numMoved > 0) {
System.arraycopy(this.mItems, pIndex + 1, this.mItems, pIndex, numMoved);
}

this.mSize--;

return oldValue;
}

@Override
public int size() {
return this.mSize;
}

@Override
public void clear() {
this.mSize = 0;
}

@Override
public int[] toArray() {
final int[] array = new int[this.mSize];
System.arraycopy(this.mItems, 0, array, 0, this.mSize);
return array ;
}

// ===========================================================
// Methods
// ===========================================================

private void ensureCapacity(final int pCapacity) {
final int currentCapacity = this.mItems.length;
if(currentCapacity < pCapacity) {
/* Increase array size. */
final int newCapacity = ((currentCapacity * 3) >> 1) + 1;
final int newItems[] = new int[newCapacity];
System.arraycopy(this.mItems, 0, newItems, 0, currentCapacity);
this.mItems = newItems;
}
}

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
122 changes: 122 additions & 0 deletions src/org/andengine/util/adt/list/LongArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package org.andengine.util.adt.list;

/**
* TODO This class could take some kind of AllocationStrategy object.
*
* (c) Zynga 2012
*
* @author Nicolas Gramlich <[email protected]>
* @since 19:35:59 - 03.05.2012
*/
public class LongArrayList implements ILongList {
// ===========================================================
// Constants
// ===========================================================

private static final int CAPACITY_INITIAL_DEFAULT = 0;

// ===========================================================
// Fields
// ===========================================================

private long[] mItems;
private int mSize;

// ===========================================================
// Constructors
// ===========================================================

public LongArrayList() {
this(LongArrayList.CAPACITY_INITIAL_DEFAULT);
}

public LongArrayList(final int pInitialCapacity) {
this.mItems = new long[pInitialCapacity];
}

// ===========================================================
// Getter & Setter
// ===========================================================

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

@Override
public boolean isEmpty() {
return this.mSize == 0;
}

@Override
public float get(final int pIndex) throws ArrayIndexOutOfBoundsException {
return this.mItems[pIndex];
}

@Override
public void add(final long pItem) {
this.ensureCapacity(this.mSize + 1);

this.mItems[this.mSize] = pItem;
this.mSize++;
}

@Override
public void add(final int pIndex, final long pItem) throws ArrayIndexOutOfBoundsException {
this.ensureCapacity(this.mSize + 1);

System.arraycopy(this.mItems, pIndex, this.mItems, pIndex + 1, this.mSize - pIndex);

this.mItems[pIndex] = pItem;
this.mSize++;
}

@Override
public float remove(final int pIndex) throws ArrayIndexOutOfBoundsException {
final float oldValue = this.mItems[pIndex];

final int numMoved = this.mSize - pIndex - 1;
if(numMoved > 0) {
System.arraycopy(this.mItems, pIndex + 1, this.mItems, pIndex, numMoved);
}

this.mSize--;

return oldValue;
}

@Override
public int size() {
return this.mSize;
}

@Override
public void clear() {
this.mSize = 0;
}

@Override
public long[] toArray() {
final long[] array = new long[this.mSize];
System.arraycopy(this.mItems, 0, array, 0, this.mSize);
return array ;
}

// ===========================================================
// Methods
// ===========================================================

private void ensureCapacity(final int pCapacity) {
final int currentCapacity = this.mItems.length;
if(currentCapacity < pCapacity) {
/* Increase array size. */
final int newCapacity = ((currentCapacity * 3) >> 1) + 1;
final long newItems[] = new long[newCapacity];
System.arraycopy(this.mItems, 0, newItems, 0, currentCapacity);
this.mItems = newItems;
}
}

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}

0 comments on commit 1e59a3f

Please sign in to comment.