summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/gdx/board/util
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2018-09-14 09:13:20 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2018-09-14 09:13:20 +0200
commit76c4a8eccdc276583c215e5ad09b5fb2f07ac53e (patch)
tree8e026033a22f2b4d3dfe02235ab40d897309321e /core/src/ch/asynk/gdx/board/util
parenta3cc66dc1fe2f468b234083baacd82a37c51fd3f (diff)
downloadgdx-boardgame-76c4a8eccdc276583c215e5ad09b5fb2f07ac53e.zip
gdx-boardgame-76c4a8eccdc276583c215e5ad09b5fb2f07ac53e.tar.gz
ch/asynk/zproject/engine -> ch/asynk/gdx/board
Diffstat (limited to 'core/src/ch/asynk/gdx/board/util')
-rw-r--r--core/src/ch/asynk/gdx/board/util/Collection.java28
-rw-r--r--core/src/ch/asynk/gdx/board/util/IterableArray.java143
-rw-r--r--core/src/ch/asynk/gdx/board/util/IterableQueue.java19
-rw-r--r--core/src/ch/asynk/gdx/board/util/IterableSet.java16
-rw-r--r--core/src/ch/asynk/gdx/board/util/IterableStack.java26
5 files changed, 232 insertions, 0 deletions
diff --git a/core/src/ch/asynk/gdx/board/util/Collection.java b/core/src/ch/asynk/gdx/board/util/Collection.java
new file mode 100644
index 0000000..8aadadf
--- /dev/null
+++ b/core/src/ch/asynk/gdx/board/util/Collection.java
@@ -0,0 +1,28 @@
+package ch.asynk.gdx.board.util;
+
+import java.util.Iterator;
+
+public interface Collection<E> extends Iterator, Iterable<E>
+{
+ 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 int indexOf(E e);
+
+ public boolean add(E e);
+
+ public boolean insert(E e, int idx);
+
+ public E remove(int idx);
+
+ public boolean remove(E e);
+}
diff --git a/core/src/ch/asynk/gdx/board/util/IterableArray.java b/core/src/ch/asynk/gdx/board/util/IterableArray.java
new file mode 100644
index 0000000..2dc4481
--- /dev/null
+++ b/core/src/ch/asynk/gdx/board/util/IterableArray.java
@@ -0,0 +1,143 @@
+package ch.asynk.gdx.board.util;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+public class IterableArray<E> implements Collection<E>
+{
+ private int idx;
+ private int s;
+ private int c;
+ transient E[] data;
+
+ @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 int indexOf(E e)
+ {
+ for (int i = 0; i < data.length; i++) {
+ if (data[i] != null && data[i].equals(e))
+ return i;
+ }
+ return -1;
+ }
+
+ @Override public boolean add(E e)
+ {
+ ensureCapacity(s + 1);
+ data[s] = e;
+ s += 1;
+ return true;
+ }
+
+ @Override public boolean insert(E e, int idx)
+ {
+ ensureCapacity(s + 1);
+ System.arraycopy(data, idx, data, idx+1, (s - idx));
+ data[idx] = e;
+ s += 1;
+ return true;
+ }
+
+ @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)
+ {
+ 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;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override public Iterator<E> iterator()
+ {
+ this.idx = 0;
+ return (Iterator<E>) this;
+ }
+
+ @Override public boolean hasNext()
+ {
+ return (idx < s);
+ }
+
+ @Override public E next()
+ {
+ E e = get(idx);
+ idx += 1;
+ return e;
+ }
+
+ @Override public void remove()
+ {
+ idx -= 1;
+ remove(idx);
+ }
+}
diff --git a/core/src/ch/asynk/gdx/board/util/IterableQueue.java b/core/src/ch/asynk/gdx/board/util/IterableQueue.java
new file mode 100644
index 0000000..8a9a74f
--- /dev/null
+++ b/core/src/ch/asynk/gdx/board/util/IterableQueue.java
@@ -0,0 +1,19 @@
+package ch.asynk.gdx.board.util;
+
+public class IterableQueue<E> extends IterableArray<E>
+{
+ public IterableQueue(int n)
+ {
+ super(n);
+ }
+
+ public void enqueue(E e)
+ {
+ add(e);
+ }
+
+ public E dequeue()
+ {
+ return remove(0);
+ }
+}
diff --git a/core/src/ch/asynk/gdx/board/util/IterableSet.java b/core/src/ch/asynk/gdx/board/util/IterableSet.java
new file mode 100644
index 0000000..7e37f4e
--- /dev/null
+++ b/core/src/ch/asynk/gdx/board/util/IterableSet.java
@@ -0,0 +1,16 @@
+package ch.asynk.gdx.board.util;
+
+public class IterableSet<E> extends IterableArray<E>
+{
+ public IterableSet(int n)
+ {
+ super(n);
+ }
+
+ @Override public boolean add(E e)
+ {
+ if (contains(e)) return false;
+ super.add(e);
+ return true;
+ }
+}
diff --git a/core/src/ch/asynk/gdx/board/util/IterableStack.java b/core/src/ch/asynk/gdx/board/util/IterableStack.java
new file mode 100644
index 0000000..1365408
--- /dev/null
+++ b/core/src/ch/asynk/gdx/board/util/IterableStack.java
@@ -0,0 +1,26 @@
+package ch.asynk.gdx.board.util;
+
+public class IterableStack<E> extends IterableArray<E>
+{
+ public IterableStack(int n)
+ {
+ super(n);
+ }
+
+ public void push(E e)
+ {
+ add(e);
+ }
+
+ public E pop()
+ {
+ if (size() <= 0) return null;
+ return remove(size() - 1);
+ }
+
+ public E getTop()
+ {
+ if (size() <= 0) return null;
+ return get(size() - 1);
+ }
+}