How to hash generic key in generic hash table impl

2019-08-01 20:19发布

I'm implementing hash table using generics, like Dictionary implemented in c#, but I'm stuck in Hash function part. I successufuly rewritten non-generic implementation of Hash table in generic one but you will notice in code that I have no idea how to hash Tkey.

class Node<T, U>
{
    public T key;
    public U value;
    public Node<T, U> next;
    public Node(T key, U value, Node<T, U> next)
    {
        this.key = key;
        this.value = value;
        this.next = next;
    }
 }

public  class HashTable<T,U>
{
    int length;
    Node<T,U>[] buckets;
    public HashTable(int length)
    {
        this.length = length;
        buckets = new Node<T,U>[length];
    }

    public void Display()
    {
        for (int bucket = 0; bucket<buckets.Length; bucket++)
        {
            Node<T,U> current = buckets[bucket];
            Console.Write(bucket + ":");
            while (current != null)
            {
                Console.Write("["+current.key+","+current.value+"]");
                current=current.next;
            }
            Console.WriteLine();
        }
    }
     //  private int Hash(T Tkey) ...
    //// non-generic version of hash function

    private int Hash(string str)
    {
        int h=0;
        foreach(var s in str)
            h=127*h+s;
        return h%length;

    }
     ////
    public void Insert(T key, U value)
    {
        int bucket = Hash(key);
        buckets[bucket] = new Node<T,U>(key, value, buckets[bucket]);
    }
    public U Search(T key)
    {
        Node<T,U> current = buckets[Hash(key)];
        while (current != null)
        {
            if (current.key.Equals(key))
                return current.value;
            current= current.next;
        }
        throw new Exception(key+ "Not found");
    }

1条回答
forever°为你锁心
2楼-- · 2019-08-01 20:57

The base object class in C# comes with the GetHashCode() method which should be used for these situations.

Some classes come with a good implementation, others just inherit the method from System.Object and do not override it.

From the MSDN documentation:

The default implementation of the GetHashCode method does not guarantee unique return values for different objects. Furthermore, the .NET Framework does not guarantee the default implementation of the GetHashCode method, and the value it returns will be the same between different versions of the .NET Framework. Consequently, the default implementation of this method must not be used as a unique object identifier for hashing purposes.

The GetHashCode method can be overridden by a derived type. Value types must override this method to provide a hash function that is appropriate for that type and to provide a useful distribution in a hash table. For uniqueness, the hash code must be based on the value of an instance field or property instead of a static field or property.

查看更多
登录 后发表回答