pub struct Queue<T> {
/// The underlying vector that stored the elements of the `Queue`.
vec: Vec<T>,
}
Expand description
The Queue
type corresponds to the same called data structure.
Additional Information
A Queue is defined as a linear data structure that is open at both ends and the operations are performed in First In First Out order.
Meaning additions to the list are made at one end, and all deletions from the list are made at the other end.
Fields
vec: Vec
The underlying vector that stored the elements of the Queue
.
Trait Implementations
impl AbiEncode for Queue<T>
impl AbiEncode for Queue<T>
pub fn abi_encode(self, buffer: Buffer) -> Buffer
impl AbiDecode for Queue<T>
impl AbiDecode for Queue<T>
pub fn abi_decode(refmut buffer: BufferReader) -> Self
impl Queue for Queue<T>
impl Queue for Queue<T>
pub fn new() -> Self
pub fn new() -> Self
Create a new Queue
.
Returns
- [Queue] - The newly created
Queue
struct.
Examples
use sway_libs::queue::Queue;
fn foo() {
let queue = Queue::new::<u64>();
assert(queue.is_empty());
}
pub fn is_empty(self) -> bool
pub fn is_empty(self) -> bool
Checks a Queue
for emptiness.
Returns
- [bool] -
true
indicated that the queue does not contain any elements, otherwisefalse
.
Examples
use sway_libs::queue::Queue;
fn foo() {
let queue = Queue::new::<u64>();
assert(queue.is_empty());
}
pub fn len(self) -> u64
pub fn len(self) -> u64
Gets the number of elements in the Queue
.
Returns
- [u64] - The total number of elements.
Examples
use sway_libs::queue::Queue;
fn foo() {
let mut queue = Queue::new::<u64>();
assert(queue.len() == 0);
queue.enqueue(5);
assert(queue.len() == 1);
}
pub fn enqueue(
refmut self,
item: T,
)
pub fn enqueue(
refmut self,
item: T,
)
Enqueues an element in the Queue
, adding it to the end of the queue.
Arguments
item
: [T] - The value to enqueue.
Examples
use sway_libs::queue::Queue;
fn foo() {
let mut queue = Queue::new::<u64>();
queue.enqueue(5);
assert(queue.peek().unwrap() == 5);
}
pub fn dequeue(refmut self) -> Option<T>
pub fn dequeue(refmut self) -> Option<T>
Dequeues the Queue
and returns the oldest element.
Returns
- [Option] - The first element to be enqueued or
None
if the queue is empty.
Examples
use sway_libs::queue::Queue;
fn foo() {
let mut queue = Queue::new::<u64>();
queue.enqueue(5);
let element = queue.dequeue().unwrap();
assert(element == 5);
assert(queue.len() == 0);
}
pub fn peek(self) -> Option<T>
pub fn peek(self) -> Option<T>
Gets the head of the queue without dequeueing the element.
Returns
- [Option] - The first element to be enqueued or
None
if the queue is empty.
Examples
use sway_libs::queue::Queue;
fn foo() {
let mut queue = Queue::new::<u64>();
queue.enqueue(5);
let element = queue.peek().unwrap();
assert(element == 5);
assert(queue.len() == 1);
}