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 TShortHashSet extends TShortHash implements Serializable { 38 39 40 static final long serialVersionUID = 1L; 41 42 46 public TShortHashSet() { 47 super(); 48 } 49 50 57 public TShortHashSet(int initialCapacity) { 58 super(initialCapacity); 59 } 60 61 69 public TShortHashSet(int initialCapacity, float loadFactor) { 70 super(initialCapacity, loadFactor); 71 } 72 73 79 public TShortHashSet(short[] array) { 80 this(array.length); 81 addAll(array); 82 } 83 84 89 public TShortHashSet(TShortHashingStrategy strategy) { 90 super(strategy); 91 } 92 93 101 public TShortHashSet(int initialCapacity, TShortHashingStrategy strategy) { 102 super(initialCapacity, strategy); 103 } 104 105 114 public TShortHashSet(int initialCapacity, float loadFactor, TShortHashingStrategy strategy) { 115 super(initialCapacity, loadFactor, strategy); 116 } 117 118 125 public TShortHashSet(short[] array, TShortHashingStrategy strategy) { 126 this(array.length, strategy); 127 addAll(array); 128 } 129 130 133 public TShortIterator iterator() { 134 return new TShortIterator(this); 135 } 136 137 143 public boolean add(short 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 short oldSet[] = _set; 166 byte oldStates[] = _states; 167 168 _set = new short[newCapacity]; 169 _states = new byte[newCapacity]; 170 171 for (int i = oldCapacity; i-- > 0;) { 172 if(oldStates[i] == FULL) { 173 short o = oldSet[i]; 174 int index = insertionIndex(o); 175 _set[index] = o; 176 _states[index] = FULL; 177 } 178 } 179 } 180 181 186 public short[] toArray() { 187 short[] result = new short[size()]; 188 short[] 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 short[] set = _set; 205 byte[] states = _states; 206 207 for (int i = set.length; i-- > 0;) { 208 set[i] = (short)0; 209 states[i] = FREE; 210 } 211 } 212 213 220 public boolean equals(Object other) { 221 if (! (other instanceof TShortHashSet)) { 222 return false; 223 } 224 final TShortHashSet that = (TShortHashSet)other; 225 if (that.size() != this.size()) { 226 return false; 227 } 228 return forEach(new TShortProcedure() { 229 public final boolean execute(short 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 TShortProcedure { 242 private int h = 0; 243 244 public int getHashCode() { 245 return h; 246 } 247 248 public final boolean execute(short key) { 249 h += _hashingStrategy.computeHashCode(key); 250 return true; 251 } 252 } 253 254 260 public boolean remove(short 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(short[] 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(short[] 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(short[] 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(short[] array) { 325 boolean changed = false; 326 Arrays.sort(array); 327 short[] 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 short val = stream.readShort(); 360 add(val); 361 } 362 } 363 } | Popular Tags |