37

一般的に、C# と .NET には既に Hashtable クラスと Dictionary クラスがあることを認識しています。

C# で Hashtable の実装を実証できる人はいますか?

更新:明確にするために、私は必ずしも完全な実装を探しているわけではなく、ハッシュテーブルのコア機能の例(つまり、追加、削除、キーによる検索)を探しているだけです。

4

5 に答える 5

128

質問がされてからずっと経っているので、あまり評価を得られるとは思っていません。しかし、私自身の非常に基本的な例を (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();
        }

これは最適な実装ではありませんが、追加、削除、および検索には機能します。連鎖と単純なモジュロ アルゴリズムを使用して、適切なバケットを見つけます。

于 2010-01-20T12:34:21.800 に答える
9

C5コレクションを見たことがありますか?ハッシュテーブルを含むソースをダウンロードできます。

于 2009-03-09T12:04:12.157 に答える
8

リフレクターを使用して、.NET Hashtable が (C# などで) どのように実装されているかを確認できます。

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

于 2009-03-09T12:08:44.907 に答える
5

もちろん、クラス ライブラリの Mono バージョンもあります。

于 2009-03-09T12:24:49.360 に答える
2

ここで、 Monoの Hashtable 実装を確認することもできます。

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

于 2009-03-09T12:23:39.143 に答える