C#中Hashtable实现的示例是什么?

Are*_*win 33 .net c# hashtable

我意识到C#和.NET通常已经有了Hashtable和Dictionary类.

任何人都可以在C#中演示Hashtable的实现吗?

更新:为了澄清,我不是必须寻找一个完整的实现,只是一个哈希表的核心功能的例子(即添加,删除,按键查找).

Ric*_*dOD 115

问题问题很久之后,所以我不希望获得多少代表.但是我决定编写自己的基本示例(少于90行代码)会很有趣:

    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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是一个小测试应用程序:

 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();
        }
Run Code Online (Sandbox Code Playgroud)

它不是最好的实现,但它适用于添加,删除和查找.它使用链接和简单的模数算法来查找适当的存储桶.

  • no..find只是O(n)最坏的情况(所有键都散列到相同的值......不太可能是hehe),但预期的情况是不变的.Insert和Delete都是常量,因为您只是创建哈希并从数组中的索引中插入/删除它.没有迭代元素. (4认同)
  • 当然,你可以看看基于Hash的集合的真实世界实现,但这个例子很干净,非常适合理解算法.它强调使用GetHashCode和Equals方法,这对于掌握HashMap的机制非常重要.非常感谢这个答案. (3认同)
  • 我知道这个问题和答案已经很老了,但我会考虑这个问题。插入\删除\查找操作不应该是 O(n) 效率吗? (2认同)

Jon*_*eet 8

你看过C5系列吗?您可以下载包含哈希表的源代码.


War*_*uck 8

您可以使用反射器查看.NET Hashtable的实现方式(例如在C#中)

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


Luk*_*ane 5

当然还有类库的Mono版本:

  • 死链接......嘘 (2认同)