Dictionary is powerful data structure to maintain data in key value pairs. Due to this key value pair dictionary becomes quite useful in coding. Developer can fetch only the data which is corresponding to a specific key.
A dictionary can have duplicate record, but key must be unique.
How dictionary maintain data and how it performs operation?
A dictionary uses linked lists to maintain data. Consider you are using a generic dictionary<string, ‘AnyType’>. Dictionary sorts and search data based on the key, now will it sort data if key is string type, for obvious reasons sorting and searching on string will be quite slow as compared to int or other enum types.
So, how does dictionary perform these operations so quickly?
Well, quite simple. It uses hash code. Dictionary converts the key value into its corresponding hash code and store only that hash code. Now searching and sorting on this hash code becomes fast.
How does it do it, practically?
Just like linked list, let’s create a Node class. To understand following code better I would say please go and watch how linked list is implemented here
- namespace Generic
- {
- public class Node<T>
- {
- public T data;
- public Node(T data)
- {
- this.data = data;
- }
- }
- }
Now to maintain key value pair and to link the objects we need to create another class
- namespace Generic
- {
- public class HashNodeMap<T>
- {
- public int key;
- public Node<T> data;
- public HashNodeMap<T> next;
- }
- }
- namespace Generic
- {
- public class GenericHashDictionary<Tkey,Tdata>
- {
- public HashNodeMap<Tdata> hashNode = null;
- public int Count;
- public object this[Tkey s]
- {
- get
- {
- return FindHashCode(s).data;
- }
- set
- {
- // You logic to update the item found
- // Ex- hashNode[“key”] = ‘AnyType’;
- }
- }
- // Generic method to create new node every time an item is added in custom dictionary
- private HashNodeMap<Tdata> CreateNode(Tkey key, Tdata data)
- {
- int hashCode = key.GetHashCode();
- HashNodeMap<Tdata> newNodeMap = new HashNodeMap<Tdata>();
- newNodeMap.data = new Node<Tdata>(data);
- newNodeMap.key = hashCode;
- return newNodeMap;
- }
- // Method to add new item in custom dictionary
- public void Add(Tkey key, Tdata data)
- {
- HashNodeMap<Tdata> current = null;
- if (hashNode == null)
- {
- hashNode = CreateNode(key, data);
- }
- else
- {
- current = hashNode;
- while (current.next != null)
- {
- current = current.next;
- }
- current.next = CreateNode(key, data);
- }
- Count++;
- }
- private Node<Tdata> FindHashCode(Tkey key)
- {
- Node<Tdata> data = null;
- int hashCode = key.GetHashCode();
- HashNodeMap<Tdata> current = hashNode;
- while (current != null)
- {
- if (current.key == hashCode)
- {
- data = current.data;
- break;
- }
- if (current.next != null)
- {
- current = current.next;
- }
- }
- return data;
- }
- }
- }
Now time to use this
- GenericHashDictionary<string, string> hdt = new GenericHashDictionary<string, string>();
- hdt.Add("userName", "ishoo");
- hdt.Add("password", "1234");
- hdt.Add("email", "ishooagarwal");
As you can see the (-ve) numbers these are keys which I used.
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.
In my next blog I will show you how to to sort remove and iterate through the custom dictionary.
Hope you enjoy it and find it helpfull
Thanks
No comments:
Post a Comment