README

#Queue

Queue is a first in first out (FIFO) data structure, allowing to process their elements in the order they come.

#Usage

#Create and Clear

You can create a queue manually by using the new or construct it using the from_array.
///|
test {
let _queue : @queue.Queue[Int] = Queue([])
let _queue1 = @queue.from_array([1, 2, 3])
}

To clear the queue, you can use the clear method.
///|
test {
let queue = @queue.from_array([1, 2, 3])
queue.clear()
}

#Length

You can get the length of the queue by using the length method. The is_empty method can be used to check if the queue is empty.
///|
test {
let queue = @queue.from_array([1, 2, 3])
@test.assert_eq(queue.length(), 3)
@test.assert_eq(queue.is_empty(), false)
}

#Pop and Push

You can add elements to the queue using the push method and remove them using the pop method.
///|
test {
let queue = @queue.Queue([])
queue.push(1)
queue.push(2)
@test.assert_eq(queue.pop(), Some(1))
@test.assert_eq(queue.pop(), Some(2))
}

#Peek

You can get the first element of the queue without removing it using the peek method.
///|
test {
let queue = @queue.from_array([1, 2, 3])
@test.assert_eq(queue.peek(), Some(1))
}

#From Iterator

///|
test {
let queue = @queue.from_iter([1, 2, 3].iter())
@test.assert_eq(queue.length(), 3)
}

#Traverse

each() iterates over elements in FIFO order. eachi() provides the index. fold() reduces the queue to a single value.

///|
test {
let queue = @queue.from_array([1, 2, 3])
// each
let buf = []
queue.each(fn(x) { buf.push(x) })
@test.assert_eq(buf, [1, 2, 3])
// eachi
let pairs = []
queue.eachi(fn(i, x) { pairs.push((i, x)) })
@test.assert_eq(pairs, [(0, 1), (1, 2), (2, 3)])
// fold
let sum = queue.fold(init=0, fn(acc, x) { acc + x })
@test.assert_eq(sum, 6)
}

#Iterator

iter() returns an Iter over the queue's elements.

///|
test {
let queue = @queue.from_array([1, 2, 3])
debug_inspect(queue.iter().to_array(), content="[1, 2, 3]")
}

#Copy and Transfer

copy() creates a shallow clone. transfer() moves all elements from one queue to the end of another, emptying the source.

///|
test {
let queue = @queue.from_array([1, 2, 3])
let cloned = queue.copy()
@test.assert_eq(cloned.pop(), Some(1))
@test.assert_eq(queue.length(), 3) // original unchanged
}

///|
test {
let dst = @queue.from_array([1, 2])
let src = @queue.from_array([3, 4])
src.transfer(dst)
@test.assert_eq(src.is_empty(), true)
@test.assert_eq(dst.length(), 4)
@test.assert_eq(dst.pop(), Some(1))
}

#
Queue

type Queue[A]

A FIFO queue backed by @deque.Deque (a growable circular buffer).

push appends to the back of the deque and peek/pop operate on the front, so all queue operations are O(1) (amortized for push).
impl Show for Queue[A]

#
Queue::Queue

#as_free_fn(of, deprecated="Use from_array instead")
#alias(of, deprecated="Use from_array instead")
#as_free_fn(from_array)
#alias(from_array)
fn[A] Queue::Queue(arr : ArrayView[A]) -> Queue[A]

Creates a new queue from an array.

Example

test {
let array = Array::makei(3, idx => idx + 1)
let queue : @queue.Queue[Int] = Queue(array)
@test.assert_eq(queue.length(), 3)
}

#
Queue::clear

fn[A] Queue::clear(self : Queue[A]) -> Unit

Clears the queue.

Example

test {
let queue : @queue.Queue[Int] = @queue.from_array([1, 2, 3, 4])
queue.clear()
}

#
Queue::copy

#alias(clone, deprecated="`clone` is deprecated, use `copy` instead")
fn[A] Queue::copy(self : Queue[A]) -> Queue[A]

Returns a copy of the queue.

Example

