Struct controlled_astar::priority_queue::PriorityQueue
source · pub struct PriorityQueue { /* private fields */ }
Expand description
Priority queue used in the A* algorithm.
Implementations§
source§impl PriorityQueue
impl PriorityQueue
sourcepub fn push(&mut self, state: State)
pub fn push(&mut self, state: State)
Adds a new State
to the queue.
§Parameters
state
: The state to be added to the queue.
§Examples
use crate::{PriorityQueue, State};
use std::collections::HashMap;
// Create a new priority queue
let mut open_set = PriorityQueue::new();
// Create a state representing a node in the A* algorithm
let state = State {
cost: 10, // f-score value calculated in AStar
position: (1, 2), // position of the node in the grid
};
// Add the state to the priority queue
open_set.push(state);
// The queue should no longer be empty
assert!(!open_set.is_empty());
sourcepub fn pop(&mut self) -> Option<State>
pub fn pop(&mut self) -> Option<State>
Removes and returns the State
with the highest priority from the queue.
§Returns
The State
with the highest priority (if available) or None
.
§Examples
use crate::{PriorityQueue, State};
use std::collections::HashMap;
// Create a new priority queue and add some states
let mut open_set = PriorityQueue::new();
open_set.push(State { cost: 10, position: (1, 2) });
open_set.push(State { cost: 5, position: (2, 3) }); // Lower cost, higher priority
// Remove and get the state with the highest priority
if let Some(state) = open_set.pop() {
assert_eq!(state.cost, 5); // Lower cost should be popped first
assert_eq!(state.position, (2, 3));
} else {
panic!("Expected a state, but got None");
}
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks if the queue is empty.
§Returns
A boolean indicating whether the queue is empty.
§Examples
use crate::{PriorityQueue, State};
// Create a new priority queue
let mut open_set = PriorityQueue::new();
// Initially, the queue should be empty
assert!(open_set.is_empty());
// Add a state to the queue
open_set.push(State { cost: 10, position: (1, 2) });
// Now the queue should not be empty
assert!(!open_set.is_empty());
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PriorityQueue
impl RefUnwindSafe for PriorityQueue
impl Send for PriorityQueue
impl Sync for PriorityQueue
impl Unpin for PriorityQueue
impl UnwindSafe for PriorityQueue
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more