summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/gdx/boardgame/utils/IterableArray.java
blob: 8ec7eb2317717c829f9978c921645d9060c9aac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package ch.asynk.gdx.boardgame.utils;

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 boolean replace(E e, int idx)
    {
        if (idx < s) {
            data[idx] = e;
            return true;
        }
        return false;
    }

    @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;
    }

    @Override public Iterator<E> iterator()
    {
        this.idx = 0;
        return this;
    }

    @Override public boolean hasNext()
    {
        return (idx < s);
    }

    @Override public E current()
    {
        return get(idx);
    }

    @Override public E next()
    {
        E e = get(idx);
        idx += 1;
        return e;
    }

    @Override public void remove()
    {
        idx -= 1;
        remove(idx);
    }
}