Skip to content

Instantly share code, notes, and snippets.

@Ultrafeel
Last active October 8, 2021 21:15
Show Gist options
  • Save Ultrafeel/6584d9d380cba56b109408ee18437a63 to your computer and use it in GitHub Desktop.
Save Ultrafeel/6584d9d380cba56b109408ee18437a63 to your computer and use it in GitHub Desktop.
Parallel.For
using System;
using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
Console.WriteLine($"***** GetHashCode not Unique with Anonymous Types *****\n");
var etalon = new { CurrentSpeed = 55, SomeBool = true };
Console.WriteLine($"Etalon properties: CurrentSpeed: {etalon.CurrentSpeed}, SomeBool:{etalon.SomeBool}");
var etalonHash = etalon.GetHashCode();
///Here values calculated on my machine.
var calculated = new { CurrentSpeed = 207886160, SomeBool = false };
if (calculated.GetType() != etalon.GetType())
throw new InvalidOperationException("not same anonymous types");
if (calculated.GetHashCode() == etalonHash && !calculated.Equals(etalon))
{
Console.WriteLine($"Same type, different Values, but equal hashes!!!:");
Console.WriteLine($" CurrentSpeed: {calculated.CurrentSpeed}, SomeBool:{calculated.SomeBool}");
return;
}
var sw = new Stopwatch();
sw.Start();
FindCollision();
Console.WriteLine($" elapsed : {sw.Elapsed}");
void FindCollision()
{
var getAndCompareHashes = InferFunc(etalon,
(speed, someBool) =>
{
var candidate = new { CurrentSpeed = speed, SomeBool = someBool };
if (candidate.GetType() != etalon.GetType())
throw new InvalidOperationException("not same anonymous types");
if (candidate.Equals(etalon))
{
return null;
}
var h2 = candidate.GetHashCode();
if (etalonHash == h2)
{
Console.WriteLine($"** {speed}!={etalon.CurrentSpeed}. Equals: {candidate.Equals(etalon)}");
Console.WriteLine($" elapsed : {sw.Elapsed}");
return candidate;
}
return null;
});
Console.WriteLine($"Collision finding. Please wait ~30 sec. depending on your hardware....");
var found = etalon;
found = null;
var mode = 1;
switch (mode)
{
case 0:
{
var query = ParallelEnumerable.Range(0, int.MaxValue).AsUnordered()
.Select(
(speedi) =>
getAndCompareHashes(speedi, true) ??
getAndCompareHashes(speedi, false) ??
getAndCompareHashes(-speedi, true) ??
getAndCompareHashes(-speedi, false)
);
found = query.Where(v => v != null).Take(1).First();
}
break;
case 1:
Parallel.For(0, int.MaxValue,
(speedi, pls) =>
{
var found1 =
getAndCompareHashes(speedi, true) ??
getAndCompareHashes(speedi, false) ??
getAndCompareHashes(-speedi, true) ??
getAndCompareHashes(-speedi, false);
if (found1 != null)
{
pls.Stop();
found = found1;
}
});
break;
}
Console.WriteLine($"Same type, different Values, but equal hashes!!:");
Console.WriteLine($" ------ equal?: {found.Equals(etalon)}. equal hashes?:{found.GetHashCode() == (etalon.GetHashCode())}");
Console.WriteLine($"found: CurrentSpeed: {found.CurrentSpeed}, SomeBool:{found.SomeBool}\n");
}
//workaround anonymous types.
Func<int, bool, T> InferFunc<T>(T _, Func<int, bool, T> f)
=> f;
@Ultrafeel
Copy link
Author

Ultrafeel commented Sep 7, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment