Skip to content

Fix improper hash collision handling in WindingFactory #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions src/main/java/info/ata4/bspsrc/util/WindingFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
*/
package info.ata4.bspsrc.util;

import info.ata4.bsplib.struct.BspData;
import info.ata4.bsplib.struct.DAreaportal;
import info.ata4.bsplib.struct.DBrush;
import info.ata4.bsplib.struct.DBrushSide;
import info.ata4.bsplib.struct.DFace;
import info.ata4.bsplib.struct.DOccluderPolyData;
import info.ata4.bsplib.struct.DPlane;
import info.ata4.bsplib.struct.*;
import info.ata4.bsplib.vector.Vector3f;
import org.apache.commons.lang3.tuple.ImmutablePair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -31,7 +27,7 @@
public class WindingFactory {

private static Map<DFace, Winding> faceCache = new HashMap<>();
private static Map<Integer, Winding> brushSideCache = new HashMap<>();
private static Map<Map.Entry<DBrush, DBrushSide>, Winding> brushSideCache = new HashMap<>();
private static Map<DAreaportal, Winding> areaportalCache = new HashMap<>();
private static Map<DOccluderPolyData, Winding> occluderCache = new HashMap<>();
private static Map<DPlane, Winding> planeCache = new HashMap<>();
Expand Down Expand Up @@ -95,12 +91,9 @@ public static Winding fromFace(BspData bsp, DFace face) {
* @return Winding for the brush side
*/
public static Winding fromSide(BspData bsp, DBrush brush, DBrushSide bside) {
int cacheHash = 5;
cacheHash = cacheHash * 14 + brush.hashCode();
cacheHash = cacheHash * 8 + bside.hashCode();

if (brushSideCache.containsKey(cacheHash)) {
return brushSideCache.get(cacheHash);
ImmutablePair<DBrush, DBrushSide> key = ImmutablePair.of(brush, bside);
if (brushSideCache.containsKey(key)) {
return brushSideCache.get(key);
}

int iplane = bside.pnum;
Expand Down Expand Up @@ -137,7 +130,7 @@ public static Winding fromSide(BspData bsp, DBrush brush, DBrushSide bside) {
throw new IllegalArgumentException("Brush side is not part of brush!");
}

brushSideCache.put(cacheHash, w);
brushSideCache.put(key, w);

// return the clipped winding
return w;
Expand Down