BPQueue

Struct BPQueue 

Source
pub struct BPQueue<T> {
    pub bucket: Vec<Dllist<(usize, T)>>,
    /* private fields */
}
Expand description

The BPQueue struct is a bounded priority queue implemented using an array of doubly-linked lists, with integer keys in a specified range.

Bounded Priority Queue with integer keys in [a..b]. Implemented by an array (bucket) of doubly-linked lists. Efficient if the keys are bounded by a small integer value.

Note that this class does not own PQ nodes. This feature allows these nodes sharable in both doubly linked list class and this class. In the FM algorithm, nodes are either attached to the gain buckets (PQ) or to the waitinglist (doubly-linked list), but cannot be in both at the same time.

Another improvement is to increase the size of the array by one element, i.e. (b - a + 2). The extra dummy array element (called sentinel) is used to reduce the boundary checking during updates.

All the member functions assume that the keys are inside the bounds.

Properties:

  • max: The maximum number of elements that can be stored in the bounded priority queue.
  • offset: The offset property represents the lower bound of the integer keys in the bounded priority queue. It is of type i32, which means it can hold both positive and negative values. The offset is used to calculate the index of the bucket in the bucket array for a given key.
  • high: The high property represents the highest priority level in the bounded priority queue. It indicates the index of the last bucket in the bucket array.
  • sentinel: A doubly linked list node that serves as a sentinel or dummy node. It is used to reduce boundary checking during updates.
  • bucket: The bucket property is a vector of doubly-linked lists. Each doubly-linked list represents a priority level, with the index of the vector representing the priority value. The elements in the doubly-linked lists are tuples containing a priority value and a value of type T.

Fields§

§bucket: Vec<Dllist<(usize, T)>>

Implementations§

Source§

impl<T: Default + Clone> BPQueue<T>

Source

pub fn new(a: i32, b: i32) -> Self

Construct a new BPQueue object

§Examples
use mywheel_rs::bpqueue::BPQueue;
let bpq = BPQueue::<i32>::new(-3, 3);

assert!(bpq.is_empty());
Source

pub fn is_empty(&self) -> bool

Whether the %BPQueue is empty.

§Examples
use mywheel_rs::bpqueue::BPQueue;
let bpq = BPQueue::<i32>::new(-3, 3);

assert!(bpq.is_empty());
Source

pub fn get_max(&self) -> i32

Get the max value

§Examples
use mywheel_rs::bpqueue::BPQueue;
let bpq = BPQueue::<i32>::new(-3, 3);

assert_eq!(bpq.get_max(), -4);
Source

pub fn clear(&mut self)

Clear reset the PQ

§Examples
use mywheel_rs::bpqueue::BPQueue;
let mut bpq = BPQueue::<i32>::new(-3, 3);
bpq.clear();

assert!(bpq.is_empty());
Source

pub fn set_key(&mut self, it: &mut Dllink<(usize, T)>, gain: i32)

Set the key object

§Examples
use mywheel_rs::bpqueue::BPQueue;
let mut bpq = BPQueue::<i32>::new(-3, 3);

assert!(bpq.is_empty());
Source

pub fn append(&mut self, it: &mut Dllink<(usize, T)>, k: i32)

Append item with external key

§Examples
use mywheel_rs::bpqueue::BPQueue;
use mywheel_rs::dllist::Dllink;

let mut bpq = BPQueue::<i32>::new(-3, 3);
let mut a = Dllink::<(usize, i32)>::new((0, 3));
bpq.append(&mut a, 0);

assert!(!bpq.is_empty());
Source

pub fn appendleft(&mut self, it: &mut Dllink<(usize, T)>, k: i32)

Append item with external key

§Examples
use mywheel_rs::bpqueue::BPQueue;
use mywheel_rs::dllist::Dllink;

let mut bpq = BPQueue::<i32>::new(-3, 3);
let mut a = Dllink::<(usize, i32)>::new((0, 3));
bpq.appendleft(&mut a, 0);

