📄 thashmap.java
字号:
///////////////////////////////////////////////////////////////////////////////// Copyright (c) 2001, Eric D. Friedman All Rights Reserved.//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.///////////////////////////////////////////////////////////////////////////////package gnu.trove;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.Collection;import java.util.ConcurrentModificationException;import java.util.Iterator;import java.util.Map.Entry;import java.util.Map;import java.util.NoSuchElementException;import java.util.Set;/** * An implementation of the Map interface which uses an open addressed * hash table to store its contents. * * Created: Sun Nov 4 08:52:45 2001 * * @author Eric D. Friedman * @version $Id: THashMap.java,v 1.1.1.1 2003/07/14 19:36:04 mccallum Exp $ */public class THashMap extends TObjectHash implements Map, Serializable { /** the values of the map */ protected transient Object[] _values; /** * Creates a new <code>THashMap</code> instance with the default * capacity and load factor. */ public THashMap() { super(); } /** * Creates a new <code>THashMap</code> instance with the default * capacity and load factor. * @param strategy used to compute hash codes and to compare objects. */ public THashMap(TObjectHashingStrategy strategy) { super(strategy); } /** * Creates a new <code>THashMap</code> instance with a prime * capacity equal to or greater than <tt>initialCapacity</tt> and * with the default load factor. * * @param initialCapacity an <code>int</code> value */ public THashMap(int initialCapacity) { super(initialCapacity); } /** * Creates a new <code>THashMap</code> instance with a prime * capacity equal to or greater than <tt>initialCapacity</tt> and * with the default load factor. * * @param initialCapacity an <code>int</code> value * @param strategy used to compute hash codes and to compare objects. */ public THashMap(int initialCapacity, TObjectHashingStrategy strategy) { super(initialCapacity, strategy); } /** * Creates a new <code>THashMap</code> instance with a prime * capacity equal to or greater than <tt>initialCapacity</tt> and * with the specified load factor. * * @param initialCapacity an <code>int</code> value * @param loadFactor a <code>float</code> value */ public THashMap(int initialCapacity, float loadFactor) { super(initialCapacity, loadFactor); } /** * Creates a new <code>THashMap</code> instance with a prime * capacity equal to or greater than <tt>initialCapacity</tt> and * with the specified load factor. * * @param initialCapacity an <code>int</code> value * @param loadFactor a <code>float</code> value * @param strategy used to compute hash codes and to compare objects. */ public THashMap(int initialCapacity, float loadFactor, TObjectHashingStrategy strategy) { super(initialCapacity, loadFactor, strategy); } /** * Creates a new <code>THashMap</code> instance which contains the * key/value pairs in <tt>map</tt>. * * @param map a <code>Map</code> value */ public THashMap(Map map) { this(map.size()); putAll(map); } /** * Creates a new <code>THashMap</code> instance which contains the * key/value pairs in <tt>map</tt>. * * @param map a <code>Map</code> value * @param strategy used to compute hash codes and to compare objects. */ public THashMap(Map map, TObjectHashingStrategy strategy) { this(map.size(), strategy); putAll(map); } /** * @return a shallow clone of this collection */ public Object clone() { THashMap m = (THashMap)super.clone(); m._values = (Object[])this._values.clone(); return m; } /** * initialize the value array of the map. * * @param initialCapacity an <code>int</code> value * @return an <code>int</code> value */ protected int setUp(int initialCapacity) { int capacity; capacity = super.setUp(initialCapacity); _values = new Object[capacity]; return capacity; } /** * Inserts a key/value pair into the map. * * @param key an <code>Object</code> value * @param value an <code>Object</code> value * @return the previous value associated with <tt>key</tt>, * or null if none was found. */ public Object put(Object key, Object value) { if (null == key) { throw new NullPointerException("null keys not supported"); } Object previous = null; Object oldKey = null; int index = insertionIndex(key); if (index < 0) { index = -index -1; previous = _values[index]; } oldKey = _set[index]; _set[index] = key; _values[index] = value; if (null == previous) { postInsertHook(oldKey == null); } return previous; } /** * Compares this map with another map for equality of their stored * entries. * * @param other an <code>Object</code> value * @return a <code>boolean</code> value */ public boolean equals(Object other) { if (! (other instanceof Map)) { return false; } Map that = (Map)other; if (that.size() != this.size()) { return false; } return forEachEntry(new EqProcedure(that)); } private static final class EqProcedure implements TObjectObjectProcedure { private final Map _otherMap; EqProcedure(Map otherMap) { _otherMap = otherMap; } public final boolean execute(Object key, Object value) { Object oValue = _otherMap.get(key); if (oValue == value || (oValue != null && oValue.equals(value))) { return true; } return false; } } /** * Executes <tt>procedure</tt> for each key in the map. * * @param procedure a <code>TObjectProcedure</code> value * @return false if the loop over the keys terminated because * the procedure returned false for some key. */ public boolean forEachKey(TObjectProcedure procedure) { return forEach(procedure); } /** * Executes <tt>procedure</tt> for each value in the map. * * @param procedure a <code>TObjectProcedure</code> value * @return false if the loop over the values terminated because * the procedure returned false for some value. */ public boolean forEachValue(TObjectProcedure procedure) { Object[] values = _values; Object[] set = _set; for (int i = values.length; i-- > 0;) { if (set[i] != null && set[i] != REMOVED && ! procedure.execute(values[i])) { return false; } } return true; } /** * Executes <tt>procedure</tt> for each key/value entry in the * map. * * @param procedure a <code>TObjectObjectProcedure</code> value * @return false if the loop over the entries terminated because * the procedure returned false for some entry. */ public boolean forEachEntry(TObjectObjectProcedure procedure) { Object[] keys = _set; Object[] values = _values; for (int i = keys.length; i-- > 0;) { if (keys[i] != null && keys[i] != REMOVED && ! procedure.execute(keys[i],values[i])) { return false; } } return true; } /** * Retains only those entries in the map for which the procedure * returns a true value. * * @param procedure determines which entries to keep * @return true if the map was modified. */ public boolean retainEntries(TObjectObjectProcedure procedure) { boolean modified = false; Object[] keys = _set; Object[] values = _values; for (int i = keys.length; i-- > 0;) { if (keys[i] != null && keys[i] != REMOVED && ! procedure.execute(keys[i],values[i])) { removeAt(i); modified = true; } } return modified; } /** * Transform the values in this map using <tt>function</tt>. * * @param function a <code>TObjectFunction</code> value */ public void transformValues(TObjectFunction function) { Object[] values = _values; Object[] set = _set; for (int i = values.length; i-- > 0;) { if (set[i] != null && set[i] != REMOVED) { values[i] = function.execute(values[i]); } } } /** * rehashes the map to the new capacity. * * @param newCapacity an <code>int</code> value */ protected void rehash(int newCapacity) { int oldCapacity = _set.length; Object oldKeys[] = _set; Object oldVals[] = _values; _set = new Object[newCapacity]; _values = new Object[newCapacity]; for (int i = oldCapacity; i-- > 0;) { if(oldKeys[i] != null && oldKeys[i] != REMOVED) { Object o = oldKeys[i]; int index = insertionIndex(o); _set[index] = o; _values[index] = oldVals[i]; } } } /** * retrieves the value for <tt>key</tt> * * @param key an <code>Object</code> value * @return the value of <tt>key</tt> or null if no such mapping exists. */ public Object get(Object key) { int index = index(key); return index < 0 ? null : _values[index]; } /** * Empties the map. * */ public void clear() { super.clear(); Object[] keys = _set; Object[] vals = _values; for (int i = keys.length; i-- > 0;) { keys[i] = null; vals[i] = null; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -