Question about Hashtable's Bucket

A

ahoneytrap

Hello all,

I've been scouring through the source code (reflector, shared source)
of the framework and was looking at the hashtable implementation.

From what I can tell, the default implentation uses an array of Bucket
objects which contain the key and value. Then the hash is calculated
elsewhere to point to the index in the array of buckets.

So hashTable.Add("tree","value") would create a bucket object with
"tree" and "value" which is added to the array (which is resized every
100 entries from what I can see). What I'm wondering is how does it
know where in the array to place the item? I appreciate some kind of
modula is done based on the int hash of "tree", but can anyone be more
precise? I'm interested in how it places it in the array rather than
the hash function itself.
 
G

Göran Andersson

Hello all,

I've been scouring through the source code (reflector, shared source)
of the framework and was looking at the hashtable implementation.

From what I can tell, the default implentation uses an array of Bucket
objects which contain the key and value. Then the hash is calculated
elsewhere to point to the index in the array of buckets.

So hashTable.Add("tree","value") would create a bucket object with
"tree" and "value" which is added to the array (which is resized every
100 entries from what I can see). What I'm wondering is how does it
know where in the array to place the item? I appreciate some kind of
modula is done based on the int hash of "tree", but can anyone be more
precise? I'm interested in how it places it in the array rather than
the hash function itself.

It takes the hashcode modulo the size of the array, so if the size is 5
and the hash code is 8, the index would be (8 % 5) = 3.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top