The fastest .NET hash generator.

I am implementing a custom GetHashCode for the System.Drawing.Point class in C #. My method currently does not fulfill the following requirement:

var hashA = MyGetHashCode(new Point(1, 0));
var hashB = MyGetHashCode(new Point(0, 1));
var hashC = MyGetHashCode(new Point(0, 0));
var hashD = MyGetHashCode(new Point(1, 1));
Assert.AreNotEqual(hashA ^ hashB, hashC ^ hashD);

To pass this test, I am sure that with the new SHA256Managed (), ComputeHash (currentHash) will work. But is there any other hash algorithm? I know that SHA256 is about security, and I don't need it.

+3
source share
7 answers

Simple hash? how about something like:

 (17 * point.X) + (23 * point.Y);

Or for a more obvious entropy:

int hash = -1047578147;
hash = (hash * -1521134295) + point.X;
hash = (hash * -1521134295) + point.Y;

(digits from C # anonymous type code)

+6
source
  • Why are you doing it? Does it System.Drawing.Pointalready have a thin hash function?

  • , , ? - .

  • , .

+3

, , , . :
http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx

GetHashCode . ,.NET Framework GetHashCode it .NET. . , .

+1

:

+1

( delphi, shoudl )

function ElfHash(id : string; tableSize : integer) : integer;
var
  i : integer;
  h,x : longint;
begin
  h := 0;
  // Obtener el valor numérico
  for i := 1 to Length(id) do
  begin
    h := (h shl 4) + Ord(id[i]);

    x := h and $F0000000;
    if x <;>; 0 then
       h = h xor (x shr 24) xor x;
  end;
  // Ajustar al tamaño de la tabla
  result := h mod tableSize;
end;
+1

, 0 N, hashcode = X+Y*N; . , , . , , N 2. .

I have used this strategy for an excellent effect in the past, but I admit that it has some real (but obvious) limitations. The biggest one is what happens when N is large enough so that N ^ 2 does not fit into your hash value (i.e. painful collisions.

0
source

Source: https://habr.com/ru/post/1709983/


All Articles