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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/625947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:47:57  来源:igfitidea点击:

What is an example of a Hashtable implementation in C#?

c#.nethashtable

提问by Arec Barrwin

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

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

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

任何人都可以在 C# 中演示 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).

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

采纳答案by Luke Quinane

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

当然还有 Mono 版本的类库:

回答by Jon Skeet

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

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

回答by Ward Werbrouck

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

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

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

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

回答by RichardOD

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):

在提出这个问题很久之后,所以我不希望获得太多代表。但是,我认为编写自己的非常基本的示例(不到 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;
        }
    }

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 chainingand a simple modulo algorithm to find the appropriate bucket.

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