Skip to main content

PagedPriorityQueue

Struct PagedPriorityQueue 

Source
pub struct PagedPriorityQueue<K, P, const PAGE: usize = DEFAULT_PAGE, D: Directory = Flat> { /* private fields */ }
Expand description

An indexed max-priority queue with a paged index and dense heap storage.

PAGE is the non-zero number of index slots per page. D selects the directory: Flat (default), Radix, or Tree. See crate::page. At most u32::MAX entries can be stored. The greatest priority is at position zero; ties have unspecified order. See the module docs for panic safety.

Implementations§

Source§

impl<K: Key, P, const PAGE: usize, D: Directory> PagedPriorityQueue<K, P, PAGE, D>

Source

pub fn new() -> Self

Creates an empty queue without allocating.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty queue with room for at least capacity entries in its dense arrays. Index pages are allocated on demand.

Source

pub fn len(&self) -> usize

Number of entries in the queue.

Source

pub fn is_empty(&self) -> bool

Whether the queue has no entries.

Source

pub fn capacity(&self) -> usize

Number of entries the dense arrays can hold without reallocating.

Source

pub fn reserve(&mut self, additional: usize)

Reserves room for at least additional more entries in both dense arrays.

Source

pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>

Reserves room for at least additional more entries in both dense arrays.

Returns an error if the capacity overflows or allocation fails. On error the entries, capacity and array addresses are preserved.

§Examples
use paged_collections::priority_queue::PagedPriorityQueue;

let mut queue: PagedPriorityQueue<usize, i32> = PagedPriorityQueue::new();
queue.try_reserve(16).unwrap();
assert!(queue.capacity() >= 16);
Source

pub fn shrink_to_fit(&mut self)

Shrinks the dense arrays and index directory. Frees every index page if the queue is empty.

Source

pub fn clear(&mut self)

Removes every entry, dropping its priority. Index pages and dense capacity stay allocated for reuse.

Source

pub fn allocated_pages(&self) -> usize

Number of allocated pages in the key index.

Source

pub fn peek(&self) -> Option<(&K, &P)>

The stored key and greatest priority, or None if empty.

Source

pub fn contains_key(&self, key: &K) -> bool

Whether the exact key is present, including its generation.

Source

pub fn priority(&self, key: &K) -> Option<&P>

The priority of the exact key, or None if absent.

Source

pub fn position(&self, key: &K) -> Option<usize>

Heap position of the exact key, or None if absent.

Positions index both slices and can change on any heap mutation.

Source

pub fn iter(&self) -> Iter<'_, K, P>

Iterates over entries in heap order, which is otherwise unspecified.

Source

pub fn keys(&self) -> Iter<'_, K> ⓘ

Iterates over keys in heap order.

Source

pub fn priorities(&self) -> Iter<'_, P> ⓘ

Iterates over priorities in heap order.

Source

pub fn key_slice(&self) -> &[K]

Keys in heap order; position zero has the greatest priority.

Source

pub fn priority_slice(&self) -> &[P]

Priorities in heap order, parallel to key_slice.

Source

pub fn drain(&mut self) -> Drain<'_, K, P, PAGE, D> ⓘ

Removes all entries eagerly and iterates over them in arbitrary order.

Index pages and dense capacity are kept. Dropping the iterator drops its remaining entries; forgetting it still leaves the queue empty. The queue stays mutably borrowed until the iterator is released.

Source§

impl<K: Key, P: Ord, const PAGE: usize, D: Directory> PagedPriorityQueue<K, P, PAGE, D>

Source

pub fn push(&mut self, key: K, priority: P) -> Option<P>

Inserts a key or updates its priority, returning the previous priority of that same key. An equal stored key is kept.

If another generation holds the slot, it is evicted and None is returned. The replacement starts at the old entry’s position and is then sifted to restore heap order.

§Panics

Panics if a new entry would exceed u32::MAX entries, if storage cannot be allocated, or if comparing priorities panics. Allocation failure leaves entries unchanged; a comparison panic has the guarantees described in the module docs.

Source

pub fn replace(&mut self, key: K, priority: P) -> Option<(K, P)>

Inserts or updates as push, but returns the entry previously in key’s slot, whether it was key itself or another generation that is now evicted (as PagedSparseMap::replace).

Unlike push, this stores key even when the old key is equal to it. Panics under the same conditions as push.

use paged_collections::id_alloc::IdAllocator;
use paged_collections::priority_queue::PagedPriorityQueue;

