Queue

Struct Queue 

Source
pub struct Queue<E: Clone> { /* private fields */ }
Expand description

A persistent (immutable) FIFO queue data structure.

Queue is implemented using two lists (front and back) to achieve amortized O(1) enqueue and dequeue operations. All operations return a new queue, leaving the original unchanged.

§Performance

  • enqueue: O(1)
  • dequeue: O(1) amortized, O(n) worst case when reversing back list
  • is_empty: O(1)
  • len: O(1) - length is cached
  • to_vec: O(n)

Implementations§

Source§

impl<E: Clone> Queue<E>

Source

pub fn empty() -> Self

Creates a new empty queue.

§Examples
use pfds::Queue;
 
let queue: Queue<i32> = Queue::empty();
assert!(queue.is_empty());
assert_eq!(queue.len(), 0);
Source

pub fn enqueue(&self, e: E) -> Self

Creates a new queue with the given element added to the back.

This operation is O(1) and shares structure with the original queue.

§Arguments
  • e - The element to add to the back of the queue
§Examples
use pfds::Queue;
 
let queue = Queue::empty().enqueue(1).enqueue(2).enqueue(3);
assert_eq!(queue.len(), 3);
Source

pub fn dequeue(&self) -> (E, Self)

Removes and returns the front element and a new queue without that element.

This operation is O(1) amortized. When the front list is empty, it reverses the back list which takes O(n) time.

§Returns

A tuple containing:

  • The front element
  • A new queue without the front element
§Panics

Panics if the queue is empty.

§Examples
use pfds::Queue;
 
let queue = Queue::empty().enqueue(1).enqueue(2);
let (first, queue2) = queue.dequeue();
assert_eq!(first, 1);
assert_eq!(queue.len(), 2);  // Original unchanged
assert_eq!(queue2.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the queue is empty.

This operation is O(1).

§Examples
use pfds::Queue;
 
let empty = Queue::<i32>::empty();
assert!(empty.is_empty());
 
let non_empty = empty.enqueue(1);
assert!(!non_empty.is_empty());
Source

pub fn len(&self) -> usize

Returns the number of elements in the queue.

This operation is O(1) as the length is cached.

§Examples
use pfds::Queue;
 
let queue = Queue::empty().enqueue(1).enqueue(2).enqueue(3);
assert_eq!(queue.len(), 3);
Source

pub fn to_vec(&self) -> Vec<E>

Converts the queue to a vector.

Elements are returned in FIFO order (oldest elements first). This operation is O(n).

§Examples
use pfds::Queue;
 
let queue = Queue::empty().enqueue(1).enqueue(2).enqueue(3);
let vec = queue.to_vec();
assert_eq!(vec, vec![1, 2, 3]); // FIFO order
Source

pub fn iter(&self) -> QueueIter<'_, E>

Returns an iterator over the queue elements.

The iterator yields elements in FIFO order (oldest first).

§Examples
use pfds::Queue;
 
let queue = Queue::empty().enqueue(1).enqueue(2).enqueue(3);
let collected: Vec<_> = queue.iter().collect();
assert_eq!(collected, vec![1, 2, 3]);

Trait Implementations§

Source§

impl<E: Clone + Clone> Clone for Queue<E>

Source§

fn clone(&self) -> Queue<E>

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

Auto Trait Implementations§

§

impl<E> Freeze for Queue<E>

§

impl<E> RefUnwindSafe for Queue<E>
where E: RefUnwindSafe,

§

impl<E> Send for Queue<E>
where E: Sync + Send,

§

impl<E> Sync for Queue<E>
where E: Sync + Send,

§

impl<E> Unpin for Queue<E>

§

impl<E> UnwindSafe for Queue<E>
where E: RefUnwindSafe,

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.