1 19 package gnu.trove; 20 21 import java.io.*; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.Set ; 25 import java.util.Arrays ; 26 import java.lang.reflect.Array ; 27 28 37 38 public class THashSet<E> extends TObjectHash<E> implements Set <E>, Externalizable { 39 static final long serialVersionUID = 1L; 40 41 45 public THashSet() { 46 super(); 47 } 48 49 55 public THashSet(TObjectHashingStrategy<E> strategy) { 56 super(strategy); 57 } 58 59 66 public THashSet(int initialCapacity) { 67 super(initialCapacity); 68 } 69 70 78 public THashSet(int initialCapacity, TObjectHashingStrategy<E> strategy) { 79 super(initialCapacity, strategy); 80 } 81 82 90 public THashSet(int initialCapacity, float loadFactor) { 91 super(initialCapacity, loadFactor); 92 } 93 94 103 public THashSet(int initialCapacity, float loadFactor, TObjectHashingStrategy<E> strategy) { 104 super(initialCapacity, loadFactor, strategy); 105 } 106 107 113 public THashSet(Collection <? extends E> collection) { 114 this(collection.size()); 115 addAll(collection); 116 } 117 118 125 public THashSet(Collection <? extends E> collection, TObjectHashingStrategy<E> strategy) { 126 this(collection.size(), strategy); 127 addAll(collection); 128 } 129 130 136 public boolean add(E obj) { 137 int index = insertionIndex(obj); 138 139 if (index < 0) { 140 return false; } 142 143 Object old = _set[index]; 144 _set[index] = obj; 145 146 postInsertHook(old == FREE); 147 return true; } 149 150 public boolean equals(Object other) { 151 if (! (other instanceof Set )) { 152 return false; 153 } 154 Set that = (Set )other; 155 if (that.size() != this.size()) { 156 return false; 157 } 158 return containsAll(that); 159 } 160 161 public int hashCode() { 162 HashProcedure p = new HashProcedure(); 163 forEach(p); 164 return p.getHashCode(); 165 } 166 167 private final class HashProcedure implements TObjectProcedure<E> { 168 private int h = 0; 169 170 public int getHashCode() { 171 return h; 172 } 173 174 public final boolean execute(E key) { 175 h += _hashingStrategy.computeHashCode(key); 176 return true; 177 } 178 } 179 180 185 protected void rehash(int newCapacity) { 186 int oldCapacity = _set.length; 187 Object oldSet[] = _set; 188 189 _set = new Object [newCapacity]; 190 Arrays.fill(_set, FREE); 191 192 for (int i = oldCapacity; i-- > 0;) { 193 if(oldSet[i] != FREE && oldSet[i] != REMOVED) { 194 E o = (E) oldSet[i]; 195 int index = insertionIndex(o); 196 if (index < 0) { throwObjectContractViolation(_set[(-index -1)], o); 198 } 199 _set[index] = o; 200 } 201 } 202 } 203 204 209 public Object [] toArray() { 210 Object [] result = new Object [size()]; 211 forEach(new ToObjectArrayProcedure(result)); 212 return result; 213 } 214 215 221 public <T> T[] toArray(T[] a) { 222 int size = size(); 223 if (a.length < size) 224 a = (T[]) Array.newInstance(a.getClass().getComponentType(), size); 225 226 forEach(new ToObjectArrayProcedure(a)); 227 228 236 if (a.length > size) { 237 a[size] = null; 238 } 239 240 return a; 241 } 242 243 246 public void clear() { 247 super.clear(); 248 Object [] set = _set; 249 250 for (int i = set.length; i-- > 0;) { 251 set[i] = FREE; 252 } 253 } 254 255 261 public boolean remove(Object obj) { 262 int index = index((E) obj); 263 if (index >= 0) { 264 removeAt(index); 265 return true; 266 } 267 return false; 268 } 269 270 276 public Iterator <E> iterator() { 277 return new TObjectHashIterator<E>(this); 278 } 279 280 287 public boolean containsAll(Collection <?> collection) { 288 for (Iterator i = collection.iterator(); i.hasNext();) { 289 if (! contains(i.next())) { 290 return false; 291 } 292 } 293 return true; 294 } 295 296 302 public boolean addAll(Collection <? extends E> collection) { 303 boolean changed = false; 304 int size = collection.size(); 305 306 ensureCapacity(size); 307 Iterator <? extends E> it = collection.iterator(); 308 while (size-- > 0) { 309 if (add(it.next())) { 310 changed = true; 311 } 312 } 313 return changed; 314 } 315 316 322 public boolean removeAll(Collection <?> collection) { 323 boolean changed = false; 324 int size = collection.size(); 325 Iterator it; 326 327 it = collection.iterator(); 328 while (size-- > 0) { 329 if (remove(it.next())) { 330 changed = true; 331 } 332 } 333 return changed; 334 } 335 336 343 public boolean retainAll(Collection <?> collection) { 344 boolean changed = false; 345 int size = size(); 346 Iterator it; 347 348 it = iterator(); 349 while (size-- > 0) { 350 if (! collection.contains(it.next())) { 351 it.remove(); 352 changed = true; 353 } 354 } 355 return changed; 356 } 357 358 359 public void writeExternal( ObjectOutput out ) throws IOException { 360 out.writeByte( 0 ); 362 363 out.writeInt( _size ); 365 366 SerializationProcedure writeProcedure = new SerializationProcedure( out ); 368 if (! forEach(writeProcedure)) { 369 throw writeProcedure.exception; 370 } 371 } 372 373 public void readExternal( ObjectInput in ) 374 throws IOException, ClassNotFoundException { 375 376 in.readByte(); 378 379 int size = in.readInt(); 381 setUp( size ); 382 383 while (size-- > 0) { 385 E val = (E) in.readObject(); 386 add(val); 387 } 388 } 389 } | Popular Tags |