From d382de2b52ff79965d36099d7526a91eced39d0a Mon Sep 17 00:00:00 2001 From: Leijurv Date: Fri, 3 Aug 2018 12:50:09 -0400 Subject: [PATCH] moving away from linked list, so remove nextOpen from PathNode --- .../bot/pathing/calc/AStarPathFinder.java | 1 - .../bot/pathing/calc/LinkedListOpenSet.java | 33 ++++++++++++------- .../baritone/bot/pathing/calc/PathNode.java | 6 +--- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java index 2d6b1c1e..638e0bb4 100644 --- a/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java @@ -54,7 +54,6 @@ public class AStarPathFinder extends AbstractNodeCostSearch { PathNode currentNode = openSet.removeLowest(); mostRecentConsidered = currentNode; currentNode.isOpen = false; - currentNode.nextOpen = null; BlockPos currentNodePos = currentNode.pos; numNodes++; if (System.currentTimeMillis() > lastPrintout + 1000) {//print once a second diff --git a/src/main/java/baritone/bot/pathing/calc/LinkedListOpenSet.java b/src/main/java/baritone/bot/pathing/calc/LinkedListOpenSet.java index b4a74e13..006662e6 100644 --- a/src/main/java/baritone/bot/pathing/calc/LinkedListOpenSet.java +++ b/src/main/java/baritone/bot/pathing/calc/LinkedListOpenSet.java @@ -5,13 +5,15 @@ package baritone.bot.pathing.calc; * It has incredbly fast insert performance, at the cost of O(n) removeLowest. */ public class LinkedListOpenSet implements IOpenSet { - private PathNode first = null; + private Node first = null; public boolean isEmpty() { return first == null; } - public void insert(PathNode node) { + public void insert(PathNode pathNode) { + Node node = new Node(); + node.val = pathNode; node.nextOpen = first; first = node; } @@ -20,18 +22,18 @@ public class LinkedListOpenSet implements IOpenSet { if (first == null) { return null; } - PathNode current = first.nextOpen; + Node current = first.nextOpen; if (current == null) { - PathNode n = first; + Node n = first; first = null; - return n; + return n.val; } - PathNode previous = first; - double bestValue = first.combinedCost; - PathNode bestNode = first; - PathNode beforeBest = null; + Node previous = first; + double bestValue = first.val.combinedCost; + Node bestNode = first; + Node beforeBest = null; while (current != null) { - double comp = current.combinedCost; + double comp = current.val.combinedCost; if (comp < bestValue) { bestValue = comp; bestNode = current; @@ -42,9 +44,16 @@ public class LinkedListOpenSet implements IOpenSet { } if (beforeBest == null) { first = first.nextOpen; - return bestNode; + bestNode.nextOpen = null; + return bestNode.val; } beforeBest.nextOpen = bestNode.nextOpen; - return bestNode; + bestNode.nextOpen = null; + return bestNode.val; + } + + public static class Node { //wrapper with next + Node nextOpen; + PathNode val; } } diff --git a/src/main/java/baritone/bot/pathing/calc/PathNode.java b/src/main/java/baritone/bot/pathing/calc/PathNode.java index 8e27c1c8..9ee0dcb7 100644 --- a/src/main/java/baritone/bot/pathing/calc/PathNode.java +++ b/src/main/java/baritone/bot/pathing/calc/PathNode.java @@ -30,14 +30,10 @@ class PathNode { /** * Is this a member of the open set in A*? (only used during pathfinding) + * Instead of doing a costly member check in the open set, cache membership in each node individually too. */ boolean isOpen; - /** - * In the linked list of open nodes, which one is next? (only used during pathfinding) - */ - PathNode nextOpen; - public PathNode(BlockPos pos, Goal goal) { this.pos = pos; this.previous = null;