Created
April 13, 2018 07:18
-
-
Save DarkGuardsman/36b277d2eba3bc17ccb4cb541cccdff3 to your computer and use it in GitHub Desktop.
Hash Collision check for BlockPos MC1.12 vs Pos CodingLib
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
package com.builtbroken.jlib; | |
import com.google.common.collect.HashMultimap; | |
import com.google.common.collect.Multimap; | |
import icbm.classic.lib.transform.vector.Pos; | |
import net.minecraft.util.math.BlockPos; | |
import net.minecraft.util.math.ChunkPos; | |
import java.util.Collection; | |
import java.util.HashSet; | |
import java.util.Map; | |
/** | |
* Test case created to check for collisions inside a chunk when using the BlockPos hash Function | |
* <p> | |
* Original code provided by Paul Fulham on MMD discord. Modified to compare block pos with pos | |
*/ | |
public class Main | |
{ | |
public static void main(String[] args) | |
{ | |
for (int i = 0; i < 100; i++) | |
{ | |
int x = (int) (Math.random() * Short.MAX_VALUE); | |
int z = (int) (Math.random() * Short.MAX_VALUE); | |
int c = checkCollisions(x, z); | |
int c2 = checkCollisionsPos(x, z); | |
} | |
for (int i = 0; i < 100; i++) | |
{ | |
int x = -(int) (Math.random() * Short.MAX_VALUE); | |
int z = -(int) (Math.random() * Short.MAX_VALUE); | |
int c = checkCollisions(x, z); | |
int c2 = checkCollisionsPos(x, z); | |
} | |
} | |
public static int checkCollisions(int chunk_x, int chunk_z) | |
{ | |
System.out.println("Checking: " + chunk_x + ", " + chunk_z); | |
ChunkPos chunk = new ChunkPos(chunk_x, chunk_z); | |
Multimap<Integer, BlockPos> map = HashMultimap.create(); | |
HashSet<Integer> hashSet = new HashSet(); | |
//Generate positions | |
for (BlockPos p : BlockPos.getAllInBox(chunk.getBlock(0, 0, 0), chunk.getBlock(15, 255, 15))) | |
{ | |
hashSet.add(p.hashCode()); | |
map.put(p.hashCode(), p); | |
} | |
//Find collisions | |
int groups = 0, total = 0; | |
for (Map.Entry<Integer, Collection<BlockPos>> entry : map.asMap().entrySet()) | |
{ | |
if (entry.getValue().size() > 1) | |
{ | |
groups++; | |
total += entry.getValue().size() - 1; //-1 as a single value is not a collision until more values are added | |
} | |
} | |
//Output data | |
System.out.println("\tGroups: " + groups); | |
System.out.println("\tCollisions: " + total); | |
System.out.println("\tHashSet: " + hashSet.size()); | |
return total; | |
} | |
public static int checkCollisionsPos(int chunk_x, int chunk_z) | |
{ | |
System.out.println("Checking: " + chunk_x + ", " + chunk_z); | |
ChunkPos chunk = new ChunkPos(chunk_x, chunk_z); | |
Multimap<Integer, Pos> map = HashMultimap.create(); | |
HashSet<Integer> hashSet = new HashSet(); | |
//Generate positions | |
for (BlockPos blockPos : BlockPos.getAllInBox(chunk.getBlock(0, 0, 0), chunk.getBlock(15, 255, 15))) | |
{ | |
Pos p = new Pos(blockPos); | |
hashSet.add(p.hashCode()); | |
map.put(p.hashCode(), p); | |
} | |
//Find collisions | |
int groups = 0, total = 0; | |
for (Map.Entry<Integer, Collection<Pos>> entry : map.asMap().entrySet()) | |
{ | |
if (entry.getValue().size() > 1) | |
{ | |
groups++; | |
total += entry.getValue().size() - 1; //-1 as a single value is not a collision until more values are added | |
} | |
} | |
//Output data | |
System.out.println("\tGroups: " + groups); | |
System.out.println("\tCollisions: " + total); | |
System.out.println("\tHashSet: " + hashSet.size()); | |
return total; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment