gnu.trove.map.hash.TIntIntHashMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
High performance collections for Java
The newest version!
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2001, Eric D. Friedman All Rights Reserved.
// Copyright (c) 2009, Rob Eden All Rights Reserved.
// Copyright (c) 2009, Jeff Randall 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.map.hash;
//////////////////////////////////////////////////
// THIS IS A GENERATED CLASS. DO NOT HAND EDIT! //
//////////////////////////////////////////////////
import gnu.trove.map.TIntIntMap;
import gnu.trove.function.TIntFunction;
import gnu.trove.procedure.*;
import gnu.trove.set.*;
import gnu.trove.iterator.*;
import gnu.trove.iterator.hash.*;
import gnu.trove.impl.hash.*;
import gnu.trove.impl.HashFunctions;
import gnu.trove.*;
import java.io.*;
import java.util.*;
/**
* An open addressed Map implementation for int keys and int values.
*
* @author Eric D. Friedman
* @author Rob Eden
* @author Jeff Randall
* @version $Id: _K__V_HashMap.template,v 1.1.2.16 2010/03/02 04:09:50 robeden Exp $
*/
public class TIntIntHashMap extends TIntIntHash implements TIntIntMap, Externalizable {
static final long serialVersionUID = 1L;
/** the values of the map */
protected transient int[] _values;
/**
* Creates a new TIntIntHashMap
instance with the default
* capacity and load factor.
*/
public TIntIntHashMap() {
super();
}
/**
* Creates a new TIntIntHashMap
instance with a prime
* capacity equal to or greater than initialCapacity and
* with the default load factor.
*
* @param initialCapacity an int
value
*/
public TIntIntHashMap( int initialCapacity ) {
super( initialCapacity );
}
/**
* Creates a new TIntIntHashMap
instance with a prime
* capacity equal to or greater than initialCapacity and
* with the specified load factor.
*
* @param initialCapacity an int
value
* @param loadFactor a float
value
*/
public TIntIntHashMap( int initialCapacity, float loadFactor ) {
super( initialCapacity, loadFactor );
}
/**
* Creates a new TIntIntHashMap
instance with a prime
* capacity equal to or greater than initialCapacity and
* with the specified load factor.
*
* @param initialCapacity an int
value
* @param loadFactor a float
value
* @param noEntryKey a int
value that represents
* null for the Key set.
* @param noEntryValue a int
value that represents
* null for the Value set.
*/
public TIntIntHashMap( int initialCapacity, float loadFactor,
int noEntryKey, int noEntryValue ) {
super( initialCapacity, loadFactor, noEntryKey, noEntryValue );
}
/**
* Creates a new TIntIntHashMap
instance containing
* all of the entries in the map passed in.
*
* @param keys a int array containing the keys for the matching values.
* @param values a int array containing the values.
*/
public TIntIntHashMap( int[] keys, int[] values ) {
super( Math.max( keys.length, values.length ) );
int size = Math.min( keys.length, values.length );
for ( int i = 0; i < size; i++ ) {
this.put( keys[i], values[i] );
}
}
/**
* Creates a new TIntIntHashMap
instance containing
* all of the entries in the map passed in.
*
* @param map a TIntIntMap that will be duplicated.
*/
public TIntIntHashMap( TIntIntMap map ) {
super( map.size() );
if ( map instanceof TIntIntHashMap ) {
TIntIntHashMap hashmap = ( TIntIntHashMap ) map;
this._loadFactor = Math.abs( hashmap._loadFactor );
this.no_entry_key = hashmap.no_entry_key;
this.no_entry_value = hashmap.no_entry_value;
//noinspection RedundantCast
if ( this.no_entry_key != ( int ) 0 ) {
Arrays.fill( _set, this.no_entry_key );
}
//noinspection RedundantCast
if ( this.no_entry_value != ( int ) 0 ) {
Arrays.fill( _values, this.no_entry_value );
}
setUp( saturatedCast( fastCeil( DEFAULT_CAPACITY / (double) _loadFactor ) ) );
}
putAll( map );
}
/**
* initializes the hashtable to a prime capacity which is at least
* initialCapacity + 1.
*
* @param initialCapacity an int
value
* @return the actual capacity chosen
*/
protected int setUp( int initialCapacity ) {
int capacity;
capacity = super.setUp( initialCapacity );
_values = new int[capacity];
return capacity;
}
/**
* rehashes the map to the new capacity.
*
* @param newCapacity an int
value
*/
/** {@inheritDoc} */
protected void rehash( int newCapacity ) {
int oldCapacity = _set.length;
int oldKeys[] = _set;
int oldVals[] = _values;
byte oldStates[] = _states;
_set = new int[newCapacity];
_values = new int[newCapacity];
_states = new byte[newCapacity];
for ( int i = oldCapacity; i-- > 0; ) {
if( oldStates[i] == FULL ) {
int o = oldKeys[i];
int index = insertKey( o );
_values[index] = oldVals[i];
}
}
}
/** {@inheritDoc} */
public int put( int key, int value ) {
int index = insertKey( key );
return doPut( key, value, index );
}
/** {@inheritDoc} */
public int putIfAbsent( int key, int value ) {
int index = insertKey( key );
if (index < 0)
return _values[-index - 1];
return doPut( key, value, index );
}
private int doPut( int key, int value, int index ) {
int previous = no_entry_value;
boolean isNewMapping = true;
if ( index < 0 ) {
index = -index -1;
previous = _values[index];
isNewMapping = false;
}
_values[index] = value;
if (isNewMapping) {
postInsertHook( consumeFreeSlot );
}
return previous;
}
/** {@inheritDoc} */
public void putAll( Map extends Integer, ? extends Integer> map ) {
ensureCapacity( map.size() );
// could optimize this for cases when map instanceof THashMap
for ( Map.Entry extends Integer, ? extends Integer> entry : map.entrySet() ) {
this.put( entry.getKey().intValue(), entry.getValue().intValue() );
}
}
/** {@inheritDoc} */
public void putAll( TIntIntMap map ) {
ensureCapacity( map.size() );
TIntIntIterator iter = map.iterator();
while ( iter.hasNext() ) {
iter.advance();
this.put( iter.key(), iter.value() );
}
}
/** {@inheritDoc} */
public int get( int key ) {
int index = index( key );
return index < 0 ? no_entry_value : _values[index];
}
/** {@inheritDoc} */
public void clear() {
super.clear();
Arrays.fill( _set, 0, _set.length, no_entry_key );
Arrays.fill( _values, 0, _values.length, no_entry_value );
Arrays.fill( _states, 0, _states.length, FREE );
}
/** {@inheritDoc} */
public boolean isEmpty() {
return 0 == _size;
}
/** {@inheritDoc} */
public int remove( int key ) {
int prev = no_entry_value;
int index = index( key );
if ( index >= 0 ) {
prev = _values[index];
removeAt( index ); // clear key,state; adjust size
}
return prev;
}
/** {@inheritDoc} */
protected void removeAt( int index ) {
_values[index] = no_entry_value;
super.removeAt( index ); // clear key, state; adjust size
}
/** {@inheritDoc} */
public TIntSet keySet() {
return new TKeyView();
}
/** {@inheritDoc} */
public int[] keys() {
int[] keys = new int[size()];
if ( keys.length == 0 ) {
return keys; // nothing to copy
}
int[] k = _set;
byte[] states = _states;
for ( int i = k.length, j = 0; i-- > 0; ) {
if ( states[i] == FULL ) {
keys[j++] = k[i];
}
}
return keys;
}
/** {@inheritDoc} */
public int[] keys( int[] array ) {
int size = size();
if ( size == 0 ) {
return array; // nothing to copy
}
if ( array.length < size ) {
array = new int[size];
}
int[] keys = _set;
byte[] states = _states;
for ( int i = keys.length, j = 0; i-- > 0; ) {
if ( states[i] == FULL ) {
array[j++] = keys[i];
}
}
return array;
}
/** {@inheritDoc} */
public TIntCollection valueCollection() {
return new TValueView();
}
/** {@inheritDoc} */
public int[] values() {
int[] vals = new int[size()];
if ( vals.length == 0 ) {
return vals; // nothing to copy
}
int[] v = _values;
byte[] states = _states;
for ( int i = v.length, j = 0; i-- > 0; ) {
if ( states[i] == FULL ) {
vals[j++] = v[i];
}
}
return vals;
}
/** {@inheritDoc} */
public int[] values( int[] array ) {
int size = size();
if ( size == 0 ) {
return array; // nothing to copy
}
if ( array.length < size ) {
array = new int[size];
}
int[] v = _values;
byte[] states = _states;
for ( int i = v.length, j = 0; i-- > 0; ) {
if ( states[i] == FULL ) {
array[j++] = v[i];
}
}
return array;
}
/** {@inheritDoc} */
public boolean containsValue( int val ) {
byte[] states = _states;
int[] vals = _values;
for ( int i = vals.length; i-- > 0; ) {
if ( states[i] == FULL && val == vals[i] ) {
return true;
}
}
return false;
}
/** {@inheritDoc} */
public boolean containsKey( int key ) {
return contains( key );
}
/** {@inheritDoc} */
public TIntIntIterator iterator() {
return new TIntIntHashIterator( this );
}
/** {@inheritDoc} */
public boolean forEachKey( TIntProcedure procedure ) {
return forEach( procedure );
}
/** {@inheritDoc} */
public boolean forEachValue( TIntProcedure procedure ) {
byte[] states = _states;
int[] values = _values;
for ( int i = values.length; i-- > 0; ) {
if ( states[i] == FULL && ! procedure.execute( values[i] ) ) {
return false;
}
}
return true;
}
/** {@inheritDoc} */
public boolean forEachEntry( TIntIntProcedure procedure ) {
byte[] states = _states;
int[] keys = _set;
int[] values = _values;
for ( int i = keys.length; i-- > 0; ) {
if ( states[i] == FULL && ! procedure.execute( keys[i], values[i] ) ) {
return false;
}
}
return true;
}
/** {@inheritDoc} */
public void transformValues( TIntFunction function ) {
byte[] states = _states;
int[] values = _values;
for ( int i = values.length; i-- > 0; ) {
if ( states[i] == FULL ) {
values[i] = function.execute( values[i] );
}
}
}
/** {@inheritDoc} */
public boolean retainEntries( TIntIntProcedure procedure ) {
boolean modified = false;
byte[] states = _states;
int[] keys = _set;
int[] values = _values;
// Temporarily disable compaction. This is a fix for bug #1738760
tempDisableAutoCompaction();
try {
for ( int i = keys.length; i-- > 0; ) {
if ( states[i] == FULL && ! procedure.execute( keys[i], values[i] ) ) {
removeAt( i );
modified = true;
}
}
}
finally {
reenableAutoCompaction( true );
}
return modified;
}
/** {@inheritDoc} */
public boolean increment( int key ) {
return adjustValue( key, ( int ) 1 );
}
/** {@inheritDoc} */
public boolean adjustValue( int key, int amount ) {
int index = index( key );
if (index < 0) {
return false;
} else {
_values[index] += amount;
return true;
}
}
/** {@inheritDoc} */
public int adjustOrPutValue( int key, int adjust_amount, int put_amount ) {
int index = insertKey( key );
final boolean isNewMapping;
final int newValue;
if ( index < 0 ) {
index = -index -1;
newValue = ( _values[index] += adjust_amount );
isNewMapping = false;
} else {
newValue = ( _values[index] = put_amount );
isNewMapping = true;
}
byte previousState = _states[index];
if ( isNewMapping ) {
postInsertHook(consumeFreeSlot);
}
return newValue;
}
/** a view onto the keys of the map. */
protected class TKeyView implements TIntSet {
/** {@inheritDoc} */
public TIntIterator iterator() {
return new TIntIntKeyHashIterator( TIntIntHashMap.this );
}
/** {@inheritDoc} */
public int getNoEntryValue() {
return no_entry_key;
}
/** {@inheritDoc} */
public int size() {
return _size;
}
/** {@inheritDoc} */
public boolean isEmpty() {
return 0 == _size;
}
/** {@inheritDoc} */
public boolean contains( int entry ) {
return TIntIntHashMap.this.contains( entry );
}
/** {@inheritDoc} */
public int[] toArray() {
return TIntIntHashMap.this.keys();
}
/** {@inheritDoc} */
public int[] toArray( int[] dest ) {
return TIntIntHashMap.this.keys( dest );
}
/**
* Unsupported when operating upon a Key Set view of a TIntIntMap
*
* {@inheritDoc}
*/
public boolean add( int entry ) {
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
public boolean remove( int entry ) {
return no_entry_value != TIntIntHashMap.this.remove( entry );
}
/** {@inheritDoc} */
public boolean containsAll( Collection> collection ) {
for ( Object element : collection ) {
if ( element instanceof Integer ) {
int ele = ( ( Integer ) element ).intValue();
if ( ! TIntIntHashMap.this.containsKey( ele ) ) {
return false;
}
} else {
return false;
}
}
return true;
}
/** {@inheritDoc} */
public boolean containsAll( TIntCollection collection ) {
TIntIterator iter = collection.iterator();
while ( iter.hasNext() ) {
if ( ! TIntIntHashMap.this.containsKey( iter.next() ) ) {
return false;
}
}
return true;
}
/** {@inheritDoc} */
public boolean containsAll( int[] array ) {
for ( int element : array ) {
if ( ! TIntIntHashMap.this.contains( element ) ) {
return false;
}
}
return true;
}
/**
* Unsupported when operating upon a Key Set view of a TIntIntMap
*
* {@inheritDoc}
*/
public boolean addAll( Collection extends Integer> collection ) {
throw new UnsupportedOperationException();
}
/**
* Unsupported when operating upon a Key Set view of a TIntIntMap
*
* {@inheritDoc}
*/
public boolean addAll( TIntCollection collection ) {
throw new UnsupportedOperationException();
}
/**
* Unsupported when operating upon a Key Set view of a TIntIntMap
*
* {@inheritDoc}
*/
public boolean addAll( int[] array ) {
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@SuppressWarnings({"SuspiciousMethodCalls"})
public boolean retainAll( Collection> collection ) {
boolean modified = false;
TIntIterator iter = iterator();
while ( iter.hasNext() ) {
if ( ! collection.contains( Integer.valueOf ( iter.next() ) ) ) {
iter.remove();
modified = true;
}
}
return modified;
}
/** {@inheritDoc} */
public boolean retainAll( TIntCollection collection ) {
if ( this == collection ) {
return false;
}
boolean modified = false;
TIntIterator iter = iterator();
while ( iter.hasNext() ) {
if ( ! collection.contains( iter.next() ) ) {
iter.remove();
modified = true;
}
}
return modified;
}
/** {@inheritDoc} */
public boolean retainAll( int[] array ) {
boolean changed = false;
Arrays.sort( array );
int[] set = _set;
byte[] states = _states;
for ( int i = set.length; i-- > 0; ) {
if ( states[i] == FULL && ( Arrays.binarySearch( array, set[i] ) < 0) ) {
removeAt( i );
changed = true;
}
}
return changed;
}
/** {@inheritDoc} */
public boolean removeAll( Collection> collection ) {
boolean changed = false;
for ( Object element : collection ) {
if ( element instanceof Integer ) {
int c = ( ( Integer ) element ).intValue();
if ( remove( c ) ) {
changed = true;
}
}
}
return changed;
}
/** {@inheritDoc} */
public boolean removeAll( TIntCollection collection ) {
if ( this == collection ) {
clear();
return true;
}
boolean changed = false;
TIntIterator iter = collection.iterator();
while ( iter.hasNext() ) {
int element = iter.next();
if ( remove( element ) ) {
changed = true;
}
}
return changed;
}
/** {@inheritDoc} */
public boolean removeAll( int[] array ) {
boolean changed = false;
for ( int i = array.length; i-- > 0; ) {
if ( remove( array[i] ) ) {
changed = true;
}
}
return changed;
}
/** {@inheritDoc} */
public void clear() {
TIntIntHashMap.this.clear();
}
/** {@inheritDoc} */
public boolean forEach( TIntProcedure procedure ) {
return TIntIntHashMap.this.forEachKey( procedure );
}
@Override
public boolean equals( Object other ) {
if (! (other instanceof TIntSet)) {
return false;
}
final TIntSet that = ( TIntSet ) other;
if ( that.size() != this.size() ) {
return false;
}
for ( int i = _states.length; i-- > 0; ) {
if ( _states[i] == FULL ) {
if ( ! that.contains( _set[i] ) ) {
return false;
}
}
}
return true;
}
@Override
public int hashCode() {
int hashcode = 0;
for ( int i = _states.length; i-- > 0; ) {
if ( _states[i] == FULL ) {
hashcode += HashFunctions.hash( _set[i] );
}
}
return hashcode;
}
@Override
public String toString() {
final StringBuilder buf = new StringBuilder( "{" );
forEachKey( new TIntProcedure() {
private boolean first = true;
public boolean execute( int key ) {
if ( first ) {
first = false;
} else {
buf.append( ", " );
}
buf.append( key );
return true;
}
} );
buf.append( "}" );
return buf.toString();
}
}
/** a view onto the values of the map. */
protected class TValueView implements TIntCollection {
/** {@inheritDoc} */
public TIntIterator iterator() {
return new TIntIntValueHashIterator( TIntIntHashMap.this );
}
/** {@inheritDoc} */
public int getNoEntryValue() {
return no_entry_value;
}
/** {@inheritDoc} */
public int size() {
return _size;
}
/** {@inheritDoc} */
public boolean isEmpty() {
return 0 == _size;
}
/** {@inheritDoc} */
public boolean contains( int entry ) {
return TIntIntHashMap.this.containsValue( entry );
}
/** {@inheritDoc} */
public int[] toArray() {
return TIntIntHashMap.this.values();
}
/** {@inheritDoc} */
public int[] toArray( int[] dest ) {
return TIntIntHashMap.this.values( dest );
}
public boolean add( int entry ) {
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
public boolean remove( int entry ) {
int[] values = _values;
byte[] states = _states;
for ( int i = values.length; i-- > 0; ) {
if ( ( states[i] != FREE && states[i] != REMOVED ) && entry == values[i] ) {
removeAt( i );
return true;
}
}
return false;
}
/** {@inheritDoc} */
public boolean containsAll( Collection> collection ) {
for ( Object element : collection ) {
if ( element instanceof Integer ) {
int ele = ( ( Integer ) element ).intValue();
if ( ! TIntIntHashMap.this.containsValue( ele ) ) {
return false;
}
} else {
return false;
}
}
return true;
}
/** {@inheritDoc} */
public boolean containsAll( TIntCollection collection ) {
TIntIterator iter = collection.iterator();
while ( iter.hasNext() ) {
if ( ! TIntIntHashMap.this.containsValue( iter.next() ) ) {
return false;
}
}
return true;
}
/** {@inheritDoc} */
public boolean containsAll( int[] array ) {
for ( int element : array ) {
if ( ! TIntIntHashMap.this.containsValue( element ) ) {
return false;
}
}
return true;
}
/** {@inheritDoc} */
public boolean addAll( Collection extends Integer> collection ) {
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
public boolean addAll( TIntCollection collection ) {
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
public boolean addAll( int[] array ) {
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@SuppressWarnings({"SuspiciousMethodCalls"})
public boolean retainAll( Collection> collection ) {
boolean modified = false;
TIntIterator iter = iterator();
while ( iter.hasNext() ) {
if ( ! collection.contains( Integer.valueOf ( iter.next() ) ) ) {
iter.remove();
modified = true;
}
}
return modified;
}
/** {@inheritDoc} */
public boolean retainAll( TIntCollection collection ) {
if ( this == collection ) {
return false;
}
boolean modified = false;
TIntIterator iter = iterator();
while ( iter.hasNext() ) {
if ( ! collection.contains( iter.next() ) ) {
iter.remove();
modified = true;
}
}
return modified;
}
/** {@inheritDoc} */
public boolean retainAll( int[] array ) {
boolean changed = false;
Arrays.sort( array );
int[] values = _values;
byte[] states = _states;
for ( int i = values.length; i-- > 0; ) {
if ( states[i] == FULL && ( Arrays.binarySearch( array, values[i] ) < 0) ) {
removeAt( i );
changed = true;
}
}
return changed;
}
/** {@inheritDoc} */
public boolean removeAll( Collection> collection ) {
boolean changed = false;
for ( Object element : collection ) {
if ( element instanceof Integer ) {
int c = ( ( Integer ) element ).intValue();
if ( remove( c ) ) {
changed = true;
}
}
}
return changed;
}
/** {@inheritDoc} */
public boolean removeAll( TIntCollection collection ) {
if ( this == collection ) {
clear();
return true;
}
boolean changed = false;
TIntIterator iter = collection.iterator();
while ( iter.hasNext() ) {
int element = iter.next();
if ( remove( element ) ) {
changed = true;
}
}
return changed;
}
/** {@inheritDoc} */
public boolean removeAll( int[] array ) {
boolean changed = false;
for ( int i = array.length; i-- > 0; ) {
if ( remove( array[i] ) ) {
changed = true;
}
}
return changed;
}
/** {@inheritDoc} */
public void clear() {
TIntIntHashMap.this.clear();
}
/** {@inheritDoc} */
public boolean forEach( TIntProcedure procedure ) {
return TIntIntHashMap.this.forEachValue( procedure );
}
/** {@inheritDoc} */
@Override
public String toString() {
final StringBuilder buf = new StringBuilder( "{" );
forEachValue( new TIntProcedure() {
private boolean first = true;
public boolean execute( int value ) {
if ( first ) {
first = false;
} else {
buf.append( ", " );
}
buf.append( value );
return true;
}
} );
buf.append( "}" );
return buf.toString();
}
}
class TIntIntKeyHashIterator extends THashPrimitiveIterator implements TIntIterator {
/**
* Creates an iterator over the specified map
*
* @param hash the TPrimitiveHash we will be iterating over.
*/
TIntIntKeyHashIterator( TPrimitiveHash hash ) {
super( hash );
}
/** {@inheritDoc} */
public int next() {
moveToNextIndex();
return _set[_index];
}
/** @{inheritDoc} */
public void remove() {
if ( _expectedSize != _hash.size() ) {
throw new ConcurrentModificationException();
}
// Disable auto compaction during the remove. This is a workaround for bug 1642768.
try {
_hash.tempDisableAutoCompaction();
TIntIntHashMap.this.removeAt( _index );
}
finally {
_hash.reenableAutoCompaction( false );
}
_expectedSize--;
}
}
class TIntIntValueHashIterator extends THashPrimitiveIterator implements TIntIterator {
/**
* Creates an iterator over the specified map
*
* @param hash the TPrimitiveHash we will be iterating over.
*/
TIntIntValueHashIterator( TPrimitiveHash hash ) {
super( hash );
}
/** {@inheritDoc} */
public int next() {
moveToNextIndex();
return _values[_index];
}
/** @{inheritDoc} */
public void remove() {
if ( _expectedSize != _hash.size() ) {
throw new ConcurrentModificationException();
}
// Disable auto compaction during the remove. This is a workaround for bug 1642768.
try {
_hash.tempDisableAutoCompaction();
TIntIntHashMap.this.removeAt( _index );
}
finally {
_hash.reenableAutoCompaction( false );
}
_expectedSize--;
}
}
class TIntIntHashIterator extends THashPrimitiveIterator implements TIntIntIterator {
/**
* Creates an iterator over the specified map
*
* @param map the TIntIntHashMap we will be iterating over.
*/
TIntIntHashIterator( TIntIntHashMap map ) {
super( map );
}
/** {@inheritDoc} */
public void advance() {
moveToNextIndex();
}
/** {@inheritDoc} */
public int key() {
return _set[_index];
}
/** {@inheritDoc} */
public int value() {
return _values[_index];
}
/** {@inheritDoc} */
public int setValue( int val ) {
int old = value();
_values[_index] = val;
return old;
}
/** @{inheritDoc} */
public void remove() {
if ( _expectedSize != _hash.size() ) {
throw new ConcurrentModificationException();
}
// Disable auto compaction during the remove. This is a workaround for bug 1642768.
try {
_hash.tempDisableAutoCompaction();
TIntIntHashMap.this.removeAt( _index );
}
finally {
_hash.reenableAutoCompaction( false );
}
_expectedSize--;
}
}
/** {@inheritDoc} */
@Override
public boolean equals( Object other ) {
if ( ! ( other instanceof TIntIntMap ) ) {
return false;
}
TIntIntMap that = ( TIntIntMap ) other;
if ( that.size() != this.size() ) {
return false;
}
int[] values = _values;
byte[] states = _states;
int this_no_entry_value = getNoEntryValue();
int that_no_entry_value = that.getNoEntryValue();
for ( int i = values.length; i-- > 0; ) {
if ( states[i] == FULL ) {
int key = _set[i];
if ( !that.containsKey( key ) ) return false;
int that_value = that.get( key );
int this_value = values[i];
if ((this_value != that_value)
&& ( (this_value != this_no_entry_value)
|| (that_value != that_no_entry_value))
) {
return false;
}
}
}
return true;
}
/** {@inheritDoc} */
@Override
public int hashCode() {
int hashcode = 0;
byte[] states = _states;
for ( int i = _values.length; i-- > 0; ) {
if ( states[i] == FULL ) {
hashcode += HashFunctions.hash( _set[i] ) ^
HashFunctions.hash( _values[i] );
}
}
return hashcode;
}
/** {@inheritDoc} */
@Override
public String toString() {
final StringBuilder buf = new StringBuilder( "{" );
forEachEntry( new TIntIntProcedure() {
private boolean first = true;
public boolean execute( int key, int value ) {
if ( first ) first = false;
else buf.append( ", " );
buf.append(key);
buf.append("=");
buf.append(value);
return true;
}
});
buf.append( "}" );
return buf.toString();
}
/** {@inheritDoc} */
public void writeExternal(ObjectOutput out) throws IOException {
// VERSION
out.writeByte( 0 );
// SUPER
super.writeExternal( out );
// NUMBER OF ENTRIES
out.writeInt( _size );
// ENTRIES
for ( int i = _states.length; i-- > 0; ) {
if ( _states[i] == FULL ) {
out.writeInt( _set[i] );
out.writeInt( _values[i] );
}
}
}
/** {@inheritDoc} */
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
// VERSION
in.readByte();
// SUPER
super.readExternal( in );
// NUMBER OF ENTRIES
int size = in.readInt();
setUp( size );
// ENTRIES
while (size-- > 0) {
int key = in.readInt();
int val = in.readInt();
put(key, val);
}
}
} // TIntIntHashMap
© 2015 - 2024 Weber Informatics LLC | Privacy Policy