1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

replace priority_queue crate with std BinaryHeap

This commit is contained in:
mat 2024-12-26 05:52:46 +00:00
parent e11a902fba
commit 3c83e5b24a
4 changed files with 19 additions and 32 deletions

12
Cargo.lock generated
View file

@ -236,7 +236,6 @@ dependencies = [
"num-format",
"num-traits",
"parking_lot",
"priority-queue",
"rand",
"rustc-hash 2.1.0",
"serde",
@ -2298,17 +2297,6 @@ dependencies = [
"syn",
]
[[package]]
name = "priority-queue"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "714c75db297bc88a63783ffc6ab9f830698a6705aa0201416931759ef4c8183d"
dependencies = [
"autocfg",
"equivalent",
"indexmap",
]
[[package]]
name = "proc-macro2"
version = "1.0.92"

View file

@ -56,7 +56,6 @@ nohash-hasher = "0.2.0"
num-bigint = "0.4.6"
num-traits = "0.2.19"
parking_lot = "0.12.3"
priority-queue = "2.1.1"
proc-macro2 = "1.0.92"
quote = "1.0.37"
rand = "0.8.5"
@ -79,6 +78,7 @@ tracing = "0.1.41"
tracing-subscriber = "0.3.19"
hickory-resolver = { version = "0.24.2", default-features = false }
uuid = "1.11.0"
num-format = "0.4.4"
# --- Profile Settings ---

View file

@ -35,10 +35,9 @@ derive_more = { workspace = true, features = ["deref", "deref_mut"] }
futures = { workspace = true }
futures-lite = { workspace = true }
nohash-hasher = { workspace = true }
num-format = "0.4.4"
num-format = { workspace = true }
num-traits = { workspace = true }
parking_lot = { workspace = true }
priority-queue = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true, optional = true }
thiserror = { workspace = true }

View file

@ -1,12 +1,12 @@
use std::{
cmp::Reverse,
cmp::{self},
collections::BinaryHeap,
fmt::Debug,
hash::Hash,
time::{Duration, Instant},
};
use num_format::ToFormattedString;
use priority_queue::PriorityQueue;
use rustc_hash::FxHashMap;
use tracing::{debug, trace, warn};
@ -52,8 +52,8 @@ where
{
let start_time = Instant::now();
let mut open_set = PriorityQueue::new();
open_set.push(start, Reverse(Weight(0.)));
let mut open_set = BinaryHeap::<WeightedNode<P>>::new();
open_set.push(WeightedNode(start, 0.));
let mut nodes: FxHashMap<P, Node<P, M>> = FxHashMap::default();
nodes.insert(
start,
@ -71,7 +71,7 @@ where
let mut num_nodes = 0;
while let Some((current_node, _)) = open_set.pop() {
while let Some(WeightedNode(current_node, _)) = open_set.pop() {
num_nodes += 1;
if success(current_node) {
debug!("Nodes considered: {num_nodes}");
@ -106,7 +106,7 @@ where
f_score,
},
);
open_set.push(neighbor.movement.target, Reverse(Weight(f_score)));
open_set.push(WeightedNode(neighbor.movement.target, f_score));
for (coefficient_i, &coefficient) in COEFFICIENTS.iter().enumerate() {
let node_score = heuristic + tentative_g_score / coefficient;
@ -118,8 +118,8 @@ where
}
}
// check for timeout every ~1ms
if num_nodes % 1000 == 0 {
// check for timeout every ~20ms
if num_nodes % 10000 == 0 {
let timed_out = match timeout {
PathfinderTimeout::Time(max_duration) => start_time.elapsed() > max_duration,
PathfinderTimeout::Nodes(max_nodes) => num_nodes > max_nodes,
@ -218,17 +218,17 @@ impl<P: Hash + Copy + Clone, M: Clone> Clone for Movement<P, M> {
}
#[derive(PartialEq)]
pub struct Weight(f32);
impl Ord for Weight {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0
.partial_cmp(&other.0)
.unwrap_or(std::cmp::Ordering::Equal)
pub struct WeightedNode<P: PartialEq>(P, f32);
impl<P: PartialEq> Ord for WeightedNode<P> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
// intentionally inverted to make the BinaryHeap a min-heap
other.1.partial_cmp(&self.1).unwrap_or(cmp::Ordering::Equal)
}
}
impl Eq for Weight {}
impl PartialOrd for Weight {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
impl<P: PartialEq> Eq for WeightedNode<P> {}
impl<P: PartialEq> PartialOrd for WeightedNode<P> {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}