Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hash an int[] in c#

Tags:

c#

.net

int

hash

I'm trying to think of a way to override GetHashCode() when called from a Vector2[]. This code produces non unique hashes for objects that I know to be equal: I pass the following class the same rectangle, and different hash codes are generated.

public Shape(Rectangle r)
        {
            edges = new Vector2[4];
            edges[0] = new Vector2(0, 0);
            edges[1] = new Vector2(r.Width, 0);
            edges[2] = new Vector2(r.Width, r.Height);
            edges[3] = new Vector2(0, r.Height);
            Console.Write(edges.GetHashCode() + "\n");
            Position = new Vector2(r.X, r.Y);                
        }

A Vector2 array is just bunch of ints. How can I create a unique hash for a list of ints?

like image 778
Max Kessler Avatar asked Dec 11 '12 01:12

Max Kessler


People also ask

Can you hash an integer?

The most commonly used method for hashing integers is called modular hashing: we choose the array size M to be prime, and, for any positive integer key k, compute the remainder when dividing k by M. This function is very easy to compute (k % M, in Java), and is effective in dispersing the keys evenly between 0 and M-1.

Can I hash an array?

While an array can be used to construct hash tables, array indexes its elements using integers. However, if we want to store data and use keys other than integer, such as 'string', we may want to use dictionary. Dictionaries in Python are implemented using hash tables.

Is there a hash function in C?

A Hash Table in C/C++ (Associative array) is a data structure that maps keys to values. This uses a hash function to compute indexes for a key. Based on the Hash Table index, we can store the value at the appropriate location.

How do you hash a number in C++?

std::hash class in C++ STL It is used to get the hash value of the argument that is being passed to it. If the argument doesn't change, the value doesn't change either. Member functions: This Hash class only has one member function: operator(): It returns hashed value for given argument.


1 Answers

You can use something like this:

public static int CombineHashCodes(params int[] hashCodes)
{
    if (hashCodes == null)
    {
        throw new ArgumentNullException("hashCodes");
    }

    if (hashCodes.Length == 0)
    {
        throw new IndexOutOfRangeException();
    }

    if (hashCodes.Length == 1)
    {
        return hashCodes[0];
    }

    var result = hashCodes[0];

    for (var i = 1; i < hashCodes.Length; i++)
    {
        result = CombineHashCodes(result, hashCodes[i]);
    }

    return result;
}

private static int CombineHashCodes(int h1, int h2)
{
    return (h1 << 5) + h1 ^ h2;

    // another implementation
    //unchecked
    //{
    //    var hash = 17;

    //    hash = hash * 23 + h1;
    //    hash = hash * 23 + h2;

    //    return hash;
    //}
}
like image 62
Kirill Polishchuk Avatar answered Sep 28 '22 14:09

Kirill Polishchuk