Debian Codegraph @

debian/additionalSrc/trove4j/util/src/gnu/trove/generate/TPHashSet.template

raw
1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2001, Eric D. Friedman All Rights Reserved.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 ///////////////////////////////////////////////////////////////////////////////
18 // THIS FILE IS AUTOGENERATED, PLEASE DO NOT EDIT OR ELSE
19 package gnu.trove;
20
21 import java.io.IOException;
22 import java.io.ObjectInputStream;
23 import java.io.ObjectOutputStream;
24 import java.util.Arrays;
25
26 /**
27 * An open addressed set implementation for #e# primitives.
28 *
29 * Created: Sat Nov 3 10:38:17 2001
30 *
31 * @author Eric D. Friedman
32 */
33
34 public class T#E#HashSet extends T#E#Hash {
35
36 /**
37 * Creates a new <code>T#E#HashSet</code> instance with the default
38 * capacity and load factor.
39 */
40 public T#E#HashSet() {
41 }
42
43 /**
44 * Creates a new <code>T#E#HashSet</code> instance with a prime
45 * capacity equal to or greater than <tt>initialCapacity</tt> and
46 * with the default load factor.
47 *
48 * @param initialCapacity an <code>int</code> value
49 */
50 public T#E#HashSet(int initialCapacity) {
51 super(initialCapacity);
52 }
53
54 /**
55 * Creates a new <code>T#E#HashSet</code> instance with a prime
56 * capacity equal to or greater than <tt>initialCapacity</tt> and
57 * with the specified load factor.
58 *
59 * @param initialCapacity an <code>int</code> value
60 * @param loadFactor a <code>float</code> value
61 */
62 public T#E#HashSet(int initialCapacity, float loadFactor) {
63 super(initialCapacity, loadFactor);
64 }
65
66 /**
67 * Creates a new <code>T#E#HashSet</code> instance containing the
68 * elements of <tt>array</tt>.
69 *
70 * @param array an array of <code>#e#</code> primitives
71 */
72 public T#E#HashSet(#e#[] array) {
73 this(array.length);
74 addAll(array);
75 }
76
77 /**
78 * Creates a new <code>T#E#Hash</code> instance with the default
79 * capacity and load factor.
80 * @param strategy used to compute hash codes and to compare keys.
81 */
82 public T#E#HashSet(T#E#HashingStrategy strategy) {
83 super(strategy);
84 }
85
86 /**
87 * Creates a new <code>T#E#Hash</code> instance whose capacity
88 * is the next highest prime above <tt>initialCapacity + 1</tt>
89 * unless that value is already prime.
90 *
91 * @param initialCapacity an <code>int</code> value
92 * @param strategy used to compute hash codes and to compare keys.
93 */
94 public T#E#HashSet(int initialCapacity, T#E#HashingStrategy strategy) {
95 super(initialCapacity, strategy);
96 }
97
98 /**
99 * Creates a new <code>T#E#Hash</code> instance with a prime
100 * value at or near the specified capacity and load factor.
101 *
102 * @param initialCapacity used to find a prime capacity for the table.
103 * @param loadFactor used to calculate the threshold over which
104 * rehashing takes place.
105 * @param strategy used to compute hash codes and to compare keys.
106 */
107 public T#E#HashSet(int initialCapacity, float loadFactor, T#E#HashingStrategy strategy) {
108 super(initialCapacity, loadFactor, strategy);
109 }
110
111 /**
112 * Creates a new <code>T#E#HashSet</code> instance containing the
113 * elements of <tt>array</tt>.
114 *
115 * @param array an array of <code>#e#</code> primitives
116 * @param strategy used to compute hash codes and to compare keys.
117 */
118 public T#E#HashSet(#e#[] array, T#E#HashingStrategy strategy) {
119 this(array.length, strategy);
120 addAll(array);
121 }
122
123 /**
124 * @return a T#E#Iterator with access to the values in this set
125 */
126 public T#E#Iterator iterator() {
127 return new T#E#Iterator(this);
128 }
129
130 /**
131 * Inserts a value into the set.
132 *
133 * @param val an <code>#e#</code> value
134 * @return true if the set was modified by the add operation
135 */
136 public boolean add(#e# val) {
137 int index = insertionIndex(val);
138
139 if (index < 0) {
140 return false; // already present in set, nothing to add
141 }
142
143 byte previousState = _states[index];
144 _set[index] = val;
145 _states[index] = FULL;
146 postInsertHook(previousState == FREE);
147
148 return true; // yes, we added something
149 }
150
151 /**
152 * Expands the set to accommodate new values.
153 *
154 * @param newCapacity an <code>int</code> value
155 */
156 @Override
157 protected void rehash(int newCapacity) {
158 int oldCapacity = capacity();
159 #e#[] oldSet = _set;
160 byte[] oldStates = _states;
161
162 _set = new #e#[newCapacity];
163 _states = new byte[newCapacity];
164
165 for (int i = oldCapacity; i-- > 0;) {
166 if(oldStates[i] == FULL) {
167 #e# o = oldSet[i];
168 int index = insertionIndex(o);
169 _set[index] = o;
170 _states[index] = FULL;
171 }
172 }
173 }
174
175 /**
176 * Returns a new array containing the values in the set.
177 *
178 * @return an <code>#e#[]</code> value
179 */
180 public #e#[] toArray() {
181 #e#[] result = new #e#[size()];
182 #e#[] set = _set;
183 byte[] states = _states;
184 if (states != null) {
185 for (int i = states.length, j = 0; i-- > 0;) {
186 if (states[i] == FULL) {
187 result[j++] = set[i];
188 }
189 }
190 }
191 return result;
192 }
193
194 /**
195 * Empties the set.
196 */
197 @Override
198 public void clear() {
199 super.clear();
200 #e#[] set = _set;
201 byte[] states = _states;
202 if (states != null) {
203 for (int i = set.length; i-- > 0;) {
204 set[i] = (#e#)0;
205 states[i] = FREE;
206 }
207 }
208 }
209
210 /**
211 * Compares this set with another set for equality of their stored
212 * entries.
213 *
214 * @param other an <code>Object</code> value
215 * @return a <code>boolean</code> value
216 */
217 @Override
218 public boolean equals(Object other) {
219 if (!(other instanceof T#E#HashSet)) {
220 return false;
221 }
222 final T#E#HashSet that = (T#E#HashSet)other;
223 if (that.size() != size()) {
224 return false;
225 }
226 return forEach(new T#E#Procedure() {
227 public final boolean execute(#e# value) {
228 return that.contains(value);
229 }
230 });
231 }
232
233 @Override
234 public int hashCode() {
235 HashProcedure p = new HashProcedure();
236 forEach(p);
237 return p.getHashCode();
238 }
239
240 private final class HashProcedure implements T#E#Procedure {
241 private int h;
242
243 HashProcedure() {
244 }
245
246 public int getHashCode() {
247 return h;
248 }
249
250 public final boolean execute(#e# key) {
251 h += _hashingStrategy.computeHashCode(key);
252 return true;
253 }
254 }
255
256 /**
257 * Removes <tt>val</tt> from the set.
258 *
259 * @param val an <code>#e#</code> value
260 * @return true if the set was modified by the remove operation.
261 */
262 public boolean remove(#e# val) {
263 int index = index(val);
264 if (index >= 0) {
265 removeAt(index);
266 return true;
267 }
268 return false;
269 }
270
271 /**
272 * Tests the set to determine if all of the elements in
273 * <tt>array</tt> are present.
274 *
275 * @param array an <code>array</code> of #e# primitives.
276 * @return true if all elements were present in the set.
277 */
278 public boolean containsAll(#e#[] array) {
279 for (int i = array.length; i-- > 0;) {
280 if (!contains(array[i])) {
281 return false;
282 }
283 }
284 return true;
285 }
286
287 /**
288 * Adds all of the elements in <tt>array</tt> to the set.
289 *
290 * @param array an <code>array</code> of #e# primitives.
291 * @return true if the set was modified by the add all operation.
292 */
293 public boolean addAll(#e#[] array) {
294 boolean changed = false;
295 for (int i = array.length; i-- > 0;) {
296 if (add(array[i])) {
297 changed = true;
298 }
299 }
300 return changed;
301 }
302
303 /**
304 * Removes all of the elements in <tt>array</tt> from the set.
305 *
306 * @param array an <code>array</code> of #e# primitives.
307 * @return true if the set was modified by the remove all operation.
308 */
309 public boolean removeAll(#e#[] array) {
310 boolean changed = false;
311 for (int i = array.length; i-- > 0;) {
312 if (remove(array[i])) {
313 changed = true;
314 }
315 }
316 return changed;
317 }
318
319 /**
320 * Removes any values in the set which are not contained in
321 * <tt>array</tt>.
322 *
323 * @param array an <code>array</code> of #e# primitives.
324 * @return true if the set was modified by the retain all operation
325 */
326 public boolean retainAll(#e#[] array) {
327 Arrays.sort(array);
328 #e#[] set = _set;
329 byte[] states = _states;
330
331 boolean changed = false;
332 if (set != null) {
333 for (int i = set.length; i-- > 0;) {
334 if (states[i] == FULL && Arrays.binarySearch(array,set[i]) < 0) {
335 remove(set[i]);
336 changed = true;
337 }
338 }
339 }
340 return changed;
341 }
342
343 private void writeObject(ObjectOutputStream stream)
344 throws IOException {
345 stream.defaultWriteObject();
346
347 // number of entries
348 stream.writeInt(_size);
349
350 SerializationProcedure writeProcedure = new SerializationProcedure(stream);
351 if (!forEach(writeProcedure)) {
352 throw writeProcedure.exception;
353 }
354 }
355
356 private void readObject(ObjectInputStream stream)
357 throws IOException, ClassNotFoundException {
358 stream.defaultReadObject();
359
360 int size = stream.readInt();
361 setUp(size);
362 while (size-- > 0) {
363 #e# val = stream.read#E#();
364 add(val);
365 }
366 }
367
368 @Override
369 public String toString() {
370 final StringBuilder sb = new StringBuilder();
371 forEach(new T#E#Procedure() {
372 public boolean execute(#e# value) {
373 if (sb.length() != 0) {
374 sb.append(',').append(' ');
375 }
376 sb.append(value);
377 return true;
378 }
379 });
380 sb.append(']');
381 sb.insert(0, '[');
382 return sb.toString();
383 }
384
385 } // T#E#HashSet