set

class set #(type T =  int) extends set_base#( T )

Implements the set_base using an associative array.

Parameter

Toptional The type of data collected in a set.  The default is int.
Summary
setImplements the set_base using an associative array.
Functions
newCreates a new set.
addvirtual Adds the given element to this set if it is not already present.
clearvirtual Removes all of the elements from this set.
clonevirtual Returns a shallow copy of this set.
containsvirtual Returns 1 if this set contains the specified element.
is_emptyvirtual Returns 1 if this set contains no elements.
get_iteratorvirtual Returns an iterator over the elements in this set.
removevirtual Removes the given element from this set if it is present.
sizevirtual Returns the number of elements in this set.

Functions

new

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

Creates a new set.

Argument

coptional A collection whose elements are to be added to this set.
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

set#(int) int_set = new();

add

virtual function bit add(e)

virtual Adds the given element to this set if it is not already present.

Argument

eAn element to be added to this set.

Returns

If this set did not contain the given element, returns 1.  Otherwise, returns 0.

Example

set#(int) int_set = new();

assert( int_set.add( 123 ) == 1 );
assert( int_set.add( 123 ) == 0 ); // 123 is already in the set

clear

virtual function void clear()

virtual Removes all of the elements from this set.

Example

set#(int) int_set = new();

int_set.clear();

clone

virtual function collection#(T) clone()

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

Returns

A copy of this set.

Example

set#(int) int_set = new();
collection#(int) cloned;

cloned = int_set.clone();

contains

virtual function bit contains(e)

virtual Returns 1 if this set contains the specified element.

Argument

eAn element to be checked.

Returns

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

Example

set#(int) int_set = new();

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

is_empty

virtual function bit is_empty()

virtual Returns 1 if this set contains no elements.

Returns

If this set is empty, returns 1.  Otherwise, returns 0.

Example

set#(int) int_set = new();

assert( int_set.is_empty() == 1 );
void'( int_set.add( 123 ) );
assert( int_set.is_empty() == 0 );

get_iterator

virtual function iterator#(T) get_iterator()

virtual Returns an iterator over the elements in this set.

Returns

An iterator.

Example

set#(int) int_set = new();
iterator#(int) it;
string s;

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

remove

virtual function bit remove(e)

virtual Removes the given element from this set if it is present.

Argument

eAn element to remove.

Returns

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

Example

set#(int) int_set = new();

void'( int_set.add( 123 ) );
assert( int_set.remove( 123 ) == 1 );
assert( int_set.remove( 123 ) == 0 ); // already removed

size

virtual function int size()

virtual Returns the number of elements in this set.

Returns

The number of elements in this set.

Example

set#(int) int_set = new();

void'( int_set.add( 123 ) );
assert( int_set.size() == 1 );
class set #(type T =  int) extends set_base#( T )
Implements the set_base using an associative array.
virtual class set_base #(type T =  int) extends collection#( T )
virtual Defines the core functionality of a set.
function new(collection#(T) c =  null,
comparator#(T) cmp =  null,
formatter#(T) fmtr =  null)
Creates a new set.
virtual function bit add(e)
virtual Adds the given element to this set if it is not already present.
virtual function void clear()
virtual Removes all of the elements from this set.
virtual function collection#(T) clone()
virtual Returns a shallow copy of this set.
virtual function bit contains(e)
virtual Returns 1 if this set contains the specified element.
virtual function bit is_empty()
virtual Returns 1 if this set contains no elements.
virtual function iterator#(T) get_iterator()
virtual Returns an iterator over the elements in this set.
virtual function bit remove(e)
virtual Removes the given element from this set if it is present.
virtual function int size()
virtual Returns the number of elements in this set.
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.