NextPVR Forums
  • ______
  • Home
  • New Posts
  • Wiki
  • Members
  • Help
  • Search
  • Register
  • Login
  • Home
  • Wiki
  • Members
  • Help
  • Search
NextPVR Forums Public Developers v
« Previous 1 … 60 61 62 63 64 … 93 Next »
Understanding Hash codes (C#)

 
  • 0 Vote(s) - 0 Average
Understanding Hash codes (C#)
bgowland
Offline

Posting Freak

West Yorkshire, UK
Posts: 4,595
Threads: 387
Joined: Dec 2004
#1
2006-01-27, 07:31 PM
For my SuDoku plugin I maintain two two-dimensional int arrays, i.e., defined as int[,] not of type Array. One is the answer 'grid' and contains what the player should be trying to reach, the other is the playing 'grid' and contains whatever they have filled in so far along the way plus the numbers which were prefilled for them.

Whenever a number key is pressed, I compare the answer grid to the playing grid, element by element, which seems inefficient to do.

What I've read about object.GetHashCode suggests that if I compare the hash code of each array and they DON'T match, I can be guaranteed that the data in the two arrays don't match. If the hash codes DO match, however, it suggests the data in the arrays do match but I should perform a definitive element-by-element comparison to be sure.

Does that make sense?
Snarky
Offline

Junior Member

Posts: 43
Threads: 7
Joined: Oct 2005
#2
2006-02-01, 12:24 AM (This post was last modified: 2006-02-01, 12:38 AM by Snarky.)
Actually, any square-bracket array in C# (like your int[,]) is implemented as an Array object under the hood. Or, at least, .NET will treat it as an Array object when necessary.

So the question comes down to if and how the Array class overrides GetHashCode. According to the MSDN documentation, it doesn't. That means it cannot actually be looking at the entries in the array to generate the hash code. So no, this method won't work.

Edit: In addition, GetHashCode isn't in general the right method to use to compare whether two objects are equal. You should just use Equals (semantics demand that the two methods should be consistent). However, Array doesn't override Equals, either.
Snarky
Offline

Junior Member

Posts: 43
Threads: 7
Joined: Oct 2005
#3
2006-02-01, 12:32 AM
If I understand what you're trying to do correctly, you want to compare two two-dimensional arrays to see whether every entry is the same. You need to do this every time a user has changed the value of a single entry in one of the arrays.

Actually, as long as the arrays are small (less than say 50x50 in dimensions), checking every element isn't going to be a problem. Yeah, it's inefficient, but it's quick to code, and simple enough that you're unlikely to introduce any bugs.

If you want to be more clever about it, I would recommend you scan the arrays once, keep a count of how many entries differ (Edit: Actually, you don't even have to scan them. You can just take the total size of the array and subtract the number of pre-filled entries), then each time the user changes one of the entries you would check the before and after values to see whether the number of mismatches has been incremented or decremented. When the number of mismatched entries reaches zero, you know the two arrays are identical.
bgowland
Offline

Posting Freak

West Yorkshire, UK
Posts: 4,595
Threads: 387
Joined: Dec 2004
#4
2006-02-01, 04:19 PM
Snarky Wrote:In addition, GetHashCode isn't in general the right method to use to compare whether two objects are equal.
Agreed - that was what I was trying to clarify. C# in a Nutshell suggests that comparing hash codes can be used to tell if two objects are NOT equal and that it could potentially be faster than doing a detailed comparison.

Anyway - no matter. What you said about .NET treating the int[,] array as an Array object when necessary clarifies things and obviously this would apply for the GetHashCode method inherited from Object. Even if I were to override this in my Grid class, I would have to implement the code to generate the hash code myself which would mean having to access each array element anyway. If I'm going to do this then I may as well just carry on with the way I'm doing it.

Cheers,
Brian
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)



Possibly Related Threads…
Thread Author Replies Views Last Post
  Event codes mvallevand 10 3,930 2011-04-06, 04:26 AM
Last Post: mvallevand
  Understanding CompositeImage bgowland 5 2,399 2006-05-19, 07:27 PM
Last Post: bgowland

  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Designed by D&D, modified by NextPVR - Powered by MyBB

Linear Mode
Threaded Mode