deque

class deque #(type T =  int) extends collection#( T )

Implements a double-ended queue using a queue.

Parameter

Toptional The type of data collected in a deque.  The default is int.
Summary
dequeImplements a double-ended queue using a queue.
Functions
newCreates a new deque.
addvirtual Adds the given element at the end of this deque.
add_firstvirtual Adds the given element at the front of this deque.
add_lastvirtual Adds the given element at the end of this deque.
clearvirtual Removes all of the elements from this deque.
clonevirtual Returns a shallow copy of this deque.
containsvirtual Returns 1 if this deque contains the specified element.
getvirtual Retrieves the head of the deque and remove the element.
get_firstvirtual Retrieves the head of the deque and remove the element.
get_lastvirtual Retrieves the tail of the deque and remove the element.
get_iteratorvirtual Returns an iterator over the elements in this deque.
get_descending_iteratorvirtual Returns an iterator over the elements in this deque in reverse order.
peekvirtual Retrieves the head of the deque but does not remove the element.
peek_firstvirtual Retrieves the head of the deque but does not remove the element.
peek_lastvirtual Retrieves the tail of the deque but does not remove the element.
popvirtual Pops an element from the deque as if it is a stack.
pushvirtual Pushes an element to the deque as if it is a stack.
removevirtual Removes the specified element from this deque.
remove_firstvirtual Removes the first element from this deque.
remove_lastvirtual Removes the last element from this deque.
remove_first_occurrencevirtual Removes the first occurrence of the specified element by traversing the deque from head to tail.
remove_last_occurrencevirtual Removes the last occurrence of the specified element by traversing the deque from tail to head.
sizevirtual Returns the number of elements in this deque.

Functions

new

