質問

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