From b494b2e098b79b927bb1a5e5f8975d0572bad865 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Mon, 8 Oct 2018 11:20:34 +0200 Subject: ch.asynk.gdx.boardgame.util -> ch.asynk.gdx.boardgame.utils --- core/src/ch/asynk/gdx/boardgame/ui/Assembly.java | 2 +- .../ch/asynk/gdx/boardgame/util/Collection.java | 28 ---- .../ch/asynk/gdx/boardgame/util/IterableArray.java | 143 --------------------- .../ch/asynk/gdx/boardgame/util/IterableQueue.java | 19 --- .../ch/asynk/gdx/boardgame/util/IterableSet.java | 16 --- .../ch/asynk/gdx/boardgame/util/IterableStack.java | 26 ---- .../ch/asynk/gdx/boardgame/utils/Collection.java | 28 ++++ .../asynk/gdx/boardgame/utils/IterableArray.java | 143 +++++++++++++++++++++ .../asynk/gdx/boardgame/utils/IterableQueue.java | 19 +++ .../ch/asynk/gdx/boardgame/utils/IterableSet.java | 16 +++ .../asynk/gdx/boardgame/utils/IterableStack.java | 26 ++++ 11 files changed, 233 insertions(+), 233 deletions(-) delete mode 100644 core/src/ch/asynk/gdx/boardgame/util/Collection.java delete mode 100644 core/src/ch/asynk/gdx/boardgame/util/IterableArray.java delete mode 100644 core/src/ch/asynk/gdx/boardgame/util/IterableQueue.java delete mode 100644 core/src/ch/asynk/gdx/boardgame/util/IterableSet.java delete mode 100644 core/src/ch/asynk/gdx/boardgame/util/IterableStack.java create mode 100644 core/src/ch/asynk/gdx/boardgame/utils/Collection.java create mode 100644 core/src/ch/asynk/gdx/boardgame/utils/IterableArray.java create mode 100644 core/src/ch/asynk/gdx/boardgame/utils/IterableQueue.java create mode 100644 core/src/ch/asynk/gdx/boardgame/utils/IterableSet.java create mode 100644 core/src/ch/asynk/gdx/boardgame/utils/IterableStack.java diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Assembly.java b/core/src/ch/asynk/gdx/boardgame/ui/Assembly.java index c603383..5e71a46 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Assembly.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Assembly.java @@ -3,7 +3,7 @@ package ch.asynk.gdx.boardgame.ui; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; -import ch.asynk.gdx.boardgame.util.IterableSet; +import ch.asynk.gdx.boardgame.utils.IterableSet; public abstract class Assembly extends Element { diff --git a/core/src/ch/asynk/gdx/boardgame/util/Collection.java b/core/src/ch/asynk/gdx/boardgame/util/Collection.java deleted file mode 100644 index 1d7d5e4..0000000 --- a/core/src/ch/asynk/gdx/boardgame/util/Collection.java +++ /dev/null @@ -1,28 +0,0 @@ -package ch.asynk.gdx.boardgame.util; - -import java.util.Iterator; - -public interface Collection extends Iterator, Iterable -{ - 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/boardgame/util/IterableArray.java b/core/src/ch/asynk/gdx/boardgame/util/IterableArray.java deleted file mode 100644 index ba57de1..0000000 --- a/core/src/ch/asynk/gdx/boardgame/util/IterableArray.java +++ /dev/null @@ -1,143 +0,0 @@ -package ch.asynk.gdx.boardgame.util; - -import java.util.Arrays; -import java.util.Iterator; - -public class IterableArray implements Collection -{ - 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 iterator() - { - this.idx = 0; - return (Iterator) 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/boardgame/util/IterableQueue.java b/core/src/ch/asynk/gdx/boardgame/util/IterableQueue.java deleted file mode 100644 index 16ab442..0000000 --- a/core/src/ch/asynk/gdx/boardgame/util/IterableQueue.java +++ /dev/null @@ -1,19 +0,0 @@ -package ch.asynk.gdx.boardgame.util; - -public class IterableQueue extends IterableArray -{ - 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/boardgame/util/IterableSet.java b/core/src/ch/asynk/gdx/boardgame/util/IterableSet.java deleted file mode 100644 index 4132771..0000000 --- a/core/src/ch/asynk/gdx/boardgame/util/IterableSet.java +++ /dev/null @@ -1,16 +0,0 @@ -package ch.asynk.gdx.boardgame.util; - -public class IterableSet extends IterableArray -{ - 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/boardgame/util/IterableStack.java b/core/src/ch/asynk/gdx/boardgame/util/IterableStack.java deleted file mode 100644 index 7400718..0000000 --- a/core/src/ch/asynk/gdx/boardgame/util/IterableStack.java +++ /dev/null @@ -1,26 +0,0 @@ -package ch.asynk.gdx.boardgame.util; - -public class IterableStack extends IterableArray -{ - 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); - } -} diff --git a/core/src/ch/asynk/gdx/boardgame/utils/Collection.java b/core/src/ch/asynk/gdx/boardgame/utils/Collection.java new file mode 100644 index 0000000..ce22795 --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/utils/Collection.java @@ -0,0 +1,28 @@ +package ch.asynk.gdx.boardgame.utils; + +import java.util.Iterator; + +public interface Collection extends Iterator, Iterable +{ + 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/boardgame/utils/IterableArray.java b/core/src/ch/asynk/gdx/boardgame/utils/IterableArray.java new file mode 100644 index 0000000..b32e290 --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/utils/IterableArray.java @@ -0,0 +1,143 @@ +package ch.asynk.gdx.boardgame.utils; + +import java.util.Arrays; +import java.util.Iterator; + +public class IterableArray implements Collection +{ + 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 iterator() + { + this.idx = 0; + return (Iterator) 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/boardgame/utils/IterableQueue.java b/core/src/ch/asynk/gdx/boardgame/utils/IterableQueue.java new file mode 100644 index 0000000..d99f225 --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/utils/IterableQueue.java @@ -0,0 +1,19 @@ +package ch.asynk.gdx.boardgame.utils; + +public class IterableQueue extends IterableArray +{ + 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/boardgame/utils/IterableSet.java b/core/src/ch/asynk/gdx/boardgame/utils/IterableSet.java new file mode 100644 index 0000000..0d0cb95 --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/utils/IterableSet.java @@ -0,0 +1,16 @@ +package ch.asynk.gdx.boardgame.utils; + +public class IterableSet extends IterableArray +{ + 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/boardgame/utils/IterableStack.java b/core/src/ch/asynk/gdx/boardgame/utils/IterableStack.java new file mode 100644 index 0000000..bcacbfb --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/utils/IterableStack.java @@ -0,0 +1,26 @@ +package ch.asynk.gdx.boardgame.utils; + +public class IterableStack extends IterableArray +{ + 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); + } +} -- cgit v1.1-2-g2b99