function new(collection#(T) c =  null,
comparator#(T) cmp =  null,
formatter#(T) fmtr =  null)

Creates a new deque.

Argument

coptional A collection whose elements are to be added to this deque.
cmpoptional A strategy object used to compare the elements of type T.  If not specified or null, comparator #(T) is used.  The default is null.
fmtroptional A strategy object that provides a function to convert the element of type T to a string.  If not specified or null, hex_formatter #(T) is used.  The default is null.

Example

deque#(int) int_dq = new();

add

virtual function bit add(e)

virtual Adds the given element at the end of this deque.

Argument

eAn element to be added to this deque.

Returns

Always returns 1.

Example

deque#(int) int_dq = new();

assert( int_dq.add( 123 ) == 1 );

add_first

virtual function void add_first(e)

virtual Adds the given element at the front of this deque.

Argument

eAn element to be added to this deque.

Returns

None.

Example

deque#(int) int_dq = new();

int_dq.add_first( 123 );

add_last

virtual function void add_last(e)

virtual Adds the given element at the end of this deque.

Argument

eAn element to be added to this deque.

Returns

None.

Example

deque#(int) int_dq = new();

int_dq.add_last( 123 );

clear

virtual function void clear()

virtual Removes all of the elements from this deque.

Returns

None.

Example

deque#(int) int_dq = new();

int_dq.clear();

clone

virtual function collection#(T) clone()

virtual Returns a shallow copy of this deque.  The element themselves are not cloned.

Returns

A copy of this deque.

Example

deque#(int) int_dq = new();
collection#(int) cloned;

cloned = int_dq.clone();

contains

virtual function bit contains(e)

virtual Returns 1 if this deque contains the specified element.

Argument

eAn element to be checked.

Returns

If this deque contains e, returns 1.  Otherwise, returns 0.

Example

deque#(int) int_dq = new();

void'( int_dq.add( 123 ) );
assert( int_dq.contains( 123 ) == 1 );
assert( int_dq.contains( 456 ) == 0 );

get

virtual function bit get(ref e)

virtual Retrieves the head of the deque and remove the element.  This function is equivalent to get_first.

Argument

eThe head of the deque to be returned.

Returns

If e is valid, returns 1.  Otherwise, returns 0.

Example

deque#(int) int_dq = new();
int i;

void'( int_dq.add( 123 ) );
assert( int_dq.get( i ) == 1 );
assert( i == 123 );

See Also

get_first

get_first

virtual function bit get_first(ref e)

virtual Retrieves the head of the deque and remove the element.

Argument

eThe head of the deque to be returned.

Returns

If e is valid, returns 1.  Otherwise, returns 0.

Example

deque#(int) int_dq = new();
int i;

void'( int_dq.add( 123 ) );
assert( int_dq.get_first( i ) == 1 );
assert( i == 123 );

See Also

get

get_last

virtual function bit get_last(ref e)

virtual Retrieves the tail of the deque and remove the element.

Argument

eThe tail of the deque to be returned.

Returns

If e is valid, returns 1.  Otherwise, returns 0.

Example

deque#(int) int_dq = new();
int i;

void'( int_dq.add( 123 ) );
assert( int_dq.get_last( i ) == 1 );
assert( i == 123 );

See Also

get_first

get_iterator

virtual function iterator#(T) get_iterator()

virtual Returns an iterator over the elements in this deque.

Returns

An iterator.

Example

deque#(int) int_dq = new();
iterator#(int) it;
string s;

void'( int_dq.add( 123 ) );
void'( int_dq.add( 456 ) );
it = int_dq.get_iterator();
while ( it.has_next() ) s = { s, $sformatf( "%0d ", it.next() ) };
assert( s == "123 456 " );

get_descending_iterator

virtual function iterator#(T) get_descending_iterator()

virtual Returns an iterator over the elements in this deque in reverse order.

Returns

A descending iterator.

Example

deque#(int) int_dq = new();
iterator#(int) it;
string s;

void'( int_dq.add( 123 ) );
void'( int_dq.add( 456 ) );
it = int_dq.get_descending_iterator();
while ( it.has_next() ) s = { s, $sformatf( "%0d ", it.next() ) };
assert( s == "456 123 " );

peek

virtual function bit peek(ref e)

virtual Retrieves the head of the deque but does not remove the element.  This function is equivalent to peek_first.

Argument

eThe head of the deque to be returned.

Returns

If e is valid, returns 1.  Otherwise, returns 0.

Example

deque#(int) int_dq = new();
int i;

void'( int_dq.add( 123 ) );
void'( int_dq.add( 456 ) );
assert( int_dq.peek( i ) == 1 );
assert( i == 123 );

See Also

peek_first

peek_first

virtual function bit peek_first(ref e)

virtual Retrieves the head of the deque but does not remove the element.

Argument

eThe head of the deque to be returned.

Returns

If e is valid, returns 1.  Otherwise, returns 0.

Example

deque#(int) int_dq = new();
int i;

void'( int_dq.add( 123 ) );
void'( int_dq.add( 456 ) );
assert( int_dq.peek_first( i ) == 1 );
assert( i == 123 );

See Also

peek, peek_last

peek_last

virtual function bit peek_last(ref e)

virtual Retrieves the tail of the deque but does not remove the element.

Argument

eThe tail of the deque to be returned.

Returns

If e is valid, returns 1.  Otherwise, returns 0.

Example

deque#(int) int_dq = new();
int i;

void'( int_dq.add( 123 ) );
void'( int_dq.add( 456 ) );
assert( int_dq.peek_last( i ) == 1 );
assert( i == 456 );

See Also

peek_first

pop

virtual function T pop()

virtual Pops an element from the deque as if it is a stack.

Returns

The head of the deque.  If the deque is empty, the value of type T read from a nonexistent queue entry is returned (See IEEE Std 1800-2012 Table 7-1).

Example

deque#(int) int_dq = new();

int_dq.push( 123 );
int_dq.push( 456 );
assert( int_dq.pop() == 456 );

push

virtual function void push(e)

virtual Pushes an element to the deque as if it is a stack.

Argument

eAn element to push.

Returns

None.

Example

deque#(int) int_dq = new();

int_dq.push( 123 );

remove

virtual function bit remove(e)

virtual Removes the specified element from this deque.  This function is equivalent to remove_first_occurrence.

Argument

eAn element to be removed.

Returns

If this deque changed as a result of the call, 1 is returned.  Otherwise, 0 is returned.

Example

deque#(int) int_dq = new();

void'( int_dq.add( 123 ) );
void'( int_dq.add( 456 ) );
assert( int_dq.remove( 123 ) == 1 );
assert( int_dq.remove( 789 ) == 0 );

See Also

<remove_first_occurence>

remove_first

virtual function bit remove_first()

virtual Removes the first element from this deque.

Returns

If this deque is not empty, 1 is returned.  Otherwise, 0 is returned.

Example

deque#(int) int_dq = new();

void'( int_dq.add( 123 ) );
assert( int_dq.remove_first() == 1 );
assert( int_dq.remove_first() == 0 ); // deque is empty

remove_last

virtual function bit remove_last()

virtual Removes the last element from this deque.

Returns

If this deque is not empty, 1 is returned.  Otherwise, 0 is returned.

Example

deque#(int) int_dq = new();

void'( int_dq.add( 123 ) );
assert( int_dq.remove_last() == 1 );
assert( int_dq.remove_last() == 0 ); // deque is empty

remove_first_occurrence

virtual function bit remove_first_occurrence(e)

virtual Removes the first occurrence of the specified element by traversing the deque from head to tail.

Argument

eAn element to be removed.

Returns

If this deque changed as a result of the call, 1 is returned.  Otherwise, 0 is returned.

Example

deque#(int) int_dq = new();

void'( int_dq.add( 123 ) );
void'( int_dq.add( 456 ) );
assert( int_dq.remove_first_occurrence( 123 ) == 1 );
assert( int_dq.remove_first_occurrence( 789 ) == 0 );

See Also

remove, remove_last_occurrence

remove_last_occurrence

virtual function bit remove_last_occurrence(e)

virtual Removes the last occurrence of the specified element by traversing the deque from tail to head.

Argument

eAn element to be removed.

Returns

If this deque changed as a result of the call, 1 is returned.  Otherwise, 0 is returned.

Example

deque#(int) int_dq = new();

void'( int_dq.add( 123 ) );
void'( int_dq.add( 456 ) );
assert( int_dq.remove_last_occurrence( 123 ) == 1 );
assert( int_dq.remove_last_occurrence( 789 ) == 0 );

See Also

remove, remove_first_occurrence

size

virtual function int size()

virtual Returns the number of elements in this deque.

Returns

The number of elements in this deque.

Example

deque#(int) int_dq = new();

void'( int_dq.add( 123 ) );
assert( int_dq.size() == 1 );
class deque #(type T =  int) extends collection#( T )
Implements a double-ended queue using a queue.
function new(collection#(T) c =  null,
comparator#(T) cmp =  null,
formatter#(T) fmtr =  null)
Creates a new deque.
virtual function bit add(e)
virtual Adds the given element at the end of this deque.
virtual function void add_first(e)
virtual Adds the given element at the front of this deque.
virtual function void add_last(e)
virtual Adds the given element at the end of this deque.
virtual function void clear()
virtual Removes all of the elements from this deque.
virtual function collection#(T) clone()
virtual Returns a shallow copy of this deque.
virtual function bit contains(e)
virtual Returns 1 if this deque contains the specified element.
virtual function bit get(ref e)
virtual Retrieves the head of the deque and remove the element.
virtual function bit get_first(ref e)
virtual Retrieves the head of the deque and remove the element.
virtual function bit get_last(ref e)
virtual Retrieves the tail of the deque and remove the element.
virtual function iterator#(T) get_iterator()
virtual Returns an iterator over the elements in this deque.
virtual function iterator#(T) get_descending_iterator()
virtual Returns an iterator over the elements in this deque in reverse order.
virtual function bit peek(ref e)
virtual Retrieves the head of the deque but does not remove the element.
virtual function bit peek_first(ref e)
virtual Retrieves the head of the deque but does not remove the element.
virtual function bit peek_last(ref e)
virtual Retrieves the tail of the deque but does not remove the element.
virtual function T pop()
virtual Pops an element from the deque as if it is a stack.
virtual function void push(e)
virtual Pushes an element to the deque as if it is a stack.
virtual function bit remove(e)
virtual Removes the specified element from this deque.
virtual function bit remove_first()
virtual Removes the first element from this deque.
virtual function bit remove_last()
virtual Removes the last element from this deque.
virtual function bit remove_first_occurrence(e)
virtual Removes the first occurrence of the specified element by traversing the deque from head to tail.
virtual function bit remove_last_occurrence(e)
virtual Removes the last occurrence of the specified element by traversing the deque from tail to head.
virtual function int size()
virtual Returns the number of elements in this deque.
class comparator#(type T =  int)
singleton Provides strategies to compare objects.
class hex_formatter #(type T =  int) extends formatter#( T )
singleton Provides a strategy to convert an object of type T to a string using a hexadecimal format.