65.9K
CodeProject is changing. Read more.
Home

CustomDictionary Class

starIconstarIconemptyStarIconemptyStarIconemptyStarIcon

2.00/5 (1 vote)

Nov 10, 2010

CPOL
viewsIcon

14858

Introduction

I was working on a project and I needed to use an object like Dictionary object in .net framework but I needed to use an object that accepts duplicate keys! I read few articles including: http://www.codeproject.com/KB/recipes/multikey-dictionary.aspx but I finally decided to write my own version of it.

The CustomDictionary is a C# class which extends the functionality of the Dictionary object provided in .net framework. It accepts multiple keys (duplicate key entries) and implements similar interface to IDictionary which developers are familiar with. However I suggest you to look at the Lookup<tkey,telement> class under System.Linq namespace as well if you are looking for using a class within .net framework.

Using the Code

The next section shows the source code of the CustomInterface interface, CustomDictionary class and an example which illustrates how to instantiate and use the CustomDictionary class in your application:

public interface ICustomDictionary<tkey,> 
{      
   ICollection<tkey> Keys { get; }
   ICollection<tvalue> Values { get; }
   bool ContainsKey(TKey key);
   void Add(TKey key, TValue value);
   void Add(KeyValuePair<tkey,> item);
   bool Remove(TKey key);
   bool TryGetValues(TKey key, out IList<tvalue> values);
}
    /// <summary>
    /// Represents a collection of keys and values. The custom dictionary class which accepts duplicate keys. 
    /// </summary>
    /// <typeparam name="TKey">The type of the key.</typeparam>
    /// <typeparam name="TValue">The type of the value.</typeparam>
    public class CustomDictionary<tkey,>: ICustomDictionary<tkey,tvalue>       
    {
        private IDictionary<guid,tkey> dictionaryKeys = new Dictionary<guid,tkey>();
        private IDictionary<guid,tvalue> dictionaryValues = new Dictionary<guid,tvalue>();
        ICollection<tkey> ICustomDictionary<tkey,>.Keys
        {
            get { return dictionaryKeys.Values; }
        }
        ICollection<tvalue> ICustomDictionary<tkey,>.Values
        {
            get { return dictionaryValues.Values; }
        }
        bool ICustomDictionary<tkey,>.ContainsKey(TKey key)
        {
            return dictionaryKeys.Any(keyValuePair => keyValuePair.Value.Equals(key));            
        }
        void ICustomDictionary<tkey,>.Add(TKey key, TValue value)
        {
            var association = Guid.NewGuid();
            dictionaryKeys.Add(association, key);
            dictionaryValues.Add(association, value);
        }
        bool ICustomDictionary<tkey,>.Remove(TKey key)
        {
            //find associations
            var associationsList =
            (from keyValuePair in dictionaryKeys 
             where keyValuePair.Value.Equals(key)
             select keyValuePair.Key).ToList();
            //remove from Dictionaries
            foreach (Guid guid in associationsList)
            {
                dictionaryKeys.Remove(guid);
                dictionaryValues.Remove(guid);
            }
            return (associationsList.Count > 0);
        }
        bool ICustomDictionary<tkey,>.TryGetValues(TKey key, out IList values)
        {
            IList<tvalue> list = new List<tvalue>();
            foreach (KeyValuePair<guid,> keyValuePair in dictionaryKeys)
            {
                if (keyValuePair.Value.Equals(key))
                {
                    //get the GUID (association key) 
                    var association = keyValuePair.Key;
                    // use the GUID to find the TB in the Values dictionary
                    list.Add(dictionaryValues[association]);
                }
            }
            values = list;
            return (list.Count > 0);
        }
        void ICustomDictionary<tkey,>.Add(KeyValuePair<tkey,> item)
        {
            var association = Guid.NewGuid();
            dictionaryKeys.Add(association, item.Key);
            dictionaryValues.Add(association, item.Value);
        }
    }

How to Use

private void button2_Click(object sender, EventArgs e)
        {
            ICustomDictionary<string,> customDictionary = new CustomDictionary<string,>();
            customDictionary.Add("1", "1A");
            customDictionary.Add("2", "2A");
            customDictionary.Add("3", "3A");
            customDictionary.Add("4", "4A");
            customDictionary.Add("1", "11A");
            customDictionary.Add("1", "111A");
            customDictionary.Add("2", "22A");
            customDictionary.Add(new KeyValuePair<string,>("2", "222A"));
            IList<string> list = new List<string>();
            bool re = customDictionary.TryGetValues("1", out list);
            ICollection<string> Keys = customDictionary.Keys;
            ICollection<string> vlaues = customDictionary.Values;
            bool contains = customDictionary.ContainsKey("4");
            bool removed = customDictionary.Remove("1");
        }