[C # advanced series] Topic 2: Do you know why Dictionary search is fast ?,

Source: Internet
Author: User

[C # advanced series] Topic 2: Do you know why Dictionary search is fast ?,
I. Preface

In the previous interview, I was asked if you understand the internal implementation mechanism of Dictionary? At that time, it was just a simple question and answer: the internal structure of Dictionary is a hash table, which can be quickly searched. However, it is not clear to know more about it. So after the interview, I plan to study the source code of Dictionary. So we have this article.

Ii. Analysis of Dictionary source code

As we all know, Microsoft has now opened up the. NET Framework source code. The online source code can be viewed at: http://referencesource.microsoft.com /. Find the source code of the. NET Framework class. Let's take a look at the Dictionary source code.

2.1 Add Element

First, let's look at the implementation of the Dictionary. Add method. For better implementation, the core part of the Dictionary source code is extracted for analysis. The detailed analysis code is as follows:

// Buckets is a Hash table used to store the Key's Hash value // entries is used to store the element list // count is the number of elements private void Insert (TKey key, TValue value, bool add) {if (key = null) {throw new ArgumentNullException (key. toString ();} // first allocate the space of buckets and entries if (buckets = null) Initialize (0); int hashCode = comparer. getHashCode (key) & 0x7FFFFFFF; // calculate the HashCode int targetBucket = hashCode % buckets corresponding to the key value. length; // calculate the remainder of the hash value to obtain the location where the hash table needs to be assigned # if FEATURE_RANDOMIZED_STRING_HASHING int collisionCount = 0; # endif // processing logic for conflicting processing (int I = buckets [targetBucket]; I> = 0; I = entries [I]. next) {if (entries [I]. hashCode = hashCode & comparer. equals (entries [I]. key, key) {if (add) {throw new ArgumentNullException ();} entries [I]. value = value; version ++; return ;}# if FEATURE_RANDOMIZED_STRING_HASHING collisionCount ++; # endif} int index; // index records the position of an element in the element list. if (freeCount> 0) {index = freeList; freeList = entries [index]. next; freeCount --;} else {// if the hash value in the hash table is full, retrieve the value from the primers array again as the new size of the hash table if (count = entries. length) {Resize (); targetBucket = hashCode % buckets. length ;}/// if the size is not full, the logical index = count; count ++;} // assigns the entries [index] value to the element list. hashCode = hashCode; entries [index]. next = buckets [targetBucket]; entries [index]. key = key; entries [index]. value = value; // assign values to the hash table buckets [targetBucket] = index; version ++; # if FEATURE_RANDOMIZED_STRING_HASHING if (collisionCount> HashHelpers. hashCollisionThreshold & HashHelpers. isWellKnownEqualityComparer (comparer) {comparer = (IEqualityComparer <TKey>) HashHelpers. getRandomizedEqualityComparer (comparer); Resize (entries. length, true) ;}# endif}

The following example shows how to Add the element code to better understand the implementation principle of the Add method.

  Dictionary<int, string> myDictionary = new Dictionary<int, string>();            myDictionary.Add(1, "Item 1");            myDictionary.Add(2, "Item 2");            myDictionary.Add(3, "Item 3");

When the first element is added, the space and initial size of the buckets array and entries array in the hash table are 3. After the allocation, the hash value of the key value of the added element is calculated, hash Value Calculation is implemented by a specific hash algorithm. Assume that the hash value of 1 is 9, targetBucket = 9% buckets. if the Length (3) value is 0 and the index value is 0, the first element is stored in the first position in the entries list, and the hash table is assigned a value, at this time, the value of the value is 0th, and its value is the index value. Therefore, it is 0. After the first element is inserted, the internal structure of the Dictionary is as follows:

Public TValue this [TKey key] {get {int I = FindEntry (key); // obtain the corresponding Value if (I> = 0) directly from the position where the element exists) return entries [I]. value; throw new KeyNotFoundException (); return default (TValue) ;}set {Insert (key, value, false) ;}} private int FindEntry (TKey key) {if (key = null) {throw new ArgumentNullException ();} if (buckets! = Null) {// obtain the hash value corresponding to the Key value int hashCode = comparer. getHashCode (key) & 0x7FFFFFFF; // query the position of an element in the element list. If no conflict exists, the search speed is O (1 ), if there is a conflict, it is O (N), and N is the number of conflicts for (int I = buckets [hashCode % buckets. length]; I> = 0; I = entries [I]. next) {if (entries [I]. hashCode = hashCode & comparer. equals (entries [I]. key, key) return I;} return-1 ;}

The Code shows that our previous analysis is correct. It can be understood that the reason why Dictionary can quickly find elements is that it uses a hash table internally to store the corresponding positions of elements, then, we can quickly locate the position index of the element in the hash table by using the hash Value, so as to quickly obtain the Value corresponding to the key.

Iv. Summary

It can be said that the implementation principle of Dictionary is also a way of changing the space for time. Instead, a bucket is used to store the location of elements, thus improving the search speed.

Next, we will open a new domain-driven design series. Please make a lot of bricks.

Download all source code in this article: DictonaryInDepth.zip

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.