From 483c27d9190569dd3933b38209a2dc058bff7e3c Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Tue, 11 Feb 2025 12:58:12 +0700 Subject: [PATCH] Further refine cable path search performance --- .../block/entity/cable/EnergyCableGraph.kt | 55 ++++++++++++++++++- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableGraph.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableGraph.kt index 6ab9b9646..2dacd80b6 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableGraph.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableGraph.kt @@ -21,6 +21,56 @@ import kotlin.collections.HashSet import kotlin.collections.LinkedHashSet import kotlin.math.ln +private class LinkedPriorityQueue> { + private class Entry(val value: T) { + var next: Entry? = null + } + + private var head: Entry? = null + + fun isNotEmpty(): Boolean { + return head != null + } + + fun add(value: T) { + if (head == null) { + head = Entry(value) + } else if (head!!.value >= value) { + val entry = Entry(value) + entry.next = head + head = entry + } else { + var current = head + var previous = current + + while (current != null) { + if (current.value >= value) { + val entry = Entry(value) + entry.next = current + previous!!.next = entry + return + } + + previous = current + current = current.next + } + + previous!!.next = Entry(value) + } + } + + fun remove(): T? { + val head = head + + if (head != null) { + this.head = head.next + return head.value + } + + return null + } +} + class EnergyCableGraph : GraphNodeList() { private val livelyNodes = HashSet() private val livelyNodesList = ArrayList() @@ -421,7 +471,7 @@ class EnergyCableGraph : GraphNodeList() + val openNodes = LinkedPriorityQueue() val seenNodes = HashSet() openNodes.add(SearchNode(a, b, null, seenTop)) @@ -430,8 +480,7 @@ class EnergyCableGraph : GraphNodeList