tags:

views:

1890

answers:

6

I realize C# and .NET in general already has the Hashtable and Dictionary classes.

Can anyone demonstrate in C# an implementation of a Hashtable?

Update: To clarify, I'm not ncessarily looking for a complete implementation, just an example of the core features of a hashtable (i.e. add,remove, find by key).

+7  A: 

Have you looked at the C5 collections? You can download the source which includes a hash table.

Jon Skeet
Thanks for the link. I was hoping for a small basic example of Add/Remove with the hashing modulo including, but not sure if that will fill the entire page
Arec Barrwin
+6  A: 

You can see how the .NET Hashtable is implemented (for example in C#) using reflector

http://www.red-gate.com/products/reflector/

Ward Werbrouck
+2  A: 

You can view a simple 'grow-only' hashtable here, which should give you an idea of a simple implementation.

Disclaimer: There is probably a few bugs in the code, but the principle is the same :)

leppie
+2  A: 

You could also look at the Hashtable implementation from Mono here:

http://anonsvn.mono-project.com/viewvc/trunk/mcs/class/corlib/System.Collections/Hashtable.cs?revision=111788&view=markup

Paul Dixon
+3  A: 

There is also the Mono version of the class libraries of course:

Luke Quinane
+5  A: 

Long after the question has been asked, so I don't expect to earn much rep. However I decided it would be fun to write my own very basic example (in less than 90 lines of code):

    public struct KeyValue<K, V>
    {
        public K Key { get; set; }
        public V Value { get; set; }
    }

    public class FixedSizeGenericHashTable<K,V>
    {
        private readonly int size;
        private readonly LinkedList<KeyValue<K,V>>[] items;

        public FixedSizeGenericHashTable(int size)
        {
            this.size = size;
            items = new LinkedList<KeyValue<K,V>>[size];
        }

        protected int GetArrayPosition(K key)
        {
            int position = key.GetHashCode() % size;
            return Math.Abs(position);
        }

        public V Find(K key)
        {
            int position = GetArrayPosition(key);
            LinkedList<KeyValue<K, V>> linkedList = GetLinkedList(position);
            foreach (KeyValue<K,V> item in linkedList)
            {
                if (item.Key.Equals(key))
                {
                    return item.Value;
                }
            }

            return default(V);
        }

        public void Add(K key, V value)
        {
            int position = GetArrayPosition(key);
            LinkedList<KeyValue<K, V>> linkedList = GetLinkedList(position);
            KeyValue<K, V> item = new KeyValue<K, V>() { Key = key, Value = value };
            linkedList.AddLast(item);
        }

        public void Remove(K key)
        {
            int position = GetArrayPosition(key);
            LinkedList<KeyValue<K, V>> linkedList = GetLinkedList(position);
            bool itemFound = false;
            KeyValue<K, V> foundItem = default(KeyValue<K, V>);
            foreach (KeyValue<K,V> item in linkedList)
            {
                if (item.Key.Equals(key))
                {
                    itemFound = true;
                    foundItem = item;
                }
            }

            if (itemFound)
            {
                linkedList.Remove(foundItem);
            }
        }

        protected LinkedList<KeyValue<K, V>> GetLinkedList(int position)
        {
            LinkedList<KeyValue<K, V>> linkedList = items[position];
            if (linkedList == null)
            {
                linkedList = new LinkedList<KeyValue<K, V>>();
                items[position] = linkedList;
            }

            return linkedList;
        }
    }

Here's a little test application:

 static void Main(string[] args)
        {
            FixedSizeGenericHashTable<string, string> hash = new FixedSizeGenericHashTable<string, string>(20);

            hash.Add("1", "item 1");
            hash.Add("2", "item 2");
            hash.Add("dsfdsdsd", "sadsadsadsad");

            string one = hash.Find("1");
            string two = hash.Find("2");
            string dsfdsdsd = hash.Find("dsfdsdsd");
            hash.Remove("1");
            Console.ReadLine();
        }

It's not the best implementation, but it works for Add, Remove and Find. It uses chaining and a simple modulo algorithm to find the appropriate bucket.

RichardOD