Home > Net >  Please help look at the conditions of the Hashtable collision
Please help look at the conditions of the Hashtable collision

Time:03-17

https://source.dot.net/#System.Private.CoreLib/Hashtable.cs



The code above under what circumstances will go _buckets [bucketNumber] key==_buckets & amp; & ((_buckets [bucketNumber] hash_coll & amp; Unchecked (0 x80000000))==0)) this logic
See for a long time the key is set to _buckets entry is only in the remove when the collision occurred, hash_coll will retain collision tags at the same time, the above the logic seems unlikely to hit

Have a familiar friend can help to look at, where is my legacy, what circumstance will hit the condition


//Inserts an entry into this hashtable. This method is called from the Set 
//and Add the methods. If the Add parameter is true, and the given key already
//the exists in the hashtable, an exception is thrown.
Private void Insert (object key, the object? Nvalue, bool add)
{
If (key==null)
{
Throw new ArgumentNullException (nameof (key), SR., ArgumentNull_Key);
}

If (_count & gt;=_loadsize)
{
Expand ();
}
Else if (_occupancy & gt; _loadsize & amp; & _count & gt; 100)
{
Rehash ();
}

//Assume we only have one thread writing concurrently. The Modify
//buckets to contain the new data, as long as we insert in the right order.
Uint hashcode=InitHash (key, _buckets Length, out uint seed, out uint incr);
Int ntry=0;
Int emptySlotNumber=1;//We use the empty slot number to cache the first empty slot. We chose to reuse slots
//create by remove that have the collision bit set over the using up of new slots.
Int bucketNumber=(int) (seed % (uint) _buckets. The Length).
Do
{
//Set emptySlot number to current bucket if it is the first available bucket that we have seen
//that once contained an entry and also has had a collision.
//We need to search this - collision chain because We have to ensure that there are no
//duplicate entries in the table.
If (emptySlotNumber==1 & amp; & (_buckets [bucketNumber]. Key==_buckets) & amp; & (_buckets [bucketNumber] hash_coll & lt; 0))//(((buckets) [r]. BucketNumber hash_coll & amp; Unchecked (0 x80000000))!
=0)))EmptySlotNumber=bucketNumber;

//Insert the key/value pair into this bucket if this bucket is empty and has never contained an entry
///
//This bucket once contained an entry but there has never had a collision
If ((_buckets [bucketNumber]. Key==null) | |
(_buckets [bucketNumber]. Key==_buckets & amp; & ((_buckets [bucketNumber] hash_coll & amp; Unchecked (0 x80000000))==0)))
{
//If we have found an available bucket that has never had a collision, but we 've seen the an available
//the bucket in the past that has the collision bit set, use the previous bucket home
If (emptySlotNumber!=1)//Reuse slot
BucketNumber=emptySlotNumber;

//We pretty much have to insert in this order. Don 't set the hash
//code until the value & amp; The key are set appropriately.
_isWriterInProgress=true;
_buckets [bucketNumber]. Val=nvalue;
_buckets [bucketNumber] key=key;
_buckets [bucketNumber] hash_coll |=(int) hashcode;
_count + +;
UpdateVersion ();
_isWriterInProgress=false;

return;
}

//The current bucket is in use
///
//it is available, but has had the collision bit set and we have already found an available bucket
If (((_buckets [bucketNumber] hash_coll & amp; 0 x7fffffff)==hashcode) & amp; &
KeyEquals (_buckets [bucketNumber]. The key, the key))
{
If (add)
{
Throw new ArgumentException (SR. The Format (SR) Argument_AddingDuplicate__, _buckets [bucketNumber]. The key, the key));
}
_isWriterInProgress=true;
_buckets [bucketNumber]. Val=nvalue;
UpdateVersion ();
_isWriterInProgress=false;

return;
}

//The current bucket is full, and we have therefore collided. We need to set The collision bit
//unless we have remembered an available slot previously.
If (emptySlotNumber==1)
{//We don 't need to set the collision bit here since We already have an empty slot
If (_buckets [bucketNumber] hash_coll & gt;=0)
{
_buckets [bucketNumber] hash_coll |=unchecked ((int) 0 x80000000);
_occupancy + +;
}
}

BucketNumber=(int) (((long) bucketNumber + incr) % (uint) _buckets. The Length).
} while (+ + ntry & lt; _buckets. Length);

//This code is here if and only if there were no buckets without a collision bit set in the - table
If (emptySlotNumber!=1)
{
//We pretty much have to insert in this order. Don 't set the hash
//code until the value & amp; The key are set appropriately.
_isWriterInProgress=true;
_buckets [emptySlotNumber]. Val=nvalue;
_buckets [emptySlotNumber] key=key;
_buckets [emptySlotNumber] hash_coll |=(int) hashcode;
_count + +;
UpdateVersion ();
_isWriterInProgress=false;

return;
}

//If you see this assert, make sure the load factor & amp; The count are reasonable.
nullnullnullnullnull
  •  Tags:  
  • C#
  • Related