Created
June 8, 2017 17:28
-
-
Save maritaria/49cf45ef52a1511c69f3afdc9a517b1a to your computer and use it in GitHub Desktop.
Hash collision test (using blocks to reach larger coordinates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace ConsoleApplication4 | |
{ | |
public class Program | |
{ | |
private struct Vector | |
{ | |
private int x, y, z; | |
public Vector(int x, int y, int z) | |
{ | |
this.x = x; | |
this.y = y; | |
this.z = z; | |
} | |
public override string ToString() | |
{ | |
return string.Format("({0}, {1}, {2})", x, y, z); | |
} | |
} | |
private static Dictionary<int, Vector> calculatedHashes = new Dictionary<int, Vector>(); | |
private static int RotateShift(int value, int shift) | |
{ | |
return (value >> shift) | (value << (32 - shift)); | |
} | |
private static void TestHashCode(int x, int y, int z) | |
{ | |
int hashcode = (x ^ RotateShift(y, 10) ^ RotateShift(z, 20)); | |
Vector coord = new Vector(x, y, z); | |
Vector existing; | |
if (calculatedHashes.TryGetValue(hashcode, out existing)) | |
{ | |
Console.WriteLine("{0} == {1}", existing, coord); | |
} | |
else | |
{ | |
calculatedHashes.Add(hashcode, coord); | |
} | |
} | |
public static void Main(string[] args) | |
{ | |
int blocks = 8; | |
int limit = 128; | |
for (int blockX = 0; blockX < blocks; blockX++) | |
{ | |
for (int blockY = 0; blockY < blocks; blockY++) | |
{ | |
for (int blockZ = 0; blockZ < blocks; blockZ++) | |
{ | |
for (int x = 0; x < limit; x++) | |
{ | |
for (int y = 0; y < limit; y++) | |
{ | |
for (int z = 0; z < limit; z++) | |
{ | |
TestHashCode((blockX * limit) + x, (blockY * limit) + y, (blockZ * limit) + z); | |
} | |
} | |
} | |
} | |
} | |
} | |
Console.WriteLine("End of test"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment