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>
impl<K: Key, P, const PAGE: usize, D: Directory> PagedPriorityQueue<K, P, PAGE, D>
Sourcepub fn with_capacity(capacity: usize) -> Self
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.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Number of entries the dense arrays can hold without reallocating.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves room for at least additional more entries in both dense arrays.
Sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
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);Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the dense arrays and index directory. Frees every index page if the queue is empty.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes every entry, dropping its priority. Index pages and dense capacity stay allocated for reuse.
Sourcepub fn allocated_pages(&self) -> usize
pub fn allocated_pages(&self) -> usize
Number of allocated pages in the key index.
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Whether the exact key is present, including its generation.
Sourcepub fn priority(&self, key: &K) -> Option<&P>
pub fn priority(&self, key: &K) -> Option<&P>
The priority of the exact key, or None if absent.
Sourcepub fn position(&self, key: &K) -> Option<usize>
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.
Sourcepub fn iter(&self) -> Iter<'_, K, P>
pub fn iter(&self) -> Iter<'_, K, P>
Iterates over entries in heap order, which is otherwise unspecified.
Sourcepub fn priorities(&self) -> Iter<'_, P> ⓘ
pub fn priorities(&self) -> Iter<'_, P> ⓘ
Iterates over priorities in heap order.
Sourcepub fn priority_slice(&self) -> &[P]
pub fn priority_slice(&self) -> &[P]
Priorities in heap order, parallel to key_slice.
Sourcepub fn drain(&mut self) -> Drain<'_, K, P, PAGE, D> ⓘ
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>
impl<K: Key, P: Ord, const PAGE: usize, D: Directory> PagedPriorityQueue<K, P, PAGE, D>
Sourcepub fn push(&mut self, key: K, priority: P) -> Option<P>
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.
Sourcepub fn replace(&mut self, key: K, priority: P) -> Option<(K, P)>
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)));Sourcepub fn try_push(&mut self, key: K, priority: P) -> Result<Option<P>, (K, P)>
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.
Sourcepub fn push_increase(&mut self, key: K, priority: P) -> bool
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.
Sourcepub fn push_decrease(&mut self, key: K, priority: P) -> bool
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.
Sourcepub fn change_priority(&mut self, key: &K, priority: P) -> Option<P>
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.
Sourcepub fn change_priority_with(
&mut self,
key: &K,
change: impl FnOnce(&mut P),
) -> bool
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.
Sourcepub fn pop(&mut self) -> Option<(K, P)>
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.
Sourcepub fn remove(&mut self, key: &K) -> Option<(K, P)>
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.
Sourcepub fn into_sorted_vec(self) -> Vec<(K, P)>
pub fn into_sorted_vec(self) -> Vec<(K, P)>
Consumes the queue into entries in ascending priority order. Equal priorities have unspecified order.
Sourcepub fn into_sorted_iter(self) -> IntoSortedIter<K, P, PAGE, D> ⓘ
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>
impl<K: Key, P: Clone, const PAGE: usize, D: Directory> Clone for PagedPriorityQueue<K, P, PAGE, D>
Source§impl<K: Key + Debug, P: Debug, const PAGE: usize, D: Directory> Debug for PagedPriorityQueue<K, P, PAGE, D>
impl<K: Key + Debug, P: Debug, const PAGE: usize, D: Directory> Debug for PagedPriorityQueue<K, P, PAGE, D>
Source§impl<K: Key, P, const PAGE: usize, D: Directory> Default for PagedPriorityQueue<K, P, PAGE, D>
impl<K: Key, P, const PAGE: usize, D: Directory> Default for PagedPriorityQueue<K, P, PAGE, D>
Source§impl<'de, K, P, const PAGE: usize, D> Deserialize<'de> for PagedPriorityQueue<K, P, PAGE, D>
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.
impl<'de, K, P, const PAGE: usize, D> Deserialize<'de> for PagedPriorityQueue<K, P, PAGE, D>
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>
fn deserialize<De: Deserializer<'de>>( deserializer: De, ) -> Result<Self, De::Error>
impl<K: Key, P: Eq, const PAGE: usize, D: Directory> Eq for PagedPriorityQueue<K, P, PAGE, D>
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.
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)
fn extend<I: IntoIterator<Item = (K, P)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: T)
fn extend_one(&mut self, item: T)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)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.
impl<K: Key, P: Ord, const PAGE: usize, D: Directory> From<PagedPriorityQueue<K, P, PAGE, D>> for ConcurrentPriorityQueue<K, P, PAGE, D>
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
fn from(queue: PagedPriorityQueue<K, P, PAGE, D>) -> Self
Source§impl<K: Key, P: Ord, const PAGE: usize, D: Directory> FromIterator<(K, P)> for PagedPriorityQueue<K, P, PAGE, D>
impl<K: Key, P: Ord, const PAGE: usize, D: Directory> FromIterator<(K, P)> for PagedPriorityQueue<K, P, PAGE, D>
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.
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§impl<'a, K: Key, P, const PAGE: usize, D: Directory> IntoIterator for &'a PagedPriorityQueue<K, P, PAGE, D>
impl<'a, K: Key, P, const PAGE: usize, D: Directory> IntoIterator for &'a PagedPriorityQueue<K, P, PAGE, D>
Source§impl<K: Key, P, const PAGE: usize, D: Directory> IntoIterator for PagedPriorityQueue<K, P, PAGE, D>
impl<K: Key, P, const PAGE: usize, D: Directory> IntoIterator for PagedPriorityQueue<K, P, PAGE, D>
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.
impl<'a, K: Key + Sync, P: Sync, const PAGE: usize, D: Directory> IntoParallelIterator for &'a PagedPriorityQueue<K, P, PAGE, D>
rayon only.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.
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.
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>
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>
impl<K, P, const PAGE: usize, D> Sync for PagedPriorityQueue<K, P, PAGE, D>
impl<K, P, const PAGE: usize, D> Unpin for PagedPriorityQueue<K, P, PAGE, D>
impl<K, P, const PAGE: usize, D> UnsafeUnpin for PagedPriorityQueue<K, P, PAGE, D>
impl<K, P, const PAGE: usize, D> UnwindSafe for PagedPriorityQueue<K, P, PAGE, D>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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