assert!(!bpq.is_empty());
Source

pub fn appendleft_direct(&mut self, it: &mut Dllink<(usize, T)>)

Append item with internal key

§Examples
use mywheel_rs::bpqueue::BPQueue;
use mywheel_rs::dllist::Dllink;

let mut bpq = BPQueue::<i32>::new(-3, 3);
let mut a = Dllink::<(usize, i32)>::new((0, 3));
bpq.appendleft_direct(&mut a);

assert!(!bpq.is_empty());
Source

pub fn popleft(&mut self) -> *mut Dllink<(usize, T)>

Pop node with the highest key

§Examples
use mywheel_rs::bpqueue::BPQueue;
use mywheel_rs::dllist::Dllink;

let mut bpq = BPQueue::<i32>::new(-3, 3);
let mut a = Dllink::<(usize, i32)>::new((0, 3));
bpq.append(&mut a, 0);
let d = bpq.popleft();
let (key, v) = unsafe { (*d).data.clone() };

assert_eq!(key, 4);
assert_eq!(v, 3);
Source

pub fn detach(&mut self, it: &mut Dllink<(usize, T)>)

Detach the item from BPQueue

§Examples
use mywheel_rs::bpqueue::BPQueue;
use mywheel_rs::dllist::Dllink;

let mut bpq = BPQueue::<i32>::new(-3, 3);
let mut a = Dllink::<(usize, i32)>::new((0, 3));
bpq.append(&mut a, 0);
bpq.detach(&mut a);

assert!(bpq.is_empty());
Source

pub fn decrease_key(&mut self, it: &mut Dllink<(usize, T)>, delta: usize)

Decrease key by delta

Note that the order of items with same key will not be preserved. For the FM algorithm, this is a desired behavior.

§Examples
use mywheel_rs::bpqueue::BPQueue;
use mywheel_rs::dllist::Dllink;

let mut bpq = BPQueue::<i32>::new(-3, 3);
let mut a = Dllink::<(usize, i32)>::new((0, 3));
bpq.append(&mut a, 0);
bpq.decrease_key(&mut a, 1);

assert_eq!(bpq.get_max(), -1);
Source

pub fn increase_key(&mut self, it: &mut Dllink<(usize, T)>, delta: usize)

Increase key by delta

Note that the order of items with same key will not be preserved. For the FM algorithm, this is a desired behavior.

§Examples
use mywheel_rs::bpqueue::BPQueue;
use mywheel_rs::dllist::Dllink;

let mut bpq = BPQueue::<i32>::new(-3, 3);
let mut a = Dllink::<(usize, i32)>::new((0, 3));
bpq.append(&mut a, 0);
bpq.increase_key(&mut a, 1);

assert_eq!(bpq.get_max(), 1);
Source

pub fn modify_key(&mut self, it: &mut Dllink<(usize, T)>, delta: i32)

Modify key by delta

Note that the order of items with same key will not be preserved. For the FM algorithm, this is a desired behavior.

§Examples
use mywheel_rs::bpqueue::BPQueue;
use mywheel_rs::dllist::Dllink;

let mut bpq = BPQueue::<i32>::new(-3, 3);
let mut a = Dllink::<(usize, i32)>::new((0, 3));
bpq.append(&mut a, 0);
bpq.modify_key(&mut a, -1);

assert_eq!(bpq.get_max(), -1);
Source§

impl<T: Default> BPQueue<T>

Source

pub fn iter_mut(&mut self) -> BPQueueIterator<'_, T>

Return a new DllIterator object

Trait Implementations§

Source§

impl<T: Debug> Debug for BPQueue<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BPQueue<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for BPQueue<T>
where T: RefUnwindSafe,

§

impl<T> !Send for BPQueue<T>

§

impl<T> !Sync for BPQueue<T>

§

impl<T> Unpin for BPQueue<T>
where T: Unpin,

§

impl<T> UnwindSafe for BPQueue<T>

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>,

Source§

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>,

Source§

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.