A mutable double-ended queue of elements. The queue has two ends, front and back. Elements can be added and removed at the two ends.
This can be used for different use cases, such as:
pushBack() and popFront()pushFront() and popFront().Example:
import Queue "mo:core/Queue";
persistent actor {
let orders = Queue.empty<Text>();
Queue.pushBack(orders, "Motoko");
Queue.pushBack(orders, "Mops");
Queue.pushBack(orders, "IC");
assert Queue.popFront(orders) == ?"Motoko";
assert Queue.popFront(orders) == ?"Mops";
assert Queue.popFront(orders) == ?"IC";
assert Queue.popFront(orders) == null;
}
The internal implementation is a doubly-linked list.
Performance:
O(1) for push, pop, and peek operations.O(n).
n denotes the number of elements stored in the queue.public func toPure<T>(self : Queue<T>) : PureQueue.Queue<T>Converts a mutable queue to an immutable, purely functional queue.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.fromIter<Nat>([1, 2, 3].values());
let pureQueue = Queue.toPure<Nat>(queue);
}
Runtime: O(n)
Space: O(n)
n denotes the number of elements stored in the queue.
@deprecated M0235
public func fromPure<T>(pureQueue : PureQueue.Queue<T>) : Queue<T>Converts an immutable, purely functional queue to a mutable queue.
Example:
import Queue "mo:core/Queue";
import PureQueue "mo:core/pure/Queue";
persistent actor {
let pureQueue = PureQueue.fromIter<Nat>([1, 2, 3].values());
let queue = Queue.fromPure<Nat>(pureQueue);
}
Runtime: O(n)
Space: O(n)
n denotes the number of elements stored in the queue.
@deprecated M0235
public func empty<T>() : Queue<T>Create a new empty mutable double-ended queue.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.empty<Text>();
assert Queue.size(queue) == 0;
}
Runtime: O(1).
Space: O(1).
public func singleton<T>(element : T) : Queue<T>Creates a new queue with a single element.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.singleton<Nat>(123);
assert Queue.size(queue) == 1;
}
Runtime: O(1) Space: O(1)
public func clear<T>(self : Queue<T>)Removes all elements from the queue.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.fromIter<Nat>([1, 2, 3].values());
Queue.clear(queue);
assert Queue.isEmpty(queue);
}
Runtime: O(1) Space: O(1)
public func clone<T>(self : Queue<T>) : Queue<T>Creates a deep copy of the queue.
Example:
import Queue "mo:core/Queue";
persistent actor {
let original = Queue.fromIter<Nat>([1, 2, 3].values());
let copy = Queue.clone(original);
Queue.clear(original);
assert Queue.size(original) == 0;
assert Queue.size(copy) == 3;
}
Runtime: O(n)
Space: O(n)
n denotes the number of elements stored in the queue.
public func size<T>(self : Queue<T>) : NatReturns the number of elements in the queue.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.fromIter<Text>(["A", "B", "C"].values());
assert Queue.size(queue) == 3;
}
Runtime: O(1) Space: O(1)
public func isEmpty<T>(self : Queue<T>) : BoolReturns true if the queue contains no elements.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.empty<Nat>();
assert Queue.isEmpty(queue);
}
Runtime: O(1) Space: O(1)
public func contains<T>(
self : Queue<T>,
equal : (implicit : (T, T) -> Bool),
element : T
) : BoolChecks if an element exists in the queue using the provided equality function.
Example:
import Queue "mo:core/Queue";
import Nat "mo:core/Nat";
persistent actor {
let queue = Queue.fromIter<Nat>([1, 2, 3].values());
assert Queue.contains(queue, Nat.equal, 2);
}
Runtime: O(n)
Space: O(1)
n denotes the number of elements stored in the queue.
public func peekFront<T>(self : Queue<T>) : ?TReturns the first element in the queue without removing it. Returns null if the queue is empty.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.fromIter<Nat>([1, 2, 3].values());
assert Queue.peekFront(queue) == ?1;
}
Runtime: O(1) Space: O(1)
public func peekBack<T>(self : Queue<T>) : ?TReturns the last element in the queue without removing it. Returns null if the queue is empty.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.fromIter<Nat>([1, 2, 3].values());
assert Queue.peekBack(queue) == ?3;
}
Runtime: O(1) Space: O(1)
public func pushFront<T>(self : Queue<T>, element : T)Adds an element to the front of the queue.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.empty<Nat>();
Queue.pushFront(queue, 1);
assert Queue.peekFront(queue) == ?1;
}
Runtime: O(1) Space: O(1)
public func pushBack<T>(self : Queue<T>, element : T)Adds an element to the back of the queue.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.empty<Nat>();
Queue.pushBack(queue, 1);
assert Queue.peekBack(queue) == ?1;
}
Runtime: O(1) Space: O(1)
public func popFront<T>(self : Queue<T>) : ?TRemoves and returns the first element in the queue. Returns null if the queue is empty.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.fromIter<Nat>([1, 2, 3].values());
assert Queue.popFront(queue) == ?1;
assert Queue.size(queue) == 2;
}
Runtime: O(1) Space: O(1)
public func popBack<T>(self : Queue<T>) : ?TRemoves and returns the last element in the queue. Returns null if the queue is empty.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.fromIter<Nat>([1, 2, 3].values());
assert Queue.popBack(queue) == ?3;
assert Queue.size(queue) == 2;
}
Runtime: O(1) Space: O(1)
public func fromIter<T>(iter : Iter.Iter<T>) : Queue<T>Creates a new queue from an iterator.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.fromIter<Text>(["A", "B", "C"].values());
assert Queue.size(queue) == 3;
}
Runtime: O(n)
Space: O(n)
n denotes the number of elements stored in the queue.
public func toQueue<T>(self : Iter.Iter<T>) : Queue<T>Converts an iterator to a queue.
Example:
import Queue "mo:core/Queue";
persistent actor {
transient let iter = ["A", "B", "C"].values();
let queue = iter.toQueue<Text>();
assert Queue.size(queue) == 3;
}
Runtime: O(n)
Space: O(n)
n denotes the number of elements stored in the queue.
public func fromArray<T>(array : [T]) : Queue<T>Creates a new queue from an array. Elements appear in the same order as in the array.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.fromArray<Text>(["A", "B", "C"]);
assert Queue.size(queue) == 3;
assert Queue.peekFront(queue) == ?"A";
}
Runtime: O(n)
Space: O(n)
n denotes the number of elements stored in the array.
public func fromVarArray<T>(array : [var T]) : Queue<T>public func toArray<T>(self : Queue<T>) : [T]Creates a new immutable array containing all elements from the queue. Elements appear in the same order as in the queue (front to back).
Example:
import Queue "mo:core/Queue";
import Array "mo:core/Array";
persistent actor {
let queue = Queue.fromArray<Text>(["A", "B", "C"]);
let array = Queue.toArray(queue);
assert array == ["A", "B", "C"];
}
Runtime: O(n)
Space: O(n)
n denotes the number of elements stored in the queue.
public func toVarArray<T>(self : Queue<T>) : [var T]public func values<T>(self : Queue<T>) : Iter.Iter<T>Returns an iterator over the elements in the queue. Iterates from front to back.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.fromIter<Text>(["A", "B", "C"].values());
transient let iter = Queue.values(queue);
assert iter.next() == ?"A";
assert iter.next() == ?"B";
assert iter.next() == ?"C";
assert iter.next() == null;
}
Runtime: O(1) for iterator creation, O(n) for full iteration Space: O(1)
public func all<T>(self : Queue<T>, predicate : T -> Bool) : BoolTests whether all elements in the queue satisfy the given predicate.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.fromIter<Nat>([2, 4, 6].values());
assert Queue.all<Nat>(queue, func(x) { x % 2 == 0 });
}
Runtime: O(n) Space: O(1)
public func any<T>(self : Queue<T>, predicate : T -> Bool) : BoolTests whether any element in the queue satisfies the given predicate.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.fromIter<Nat>([1, 2, 3].values());
assert Queue.any<Nat>(queue, func (x) { x > 2 });
}
Runtime: O(n)
Space: O(1)
n denotes the number of elements stored in the queue.
public func forEach<T>(self : Queue<T>, operation : T -> ())Applies the given operation to all elements in the queue.
Example:
import Queue "mo:core/Queue";
persistent actor {
var sum = 0;
let queue = Queue.fromIter<Nat>([1, 2, 3].values());
Queue.forEach<Nat>(queue, func(x) { sum += x });
assert sum == 6;
}
Runtime: O(n)
Space: O(1)
n denotes the number of elements stored in the queue.
public func map<T, U>(self : Queue<T>, project : T -> U) : Queue<U>Creates a new queue by applying the given function to all elements.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.fromIter<Nat>([1, 2, 3].values());
let doubled = Queue.map<Nat, Nat>(queue, func(x) { x * 2 });
assert Queue.peekFront(doubled) == ?2;
}
Runtime: O(n)
Space: O(n)
n denotes the number of elements stored in the queue.
public func filter<T>(self : Queue<T>, criterion : T -> Bool) : Queue<T>Creates a new queue containing only elements that satisfy the given predicate.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.fromIter<Nat>([1, 2, 3, 4].values());
let evens = Queue.filter<Nat>(queue, func(x) { x % 2 == 0 });
assert Queue.size(evens) == 2;
}
Runtime: O(n)
Space: O(n)
n denotes the number of elements stored in the queue.
public func filterMap<T, U>(self : Queue<T>, project : T -> ?U) : Queue<U>Creates a new queue by applying the given function to all elements and keeping only the non-null results.
Example:
import Queue "mo:core/Queue";
persistent actor {
let queue = Queue.fromIter<Nat>([1, 2, 3, 4].values());
let evenDoubled = Queue.filterMap<Nat, Nat>(
queue,
func(x) {
if (x % 2 == 0) { ?(x * 2) } else { null }
}
);
assert Queue.size(evenDoubled) == 2;
}
Runtime: O(n)
Space: O(n)
n denotes the number of elements stored in the queue.
public func equal<T>(
self : Queue<T>,
other : Queue<T>,
equal : (implicit : (T, T) -> Bool)
) : BoolCompares two queues for equality using the provided equality function.
Example:
import Queue "mo:core/Queue";
import Nat "mo:core/Nat";
persistent actor {
let queue1 = Queue.fromIter<Nat>([1, 2, 3].values());
let queue2 = Queue.fromIter<Nat>([1, 2, 3].values());
assert Queue.equal(queue1, queue2, Nat.equal);
}
Runtime: O(n)
Space: O(1)
n denotes the number of elements stored in the queue.
public func toText<T>(self : Queue<T>, format : (implicit : (toText : T -> Text))) : TextConverts a queue to its string representation using the provided element formatter.
Example:
import Queue "mo:core/Queue";
import Nat "mo:core/Nat";
persistent actor {
let queue = Queue.fromIter<Nat>([1, 2, 3].values());
assert Queue.toText(queue, Nat.toText) == "Queue[1, 2, 3]";
}
Runtime: O(n)
Space: O(n)
n denotes the number of elements stored in the queue.
public func compare<T>(
self : Queue<T>,
other : Queue<T>,
compare : (implicit : (T, T) -> Order.Order)
) : Order.OrderCompares two queues using the provided comparison function. Returns #less, #equal, or #greater.
Example:
import Queue "mo:core/Queue";
import Nat "mo:core/Nat";
persistent actor {
let queue1 = Queue.fromIter<Nat>([1, 2].values());
let queue2 = Queue.fromIter<Nat>([1, 2, 3].values());
assert Queue.compare(queue1, queue2, Nat.compare) == #less;
}
Runtime: O(n)
Space: O(1)
n denotes the number of elements stored in the queue.