aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/collections/TinyIdentitySet.java
blob: f9fe365c4c735eaf6271d6bb2bd5f38f8b9e3e12 (plain) (blame)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.collections;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;

/**
 * A Set implementation which only considers object identity. It should only be
 * used for small number of objects, as it is implemented as scanning an
 * ArrayList for identity matches. In other words: Performance will only be
 * acceptable for <i>small</i> sets.
 *
 * The rationale for this class is the high cost of the object identifier used
 * in IdentityHashMap, where the key set is often used as an identity set.
 *
 * @author Steinar Knutsen
 * @see java.util.IdentityHashMap
 * @param <E> the type contained in the Set
 */
public final class TinyIdentitySet<E> implements Set<E> {
    private class ArrayIterator<T> implements Iterator<E> {
        private int i = -1;
        private boolean removed = false;

        @Override
        public boolean hasNext() {
            return i + 1 < size;
        }

        @SuppressWarnings("unchecked")
        @Override
        public E next() {
            if (!hasNext()) {
                throw new NoSuchElementException("No more elements available");
            }
            removed = false;
            return (E) entries[++i];
        }

        @Override
        public void remove() {
            if (removed) {
                throw new IllegalStateException(
                        "Trying to remove same element twice.");
            }
            if (i == -1) {
                throw new IllegalStateException(
                        "Trying to remove before entering iterator.");
            }
            delete(i--);
            removed = true;
        }

    }

    private Object[] entries;
    private int size = 0;

    /**
     * Create a set with an initial capacity of initSize. The internal array
     * will grow automatically with a linear growth rate if more elements than
     * initSize are added.
     *
     * @param initSize
     *            initial size of internal element array
     */
    public TinyIdentitySet(final int initSize) {
        entries = new Object[initSize];
    }

    /**
     * Expose the index in the internal array of a given object. -1 is returned
     * if the object is not present in the internal array.
     *
     * @param e
     *            an object to check whether exists in this set
     * @return the index of the argument e in the internal array, or -1 if the
     *         object is not present
     */
    public int indexOf(final Object e) {
        for (int i = 0; i < size; ++i) {
            if (e == entries[i]) {
                return i;
            }
        }
        return -1;
    }

    private void clean() {
        int offset = 0;
        for (int i = 0; i < size; ++i) {
            if (entries[i] == null) {
                ++offset;
            } else {
                entries[i - offset] = entries[i];
            }
        }
        size -= offset;
    }

    private void grow() {
        // linear growth, as we should always be working on small sets
        entries = Arrays.copyOf(entries, entries.length + 10);
    }

    private void append(final Object arg) {
        if (size == entries.length) {
            grow();
        }
        entries[size++] = arg;
    }

    @Override
    public boolean add(final E arg) {
        final int i = indexOf(arg);
        if (i >= 0) {
            return false;
        }
        append(arg);
        return true;
    }

    @Override
    public boolean addAll(final Collection<? extends E> arg) {
        boolean changed = false;
        for (final E entry : arg) {
            changed |= add(entry);
        }
        return changed;
    }

    @Override
    public void clear() {
        size = 0;
    }

    @Override
    public boolean contains(final Object arg) {
        return indexOf(arg) >= 0;
    }

    /**
     * This is an extremely expensive implementation of
     * {@link Set#containsAll(Collection)}. It is implemented as O(n**2).
     */
    @Override
    public boolean containsAll(final Collection<?> arg) {
        for (final Object entry : arg) {
            if (indexOf(entry) < 0) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public Iterator<E> iterator() {
        return new ArrayIterator<E>();
    }

    private void delete(int i) {
        if (i < 0 || i >= size) {
            return;
        }
        --size;
        while (i < size) {
            entries[i] = entries[i + 1];
            ++i;
        }
        entries[i] = null;
    }

    @Override
    public boolean remove(final Object arg) {
        final int i = indexOf(arg);
        if (i < 0) {
            return false;
        }
        delete(i);
        return true;
    }

    /**
     * This is an extremely expensive implementation of
     * {@link Set#removeAll(Collection)}. It is implemented as O(n**2).
     */
    @Override
    public boolean removeAll(final Collection<?> arg) {
        boolean changed = false;
        for (final Object entry : arg) {
            final int i = indexOf(entry);
            if (i >= 0) {
                entries[i] = null;
                changed = true;
            }
        }
        if (changed) {
            clean();
        }
        return changed;
    }

    /**
     * This is an extremely expensive implementation of
     * {@link Set#retainAll(Collection)}. It is implemented as O(n**2).
     */
    @Override
    public boolean retainAll(final Collection<?> arg) {
        boolean changed = false;
        for (int i = 0; i < size; ++i) {
            final Object entry = entries[i];
            boolean exists = false;
            // cannot use Collection.contains(), as we want identity
            for (final Object v : arg) {
                if (v == entry) {
                    exists = true;
                    break;
                }
            }
            if (!exists) {
                entries[i] = null;
                changed = true;
            }
        }
        if (changed) {
            clean();
        }
        return changed;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Object[] toArray() {
        return Arrays.copyOf(entries, size);
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> T[] toArray(final T[] arg) {
        return Arrays.copyOf(entries, size, (Class<T[]>) arg.getClass());
    }

}