let mut ids = IdAllocator::new();
let old = ids.alloc();
ids.free(old);
let new = ids.alloc(); // same slot, next generation
let mut queue: PagedPriorityQueue<_, i32> = PagedPriorityQueue::new();
queue.push(old, 5);
assert_eq!(queue.replace(new, 7), Some((old, 5)));
assert_eq!(queue.replace(new, 9), Some((new, 7)));
assert_eq!(queue.pop(), Some((new, 9)));
Source

pub fn try_push(&mut self, key: K, priority: P) -> Result<Option<P>, (K, P)>

Inserts or updates as push, but returns the supplied pair unchanged if another generation holds the slot.

Updating the same key is allowed and returns its previous priority. Panics under the same conditions as push.

Source

pub fn push_increase(&mut self, key: K, priority: P) -> bool

Inserts an absent key, or updates it only if priority is strictly greater. Returns whether the queue changed.

An absent generation evicts another generation as push does, regardless of the evicted priority. Panics as push does.

Source

pub fn push_decrease(&mut self, key: K, priority: P) -> bool

Inserts an absent key, or updates it only if priority is strictly smaller. Returns whether the queue changed.

An absent generation evicts another generation as push does, regardless of the evicted priority. Panics as push does.

Source

pub fn change_priority(&mut self, key: &K, priority: P) -> Option<P>

Changes the priority of a present key, returning its old priority. Returns None without changing the queue if the exact key is absent.

A comparison panic has the guarantees in the module docs.

Source

pub fn change_priority_with( &mut self, key: &K, change: impl FnOnce(&mut P), ) -> bool

Calls change on a present key’s priority and restores heap order. Returns whether the exact key was present; an absent key does not call the closure.

If the closure or a comparison panics, its changes remain and heap order may be broken, but entries stay findable and correctly paired.

Source

pub fn pop(&mut self) -> Option<(K, P)>

Removes and returns the entry with the greatest priority.

Returns None if empty. If comparing priorities panics, the removed entry is dropped and the remaining entries stay findable and paired, though heap order may be broken.

Source

pub fn remove(&mut self, key: &K) -> Option<(K, P)>

Removes and returns the stored key and priority for the exact key.

Returns None if absent. A comparison panic has the same guarantees as pop.

Source

pub fn into_sorted_vec(self) -> Vec<(K, P)>

Consumes the queue into entries in ascending priority order. Equal priorities have unspecified order.

Source

pub fn into_sorted_iter(self) -> IntoSortedIter<K, P, PAGE, D> ⓘ

Consumes the queue and repeatedly pops entries in descending priority order. Equal priorities have unspecified order.

Trait Implementations§

Source§

impl<K: Key, P: Clone, const PAGE: usize, D: Directory> Clone for PagedPriorityQueue<K, P, PAGE, D>
where D::Store<Slot, PAGE>: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Key + Debug, P: Debug, const PAGE: usize, D: Directory> Debug for PagedPriorityQueue<K, P, PAGE, D>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<K: Key, P, const PAGE: usize, D: Directory> Default for PagedPriorityQueue<K, P, PAGE, D>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, K, P, const PAGE: usize, D> Deserialize<'de> for PagedPriorityQueue<K, P, PAGE, D>
where K: Key + Deserialize<'de>, P: Ord + Deserialize<'de>, D: Directory,

Available on crate feature serde only.

Deserializes pairs by repeated push, so later entries in a slot win. Memory follows key indices: use Radix or Tree for widely spread keys.

Source§

fn deserialize<De: Deserializer<'de>>( deserializer: De, ) -> Result<Self, De::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<K: Key, P: Eq, const PAGE: usize, D: Directory> Eq for PagedPriorityQueue<K, P, PAGE, D>

Queue PartialEq is an equivalence relation when priorities implement Eq.

Source§

impl<K: Key, P: Ord, const PAGE: usize, D: Directory> Extend<(K, P)> for PagedPriorityQueue<K, P, PAGE, D>

Repeatedly pushes entries, so later entries in a slot win.

Source§

fn extend<I: IntoIterator<Item = (K, P)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K: Key, P: Ord, const PAGE: usize, D: Directory> From<PagedPriorityQueue<K, P, PAGE, D>> for ConcurrentPriorityQueue<K, P, PAGE, D>

Available on crate feature concurrent only.

Moves a plain queue into the default number of shards by re-pushing its entries in O(n log n) time.

Source§

fn from(queue: PagedPriorityQueue<K, P, PAGE, D>) -> Self

Converts to this type from the input type.
Source§

impl<K: Key, P: Ord, const PAGE: usize, D: Directory> FromIterator<(K, P)> for PagedPriorityQueue<K, P, PAGE, D>

Source§

fn from_iter<I: IntoIterator<Item = (K, P)>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<K: Key + Hash, P: Hash, const PAGE: usize, D: Directory> Hash for PagedPriorityQueue<K, P, PAGE, D>

Hashes the key-priority pairs in any order, consistent with PartialEq.

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, K: Key, P, const PAGE: usize, D: Directory> IntoIterator for &'a PagedPriorityQueue<K, P, PAGE, D>

Source§

type Item = (&'a K, &'a P)

The type of the elements being iterated over.
Source§

type IntoIter = Zip<Iter<'a, K>, Iter<'a, P>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K: Key, P, const PAGE: usize, D: Directory> IntoIterator for PagedPriorityQueue<K, P, PAGE, D>

Source§

type Item = (K, P)

The type of the elements being iterated over.
Source§

type IntoIter = Zip<IntoIter<K>, IntoIter<P>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K: Key + Sync, P: Sync, const PAGE: usize, D: Directory> IntoParallelIterator for &'a PagedPriorityQueue<K, P, PAGE, D>

Available on crate feature rayon only.
Source§

type Item = (&'a K, &'a P)

The type of item that the parallel iterator will produce.
Source§

type Iter = Zip<Iter<'a, K>, Iter<'a, P>>

The parallel iterator type that will be created.
Source§

fn into_par_iter(self) -> Self::Iter

Converts self into a parallel iterator. Read more
Source§

impl<K: Key, P: PartialEq<Q>, Q, const PAGE: usize, D: Directory, const P2: usize, D2: Directory> PartialEq<PagedPriorityQueue<K, Q, P2, D2>> for PagedPriorityQueue<K, P, PAGE, D>

Compares priorities by exact key using priority, across page sizes and directories. Equality ignores heap layout.

Source§

fn eq(&self, other: &PagedPriorityQueue<K, Q, P2, D2>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<K: Key + Serialize, P: Serialize, const PAGE: usize, D: Directory> Serialize for PagedPriorityQueue<K, P, PAGE, D>

Available on crate feature serde only.

Serializes as a sequence of (key, priority) pairs in heap order.

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<K, P, const PAGE: usize, D> Forget for PagedPriorityQueue<K, P, PAGE, D>

§

impl<K, P, const PAGE: usize, D> Freeze for PagedPriorityQueue<K, P, PAGE, D>
where PagedSparseSet<K, PAGE, D>: Freeze, Vec<P>: Freeze,

§

impl<K, P, const PAGE: usize, D> Move for PagedPriorityQueue<K, P, PAGE, D>

§

impl<K, P, const PAGE: usize, D> RefUnwindSafe for PagedPriorityQueue<K, P, PAGE, D>

§

impl<K, P, const PAGE: usize, D> Send for PagedPriorityQueue<K, P, PAGE, D>
where PagedSparseSet<K, PAGE, D>: Send, Vec<P>: Send,

§

impl<K, P, const PAGE: usize, D> Sync for PagedPriorityQueue<K, P, PAGE, D>
where PagedSparseSet<K, PAGE, D>: Sync, Vec<P>: Sync,

§

impl<K, P, const PAGE: usize, D> Unpin for PagedPriorityQueue<K, P, PAGE, D>
where PagedSparseSet<K, PAGE, D>: Unpin, Vec<P>: Unpin,

§

impl<K, P, const PAGE: usize, D> UnsafeUnpin for PagedPriorityQueue<K, P, PAGE, D>
where PagedSparseSet<K, PAGE, D>: UnsafeUnpin, Vec<P>: UnsafeUnpin,

§

impl<K, P, const PAGE: usize, D> UnwindSafe for PagedPriorityQueue<K, P, PAGE, D>
where PagedSparseSet<K, PAGE, D>: UnwindSafe, Vec<P>: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<'data, I> IntoParallelRefIterator<'data> for I
where I: 'data + ?Sized, &'data I: IntoParallelIterator,

Source§

type Iter = <&'data I as IntoParallelIterator>::Iter

The type of the parallel iterator that will be returned.
Source§

type Item = <&'data I as IntoParallelIterator>::Item

The type of item that the parallel iterator will produce. This will typically be an &'data T reference type.
Source§

fn par_iter(&'data self) -> <I as IntoParallelRefIterator<'data>>::Iter

Converts self into a parallel iterator. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.