I am randomly generating a grid of characters and storing it in a char[,] array …
I need a way to ensure that i haven’t already generated a grid before serializing it to a database in binary format…what is the best way to compare two grids based on bytes? The last thing i want to do is loop through their contents as I am already pulling one of them from the db in byte form.
I was thinking checksum but not so sure if this would work.
char[,] grid = new char[8,8]; char[,] secondgrid = new char[8,8];//gets its data from db
From what I can see, you are going to have to loop over the contents (or at least, a portion of it); there is no other way of talking about an arrays contents.
Well, as a fast ‘definitely not the same’ you could compute a hash over the array – i.e. something like:
This has the risk of some false positives (reporting a dup when it is unique), but is otherwise quite cheap. Any use? You could store the hash alongside the data in the database to allow fast checks – but if you do that you should pick your own hash algorithm for
char(since it isn’t guaranteed to stay the same) – perhaps just convert to an int, for example – or to re-use the existing implementation:As an aside – for 8×8, you could always just think in terms of a 64 character string, and just check
==. This would work equally well at the database and application.