/** * @param stats the CachePerfStats to track hash collisions. Should be null unless this is used as * a query execution-time result set. */ public ResultsBag(CachePerfStats stats) { super(stats); this.map = new ObjectIntHashMap(); }
@Override public void clear() { ObjectIntHashMap.this.clear(); } }
@Override public boolean contains(Object o) { return containsKey(o); }
return false; ObjectIntHashMap m = (ObjectIntHashMap) o; if (m.size() != size()) return false; Iterator<Entry> i = entrySet().iterator(); while (i.hasNext()) { Entry e = i.next(); Object key = e.getKey(); int value = e.getValue(); if (!(m.containsKey(key))) { return false; } else if (!(value == m.get(key))) { return false;
/** * Associates the specified value with the specified key in this map. If the map previously * contained a mapping for the key, the old value is replaced. * * @param key key with which the specified value is to be associated * @param value value to be associated with the specified key * @return the previous value associated with <tt>key</tt>, or <tt>null</tt> if there was no * mapping for <tt>key</tt>. (A <tt>null</tt> return can also indicate that the map * previously associated <tt>null</tt> with <tt>key</tt>.) */ public int put(Object key, int value) { if (key == null) return putForNullKey(value); int hash = hash(hashingStrategy.hashCode(key)); int i = indexFor(hash, table.length); for (Entry e = table[i]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || hashingStrategy.equals(k, key))) { int oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; addEntry(hash, key, value, i); return 0; }
/** * This method is used instead of put by constructors and pseudoconstructors (clone, readObject). * It does not resize the table, check for comodification, etc. It calls createEntry rather than * addEntry. */ private void putForCreate(Object key, int value) { int hash = (key == null) ? 0 : hash(hashingStrategy.hashCode(key)); int i = indexFor(hash, table.length); /** * Look for preexisting entry for key. This will never happen for clone or deserialize. It will * only happen for construction if the input Map is a sorted map whose ordering is inconsistent * w/ equals. */ for (Entry e = table[i]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || (key != null && hashingStrategy.equals(k, key)))) { e.value = value; return; } } createEntry(hash, key, value, i); }
/** * Returns the value to which the specified key is mapped, or {@code null} if this map contains no * mapping for the key. * * <p> * More formally, if this map contains a mapping from a key {@code k} to a value {@code v} such * that {@code (key==null ? k==null : * key.equals(k))}, then this method returns {@code v}; otherwise it returns {@code null}. (There * can be at most one such mapping.) * * <p> * A return value of {@code null} does not <i>necessarily</i> indicate that the map contains no * mapping for the key; it's also possible that the map explicitly maps the key to {@code null}. * The {@link #containsKey containsKey} operation may be used to distinguish these two cases. * * @see #put(Object, int) */ public int get(Object key) { if (key == null) return getForNullKey(); int hash = hash(hashingStrategy.hashCode(key)); for (Entry e = table[indexFor(hash, table.length)]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || hashingStrategy.equals(k, key))) return e.value; } return 0; }
private void putAllForCreate(ObjectIntHashMap m) { for (Iterator i = m.entrySet().iterator(); i.hasNext();) { Entry e = (Entry) i.next(); putForCreate(e.getKey(), e.getValue()); } }
@Override protected Iterator mapEntryIterator() { return this.map.entrySet().iterator(); }
@Override protected int mapGet(Object element) { return this.map.get(element); }
/** * Returns <tt>true</tt> if this map contains a mapping for the specified key. * * @param key The key whose presence in this map is to be tested * @return <tt>true</tt> if this map contains a mapping for the specified key. */ public boolean containsKey(Object key) { return getEntry(key) != null; }
/** * Returns a {@link Set} view of the mappings contained in this map. The set is backed by the map, * so changes to the map are reflected in the set, and vice-versa. If the map is modified while an * iteration over the set is in progress (except through the iterator's own <tt>remove</tt> * operation, or through the <tt>setValue</tt> operation on a map entry returned by the iterator) * the results of the iteration are undefined. The set supports element removal, which removes the * corresponding mapping from the map, via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, * <tt>removeAll</tt>, <tt>retainAll</tt> and <tt>clear</tt> operations. It does not support the * <tt>add</tt> or <tt>addAll</tt> operations. * * @return a set view of the mappings contained in this map */ public Set<Entry> entrySet() { return entrySet0(); }
/** * Offloaded version of put for null keys */ private int putForNullKey(int value) { for (Entry e = table[0]; e != null; e = e.next) { if (e.key == null) { int oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; addEntry(0, null, value, 0); return 0; }
/** * Returns the hash code value for this map. The hash code of a map is defined to be the sum of * the hash codes of each entry in the map's <tt>entrySet()</tt> view. This ensures that * <tt>m1.equals(m2)</tt> implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of {@link Object#hashCode}. * * <p> * This implementation iterates over <tt>entrySet()</tt>, calling * {@link java.util.Map.Entry#hashCode() hashCode()} on each element (entry) in the set, and * adding up the results. * * @return the hash code value for this map * @see java.util.Map.Entry#hashCode() * @see Object#equals(Object) * @see Set#equals(Object) */ public int hashCode() { int h = 0; Iterator<Entry> i = entrySet().iterator(); while (i.hasNext()) h += i.next().hashCode(); return h; }
public int occurrences(Object[] element) { return this.map.get(element); // returns 0 if not found }
@Override public boolean contains(Object o) { if (!(o instanceof Entry)) return false; Entry e = (Entry) o; Entry candidate = getEntry(e.getKey()); return candidate != null && candidate.equals(e); }
/** * Save the state of the <tt>HashMap</tt> instance to a stream (i.e., serialize it). * * @serialData The <i>capacity</i> of the IntHashMap (the length of the bucket array) is emitted * (int), followed by the <i>size</i> (an int, the number of key-value mappings), * followed by the key (Object) and value (Object) for each key-value mapping. The * key-value mappings are emitted in no particular order. */ private void writeObject(java.io.ObjectOutputStream s) throws IOException { Iterator<Entry> i = (size > 0) ? entrySet0().iterator() : null; // Write out the threshold, loadfactor, and any hidden stuff s.defaultWriteObject(); // Write out number of buckets s.writeInt(table.length); // Write out size (number of Mappings) s.writeInt(size); // Write out keys and values (alternating) if (i != null) { while (i.hasNext()) { Entry e = i.next(); s.writeObject(e.getKey()); s.writeInt(e.getValue()); } } }
/** * @param stats the CachePerfStats to track hash collisions. Should be null unless this is used as * a query execution-time result set. */ ResultsBag(int initialCapacity, float loadFactor, CachePerfStats stats) { this.map = new ObjectIntHashMap(initialCapacity, loadFactor); }
/** * Returns a string representation of this map. The string representation consists of a list of * key-value mappings in the order returned by the map's <tt>entrySet</tt> view's iterator, * enclosed in braces (<tt>"{}"</tt>). Adjacent mappings are separated by the characters * <tt>", "</tt> (comma and space). Each key-value mapping is rendered as the key followed by an * equals sign (<tt>"="</tt>) followed by the associated value. Keys and values are converted to * strings as by {@link String#valueOf(Object)}. * * @return a string representation of this map */ public String toString() { Iterator<Entry> i = entrySet().iterator(); if (!i.hasNext()) return "{}"; StringBuilder sb = new StringBuilder(); sb.append('{'); for (;;) { Entry e = i.next(); Object key = e.getKey(); int value = e.getValue(); sb.append(key == this ? "(this Map)" : key); sb.append('='); sb.append(value); if (!i.hasNext()) return sb.append('}').toString(); sb.append(", "); } }