c# - Create Hash Value on a List? -


I have a list & lt; MyRichObject & gt; There are 50 instances in each instance, each instance has 1 or 2 unique properties, but in a way they are all unique because there is only one place in the list, etc.

I have a unique way "Hush" in this list, it is unique from all the other lists, what is a great way to do this. NET 4?

The purpose is to make a type of "monikar" for the lists so that they can be thrown into a line and later found on the basis of their unique value.

TL; DR
  public static GetSequenceHashCode & lt; T & gt; (This is the IList & lt; T & gt; Sequence) {const int seeds = 487; Const int modifier = 31; Uncontrolled {Return Sequence. Aggregate (Seed, (Current, Item) => (Current * Modifier) ​​+ Item. GetHashCode ()); }}   

Why bother with another answer?

If you have multiple items in the list with the same hash code, then you can get the wrong results in a dangerous way. For example, consider these inputs:

  var a = new [] {"foo"}; Var b = new [] {"foo", "bar"}; Var c = new [] {"foo", "bar", "spam"}; Var d = new [] {"Schoadevil", "Hernovil", "Boulenville"};   

All these different results show that they are all unique collections. great! Now try duplicate:

  var e = new [] {"foo", "bar", "spam"};   

GetSequenceHashCode should produce the same result for both c and e - and this So far everything is fine. Let's try out the sequence of items:

  var f = new [] {"spam", "bar", "foo"};   

Uh oh ... GetSequenceHashCode indicates that both f can be found in both c and E is the e-code which is not it. Why is this happening? Using the c as an example, first break it into the actual hash code values:

  int hashC = "foo" .GetHashCode () ^ "bar" GetHashCode () ^ "Spam" .GetHashCode ();   

Since the exact numbers are not really important here and for obvious display we pretend the hash code of three strings foo = 8 , bar = 16 and spam = 32 . Therefore:

  is has c = 8 ^ 16 ^ 32;   

or to break it down into binary representation:

  8 ^ 16 ^ 32 == 56; // 8 = 00001000 // ^ // 16 = 00010000 // ^ // 32 = 00100000 // // 56 00111000   

Now you should see that the order in the list of items in this The implementation is ignored, i.e. 8 ^ 16 ^ 32 = 16 ^ 8 ^ 32 = 32 ^ 16 ^ 8 etc.

There is a problem with the second duplicate even if you believe that it is okay to have the same content in a different sequence (which is not an approach that I would encourage), I do not think That anybody will argue downward behavior will be desirable. Let's try to diversify each list with duplicates.

  var a = new [] {"foo", "bar", "spam"}; Var b = new [] {"foo", "bar", "spam", "foo"}; Var c = new [] {"foo", "bar", "spam", "foo", "foo"}; Var d = new [] {"foo", "bar", "spam", "foo", "foo", "spam", "foo", "spam", "foo"};   

While a and b generate different queues for hash, GetSequenceHashCode suggests that A , c and d are all the same. Why?

If you XOR a number with you, you essentially cancel it, eg.

  8 ^ 8 == 0; // 8 = 00001000 // ^ // 8 = 00001000 // = // 0 = 00000000   

XOR from the same number gives you the original result, i.e.

  8 ^ 8 ^ 8 == 8; // 8 = 00001000 // ^ // 8 = 00001000 // ^ // 8 = 00001000 // = // 8 = 00001000   

So if we go to a And c again, simplify hash codes replace:

  var a = new [] {8, 16, 32}; Var c = new [] (8, 16, 32, 8, 8};   

The hash code is caclulated as:

  int hashA = 8 ^ 16 ^ 32; // = 56 into hash C = 8 ^ 16 ^ 32 ^ 8 ^ 8; // = 56 / one ???? A ???? // both of these two out of each other   

and similarly with D where foo and each pair of spam > cancels itself .

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -