Priority

Struct Priority 

Source
pub struct Priority { /* private fields */ }
Expand description

A totally-ordered priority.

These priorities implement Dietz & Sleator (1987)’s solution to the order maintenance problem, which require a data structure T that supports insertion and comparison operations such that insertion constructs an element of the next greatest priority:

forall t: T, t < t.insert()

but is still lower priority than all other greater priorities:

forall t t': T s.t. t < t', t.insert() < t'

Amongst a collection of n priorities, comparison takes constant time, while insertion takes amortized log(n) time.

§Usage

let p0 = Priority::new();
let p2 = p0.insert();
let p1 = p0.insert();
let p3 = p2.insert();

assert!(p0 < p1);
assert!(p0 < p2);
assert!(p0 < p3);
assert!(p1 < p2);
assert!(p1 < p3);
assert!(p2 < p3);

§Memory management

Under the hood, these priorities are actually references to nodes of a circular linked list, allocated from an arena. Those nodes are reference-counted, which allows these priorities to be cloned. The node’s reference count is decremented when this priority is dropped, and if it reaches zero, the node is deallocated.

Priorities from different arenas cannot be compared with one another.

§Algorithm

This implementation uses Dietz & Sleator (1987)’s algorithm, also called tag-range relabeling (as opposed to Bender et al.’s list-range relabeling algorithm).

While Dietz & Sleator also propose a data structure that supports constant-time insertion, that data structure is so overwhelmingly complex that the overhead of maintaining such a data structure will overwhelm any theoretical efficiency for any reasonable number of priorities.

More recently, Bender et al. proposed an alternative solution, using a list-range relabling approach. That approach is likely more efficient on real hardware, since it favors bit-wise operations over multiplication and division. For now, this crate uses the possibly slower tag-range relabeling approach, because it was ported from a scripting language that is better suited toward floating operations. It remains to be seen which implementation is better under which circumstances.

§References

  • Paul F. Dietz and Daniel D. Sleator. Two Algorithms for Maintaining Order in a List. 1987.

  • Michael A. Bender, Richard Cole, Erik D. Demaine, Martin Farach-Colton, and Jack Zito. Two simplified algorithms for maintaining order in a list. 2002.

Implementations§

Source§

impl Priority

Source

pub fn new() -> Self

Allocate a new priority in a fresh arena.

Note that priorities allocated in separate arenas cannot be compared; to construct a Priority that can be compared to some existing priority, use Priority::insert().

Source

pub fn insert(&self) -> Self

Allocate the next greatest priority after the given self.

Trait Implementations§

Source§

impl Clone for Priority

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Priority

Source§

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

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

impl Drop for Priority

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl PartialEq for Priority

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Priority

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for Priority

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> 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> 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> 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 = 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.