Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / November 2006

Tip: Looking for answers? Try searching our database.

combining hash codes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Zachary  Turner - 08 Nov 2006 02:57 GMT
I want to use a custom object as a key in a Dictionary or SortedList,
and uniqueness is determined by more than just a specific field.  I
know the "suggested" method is to xor all the hash codes together, but
this doesn't seem safe?  For example, say

Key1 = (A, B)
Key2 = (C, D)

Is it possible to have the following situation?
A != C
B != D
A.GetHashCode() ^ B.GetHashCode() == C.GetHashCode() ^ D.GetHashCode()

Actually, I take that back.  I know it's possible.  For example, 1 ^ 2
== 5 ^ 6.  But how serious of a problem is this in reality?  There must
be some mitigating factor here that I'm missing, otherwise why would
examples of using XOR be strewn throughout the MSDN documentation?

Thanks for any insight.
Mattias Sjögren - 08 Nov 2006 05:52 GMT
>Actually, I take that back.  I know it's possible.  For example, 1 ^ 2
>== 5 ^ 6.  But how serious of a problem is this in reality?  There must
>be some mitigating factor here that I'm missing, otherwise why would
>examples of using XOR be strewn throughout the MSDN documentation?

Well hash codes don't have to be unique (they can't be), just have
"good enough" distribution. XORing is easy to understand and
implement, that's probably why it's used in MSDN. There are other
algorithms, see for example

http://groups.google.com/group/comp.lang.java.help/browse_thread/thread/3c34d38e
f488221a/50d54e5e2b16b564#50d54e5e2b16b564


Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Zachary  Turner - 08 Nov 2006 19:39 GMT
> >Actually, I take that back.  I know it's possible.  For example, 1 ^ 2
> >== 5 ^ 6.  But how serious of a problem is this in reality?  There must
[quoted text clipped - 7 lines]
>
> http://groups.google.com/group/comp.lang.java.help/browse_thread/thread/3c34d38e
f488221a/50d54e5e2b16b564#50d54e5e2b16b564

Hmm, I was about to ask another question, but I think I just realized
that for quite a while I've been misunderstanding how the HashCode
based .NET Collections work.  I know how hashtables work, and that in
typical Hashtable implementations you can have multiple entries with
the same hash code.  When I read the .NET Documentation for Hashtables
a long time ago I (somehow) mistakenly interpreted it as saying that
you cannot insert two items with the same hash code.  Instead, it just
says you cannot insert two entries A and B where A.Equals(B) is true,
which is what I expect anyway.  Call me crazy, glad I cleared that up
though, what a major oversight :)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.