📄 hashmapjava.cpp
字号:
HashMapNode target = ( ( HashMapIterator ) e ). myNode ;
int probe = target . hash % length ;
HashMapNode node = buckets [ probe ];
if ( target == node )
{
buckets [ probe ] = target . next ;
}
else
{
while ( node . next != target )
node = node . next ;
node . next = target . next ;
}
-- size ;
return target == null
target . value ;
}
/**
* Remove the elements within a specified range.
* @param first An Enumeration positioned at the first element to remove.
* @param last An Enumeration positioned immediately after the last element to remove.
* @exception IllegalArgumentException is the Enumeration isn't a
* HashMapIterator for this HashMap object.
* @return Return the number of pairs removed.
*/
public synchronized int remove ( Enumeration first , Enumeration last ) 公众同步国际 删除 ( 枚举第一 ,
{
if ( ( ! ( first instanceof HashMapIterator ) ) ||
( ! ( last instanceof HashMapIterator ) ) )
throw new IllegalArgumentException ( "Enumeration not a HashMapIterator" );
if ( ( (( HashMapIterator ) first ). myHashMap != this ) ||
( (( HashMapIterator ) last ). myHashMap != this ) )
throw new IllegalArgumentException ( "Enumeration not for this HashMap" );
HashMapIterator begin = ( HashMapIterator ) first ;
HashMapIterator end = ( HashMapIterator ) last ;
int count = 0;
while ( ! begin . equals ( end ) )
{
HashMapIterator next = new HashMapIterator ( begin );
next . advance ();
remove ( begin );
begin = next ;
++ count ;
}
return count ;
}
/**
* Find the first key/value pair based on its key and return its position.
* If the key is not found, return end().
* @param key The key to locate.
*/ * /
public synchronized HashMapIterator find ( Object key )
{
int hash = key . hashCode () & 0x7FFFFFFF;
for ( HashMapNode node = buckets [ hash % length ]; node != null ; node = node . next )
if ( hash == node . hash && comparator . execute ( node . key , key ) )
return new HashMapIterator ( node , this , HashMapIterator . PAIR );
return new HashMapIterator ( null , this , HashMapIterator . PAIR );
}
/**
* Return the number of key/value pairs that match a particular key.
* @param key The key to match against.
*/
public synchronized int count ( Object key )
{
int hash = key . hashCode () & 0x7FFFFFFF;
for ( HashMapNode node = buckets [ hash % length ]; node != null ; node = node . next )
if ( hash == node . hash && comparator . execute ( node . key , key ) )
{
if ( allowDups )
{
int n = 1;
node = node . next ;
while ( node != null && hash == node . hash && comparator . execute ( node . key , key ) )
{
++ n ;
node = node . next ;
}
return n ;
}
else
{
return 1;
}
}
return 0;
}
/**
* Return the number of values that match a given object.
* @param value The value to match against.
*/ * /
public synchronized int countValues ( Object value )
{
return Counting . count
( (
new HashMapIterator ( first (), this , HashMapIterator . VALUE );
new HashMapIterator ( null , this , HashMapIterator . VALUE );
value
); ) ;
}
/**
* Return the value associated with key, or null if the key does not exist.
* @param key The key to search against.
*/
public synchronized Object get ( Object key )
{
int hash = key . hashCode () & 0x7FFFFFFF;
for ( HashMapNode node = buckets [ hash % length ]; node != null ; node = node . next )
if ( hash == node . hash && comparator . execute ( node . key , key ) )
return node . value ;
return null ;
}
/**
* If the key doesn't exist, associate the value with the key and return null,
* otherwise replace the first value associated with the key and return the old value.
* @param key The key.
* @param value The value.
* @exception NullPointerException If the key or value are equal to null
*/
public synchronized Object put ( Object key , Object value )
{
if ( key == null || value == null )
throw new NullPointerException ();
int hash = key . hashCode () & 0x7FFFFFFF;
int probe = hash % length ;
// find if key already exists first
for ( HashMapNode node = buckets [ probe ]; node != null ; node = node . next )
if ( hash == node . hash && comparator . execute ( node . key , key ) )
{
// replace old version & return it
node . key = key ;
Object previous = node . value ;
node . value = value ;
return previous ;
}
// key doesn't exists, add appropriately
HashMapNode newNode = new HashMapNode ();
newNode . key = key ;
newNode . value = value ;
newNode . hash = hash ;
newNode . next = buckets [ probe ];
buckets [ probe ] = newNode ;
if ( ++ size > limit )
expand ();
return null ;
}
/**
* Assume that the specified object is a Pair whose first field is a key and whose
* second field is a value.If the key doesn't exist or duplicates are allowed,
* associate the value with the key and return null, otherwise don't modify the map and
* return the current value associated with the key.
* @param object The pair to add.
* @exception java.lang.IllegalArgumentException If the object is not a Pair
* @exception java.lang.NullPointerException If the object is null or if the first
* or second items in the pair are null.
*/
public Object add ( Object object )
{
if ( object == null )
throw new NullPointerException ();
if ( !( object instanceof Pair ) )
throw new IllegalArgumentException ( "object is not pair" );
if ( (( Pair ) object ). first == null || (( Pair ) object ). second == null )
throw new NullPointerException ();
Pair pair = ( Pair ) object ;
return add ( pair . first , pair . second );
}
/** / **
* If the key doesn't exist or duplicates are allowed, associate the value with the
* key and return null, otherwise don't modify the map and return the current value
* associated with the key.
* @param key The key.
* @param value The value.
* @exception java.lang.NullPointerException If the key or value is null.
*/ * /
public synchronized Object add ( Object key , Object value )
{
if ( key == null || value == null )
throw new NullPointerException ();
int hash = key . hashCode () & 0x7FFFFFFF;
int probe = hash % length ;
// find if key already exists first
for ( HashMapNode node = buckets [ probe ]; node != null ; node = node . next )
if ( hash == node . hash && comparator . execute ( node . key , key ) )
{
if ( allowDups )
{
// duplicate key, add this pair to end and return success.
HashMapNode newNode = new HashMapNode ();
newNode . key = key ; newNode 。
newNode . value = value ; newNode 。
newNode . hash = hash ; newNode 。
newNode . next = node . next ;
node . next = newNode ;
if ( ++ size > limit )
expand ();
return null ;
}
else
{
// return the value of the key/value that already exists.
return node . value ;
}
}
// key doesn't exists, add appropriately
HashMapNode newNode = new HashMapNode ();
newNode . key = key ;
newNode . value = value ;
newNode . hash = hash ;
newNode . next = buckets [ probe ];
buckets [ probe ] = newNode ;
if ( ++ size > limit )
expand ();
return null ;
}
/**
* Return an Enumeration of all my keys.
*/
public synchronized Enumeration keys ()
{
return new HashMapIterator ( first (), this , HashMapIterator . KEY );
}
/**
* Return an Enumeration of all my keys that are associated with a particular value.
* @param value The value to match.
*/
public synchronized Enumeration keys ( Object value )
{
Array array = new Array ();
for ( HashMapIterator iterator = begin (); iterator . hasMoreElements (); iterator . advance () )
if ( iterator . value (). equals ( value ) )
array . pushBack ( iterator . key () );
return array . elements ();
}
/**
* Return an Enumeration of all my values that are associated with a particular key.
* @param key The key to match.
*/
public synchronized Enumeration values ( Object key )
{
Array array = new Array ();
for ( HashMapIterator iterator = begin (); iterator . hasMoreElements (); iterator . advance () )
if ( comparator . execute ( iterator . key (), key ) )
array . pushBack ( iterator . value () );
return array . elements ();
} )
/**
* Return an iterator positioned at the first location that a
* pair with a specified key could be insert ed without violating the ordering
* criteria. If no such location is found, return an iterator positioned at end().
* @param key The key.
*/
public synchronized HashMapIterator lowerBound ( Object key )
{
return ( HashMapIterator ) equalRange ( key ). begin ;
}
/**
* Return an iterator positioned at the last location that
* a pair with a specified key could be insert ed without violating the ordering
* criteria. If no such location is found, return an iterator positioned at end().
* @param key The key.
*/
public synchronized HashMapIterator upperBound ( Object key )
{
return ( HashMapIterator ) equalRange ( key ). end ;
}
/**
* Return a range whose first element is an iterator positioned
* at the first occurence of a specific key and whose second element is an
* iterator positioned immediately after the last occurence of that key.
* Note that all key inbetween these iterators will also match the specified
* key. If no matching key is found, both ends of the range will be the
* same.
* @param object The key whose bounds are to be found.
*/
public synchronized Range equalRange ( Object key )
{ (
int hash = key . hashCode () & 0x7FFFFFFF;
for ( HashMapNode node = buckets [ hash % length ]; node != null ; node = node . next )
if ( hash == node . hash && comparator . execute ( node . key , key ) )
{
HashMapNode begin = node ;
node = node . next == null ? next ( node ) : node . next ;
while ( node != null && hash == node . hash && comparator . execute ( node . key , key ) )
node = node . next == null ? next ( node ) : node . next ;
return new Range ( new HashMapIterator ( begin , this , HashMapIterator . PAIR ),
new HashMapIterator ( node , this , HashMapIterator . PAIR ) );
}
return new Range ( end (), end () );
}
private HashMapNode first ()
{
if ( size > 0 )
for ( int i = 0; i < length ; i ++ )
if ( buckets [ i ] != null )
return buckets [ i ];
return null ;
}
private HashMapNode next ( HashMapNode node )
{
for ( int i = ( node . hash % length ) + 1; i < length ; i ++ )
if ( buckets [ i ] != null )
return buckets [ i ];
return null ;
}
/**
* Return true if adding an object to myself could result in an expansion
* of the number of hash buckets I currently use.
*/
public boolean expansionAllowed ()
{
return expandActive ;
}
/**
* Enable or disable the current expansion mode. If disabled, no new
* hash buckets will ever be created regardless of my size.
* @param allow The new expansion mode.
*/
public synchronized void allowExpansion ( boolean allow )
{
expandActive = allow ;
}
private void expand ()
{
if ( ! expansionAllowed () )
return ;
int newLength = length * 2 + 1;
HashMapNode [] newBuckets = new HashMapNode [ newLength ];
for ( int i = 0; i < length ; i ++ )
{
HashMapNode node = buckets [ i ];
while ( node != null )
{
HashMapNode current = node ;
node = node . next ;
int probe = current . hash % newLength ;
current . next = newBuckets [ probe ];
newBuckets [ probe ] = current ;
}
}
buckets = newBuckets ;
length = newLength ;
limit = ( int )( length * ratio );
}
private boolean same ( Enumeration src , Enumeration dst )
{
Array srcValues = new Array ();
Array dstValues = new Array ();
while ( src . hasMoreElements () )
srcValues . add ( src . nextElement () );
while ( dst . hasMoreElements () )
dstValues . add ( dst . nextElement () );
if ( srcValues . size () != dstValues . size () )
return false ;
for ( int i = 0; i < srcValues . size (); i ++ )
{
Object x = srcValues . at ( i );
int srcCount = 0;
int dstCount = 0;
// Count the number of times 'x' occurs in each array of values.
for ( int j = 0; j < dstValues . size (); j ++ )
{
if ( srcValues . at ( j ). equals ( x ) )
++ srcCount ;
if ( dstValues . at ( j ). equals ( x ) )
++ dstCount ;
}
if ( srcCount != dstCount )
return false ;
}
return true ;
}
private void size ( int newsize )
{
size = newsize ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -