📄 thashmap.java
字号:
} /** * Deletes a key/value pair from the map. * * @param key an <code>Object</code> value * @return an <code>Object</code> value */ public Object remove(Object key) { Object prev = null; int index = index(key); if (index >= 0) { prev = _values[index]; removeAt(index); // clear key,state; adjust size } return prev; } /** * removes the mapping at <tt>index</tt> from the map. * * @param index an <code>int</code> value */ protected void removeAt(int index) { super.removeAt(index); // clear key, state; adjust size _values[index] = null; } /** * Returns a view on the values of the map. * * @return a <code>Collection</code> value */ public Collection values() { return new ValueView(); } /** * returns a Set view on the keys of the map. * * @return a <code>Set</code> value */ public Set keySet() { return new KeyView(); } /** * Returns a Set view on the entries of the map. * * @return a <code>Set</code> value */ public Set entrySet() { return new EntryView(); } /** * checks for the presence of <tt>val</tt> in the values of the map. * * @param val an <code>Object</code> value * @return a <code>boolean</code> value */ public boolean containsValue(Object val) { Object[] set = _set; Object[] vals = _values; // special case null values so that we don't have to // perform null checks before every call to equals() if (null == val) { for (int i = vals.length; i-- > 0;) { if ((set[i] != null && set[i] != REMOVED) && val == vals[i]) { return true; } } } else { for (int i = vals.length; i-- > 0;) { if ((set[i] != null && set[i] != REMOVED) && (val == vals[i] || val.equals(vals[i]))) { return true; } } } // end of else return false; } /** * checks for the present of <tt>key</tt> in the keys of the map. * * @param key an <code>Object</code> value * @return a <code>boolean</code> value */ public boolean containsKey(Object key) { return contains(key); } /** * copies the key/value mappings in <tt>map</tt> into this map. * * @param map a <code>Map</code> value */ public void putAll(Map map) { ensureCapacity(map.size()); // could optimize this for cases when map instanceof THashMap for (Iterator i = map.entrySet().iterator(); i.hasNext();) { Map.Entry e = (Map.Entry)i.next(); put(e.getKey(),e.getValue()); } } /** * a view onto the values of the map. * */ protected class ValueView extends KeyView { public Iterator iterator() { return new THashIterator(THashMap.this) { protected Object objectAtIndex(int index) { return _values[index]; } }; } public boolean contains(Object value) { return containsValue(value); } public boolean remove(Object value) { boolean changed = false; Object[] values = _values; Object[] set = _set; for (int i = values.length; i-- > 0;) { if ((set[i] != null && set[i] != REMOVED) && value == values[i] || (null != values[i] && values[i].equals(value))) { removeAt(i); changed = true; } } return changed; } } /** * a view onto the entries of the map. * */ protected class EntryView extends KeyView { public Iterator iterator() { return new EntryIterator(THashMap.this); } public boolean remove(Object entry) { // have to effectively reimplement Map.remove here // because we need to return true/false depending on // whether the removal took place. Since the Entry's // value can be null, this means that we can't rely // on the value of the object returned by Map.remove() // to determine whether a deletion actually happened. // // Note also that the deletion is only legal if // both the key and the value match. Object key, val; int index; key = keyForEntry(entry); index = index(key); if (index >= 0) { val = valueForEntry(entry); if (val == _values[index] || (null != val && val.equals(_values[index]))) { removeAt(index); // clear key,state; adjust size return true; } } return false; } public boolean contains(Object entry) { Object val = get(keyForEntry(entry)); Object entryValue = ((Map.Entry)entry).getValue(); return entryValue == val || (null != val && val.equals(entryValue)); } protected Object valueForEntry(Object entry) { return ((Map.Entry)entry).getValue(); } protected Object keyForEntry(Object entry) { return ((Map.Entry)entry).getKey(); } } /** * a view onto the keys of the map. */ protected class KeyView implements Set { public Iterator iterator() { return new TObjectHashIterator(THashMap.this); } public boolean remove(Object key) { return (null != THashMap.this.remove(key)); } public boolean contains(Object key) { return THashMap.this.contains(key); } public boolean containsAll(Collection collection) { for (Iterator i = collection.iterator(); i.hasNext();) { if (! contains(i.next())) { return false; } } return true; } public boolean removeAll(Collection collection) { boolean changed = false; for (Iterator i = collection.iterator(); i.hasNext();) { if (remove(i.next())) { changed = true; } } return changed; } public void clear() { THashMap.this.clear(); } public boolean add(Object obj) { throw new UnsupportedOperationException(); } public int size() { return THashMap.this.size(); } public Object[] toArray() { Object[] result = new Object[size()]; Iterator e = iterator(); for (int i=0; e.hasNext(); i++) result[i] = e.next(); return result; } public Object[] toArray(Object[] a) { int size = size(); if (a.length < size) a = (Object[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size); Iterator it = iterator(); for (int i=0; i<size; i++) { a[i] = it.next(); } if (a.length > size) { a[size] = null; } return a; } public boolean isEmpty() { return THashMap.this.isEmpty(); } public boolean addAll(Collection collection) { throw new UnsupportedOperationException(); } public boolean retainAll(Collection collection) { boolean changed = false; Iterator i = iterator(); while (i.hasNext()) { if (! collection.contains(i.next())) { i.remove(); changed = true; } } return changed; } protected final class EntryIterator extends THashIterator { EntryIterator(THashMap map) { super(map); } public Object objectAtIndex(final int index) { return new Entry(_set[index], _values[index], index); } } } final class Entry implements Map.Entry { private final Object key; private Object val; private final int index; Entry(final Object key, Object value, final int index) { this.key = key; this.val = value; this.index = index; } public Object getKey() { return key; } public Object getValue() { return val; } public Object setValue(Object o) { if (_values[index] != val) { throw new ConcurrentModificationException(); } _values[index] = o; o = val; // need to return previous value val = o; // update this entry's value, in case // setValue is called again return o; } } private void writeObject(ObjectOutputStream stream) throws IOException { stream.defaultWriteObject(); // number of entries stream.writeInt(_size); SerializationProcedure writeProcedure = new SerializationProcedure(stream); if (! forEachEntry(writeProcedure)) { throw writeProcedure.exception; } } private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); int size = stream.readInt(); setUp(size); while (size-- > 0) { Object key = stream.readObject(); Object val = stream.readObject(); put(key, val); } }} // THashMap
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -