pair

class pair#(type T1 =  int,
type T2 =  T1)

Provides a pair that carries two values, which can be different types.

Parameters

T1optional The type of the first value of a pair.  The default is int.
T2optional The type of the second value of a pair.  The default is the same type as T1.
Summary
pairProvides a pair that carries two values, which can be different types.
Types
this_typeThe shorthand of pair#(T1,T2).
Properties
firstThe first value inside the pair.
secondThe second value inside the pair.
Functions
newCreates a pair of values of types T1 and T2.
eqReturns 1 if this object is equal to the specified pair.
neReturns 1 if this object is not equal to the specified pair.
ltReturns 1 if this object is less than the specified pair.
gtReturns 1 if this object is greater than the specified pair.
leReturns 1 if this object is less than or equal to the specified pair.
geReturns 1 if this object is greater than or equal to the specified pair.
cloneCreates a pair by cloning this object.
swapSwaps the contents of this pair with the ones of the specified pair.

Types

this_type

typedef pair#(T1,
T2) this_type

The shorthand of pair#(T1,T2).

Properties

first

T1 first

The first value inside the pair.

Example

pair#(int, string) p = new( 1, "apple" );
assert( p.first == 1 );

second

T2 second

The second value inside the pair.

Example

pair#(int, string) p = new( 1, "apple" );
assert( p.second == "apple" );

Functions

new

function new(T1 first,  
T2 second,  
comparator#(this_type) cmp =  null)

Creates a pair of values of types T1 and T2.

Arguments

firstThe first value of a pair.
secondThe second value of a pair.
cmpoptional A strategy object used to compare two pairs.  If not specified or null, pair_comparator #(this_type) is used.  The default is null.

Returns

None.

Example

pair#(int, string) p = new( 1, "apple" );

eq

function bit eq(const ref this_type p)

Returns 1 if this object is equal to the specified pair.  The comparison is done by the strategy object specified at the object construction.

Argument

pA pair to compare with.

Returns

If this object is equal to p, then returns 1.  Otherwise, returns 0.

Example

pair#(int, string) p = new( 1, "apple" );
pair#(int, string) q = new( 1, "apple" );
assert( p.eq( q ) == 1 ); // 1 == 1 && "apple" == "apple"

ne

function bit ne(const ref this_type p)

Returns 1 if this object is not equal to the specified pair.  The comparison is done by the strategy object specified at the object construction.

Argument

pA pair to compare with.

Returns

If this object is not equal to p, then returns 1.  Otherwise, returns 0.

Example

pair#(int, string) p = new( 1, "apple" );
pair#(int, string) q = new( 1, "orange" );
assert( p.ne( q ) == 1 ); // "apple" != "orange"

lt

function bit lt(const ref this_type p)

Returns 1 if this object is less than the specified pair.  The comparison is done by the strategy object specified at the object construction.

Argument

pA pair to compare with.

Returns

If this object is less than p, then returns 1.  Otherwise, returns 0.

Example

pair#(int, string) p = new( 1, "apple" );
pair#(int, string) q = new( 2, "apple" );
assert( p.lt( q ) == 1 ); // 1 < 2

gt

function bit gt(const ref this_type p)

Returns 1 if this object is greater than the specified pair.  The comparison is done by the strategy object specified at the object construction.

Argument

pA pair to compare with.

Returns

If this object is greater than p, then returns 1.  Otherwise, returns 0.

Example

pair#(int, string) p = new( 1, "orange" );
pair#(int, string) q = new( 1, "apple" );
assert( p.gt( q ) == 1 ); // "orange" > "apple" by the lexicographical order

le

function bit le(const ref this_type p)

Returns 1 if this object is less than or equal to the specified pair.  The comparison is done by the strategy object specified at the object construction.

Argument

pA pair to compare with.

Returns

If this object is less than or equal to p, then returns 1.  Otherwise, returns 0.

Example

pair#(int, string) p = new( 1, "apple" );
pair#(int, string) q = new( 1, "orange" );
assert( p.le( q ) == 1 ); // "apple" < "orange" by the lexicographical order

ge

function bit ge(const ref this_type p)

Returns 1 if this object is greater than or equal to the specified pair.  The comparison is done by the strategy object specified at the object construction.

Argument

pA pair to compare with.

Returns

If this object is greater than or equal to p, then returns 1.  Otherwise, returns 0.

Example

pair#(int, string) p = new( 2, "apple" );
pair#(int, string) q = new( 1, "orange" );
assert( p.ge( q ) == 1 ); // 2 > 1

clone

function this_type clone()

Creates a pair by cloning this object.

Returns

A cloned pair.

Example

pair#(int, string) p = new( 1, "apple" );
pair#(int, string) q = p.clone();
assert( p.eq( q ) == 1 );

swap

function void swap(ref this_type p)

Swaps the contents of this pair with the ones of the specified pair.

Argument

pA pair to swap the contents.

Example

pair#(int, string) p = new( 1, "apple" );
pair#(int, string) q = new( 2, "orange" );
p.swap( q );
assert( p.first == 2 );
assert( q.first == 1 );
assert( p.second == "orange" );
assert( q.second == "apple" );
class pair#(type T1 =  int,
type T2 =  T1)
Provides a pair that carries two values, which can be different types.
typedef pair#(T1,
T2) this_type
The shorthand of pair#(T1,T2).
T1 first
The first value inside the pair.
T2 second
The second value inside the pair.
function new(T1 first,  
T2 second,  
comparator#(this_type) cmp =  null)
Creates a pair of values of types T1 and T2.
function bit eq(const ref this_type p)
Returns 1 if this object is equal to the specified pair.
function bit ne(const ref this_type p)
Returns 1 if this object is not equal to the specified pair.
function bit lt(const ref this_type p)
Returns 1 if this object is less than the specified pair.
function bit gt(const ref this_type p)
Returns 1 if this object is greater than the specified pair.
function bit le(const ref this_type p)
Returns 1 if this object is less than or equal to the specified pair.
function bit ge(const ref this_type p)
Returns 1 if this object is greater than or equal to the specified pair.
function this_type clone()
Creates a pair by cloning this object.
function void swap(ref this_type p)
Swaps the contents of this pair with the ones of the specified pair.
class pair_comparator#(type T =  pair) extends comparator#(T)
singleton Provides strategies to compare pairs.