pub struct PriorityQueue { /* private fields */ }
Expand description

Priority queue used in the A* algorithm.

Implementations§

source§

impl PriorityQueue

source

pub fn new() -> Self

Creates a new, empty PriorityQueue.

§Returns

A new PriorityQueue instance.

§Examples
use crate::PriorityQueue;

// Create a new empty priority queue
let mut open_set = PriorityQueue::new();

// Check if the queue is empty
assert!(open_set.is_empty());
source

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());
source

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");
}
source

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§

source§

impl Debug for PriorityQueue

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.