473,327 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Own implementation of GetHashCode()

I have a class which should override the GetHashcode() function, which
look like this:

public class A
{
public string str1;
public string str2;

//other methods...

public override int GetHashCode()
{
return ???;
}
}

The instance members str1 and str2 are the "primary keys" of my class.
All instances of A with the same values for str1 and str2 should return
the same hash code. The implementation should be also as fast as possible.

An instance with A.str1 = "x" and A.str2 = "y" should return a different
hashcode as an instance with A.str1 = "y" and A.str2 = "x".

How to implement such a behavior and ensure that my hash code is
consistent? How does the implementation of System.String do this?

Thanks for any help and suggestions.
Matthias
Nov 17 '05 #1
8 9868
There are probably many ways, but this is one of them.

You could concatenate str1 and str2 and then use that hash code, as of the
System.String.GetHashCode implementation.
public override int GetHashCode()
{
return string.Concat(str1, str2).GetHashCode();
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Matthias Kientz" <no***********@funkinform.de> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a class which should override the GetHashcode() function, which look
like this:

public class A
{
public string str1;
public string str2;

//other methods...

public override int GetHashCode()
{
return ???;
}
}

The instance members str1 and str2 are the "primary keys" of my class.
All instances of A with the same values for str1 and str2 should return
the same hash code. The implementation should be also as fast as possible.

An instance with A.str1 = "x" and A.str2 = "y" should return a different
hashcode as an instance with A.str1 = "y" and A.str2 = "x".

How to implement such a behavior and ensure that my hash code is
consistent? How does the implementation of System.String do this?

Thanks for any help and suggestions.
Matthias

Nov 17 '05 #2
There are probably many ways, but this is one of them.

You could concatenate str1 and str2 and then use that hash code, as of the
System.String.GetHashCode implementation.
public override int GetHashCode()
{
return string.Concat(str1, str2).GetHashCode();
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Matthias Kientz" <no***********@funkinform.de> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a class which should override the GetHashcode() function, which look
like this:

public class A
{
public string str1;
public string str2;

//other methods...

public override int GetHashCode()
{
return ???;
}
}

The instance members str1 and str2 are the "primary keys" of my class.
All instances of A with the same values for str1 and str2 should return
the same hash code. The implementation should be also as fast as possible.

An instance with A.str1 = "x" and A.str2 = "y" should return a different
hashcode as an instance with A.str1 = "y" and A.str2 = "x".

How to implement such a behavior and ensure that my hash code is
consistent? How does the implementation of System.String do this?

Thanks for any help and suggestions.
Matthias

Nov 17 '05 #3
Dennis Myrén wrote:
There are probably many ways, but this is one of them.

You could concatenate str1 and str2 and then use that hash code, as of the
System.String.GetHashCode implementation.
public override int GetHashCode()
{
return string.Concat(str1, str2).GetHashCode();
}


This was my first idea, too.

But this will return the same hash code for the following instances:
InstanceA.str1 = "x"; InstanceA.str2 = "yz";
InstanceB.str1 = "xy"; InstanceB.str2 = "z";

The goal is, to find a way to ensure that different values of str1 and
str2 result in a differnt hashcode.
Nov 17 '05 #4
Dennis Myrén wrote:
There are probably many ways, but this is one of them.

You could concatenate str1 and str2 and then use that hash code, as of the
System.String.GetHashCode implementation.
public override int GetHashCode()
{
return string.Concat(str1, str2).GetHashCode();
}


This was my first idea, too.

But this will return the same hash code for the following instances:
InstanceA.str1 = "x"; InstanceA.str2 = "yz";
InstanceB.str1 = "xy"; InstanceB.str2 = "z";

The goal is, to find a way to ensure that different values of str1 and
str2 result in a differnt hashcode.
Nov 17 '05 #5

You can never guarantee that has codes will be unique, but you can use
additional calculations to reduce the chance of duplication. The
following calculates the hash codes individually, performs a bit shift
on one, and then xors them.

Note that GetHashCode should be a fast operation and if the calcuation
is too long, you can reduce the benefit of addtional uniqueness (i.e.,
time it takes to calculate for all values vs. the time it takes to
loop through the buckets for collided values).

HTH,

Sam
using System;

namespace __WinTesterCS
{
public class GetHashCodeTest
{
public static void Test()
{
Strings[] a = new Strings[5];
a[0] = new Strings("a","bc");
a[1] = new Strings("ab", "c");
a[2] = new Strings("abc","");
a[3] = new Strings("","abc");
a[4] = new Strings(null,"abc");

for(int i=0; i<a.Length; i++)
{
Console.WriteLine(a[i].String1 + " / " + a[i].String2 + " -> "
+ a[i].GetHashCode().ToString("X"));
}
}
}

public class Strings
{
public string String1;
public string String2;

public Strings(string string1, string string2)
{
String1 = string1;
String2 = string2;
}

public override int GetHashCode()
{
int i1 = String1 != null ? String1.GetHashCode() : 0;
int i2 = String2 != null ? String2.GetHashCode() : 0;
return (i1 >> 1) ^ i2;
}

}

}

B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
Nov 17 '05 #6

You can never guarantee that has codes will be unique, but you can use
additional calculations to reduce the chance of duplication. The
following calculates the hash codes individually, performs a bit shift
on one, and then xors them.

Note that GetHashCode should be a fast operation and if the calcuation
is too long, you can reduce the benefit of addtional uniqueness (i.e.,
time it takes to calculate for all values vs. the time it takes to
loop through the buckets for collided values).

HTH,

Sam
using System;

namespace __WinTesterCS
{
public class GetHashCodeTest
{
public static void Test()
{
Strings[] a = new Strings[5];
a[0] = new Strings("a","bc");
a[1] = new Strings("ab", "c");
a[2] = new Strings("abc","");
a[3] = new Strings("","abc");
a[4] = new Strings(null,"abc");

for(int i=0; i<a.Length; i++)
{
Console.WriteLine(a[i].String1 + " / " + a[i].String2 + " -> "
+ a[i].GetHashCode().ToString("X"));
}
}
}

public class Strings
{
public string String1;
public string String2;

public Strings(string string1, string string2)
{
String1 = string1;
String2 = string2;
}

public override int GetHashCode()
{
int i1 = String1 != null ? String1.GetHashCode() : 0;
int i2 = String2 != null ? String2.GetHashCode() : 0;
return (i1 >> 1) ^ i2;
}

}

}

B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
Nov 17 '05 #7
Samuel R. Neff wrote:
You can never guarantee that has codes will be unique, but you can use
additional calculations to reduce the chance of duplication. The
following calculates the hash codes individually, performs a bit shift
on one, and then xors them.

Note that GetHashCode should be a fast operation and if the calcuation
is too long, you can reduce the benefit of addtional uniqueness (i.e.,
time it takes to calculate for all values vs. the time it takes to
loop through the buckets for collided values).

HTH,

Sam
using System;

namespace __WinTesterCS
{
public class GetHashCodeTest
{
public static void Test()
{
Strings[] a = new Strings[5];
a[0] = new Strings("a","bc");
a[1] = new Strings("ab", "c");
a[2] = new Strings("abc","");
a[3] = new Strings("","abc");
a[4] = new Strings(null,"abc");

for(int i=0; i<a.Length; i++)
{
Console.WriteLine(a[i].String1 + " / " + a[i].String2 + " -> "
+ a[i].GetHashCode().ToString("X"));
}
}
}

public class Strings
{
public string String1;
public string String2;

public Strings(string string1, string string2)
{
String1 = string1;
String2 = string2;
}

public override int GetHashCode()
{
int i1 = String1 != null ? String1.GetHashCode() : 0;
int i2 = String2 != null ? String2.GetHashCode() : 0;
return (i1 >> 1) ^ i2;
}

}

}


Hi Samuel,

I've tested your suggestion, but it produces to much collisions for my
purposes (I have many short strings as keys).

My solution is to copy each character of the strings (alternating) to a
buffer, convert this to a string and get the hash code. Because this is
not very fast, I do this in the constructor and save the hash code in
the instance.

public class A
{
protected string str1;
protected string str2;
protected hash;

public A(string s1, string s2)
{
int n1 = s1.Length;
int n2 = s2.Length;
int j = 0;
str1 = s1;
str2 = s2;

// the buffer gets the double length of the largest string
int nLength = n1 > n2 ?
n1 * 2 : n2 * 2;

char[] buffer = new char[nLength];

// copy each char alternating, use spaces to fill the smaller string
for(int i = 0; i < nLength; i++)
{
buffer[i] = j < n1 ? s1[j] : ' ';
i++;
buffer[i] = j < n2 ? s2[j] : ' ';
j++;
}

// convert to string and get the hash code
hash = new String(buffer).GetHashCode();
}

public override int GetHashCode()
{
// use our saved hash code, this is very, very fast now
return hash;
}
}
Nov 17 '05 #8
You could concatenate str1 and str2 and then use that hash code, as of
the
System.String.GetHashCode implementation.
public override int GetHashCode()
{
return string.Concat(str1, str2).GetHashCode();
}


This was my first idea, too.

But this will return the same hash code for the following instances:
InstanceA.str1 = "x"; InstanceA.str2 = "yz";
InstanceB.str1 = "xy"; InstanceB.str2 = "z";

The goal is, to find a way to ensure that different values of str1 and
str2 result in a differnt hashcode.


If you have some knowledge about your strings maybe you can just add an
"unused" divider between
the strings to make them more likely to be unique for any two string pairs

public override int GetHashCode()
{
string DIV = "@"; //or even "@$¤%&098ggj494"
return string.Concat(str1+DIV, str2).GetHashCode();
}
you can move the calculation to the constructor and return a cached value of
the hashcode as you've
already shown

Martin
Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Avin Patel | last post by:
Hi I have question for GetHashCode() function, Is it correct in following code or there is more efficient way to implement GetHashCode() function class IntArray public int data public...
3
by: cmrchs | last post by:
Hi, why is it requested that when Equals() is implemented in a class that GethashCode() must be implemented as well ? thnx Chris ...
1
by: Anders Borum | last post by:
Hello! I have a framework where all objects are uniquely identified by a GUID (Global Unique Identifier). The objects are used in conjunction with lots of hashtables and I was thinking, that...
5
by: Stoyan | last post by:
Hi All, I don't understand very well this part of MSDN: "Derived classes that override GetHashCode must also override Equals to guarantee that two objects considered equal have the same hash code;...
0
by: Matthias Kientz | last post by:
I have a class which should override the GetHashcode() function, which look like this: public class A { public string str1; public string str2; //other methods...
8
by: Dan | last post by:
Hi, I did a test in C# double x1 = 1.0; double x2 = 5.29980882362664E-315; int h1 = x1.GetHashCode(); int h2 = x2.GetHashCode(); It turned out that both h1 and h2 = 1072693248
5
by: Metaman | last post by:
I'm trying to write a generic method to generate Hashcodes but am having some problems with (generic) collections. Here is the code of my method: public static int GetHashCode(object input) {...
8
by: Ashish Khandelwal | last post by:
-----See below code, string str = "blair"; string strValue = "ABC"; string str1 = "brainlessness"; string strValue1 = "XYZ"; int hash = str.GetHashCode() ; // Returns 175803953 int hash1 =...
28
by: Tony Johansson | last post by:
Hello! I can't figure out what point it is to use GetHashCode. I know that this GetHashCode is used for obtaining a unique integer value. Can somebody give me an example that prove the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.