test {
let queue : @queue.Queue[Int] = @queue.from_array([1, 2, 3, 4])
let queue2 : @queue.Queue[Int] = queue.copy()
@test.assert_eq(queue2.length(), 4)
}

#
Queue::each

fn[A] Queue::each(self : Queue[A], f : (A) -> Unit) -> Unit

Iterates over the queue.

Example

test {
let queue : @queue.Queue[Int] = @queue.from_array([1, 2, 3, 4])
let mut sum = 0
queue.each(x => sum x)
inspect(sum, content="10")
}

#
Queue::eachi

fn[A] Queue::eachi(self : Queue[A], f : (Int, A) -> Unit) -> Unit

Iterates over the queue with index.

Example

test {
let queue : @queue.Queue[Int] = @queue.from_array([1, 2, 3, 4])
let mut sum = 0
queue.eachi((i, x) => sum i * x)
inspect(sum, content="20")
}

#
Queue::fold

fn[A, B] Queue::fold(self : Queue[A], init~ : B, f : (B, A) -> B) -> B

Folds over the queue.

Example

test {
let queue : @queue.Queue[Int] = Queue([])
let sum = queue.fold(init=0, (acc, x) => acc + x)
@test.assert_eq(sum, 0)
}

#
Queue::from_iter

#as_free_fn(from_iterator, deprecated="Use Queue::from_iter instead.")
#alias(from_iterator, deprecated="`from_iterator` is deprecated, use `from_iter` instead")
#as_free_fn
fn[A] Queue::from_iter(iter : Iter[A]) -> Queue[A]

Creates a new queue from an iter.

Example

test {
let queue : @queue.Queue[Int] = @queue.from_iter(Iter::empty())
@test.assert_eq(queue.length(), 0)
}

#
Queue::is_empty

fn[A] Queue::is_empty(self : Queue[A]) -> Bool

Checks if the queue is empty.

Example

test {
let queue : @queue.Queue[Int] = Queue([])
assert_true(queue.is_empty())
}

#
Queue::iter

#alias(iterator, deprecated="`iterator` is deprecated, use `iter` instead")
fn[A] Queue::iter(self : Queue[A]) -> Iter[A]

Creates an iter from the queue.

Example

test {
let queue : @queue.Queue[Int] = @queue.from_array([5, 6, 7, 8])
let sum = queue.iter().fold((x, y) => x + y, init=0)
@test.assert_eq(sum, 26)
}

#
Queue::length

fn[A] Queue::length(self : Queue[A]) -> Int

Get the length of the queue.

Example

test {
let queue : @queue.Queue[Int] = Queue([])
@test.assert_eq(queue.length(), 0)
}

#
Queue::new

#as_free_fn(deprecated="Use `Queue([])` instead")
#deprecated("Use `Queue([])` instead")
fn[A] Queue::new() -> Queue[A]

Creates a new empty queue.

Deprecated: use Queue([]) instead.

#
Queue::peek

fn[A] Queue::peek(self : Queue[A]) -> A?

Peeks at the first value in the queue, which returns None if the queue is empty.

Example

test {
let queue : @queue.Queue[Int] = @queue.from_array([1, 2, 3, 4])
@test.assert_eq(queue.peek(), Some(1))
}

#
Queue::pop

fn[A] Queue::pop(self : Queue[A]) -> A?

Pops the first value from the queue, which returns None if the queue is empty.

Example

test {
let queue : @queue.Queue[Int] = @queue.from_array([1, 2, 3, 4])
@test.assert_eq(queue.pop(), Some(1))
}

#
Queue::push

fn[A] Queue::push(self : Queue[A], x : A) -> Unit

Adds a value to the queue.

Example

test {
let queue : @queue.Queue[Int] = Queue([])
queue.push(1)
}

#
Queue::transfer

fn[A] Queue::transfer(self : Queue[A], dst : Queue[A]) -> Unit

Transfers all elements from one queue to another.

Adds all of the elements of source to the end of destination, then clears source. If source and destination are the same queue, this is a no-op.

Example

test {
let dst : @queue.Queue[Int] = Queue([])
let src : @queue.Queue[Int] = @queue.from_array([5, 6, 7, 8])
src.transfer(dst)
}