{"id":286,"date":"2023-07-30T06:19:21","date_gmt":"2023-07-30T05:19:21","guid":{"rendered":"https:\/\/ishooscode.com\/?p=286"},"modified":"2023-07-30T06:23:30","modified_gmt":"2023-07-30T05:23:30","slug":"how-does-a-dictionary-maintains-data","status":"publish","type":"post","link":"https:\/\/ishooscode.com\/index.php\/2023\/07\/30\/how-does-a-dictionary-maintains-data\/","title":{"rendered":"How does a dictionary maintains data"},"content":{"rendered":"\n<p>Dictionary is common data structure which most of developers use to store data for quick access in future. It provides various operation, has good capabilities and has really good time complexity. But, Do you ever wonder how dictionary does all this effortlessly. A common curiosity which every developer should have is how does a dictionary maintains data internally. Well, Answer to this question is rather painless.<\/p>\n\n\n\n<p>A dictionary uses linked lists to store data. Consider you are using a generic dictionary&lt;string, \u2018<em>AnyType<\/em>\u2019&gt;. A dictionary sorts and searches data based on the key which will be timer consuming and resource consuming as well to sort on string type.&nbsp;<\/p>\n\n\n\n<p><strong>How does dictionary perform these operations so quickly?<\/strong><\/p>\n\n\n\n<p>Well, quite elementary. It uses a hash code to sort data. Dictionary converts the key value into its corresponding hash code and stores only that hash code. Now searching and sorting on hash code becomes faster.<\/p>\n\n\n\n<p><strong>How does it do it, practically?<\/strong><\/p>\n\n\n\n<p>Just like linked list, let\u2019s create a Node class. To understand following code better, I would say please go and watch how linked list is implemented&nbsp;<a href=\"https:\/\/codeinout.blogspot.com\/2019\/10\/implementing-generic-linked-list-with.html\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace Generic  \n{  \n    public class Node&lt;T&gt;  \n    {  \n        public T data;  \n        public Node(T data)  \n        {  \n            this.data = data;  \n        }  \n    }  \n}<\/code><\/pre>\n\n\n\n<p>Now to maintain a key value pair and to link the objects, we need to create another class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace Generic  \n{  \n    public class HashNodeMap&lt;T&gt;  \n    {  \n        public int key;  \n        public Node&lt;T&gt; data;  \n        public HashNodeMap&lt;T&gt; next;  \n    }  \n}  <\/code><\/pre>\n\n\n\n<p>Above, a key is used to store hash code of each key, a node stores actual data. The next step will help us link the new item.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class GenericHashDictionary&lt;Tkey,Tdata&gt;    \n    {    \n        public HashNodeMap&lt;Tdata&gt; hashNode = null;    \n        public int Count;    \n    \n        public object this&#91;Tkey s]    \n        {    \n            get    \n            {    \n                return FindHashCode(s).data;    \n            }    \n            set    \n            {    \n    \n            }    \n        }    \n    \n        private HashNodeMap&lt;Tdata&gt; CreateNode(Tkey key, Tdata data)    \n        {    \n            int hashCode = key.GetHashCode();    \n            HashNodeMap&lt;Tdata&gt; newNodeMap = new HashNodeMap&lt;Tdata&gt;();    \n            newNodeMap.data = new Node&lt;Tdata&gt;(data);    \n            newNodeMap.key = hashCode;    \n            return newNodeMap;    \n        }    \n    \n        public void Add(Tkey key, Tdata data)    \n        {    \n            HashNodeMap&lt;Tdata&gt; current = null;    \n            if (hashNode == null)    \n            {    \n                hashNode = CreateNode(key, data);    \n            }    \n            else    \n            {    \n                current = hashNode;    \n                while (current.next != null)    \n                {    \n                    current = current.next;    \n                }    \n                current.next = CreateNode(key, data);    \n            }    \n            Count++;    \n        }    \n    \n        private Node&lt;Tdata&gt; FindHashCode(Tkey key)    \n        {    \n            Node&lt;Tdata&gt; data = null;    \n            int hashCode = key.GetHashCode();    \n            HashNodeMap&lt;Tdata&gt; current = hashNode;    \n    \n            while (current != null)    \n            {    \n                if (current.key == hashCode)    \n                {    \n                    data = current.data;    \n                    break;    \n                }    \n                if (current.next != null)    \n                {    \n                    current = current.next;    \n                }    \n            }    \n            return data;    \n        }    \n}<\/code><\/pre>\n\n\n\n<p>Above are some basic operation performed in a standard dictionary.<\/p>\n\n\n\n<p>Now time to use this<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GenericHashDictionary&lt;string, string&gt; hdt = new GenericHashDictionary&lt;string, string&gt;();  \nhdt.Add(\"userName\", \"ishoo\");  \nhdt.Add(\"password\", \"1234\");  \nhdt.Add(\"email\", \"ishooagarwal\");  <\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"792\" height=\"352\" src=\"https:\/\/ishooscode.com\/wp-content\/uploads\/2023\/07\/Dictionary.png\" alt=\"How does a dictionary maintains data\" class=\"wp-image-287\" srcset=\"https:\/\/ishooscode.com\/wp-content\/uploads\/2023\/07\/Dictionary.png 792w, https:\/\/ishooscode.com\/wp-content\/uploads\/2023\/07\/Dictionary-300x133.png 300w, https:\/\/ishooscode.com\/wp-content\/uploads\/2023\/07\/Dictionary-768x341.png 768w\" sizes=\"(max-width: 792px) 100vw, 792px\" \/><\/figure>\n\n\n\n<p>As you can see the (-ve) numbers these are keys which I had used.&nbsp;<\/p>\n\n\n\n<p>I am still developing this data structure and implementing various things in it. Currently I am implementing IEnumerable and IEnumerator so that foreach loop can be performed on this custom dictionary.&nbsp;<\/p>\n\n\n\n<p>In my next blog I will show you how to to sort remove and iterate through the custom dictionary.<\/p>\n\n\n\n<p>Hope you enjoy it and find it helpful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dictionary is common data structure which most of developers use to store data for quick&#8230;<\/p>\n","protected":false},"author":1,"featured_media":289,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[22,61],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/posts\/286"}],"collection":[{"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/comments?post=286"}],"version-history":[{"count":2,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/posts\/286\/revisions"}],"predecessor-version":[{"id":291,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/posts\/286\/revisions\/291"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/media\/289"}],"wp:attachment":[{"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/media?parent=286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/categories?post=286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/tags?post=286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}