@Override public Selection selectIsIn(Collection<String> strings) { ShortOpenHashSet keys = new ShortOpenHashSet(strings.size()); for (String string : strings) { short key = getKeyForValue(string); if (key != DEFAULT_RETURN_VALUE) { keys.add(key); } } Selection results = new BitmapBackedSelection(); for (int i = 0; i < values.size(); i++) { if (keys.contains(values.getShort(i))) { results.add(i); } } return results; }
@Override public int countUnique() { ShortSet uniqueElements = new ShortOpenHashSet(); for (int i = 0; i < size(); i++) { short val = getShort(i); if (!isMissingValue(val)) { uniqueElements.add(val); } } return uniqueElements.size(); }
@Override public boolean addAll(Collection<? extends Short> c) { // The resulting collection will be at least c.size() big if (f <= .5) ensureCapacity(c.size()); // The resulting collection will be sized for c.size() elements else tryCapacity(size() + c.size()); // The resulting collection will be tentatively sized for size() + c.size() // elements return super.addAll(c); } @Override
/** * Creates a new hash set and fills it with the elements of a given array. * * @param a * an array whose elements will be used to fill the set. * @param offset * the first element to use. * @param length * the number of elements to use. * @param f * the load factor. */ public ShortOpenHashSet(final short[] a, final int offset, final int length, final float f) { this(length < 0 ? 0 : length, f); ShortArrays.ensureOffsetLength(a, offset, length); for (int i = 0; i < length; i++) add(a[offset + i]); } /**
private boolean removeNullEntry() { containsNull = false; key[n] = ((short) 0); size--; if (n > minN && size < maxFill / 4 && n > DEFAULT_INITIAL_SIZE) rehash(n / 2); return true; }
@Override public boolean containsAny(Object set) { ShortOpenHashSet setShort = (ShortOpenHashSet) set; for (int i = 0; i < this._length; i++) if (setShort.contains(((TermShortList) _mTermList).getPrimitiveValue(_buf[i]))) return true; return false; }
/** * Creates a new hash set copying a given collection. * * @param c * a {@link Collection} to be copied into the new hash set. * @param f * the load factor. */ public ShortOpenHashSet(final Collection<? extends Short> c, final float f) { this(c.size(), f); addAll(c); } /**
/** * Returns a hash code for this set. * * This method overrides the generic method provided by the superclass. Since * {@code equals()} is not overriden, it is important that the value returned by * this method is the same value as the one returned by the overriden method. * * @return a hash code for this set. */ @Override public int hashCode() { int h = 0; for (int j = realSize(), i = 0; j-- != 0;) { while (((key[i]) == ((short) 0))) i++; h += (key[i]); i++; } // Zero / null have hash zero. return h; } private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { final ShortIterator i = iterator(); s.defaultWriteObject(); for (int j = size; j-- != 0;) s.writeShort(i.nextShort()); }
@Override public void remove() { if (last == -1) throw new IllegalStateException(); if (last == n) { ShortOpenHashSet.this.containsNull = false; ShortOpenHashSet.this.key[n] = ((short) 0); } else if (pos >= 0) shiftKeys(last); else { // We're removing wrapped entries. ShortOpenHashSet.this.remove(wrapped.getShort(-pos - 1)); last = -1; // Note that we must not decrement size return; } size--; last = -1; // You can no longer remove this entry. if (ASSERTS) checkTable(); } }
@Override public ShortColumn unique() { final ShortSet values = new ShortOpenHashSet(); for (int i = 0; i < size(); i++) { if (!isMissing(i)) { values.add(getShort(i)); } } final ShortColumn column = ShortColumn.create(name() + " Unique values"); for (short value : values) { column.append(value); } return column; }
@Override public boolean addAll(ShortCollection c) { if (f <= .5) ensureCapacity(c.size()); // The resulting collection will be sized for c.size() elements else tryCapacity(size() + c.size()); // The resulting collection will be tentatively sized for size() + c.size() // elements return super.addAll(c); } @Override
/** * Creates a new hash set using elements provided by a type-specific iterator. * * @param i * a type-specific iterator whose elements will fill the set. * @param f * the load factor. */ public ShortOpenHashSet(final ShortIterator i, final float f) { this(DEFAULT_INITIAL_SIZE, f); while (i.hasNext()) add(i.nextShort()); } /**
private void ensureCapacity(final int capacity) { final int needed = arraySize(capacity, f); if (needed > n) rehash(needed); } private void tryCapacity(final long capacity) {
/** * Creates a new hash set copying a given type-specific collection. * * @param c * a type-specific collection to be copied into the new hash set. * @param f * the load factor. */ public ShortOpenHashSet(final ShortCollection c, final float f) { this(c.size(), f); addAll(c); } /**
/** * Rehashes the set. * * <p> * This method implements the basic rehashing strategy, and may be overriden by * subclasses implementing different rehashing strategies (e.g., disk-based * rehashing). However, you should not override this method unless you * understand the internal workings of this class. * * @param newN * the new size */ protected void rehash(final int newN) { final short key[] = this.key; final int mask = newN - 1; // Note that this is used by the hashing macro final short newKey[] = new short[newN + 1]; int i = n, pos; for (int j = realSize(); j-- != 0;) { while (((key[--i]) == ((short) 0))); if (!((newKey[pos = (it.unimi.dsi.fastutil.HashCommon.mix((key[i]))) & mask]) == ((short) 0))) while (!((newKey[pos = (pos + 1) & mask]) == ((short) 0))); newKey[pos] = key[i]; } n = newN; this.mask = mask; maxFill = maxFill(n, f); this.key = newKey; } /**
@Override public Selection selectIsIn(String... strings) { ShortOpenHashSet keys = new ShortOpenHashSet(strings.length); for (String string : strings) { short key = getKeyForValue(string); if (key != DEFAULT_RETURN_VALUE) { keys.add(key); } } Selection results = new BitmapBackedSelection(); for (int i = 0; i < values.size(); i++) { if (keys.contains(values.getShort(i))) { results.add(i); } } return results; }
public ShortSet asSet() { return new ShortOpenHashSet(data); }
private void tryCapacity(final long capacity) { final int needed = (int) Math.min(1 << 30, Math.max(2, HashCommon.nextPowerOfTwo((long) Math.ceil(capacity / f)))); if (needed > n) rehash(needed); } @Override