Skip to content

Instantly share code, notes, and snippets.

@maritaria
Created June 8, 2017 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maritaria/49cf45ef52a1511c69f3afdc9a517b1a to your computer and use it in GitHub Desktop.
Save maritaria/49cf45ef52a1511c69f3afdc9a517b1a to your computer and use it in GitHub Desktop.
Hash collision test (using blocks to reach larger coordinates
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