@SuppressWarnings("unchecked") public static Set<?> toFastutilHashSet(Set<?> set, Type type, FunctionRegistry registry) { // 0.25 as the load factor is chosen because the argument set is assumed to be small (<10000), // and the return set is assumed to be read-heavy. // The performance of InCodeGenerator heavily depends on the load factor being small. Class<?> javaElementType = type.getJavaType(); if (javaElementType == long.class) { return new LongOpenCustomHashSet((Collection<Long>) set, 0.25f, new LongStrategy(registry, type)); } if (javaElementType == double.class) { return new DoubleOpenCustomHashSet((Collection<Double>) set, 0.25f, new DoubleStrategy(registry, type)); } if (javaElementType == boolean.class) { return new BooleanOpenHashSet((Collection<Boolean>) set, 0.25f); } else if (!type.getJavaType().isPrimitive()) { return new ObjectOpenCustomHashSet(set, 0.25f, new ObjectStrategy(registry, type)); } else { throw new UnsupportedOperationException("Unsupported native type in set: " + type.getJavaType() + " with type " + type.getTypeSignature()); } }
public static boolean in(double doubleValue, DoubleOpenCustomHashSet set) { return set.contains(doubleValue); }
@Override public boolean addAll(Collection<? extends Double> 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
private boolean removeNullEntry() { containsNull = false; key[n] = (0); size--; if (n > minN && size < maxFill / 4 && n > DEFAULT_INITIAL_SIZE) rehash(n / 2); return true; }
/** * 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. * @param strategy * the strategy. */ public DoubleOpenCustomHashSet(final double[] a, final int offset, final int length, final float f, final it.unimi.dsi.fastutil.doubles.DoubleHash.Strategy strategy) { this(length < 0 ? 0 : length, f, strategy); DoubleArrays.ensureOffsetLength(a, offset, length); for (int i = 0; i < length; i++) add(a[offset + i]); } /**
/** * 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. * @param strategy * the strategy. */ public DoubleOpenCustomHashSet(final Collection<? extends Double> c, final float f, final it.unimi.dsi.fastutil.doubles.DoubleHash.Strategy strategy) { this(c.size(), f, strategy); 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 ((Double.doubleToLongBits(key[i]) == 0)) i++; h += (strategy.hashCode(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 DoubleIterator i = iterator(); s.defaultWriteObject(); for (int j = size; j-- != 0;) s.writeDouble(i.nextDouble()); }
@Override public void remove() { if (last == -1) throw new IllegalStateException(); if (last == n) { DoubleOpenCustomHashSet.this.containsNull = false; DoubleOpenCustomHashSet.this.key[n] = (0); } else if (pos >= 0) shiftKeys(last); else { // We're removing wrapped entries. DoubleOpenCustomHashSet.this.remove(wrapped.getDouble(-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 boolean addAll(DoubleCollection 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
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 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. * @param strategy * the strategy. */ public DoubleOpenCustomHashSet(final DoubleIterator i, final float f, final it.unimi.dsi.fastutil.doubles.DoubleHash.Strategy strategy) { this(DEFAULT_INITIAL_SIZE, f, strategy); while (i.hasNext()) add(i.nextDouble()); } /**
/** * 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. * @param strategy * the strategy. */ public DoubleOpenCustomHashSet(final DoubleCollection c, final float f, it.unimi.dsi.fastutil.doubles.DoubleHash.Strategy strategy) { this(c.size(), f, strategy); 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 double key[] = this.key; final int mask = newN - 1; // Note that this is used by the hashing macro final double newKey[] = new double[newN + 1]; int i = n, pos; for (int j = realSize(); j-- != 0;) { while ((Double.doubleToLongBits(key[--i]) == 0)); if (!(Double.doubleToLongBits( newKey[pos = (it.unimi.dsi.fastutil.HashCommon.mix(strategy.hashCode(key[i]))) & mask]) == 0)) while (!(Double.doubleToLongBits(newKey[pos = (pos + 1) & mask]) == 0)); newKey[pos] = key[i]; } n = newN; this.mask = mask; maxFill = maxFill(n, f); this.key = newKey; } /**
public static boolean in(double doubleValue, DoubleOpenCustomHashSet set) { return set.contains(doubleValue); }
@SuppressWarnings({"unchecked"}) public static Set<?> toFastutilHashSet(Set<?> set, Type type, FunctionRegistry registry) { // 0.25 as the load factor is chosen because the argument set is assumed to be small (<10000), // and the return set is assumed to be read-heavy. // The performance of InCodeGenerator heavily depends on the load factor being small. Class<?> javaElementType = type.getJavaType(); if (javaElementType == long.class) { return new LongOpenCustomHashSet((Collection<Long>) set, 0.25f, new LongStrategy(registry, type)); } if (javaElementType == double.class) { return new DoubleOpenCustomHashSet((Collection<Double>) set, 0.25f, new DoubleStrategy(registry, type)); } if (javaElementType == boolean.class) { return new BooleanOpenHashSet((Collection<Boolean>) set, 0.25f); } else if (!type.getJavaType().isPrimitive()) { return new ObjectOpenCustomHashSet(set, 0.25f, new ObjectStrategy(registry, type)); } else { throw new UnsupportedOperationException("Unsupported native type in set: " + type.getJavaType() + " with type " + type.getTypeSignature()); } }
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
public static boolean in(double doubleValue, DoubleOpenCustomHashSet set) { return set.contains(doubleValue); }
@SuppressWarnings("unchecked") public static Set<?> toFastutilHashSet(Set<?> set, Type type, FunctionRegistry registry) { // 0.25 as the load factor is chosen because the argument set is assumed to be small (<10000), // and the return set is assumed to be read-heavy. // The performance of InCodeGenerator heavily depends on the load factor being small. Class<?> javaElementType = type.getJavaType(); if (javaElementType == long.class) { return new LongOpenCustomHashSet((Collection<Long>) set, 0.25f, new LongStrategy(registry, type)); } if (javaElementType == double.class) { return new DoubleOpenCustomHashSet((Collection<Double>) set, 0.25f, new DoubleStrategy(registry, type)); } if (javaElementType == boolean.class) { return new BooleanOpenHashSet((Collection<Boolean>) set, 0.25f); } else if (!type.getJavaType().isPrimitive()) { return new ObjectOpenCustomHashSet(set, 0.25f, new ObjectStrategy(registry, type)); } else { throw new UnsupportedOperationException("Unsupported native type in set: " + type.getJavaType() + " with type " + type.getTypeSignature()); } }