我意识到 C# 和 .NET 一般来说已经有了 Hashtable 和 Dictionary 类。

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

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

有帮助吗?

解决方案

当然还有 Mono 版本的类库:

其他提示

问题问题之后很久,所以我不希望获得多少代表。但是我决定编写自己的基本示例(少于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();
        }

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

您是否查看过 C5收藏?您可以下载包含哈希表的源

您可以使用反射器

查看.NET Hashtable的实现方式(例如在C#中)

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

您可以查看简单的'仅限成长'哈希表这里,它可以让你了解一个简单的实现。

免责声明:代码中可能存在一些错误,但原理是相同的:)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top