diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2016-01-11 15:59:06 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-01-11 15:59:06 +0100 |
commit | f4b9af0c311bce96ee7b71000b3aa8ab9a2ab556 (patch) | |
tree | af808f802894e9e746bd17eb17b872d652adabe9 /core/src/ch/asynk/rustanddust/engine/util | |
parent | b2ba5d9fd27a562f5096e6c5eeacc81752376d73 (diff) | |
download | RustAndDust-f4b9af0c311bce96ee7b71000b3aa8ab9a2ab556.zip RustAndDust-f4b9af0c311bce96ee7b71000b3aa8ab9a2ab556.tar.gz |
IterableArray does not extends ArrayList but has it's own implementation
Diffstat (limited to 'core/src/ch/asynk/rustanddust/engine/util')
-rw-r--r-- | core/src/ch/asynk/rustanddust/engine/util/Collection.java | 12 | ||||
-rw-r--r-- | core/src/ch/asynk/rustanddust/engine/util/IterableArray.java | 117 |
2 files changed, 111 insertions, 18 deletions
diff --git a/core/src/ch/asynk/rustanddust/engine/util/Collection.java b/core/src/ch/asynk/rustanddust/engine/util/Collection.java index c768d45..a7b5a02 100644 --- a/core/src/ch/asynk/rustanddust/engine/util/Collection.java +++ b/core/src/ch/asynk/rustanddust/engine/util/Collection.java @@ -4,15 +4,21 @@ import java.util.Iterator; public interface Collection<E> extends Iterator, Iterable<E> { - public void clear(); - public int size(); public boolean isEmpty(); + public void clear(); + + public void ensureCapacity(int c); + + public boolean contains(E e); + public E get(int idx); public boolean add(E e); - // public boolean contains(E e); + public E remove(int idx); + + public boolean remove(E e); } diff --git a/core/src/ch/asynk/rustanddust/engine/util/IterableArray.java b/core/src/ch/asynk/rustanddust/engine/util/IterableArray.java index baa58d9..1b965fc 100644 --- a/core/src/ch/asynk/rustanddust/engine/util/IterableArray.java +++ b/core/src/ch/asynk/rustanddust/engine/util/IterableArray.java @@ -1,51 +1,138 @@ package ch.asynk.rustanddust.engine.util; -import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; -public class IterableArray<E> extends ArrayList<E> implements Collection<E> +public class IterableArray<E> implements Collection<E> { - private int i; + private int idx; private int s; + private int c; + transient E[] data; - public IterableArray() + @SuppressWarnings("unchecked") + public IterableArray(int capacity) + { + this.s = 0; + this.c = capacity; + this.data = (E[]) new Object[c]; + } + + @Override + public int size() + { + return s; + } + + @Override + public boolean isEmpty() + { + return (s == 0); + } + + @Override + public void clear() + { + for (int i = 0; i < s; i++) + data[i] = null; + s = 0; + } + + @Override + public void ensureCapacity(int min) + { + if (c > min) return; + c += (c >> 1); + if (c < min) + c = min; + data = Arrays.copyOf(data, c); + } + + @Override + public boolean contains(E e) + { + if (e == null) { + for (int i = 0; i < s; i++) { + if (data[i] == null) + return true; + } + } else { + for (int i = 0; i < s; i++) { + if (e.equals(data[i])) + return true; + } + } + return false; + } + + @Override + public E get(int i) + { + return data[i]; + } + + @Override + public boolean add(E e) { - super(); + ensureCapacity(s + 1); + data[s] = e; + s += 1; + return true; } - public IterableArray(int n) + @Override + public E remove(int i) + { + E e = data[i]; + int m = (s - i - 1); + if (m > 0) + System.arraycopy(data, i+1, data, i, m); + data[--s] = null; + + return e; + } + + @Override + public boolean remove(E e) { - super(n); + for (int i = 0; i < s; i++) { + if (e.equals(data[i])) { + int m = (s - i - 1); + if (m > 0) + System.arraycopy(data, i+1, data, i, m); + data[--s] = null; + return true; + } + } + return false; } @Override @SuppressWarnings("unchecked") public Iterator<E> iterator() { - this.i = 0; - this.s = size(); + this.idx = 0; return (Iterator<E>) this; } @Override public boolean hasNext() { - return (i < s); + return (idx < s); } @Override public E next() { - E e = get(i); - i += 1; + E e = get(idx); + idx += 1; return e; } @Override public void remove() { - i -=1; - s -= 1; - remove(i); + idx -= 1; + remove(idx); } } |