Skip to content

Queue

Spice
import "std/data/queue";

Queue<T> struct

A queue in Spice is a commonly used data structure, which uses the FiFo (first in, first out) principle.

Time complexity:
Insert: O(1)
Delete: O(1)
Search: O(n)

Queues pre-allocate space using an initial size and a resize factor to not have to re-allocate with every item pushed.

Constructors

ctor

Spice
public p Queue.ctor(unsigned long initAllocItems, const T& defaultValue)

Construct a queue pre-filled with a number of copies of a default value

Parameters

Name Type Description
initAllocItems unsigned long Number of items to insert
defaultValue const T& Value to fill the queue with

ctor

Spice
public p Queue.ctor(unsigned int initAllocItems)

Construct a queue, pre-allocating space for the given number of items

Parameters

Name Type Description
initAllocItems unsigned int Number of items to pre-allocate space for

ctor

Spice
public p Queue.ctor(unsigned long initAllocItems = INITIAL_CAPACITY)

Construct a queue, pre-allocating space for the given number of items

Parameters

Name Type Description
initAllocItems unsigned long Number of items to pre-allocate space for (default: INITIAL_CAPACITY)

ctor

Spice
public p Queue.ctor(const Queue<T>& original)

Construct a queue as a deep copy of another queue

Parameters

Name Type Description
original const Queue<T>& Queue to copy

Methods

push

Spice
public p Queue.push(const T& item)

Add an item at the end of the queue

Parameters

Name Type Description
item const T&

pop

Spice
public f<T&> Queue.pop()

Retrieve the first item and remove it

Returns: T& — First item

front

Spice
public f<T&> Queue.front()

Retrieve the first item without removing it from the queue

Returns: T& — First item

back

Spice
public f<T&> Queue.back()

Retrieve the last item without removing it from the queue

Returns: T& — Last item

getSize

Spice
public f<long> Queue.getSize()

Retrieve the current size of the queue

Returns: long — Current size of the queue

getCapacity

Spice
public f<long> Queue.getCapacity()

Retrieve the current capacity of the queue

Returns: long — Current capacity of the queue

isEmpty

Spice
public f<bool> Queue.isEmpty()

Checks if the queue contains any items at the moment

Returns: bool — Empty or not empty

isFull

Spice
public f<bool> Queue.isFull()

Checks if the queue exhausts its capacity and needs to resize at the next call of push

Returns: bool — Full or not full

reserve

Spice
public p Queue.reserve(unsigned long itemCount)

Reserves itemCount items

Parameters

Name Type Description
itemCount unsigned long

pack

Spice
public p Queue.pack()

Frees allocated memory that is not used by the queue

Operators

operator=

Spice
public p operator=<T>(Queue<T>& this, const Queue<T>& newValue)

Copy-assign the contents of another queue into this one

Parameters

Name Type Description
newValue const Queue<T>& Queue to copy from

operator==

Spice
public f<bool> operator==<T>(const Queue<T>& lhs, const Queue<T>& rhs)

Check if two queues are equal, i.e. they have the same size and equal contents in order

Parameters

Name Type Description
lhs const Queue<T>&
rhs const Queue<T>&

Returns: bool — Equal or not equal

operator!=

Spice
public f<bool> operator!=<T>(const Queue<T>& lhs, const Queue<T>& rhs)

Check if two queues are not equal

Parameters

Name Type Description
lhs const Queue<T>&
rhs const Queue<T>&

Returns: bool — Not equal or equal