/** * Produces a copy of this MultiArrangement, but only using up to the given amount of items to take. Does a shallow copy * of individual keys, so the references will be shared. * @param amount the number of items to copy from this MultiArrangement; will copy all items if greater than size * @return a new MultiArrangement with up to amount items copied from this into it. */ public MultiArrangement<K> take(int amount) { amount = Math.min(order.size, Math.max(0, amount)); MultiArrangement<K> nx = new MultiArrangement<>(amount, f); for (int i = 0; i < amount; i++) { nx.add(keyAt(i)); } return nx; } protected int positionOf(final Object k) {
public void addAllIfAbsent(K[] keyArray) { if (f <= .5) ensureCapacity(keyArray.length); else tryCapacity(size() + keyArray.length); int n = keyArray.length; for (int i = 0; i < n; i++) { addIfAbsent(keyArray[i]); } }
/** * 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; }
public int addIfAbsent(final K k) { int kPos; if(containsKey(k)) return add(k); return -1; }
public void addAll(K[] keyArray) { if (f <= .5) ensureCapacity(keyArray.length); // The resulting map will be sized for // m.size() elements else tryCapacity(size() + keyArray.length); // The resulting map will be int n = keyArray.length; for (int i = 0; i < n; i++) { add(keyArray[i]); } }
if (original == null) { if (containsNullKey) { return alterNullEntry(replacement); return add(replacement) >= 0; return add(replacement) >= 0; if (hasher.areEqual(original, curr)) return alterEntry(pos, replacement); while (true) { if ((curr = key[pos = (pos + 1) & mask]) == null) return add(replacement) >= 0; if (hasher.areEqual(original, curr)) return alterEntry(pos, replacement);
/** * 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; }
public MultiArrangement(final MultiArrangement<? extends K> a) { this(a.size(), a.f, a.hasher); ensureCapacity(a.size()); // The resulting map will be sized for System.arraycopy(a.key, 0, key, 0, a.key.length); System.arraycopy(a.value, 0, value, 0, a.value.length); order.addAll(a.order); containsNullKey = a.containsNullKey; }
/** * Different from the typical get method; clears the non-null IntVLA, container, and fills it with any positions * associated with k in this MultiArrangement. If you want to append to container instead of clearing it, use * {@link #append(IntVLA, Object)}. * @param container a non-null IntVLA; will be modified! Even if no entries are added, will be cleared * @param k a K object or something that can be treated as one to search for * @return the number of items added to container; 0 if none were added */ public int get(final IntVLA container, final Object k) { container.clear(); return append(container, k); } /**
/** * Creates a new MultiArrangement using the elements of an array. * * @param keyArray the array of keys of the new MultiArrangement. * @param f the load factor. * @throws IllegalArgumentException if <code>k</code> and <code>v</code> have different lengths. */ public MultiArrangement(final K[] keyArray, final float f) { this(keyArray.length, f); for (int i = 0; i < keyArray.length; i++) add(keyArray[i]); }
public boolean containsValue(final Object ov) { return ov != null && containsValue(((Integer) ov).intValue()); } /*
public void addAll(Collection<? extends K> keyArray) { if (f <= .5) ensureCapacity(keyArray.size()); // The resulting map will be sized for // m.size() elements else tryCapacity(size() + keyArray.size()); // The resulting map will be Iterator<? extends K> it = keyArray.iterator(); while (it.hasNext()) add(it.next()); } public void addAllIfAbsent(Map<? extends K, ? extends Integer> m) {
/** * 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; } /**
public int[] getArray(Collection<? extends K> keys) { if(keys == null) return new int[0]; int len = keys.size(), i = 0; IntVLA container = new IntVLA(len); for(K k : keys) { append(container, k); } return container.toArray(); }
/** * Creates a new Arrangement using the elements of two parallel arrays. * * @param keyArray the array of keys of the new Arrangement. * @param f the load factor. * @param hasher used to hash items; typically only needed when K is an array, where CrossHash has implementations * @throws IllegalArgumentException if <code>k</code> and <code>v</code> have different lengths. */ public MultiArrangement(final K[] keyArray, final float f, CrossHash.IHasher hasher) { this(keyArray.length, f, hasher); for (int i = 0; i < keyArray.length; i++) add(keyArray[i]); } /**
public void addAllIfAbsent(Collection<? extends K> keyArray) { if (f <= .5) ensureCapacity(keyArray.size()); else tryCapacity(size() + keyArray.size()); Iterator<? extends K> it = keyArray.iterator(); while (it.hasNext()) addIfAbsent(it.next()); }
public int[] getArray(K[] keys) { if(keys == null) return new int[0]; int len = keys.length; IntVLA container = new IntVLA(len); for (int i = 0; i < len; i++) { append(container, keys[i]); } return container.toArray(); }
/** * Creates a new Arrangement using the given collection of keys. * * @param keyColl the collection of keys of the new Arrangement. * @param f the load factor. * @throws IllegalArgumentException if <code>k</code> and <code>v</code> have different lengths. */ public MultiArrangement(final Collection<K> keyColl, final float f) { this(keyColl.size(), f); Iterator<K> ki = keyColl.iterator(); int idx = 0; while (ki.hasNext()) { add(ki.next()); } }