/** * Creates a new MultiArrangement. * <p> * <p>The actual table size will be the least power of two greater than <code>expected</code>/<code>f</code>. * * @param expected the expected number of elements in the hash set. * @param f the load factor. */ @SuppressWarnings("unchecked") public MultiArrangement(final int expected, final float f) { if (f <= 0 || f > 1) throw new IllegalArgumentException("Load factor must be greater than 0 and smaller than or equal to 1"); if (expected < 0) throw new IllegalArgumentException("The expected number of elements must be nonnegative"); this.f = f; n = arraySize(expected, f); mask = n - 1; maxFill = maxFill(n, f); key = (K[]) new Object[n + 1]; value = new IntVLA[n + 1]; //link = new long[n + 1]; order = new IntVLA(expected); hasher = CrossHash.defaultHasher; }
/** * Creates a new Arrangement. * <p> * <p>The actual table size will be the least power of two greater than <code>expected</code>/<code>f</code>. * * @param expected the expected number of elements in the hash set. * @param f the load factor. * @param hasher used to hash items; typically only needed when K is an array, where CrossHash has implementations */ @SuppressWarnings("unchecked") public MultiArrangement(final int expected, final float f, CrossHash.IHasher hasher) { if (f <= 0 || f > 1) throw new IllegalArgumentException("Load factor must be greater than 0 and smaller than or equal to 1"); if (expected < 0) throw new IllegalArgumentException("The expected number of elements must be nonnegative"); this.f = f; n = arraySize(expected, f); mask = n - 1; maxFill = maxFill(n, f); key = (K[]) new Object[n + 1]; value = new IntVLA[n + 1]; //link = new long[n + 1]; order = new IntVLA(expected); this.hasher = (hasher == null) ? CrossHash.defaultHasher : hasher; } /**
/** * Rehashes this map if the table is too large. * <p> * <P>Let <var>N</var> be the smallest table size that can hold <code>max(n,{@link #size()})</code> entries, still satisfying the load factor. If the current table size is smaller than or equal to * <var>N</var>, this method does nothing. Otherwise, it rehashes this map in a table of size <var>N</var>. * <p> * <P>This method is useful when reusing maps. {@linkplain #clear() Clearing a map} leaves the table size untouched. If you are reusing a map many times, you can call this method with a typical * size to avoid keeping around a very large table just because of a few large transient maps. * * @param n the threshold for the trimming. * @return true if there was enough memory to trim the map. * @see #trim() */ public boolean trim(final int n) { final int l = HashCommon.nextPowerOfTwo((int) Math.ceil(n / f)); if (l >= n || size > maxFill(l, f)) return true; try { rehash(l); } catch (Exception cantDoIt) { return false; } return true; }
/** * Rehashes the map, making the table as small as possible. * <p> * <P>This method rehashes the table to the smallest size satisfying the load factor. It can be used when the set will not be changed anymore, so to optimize access speed and size. * <p> * <P>If the table size is already the minimum possible, this method does nothing. * * @return true if there was enough memory to trim the map. * @see #trim(int) */ public boolean trim() { final int l = arraySize(size, f); if (l >= n || size > maxFill(l, f)) return true; try { rehash(l); } catch (Exception cantDoIt) { return false; } return true; }
maxFill = maxFill(n, f); this.key = newKey; this.value = newValue;