1 19 package gnu.trove; 20 21 import java.io.IOException; 22 import java.io.ObjectInputStream; 23 import java.io.ObjectOutputStream; 24 import java.io.Serializable; 25 import java.io.Serializable; 26 import java.util.Arrays; 27 28 36 37 public class TIntHashSet extends TIntHash implements Serializable { 38 39 40 static final long serialVersionUID = 1L; 41 42 46 public TIntHashSet() { 47 super(); 48 } 49 50 57 public TIntHashSet(int initialCapacity) { 58 super(initialCapacity); 59 } 60 61 69 public TIntHashSet(int initialCapacity, float loadFactor) { 70 super(initialCapacity, loadFactor); 71 } 72 73 79 public TIntHashSet(int[] array) { 80 this(array.length); 81 addAll(array); 82 } 83 84 89 public TIntHashSet(TIntHashingStrategy strategy) { 90 super(strategy); 91 } 92 93 101 public TIntHashSet(int initialCapacity, TIntHashingStrategy strategy) { 102 super(initialCapacity, strategy); 103 } 104 105 114 public TIntHashSet(int initialCapacity, float loadFactor, TIntHashingStrategy strategy) { 115 super(initialCapacity, loadFactor, strategy); 116 } 117 118 125 public TIntHashSet(int[] array, TIntHashingStrategy strategy) { 126 this(array.length, strategy); 127 addAll(array); 128 } 129 130 133 public TIntIterator iterator() { 134 return new TIntIterator(this); 135 } 136 137 143 public boolean add(int val) { 144 int index = insertionIndex(val); 145 146 if (index < 0) { 147 return false; } 149 150 byte previousState = _states[index]; 151 _set[index] = val; 152 _states[index] = FULL; 153 postInsertHook(previousState == FREE); 154 155 return true; } 157 158 163 protected void rehash(int newCapacity) { 164 int oldCapacity = _set.length; 165 int oldSet[] = _set; 166 byte oldStates[] = _states; 167 168 _set = new int[newCapacity]; 169 _states = new byte[newCapacity]; 170 171 for (int i = oldCapacity; i-- > 0;) { 172 if(oldStates[i] == FULL) { 173 int o = oldSet[i]; 174 int index = insertionIndex(o); 175 _set[index] = o; 176 _states[index] = FULL; 177 } 178 } 179 } 180 181 186 public int[] toArray() { 187 int[] result = new int[size()]; 188 int[] set = _set; 189 byte[] states = _states; 190 191 for (int i = states.length, j = 0; i-- > 0;) { 192 if (states[i] == FULL) { 193 result[j++] = set[i]; 194 } 195 } 196 return result; 197 } 198 199 202 public void clear() { 203 super.clear(); 204 int[] set = _set; 205 byte[] states = _states; 206 207 for (int i = set.length; i-- > 0;) { 208 set[i] = (int)0; 209 states[i] = FREE; 210 } 211 } 212 213 220 public boolean equals(Object other) { 221 if (! (other instanceof TIntHashSet)) { 222 return false; 223 } 224 final TIntHashSet that = (TIntHashSet)other; 225 if (that.size() != this.size()) { 226 return false; 227 } 228 return forEach(new TIntProcedure() { 229 public final boolean execute(int value) { 230 return that.contains(value); 231 } 232 }); 233 } 234 235 public int hashCode() { 236 HashProcedure p = new HashProcedure(); 237 forEach(p); 238 return p.getHashCode(); 239 } 240 241 private final class HashProcedure implements TIntProcedure { 242 private int h = 0; 243 244 public int getHashCode() { 245 return h; 246 } 247 248 public final boolean execute(int key) { 249 h += _hashingStrategy.computeHashCode(key); 250 return true; 251 } 252 } 253 254 260 public boolean remove(int val) { 261 int index = index(val); 262 if (index >= 0) { 263 removeAt(index); 264 return true; 265 } 266 return false; 267 } 268 269 276 public boolean containsAll(int[] array) { 277 for (int i = array.length; i-- > 0;) { 278 if (! contains(array[i])) { 279 return false; 280 } 281 } 282 return true; 283 } 284 285 291 public boolean addAll(int[] array) { 292 boolean changed = false; 293 for (int i = array.length; i-- > 0;) { 294 if (add(array[i])) { 295 changed = true; 296 } 297 } 298 return changed; 299 } 300 301 307 public boolean removeAll(int[] array) { 308 boolean changed = false; 309 for (int i = array.length; i-- > 0;) { 310 if (remove(array[i])) { 311 changed = true; 312 } 313 } 314 return changed; 315 } 316 317 324 public boolean retainAll(int[] array) { 325 boolean changed = false; 326 Arrays.sort(array); 327 int[] set = _set; 328 byte[] states = _states; 329 330 for (int i = set.length; i-- > 0;) { 331 if (states[i] == FULL && (Arrays.binarySearch(array,set[i]) < 0)) { 332 remove(set[i]); 333 changed = true; 334 } 335 } 336 return changed; 337 } 338 339 private void writeObject(ObjectOutputStream stream) 340 throws IOException { 341 stream.defaultWriteObject(); 342 343 stream.writeInt(_size); 345 346 SerializationProcedure writeProcedure = new SerializationProcedure(stream); 347 if (! forEach(writeProcedure)) { 348 throw writeProcedure.exception; 349 } 350 } 351 352 private void readObject(ObjectInputStream stream) 353 throws IOException, ClassNotFoundException { 354 stream.defaultReadObject(); 355 356 int size = stream.readInt(); 357 setUp(size); 358 while (size-- > 0) { 359 int val = stream.readInt(); 360 add(val); 361 } 362 } 363 } | Popular Tags |