【问题标题】:What is an example of a Hashtable implementation in C#?什么是 C# 中 Hashtable 实现的示例?
【发布时间】:2010-10-12 04:12:18
【问题描述】:

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

谁能用 C# 演示 Hashtable 的实现?

更新:澄清一下,我不一定要寻找完整的实现,只是哈希表核心功能的一个示例(即添加、删除、按键查找)。

【问题讨论】:

  • 我知道这是一个老问题,但实际上我已经费心用 62 行代码实现一个简单的 HashTable 来执行添加和查找。
  • 2015年可以找到here

标签: c# .net hashtable


【解决方案1】:

【讨论】:

  • @longda 现在应该修复了
  • 感谢@danfromisrael,它们应该再次修复。这次我链接到了一个标签,所以希望这会安全一段时间。
【解决方案2】:

问题被问了很久,所以我不希望获得太多代表。然而,我决定编写自己的非常基本的示例(不到 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;
        }
    }

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

 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();
        }

这不是最好的实现,但它适用于添加、删除和查找。它使用chaining 和一个简单的模算法来找到合适的桶。

【讨论】:

  • 我知道这个问题和答案已经很老了,但我会接受这个远景。 insert\delete\find 操作不应该是 O(n) 效率吗?
  • no..find 只是 O(n) 最坏的情况(所有键哈希到相同的值..不太可能呵呵),但预期的情况是不变的。插入和删除都是常量,因为您只需创建哈希并从数组中的索引中插入/删除它。没有迭代元素。
  • 当然,您可以查看基于哈希的集合的真实世界实现,但是这个示例很简洁,非常适合理解算法。它强调 GetHashCode 和 Equals 方法的使用,这对于掌握 HashMap 的机制非常重要。非常感谢这个答案。
  • @RichardOD 干得好。但是,在 Add() 方法中,您可能需要检查密钥是否已经存在。
  • 我想复习一些数据结构,所以我想看看一个简单的实现,这非常完美,谢谢!
【解决方案3】:
【解决方案4】:

您可以看到 .NET Hashtable 是如何使用反射器实现的(例如在 C# 中)

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

【讨论】:

    【解决方案5】:

    你看过C5 collections吗?您可以download the source,其中包含一个哈希表。

    【讨论】:

    • 感谢您的链接。我希望有一个添加/删除的基本示例,其中包含哈希模数,但不确定这是否会填满整个页面
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 2013-10-29
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 2017-07-22
    相关资源
    最近更新 更多