- Joined
- Mar 19, 2008
- Messages
- 3,141
Implementation of std:air from utilities library. Pair is specialized tuple, which always holds two elements which can have different types.
Demo:
JASS:
/*****************************************************************************
*
* Pair<T> v1.0.0.1
* by Bannar aka Spinnaker
*
* Binary tuple, i.e. a pair of values.
*
******************************************************************************
*
* Implementation:
*
* macro DEFINE_PAIR takes NAME, TYPE1, TYPE1
*
* NAME - sets name of pair type
* TYPE1 - type of first element of pair
* TYPE2 - type of second element of pair
*
******************************************************************************
*
* functions:
*
* function Make<Pair> takes <type1> f, <type2> s returns <Pair>
* creates pair object, deducing the target type from the types of arguments
*
*
* struct API:
*
* <type1> first
* <type2> second
* static constant string firstType
* static constant string secondType
*
* static method create takes $TYPE1$ f, $TYPE2$ s returns thistype
* default ctor
*
* static method operator [] takes thistype p returns thistype
* copy ctor
*
* method destroy takes nothing returns nothing
* default dctor
*
* method operator< takes thistype p returns boolean
* defines lessThen and greaterThen operations for pair objects
*
* method operator== takes thistype p returns boolean
* defines equalTo operation for pair objects
*
* method size takes nothing returns integer
* returns size of pair which is always 2
*
* method swap takes thistype p returns nothing
* swaps two pair objects
*
******************************************************************************/
library PairTemplate
//! textmacro DEFINE_PAIR takes NAME, TYPE1, TYPE2
struct $NAME$ extends array
$TYPE1$ first
$TYPE2$ second
static constant string firstType = "$TYPE1$"
static constant string secondType = "$TYPE2$"
private static integer count = 0
private thistype recycle
private static method allocate takes nothing returns thistype
local thistype this = thistype(0).recycle
if this == 0 then
set count = count + 1
set this = count
else
set thistype(0).recycle = this.recycle
endif
return this
endmethod
static method create takes $TYPE1$ f, $TYPE2$ s returns thistype
local thistype this = thistype.allocate()
set this.first = f
set this.second = s
return this
endmethod
// copy ctor
static method operator [] takes thistype p returns thistype
return thistype.create(p.first, p.second)
endmethod
method destroy takes nothing returns nothing
set this.recycle = thistype(0).recycle
set thistype(0).recycle = this
endmethod
method operator== takes thistype p returns boolean
return ( first == p.first ) and ( second == p.second )
endmethod
method size takes nothing returns integer
return 2
endmethod
method swap takes thistype p returns nothing
local $TYPE1$ f = first
local $TYPE2$ s = second
set first = p.first
set second = p.second
set p.first = f
set p.second = s
endmethod
endstruct
function Make$NAME$ takes $TYPE1$ f, $TYPE2$ s returns $NAME$
return $NAME$.create(f, s)
endfunction
//! endtextmacro
endlibrary
JASS:
//! runtextmacro DEFINE_PAIR("IntegerPair", "integer", "integer")
//! runtextmacro DEFINE_PAIR("StrAndIntPair", "string", "integer")
struct test_pair_struct extends array
private static method onInit takes nothing returns nothing
local IntegerPair p = IntegerPair.create(0, -1)
local StrAndIntPair q = StrAndIntPair.create("Joe", 3)
local IntegerPair c = IntegerPair.create(88, 13891)
local StrAndIntPair b = StrAndIntPair.create("Leah", 5)
call p.swap(c)
call q.swap(b)
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Element second of pair (p): " + I2S(p.second))
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Element first of pair (b): " + b.first)
endmethod
endstruct
Last edited: