Sgqvur
FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
- Reaction score
- 62
Allows sorting of arbitrary type arrays (+ bonus printing them). The "algorithm" used is called "Selection Sort" I think.
All functions/textmacros work in the range [a, b)
The comparison function which is required by the sort_handles_<asc|desc> functions and is user defined and is of the form
[ljass]function <comparison_function_name> takes <type v1, type v2> returns boolean[/ljass]
Example:
[ljass]function comp_unit_hp takes unit u1, unit u2 returns boolean[/ljass]
[ljass] return GetWidgetLife(u1) > GetWidgetLife(u2)[/ljass]
[ljass]endfunction[/ljass]
The suffix _asc means sort in ascending order (0, 1, 2, 3, 4)
The suffix _desc means sort in descending order (4, 3, 2, 1, 0)
"Interface":
For arrays of type integer and real:
[ljass]//! runtextmacro sort_numbers_asc takes string array_name, string a, string b[/ljass]
[ljass]//! runtextmacro sort_numbers_desc takes string array_name, string a, string b[/ljass]
[ljass]//! runtextmacro sort_strings_asc takes string array_name, string a, string b[/ljass]
[ljass]//! runtextmacro sort_strings_desc takes string array_name, string a, string b[/ljass]
[ljass]//! runtextmacro sort_handles_asc takes string array_name, string a, string b, string comparison_function_name, string type_name[/ljass]
[ljass]//! runtextmacro sort_handles_desc takes string array_name, string a, string b, string comparison_function_name, string type_name[/ljass]
The say_a function/textmacro stands for "say_array" (say is an easy to type name for printing used in Perl and maybe some other programming languages)
The last parameter is the conversion function's name that converts the type of the array to string. (example: I2S, R2S, S2S (Yes string 2 string =)), GetUnitName, etc.)
[ljass]//! runtextmacro say_a takes takes string array_name, string a, string b, string to_string_function_name[/ljass]
Group to array conversion that does not alter the passed group
[ljass]//! runtextmacro group2array takes string group_name, string array_name[/ljass]
Note:
Sorting big arrays (size > 50 [huge eh? =)] is not recommended especially for sorting strings becuase it is very likely to hit the oplimit count.
Why will sorting hit the oplimit? Well first of all the sorting functions/textmacros inline in the functions/methods that use them, second
the algorithm used is probably one of the worst because it requires a lot of swapping/checking, so sorting will eat your oplimit count and that's a fact.
Why not use a better sorting then? Well simply because a proper fast sorting algorithm is hard to do right and in my case I really don't know how.
Some folks proposed in the comments below to use quicksort, which is a better sorting method but it seams it's implementation (the ones I finds at least)
require functions calls and this is not practical in the case of [v]Jass[2] because arrays cannot be passed as arguments hence the reason to inline the sorting
routine. If someone knows a faster algorithm that inlines (does not require functions calls/array passing), then he might be able to write a much better sorting
routines. Although you might get away with integer/real type arrays of 50 <= size <= 150.
Example how to use:
All functions/textmacros work in the range [a, b)
The comparison function which is required by the sort_handles_<asc|desc> functions and is user defined and is of the form
[ljass]function <comparison_function_name> takes <type v1, type v2> returns boolean[/ljass]
Example:
[ljass]function comp_unit_hp takes unit u1, unit u2 returns boolean[/ljass]
[ljass] return GetWidgetLife(u1) > GetWidgetLife(u2)[/ljass]
[ljass]endfunction[/ljass]
The suffix _asc means sort in ascending order (0, 1, 2, 3, 4)
The suffix _desc means sort in descending order (4, 3, 2, 1, 0)
"Interface":
For arrays of type integer and real:
[ljass]//! runtextmacro sort_numbers_asc takes string array_name, string a, string b[/ljass]
[ljass]//! runtextmacro sort_numbers_desc takes string array_name, string a, string b[/ljass]
[ljass]//! runtextmacro sort_strings_asc takes string array_name, string a, string b[/ljass]
[ljass]//! runtextmacro sort_strings_desc takes string array_name, string a, string b[/ljass]
[ljass]//! runtextmacro sort_handles_asc takes string array_name, string a, string b, string comparison_function_name, string type_name[/ljass]
[ljass]//! runtextmacro sort_handles_desc takes string array_name, string a, string b, string comparison_function_name, string type_name[/ljass]
The say_a function/textmacro stands for "say_array" (say is an easy to type name for printing used in Perl and maybe some other programming languages)
The last parameter is the conversion function's name that converts the type of the array to string. (example: I2S, R2S, S2S (Yes string 2 string =)), GetUnitName, etc.)
[ljass]//! runtextmacro say_a takes takes string array_name, string a, string b, string to_string_function_name[/ljass]
Group to array conversion that does not alter the passed group
[ljass]//! runtextmacro group2array takes string group_name, string array_name[/ljass]
Note:
Sorting big arrays (size > 50 [huge eh? =)] is not recommended especially for sorting strings becuase it is very likely to hit the oplimit count.
Why will sorting hit the oplimit? Well first of all the sorting functions/textmacros inline in the functions/methods that use them, second
the algorithm used is probably one of the worst because it requires a lot of swapping/checking, so sorting will eat your oplimit count and that's a fact.
Why not use a better sorting then? Well simply because a proper fast sorting algorithm is hard to do right and in my case I really don't know how.
Some folks proposed in the comments below to use quicksort, which is a better sorting method but it seams it's implementation (the ones I finds at least)
require functions calls and this is not practical in the case of [v]Jass[2] because arrays cannot be passed as arguments hence the reason to inline the sorting
routine. If someone knows a faster algorithm that inlines (does not require functions calls/array passing), then he might be able to write a much better sorting
routines. Although you might get away with integer/real type arrays of 50 <= size <= 150.
JASS:
library TcA2II2A
globals
private hashtable a2i_map = InitHashtable()
private string a2i_string = ""
private integer a2i_integer = 0
private string array i2a_map
private integer i2a_integer = 0
private string i2a_string = ""
endglobals
private function SH2 takes string s returns integer
if s == StringCase(s, true) then
return StringHash(s)
else
return StringHash(s) + 1
endif
endfunction
module a2i_loader
private static method onInit takes nothing returns nothing
call SaveInteger(a2i_map, 0x100, SH2(" "), 32)
call SaveInteger(a2i_map, 0x100, SH2("!"), 33)
call SaveInteger(a2i_map, 0x100, SH2("\""), 34)
call SaveInteger(a2i_map, 0x100, SH2("#"), 35)
call SaveInteger(a2i_map, 0x100, SH2("$"), 36)
call SaveInteger(a2i_map, 0x100, SH2("%"), 37)
call SaveInteger(a2i_map, 0x100, SH2("&"), 38)
call SaveInteger(a2i_map, 0x100, SH2("'"), 39)
call SaveInteger(a2i_map, 0x100, SH2("("), 40)
call SaveInteger(a2i_map, 0x100, SH2(")"), 41)
call SaveInteger(a2i_map, 0x100, SH2("*"), 42)
call SaveInteger(a2i_map, 0x100, SH2("+"), 43)
call SaveInteger(a2i_map, 0x100, SH2(","), 44)
call SaveInteger(a2i_map, 0x100, SH2("-"), 45)
call SaveInteger(a2i_map, 0x100, SH2("."), 46)
call SaveInteger(a2i_map, 0x100, SH2("/"), 47)
call SaveInteger(a2i_map, 0x100, SH2("0"), 48)
call SaveInteger(a2i_map, 0x100, SH2("1"), 49)
call SaveInteger(a2i_map, 0x100, SH2("2"), 50)
call SaveInteger(a2i_map, 0x100, SH2("3"), 51)
call SaveInteger(a2i_map, 0x100, SH2("4"), 52)
call SaveInteger(a2i_map, 0x100, SH2("5"), 53)
call SaveInteger(a2i_map, 0x100, SH2("6"), 54)
call SaveInteger(a2i_map, 0x100, SH2("7"), 55)
call SaveInteger(a2i_map, 0x100, SH2("8"), 56)
call SaveInteger(a2i_map, 0x100, SH2("9"), 57)
call SaveInteger(a2i_map, 0x100, SH2(":"), 58)
call SaveInteger(a2i_map, 0x100, SH2(";"), 59)
call SaveInteger(a2i_map, 0x100, SH2("<"), 60)
call SaveInteger(a2i_map, 0x100, SH2("="), 61)
call SaveInteger(a2i_map, 0x100, SH2(">"), 62)
call SaveInteger(a2i_map, 0x100, SH2("?"), 63)
call SaveInteger(a2i_map, 0x100, SH2("@"), 64)
call SaveInteger(a2i_map, 0x100, SH2("A"), 65)
call SaveInteger(a2i_map, 0x100, SH2("B"), 66)
call SaveInteger(a2i_map, 0x100, SH2("C"), 67)
call SaveInteger(a2i_map, 0x100, SH2("D"), 68)
call SaveInteger(a2i_map, 0x100, SH2("E"), 69)
call SaveInteger(a2i_map, 0x100, SH2("F"), 70)
call SaveInteger(a2i_map, 0x100, SH2("G"), 71)
call SaveInteger(a2i_map, 0x100, SH2("H"), 72)
call SaveInteger(a2i_map, 0x100, SH2("I"), 73)
call SaveInteger(a2i_map, 0x100, SH2("J"), 74)
call SaveInteger(a2i_map, 0x100, SH2("K"), 75)
call SaveInteger(a2i_map, 0x100, SH2("L"), 76)
call SaveInteger(a2i_map, 0x100, SH2("M"), 77)
call SaveInteger(a2i_map, 0x100, SH2("N"), 78)
call SaveInteger(a2i_map, 0x100, SH2("O"), 79)
call SaveInteger(a2i_map, 0x100, SH2("P"), 80)
call SaveInteger(a2i_map, 0x100, SH2("Q"), 81)
call SaveInteger(a2i_map, 0x100, SH2("R"), 82)
call SaveInteger(a2i_map, 0x100, SH2("S"), 83)
call SaveInteger(a2i_map, 0x100, SH2("T"), 84)
call SaveInteger(a2i_map, 0x100, SH2("U"), 85)
call SaveInteger(a2i_map, 0x100, SH2("V"), 86)
call SaveInteger(a2i_map, 0x100, SH2("W"), 87)
call SaveInteger(a2i_map, 0x100, SH2("X"), 88)
call SaveInteger(a2i_map, 0x100, SH2("Y"), 89)
call SaveInteger(a2i_map, 0x100, SH2("Z"), 90)
call SaveInteger(a2i_map, 0x100, SH2("["), 91)
call SaveInteger(a2i_map, 0x100, SH2("\\"), 92)
call SaveInteger(a2i_map, 0x100, SH2("]"), 93)
call SaveInteger(a2i_map, 0x100, SH2("^"), 94)
call SaveInteger(a2i_map, 0x100, SH2("_"), 95)
call SaveInteger(a2i_map, 0x100, SH2("`"), 96)
call SaveInteger(a2i_map, 0x100, SH2("a"), 97)
call SaveInteger(a2i_map, 0x100, SH2("b"), 98)
call SaveInteger(a2i_map, 0x100, SH2("c"), 99)
call SaveInteger(a2i_map, 0x100, SH2("d"), 100)
call SaveInteger(a2i_map, 0x100, SH2("e"), 101)
call SaveInteger(a2i_map, 0x100, SH2("f"), 102)
call SaveInteger(a2i_map, 0x100, SH2("g"), 103)
call SaveInteger(a2i_map, 0x100, SH2("h"), 104)
call SaveInteger(a2i_map, 0x100, SH2("i"), 105)
call SaveInteger(a2i_map, 0x100, SH2("j"), 106)
call SaveInteger(a2i_map, 0x100, SH2("k"), 107)
call SaveInteger(a2i_map, 0x100, SH2("l"), 108)
call SaveInteger(a2i_map, 0x100, SH2("m"), 109)
call SaveInteger(a2i_map, 0x100, SH2("n"), 110)
call SaveInteger(a2i_map, 0x100, SH2("o"), 111)
call SaveInteger(a2i_map, 0x100, SH2("p"), 112)
call SaveInteger(a2i_map, 0x100, SH2("q"), 113)
call SaveInteger(a2i_map, 0x100, SH2("r"), 114)
call SaveInteger(a2i_map, 0x100, SH2("s"), 115)
call SaveInteger(a2i_map, 0x100, SH2("t"), 116)
call SaveInteger(a2i_map, 0x100, SH2("u"), 117)
call SaveInteger(a2i_map, 0x100, SH2("v"), 118)
call SaveInteger(a2i_map, 0x100, SH2("w"), 119)
call SaveInteger(a2i_map, 0x100, SH2("x"), 120)
call SaveInteger(a2i_map, 0x100, SH2("y"), 121)
call SaveInteger(a2i_map, 0x100, SH2("z"), 122)
call SaveInteger(a2i_map, 0x100, SH2("{"), 123)
call SaveInteger(a2i_map, 0x100, SH2("|"), 124)
call SaveInteger(a2i_map, 0x100, SH2("}"), 125)
call SaveInteger(a2i_map, 0x100, SH2("~"), 126)
endmethod
endmodule
struct a2i_loader_dummy
implement a2i_loader
endstruct
function sub_A2I takes nothing returns nothing
if 1 == StringLength(a2i_string) then
set a2i_integer = LoadInteger(a2i_map, 0x100, SH2(a2i_string))
else
set a2i_integer = LoadInteger(a2i_map, 0x100, SH2(SubString(a2i_string, 0, 1))) * 0x1000000 + /*
*/ LoadInteger(a2i_map, 0x100, SH2(SubString(a2i_string, 1, 2))) * 0x10000 + /*
*/ LoadInteger(a2i_map, 0x100, SH2(SubString(a2i_string, 2, 3))) * 0x100 + /*
*/ LoadInteger(a2i_map, 0x100, SH2(SubString(a2i_string, 3, 4)))
endif
endfunction
function A2I takes string a returns integer
set a2i_string = a
call ExecuteFunc("sub_A2I")
return a2i_integer
endfunction
module i2a_loader
private static method onInit takes nothing returns nothing
set i2a_map[32] = " "
set i2a_map[33] = "!"
set i2a_map[34] = "\""
set i2a_map[35] = "#"
set i2a_map[36] = "$"
set i2a_map[37] = "%"
set i2a_map[38] = "&"
set i2a_map[39] = "'"
set i2a_map[40] = "("
set i2a_map[41] = ")"
set i2a_map[42] = "*"
set i2a_map[43] = "+"
set i2a_map[44] = ","
set i2a_map[45] = "-"
set i2a_map[46] = "."
set i2a_map[47] = "/"
set i2a_map[48] = "0"
set i2a_map[49] = "1"
set i2a_map[50] = "2"
set i2a_map[51] = "3"
set i2a_map[52] = "4"
set i2a_map[53] = "5"
set i2a_map[54] = "6"
set i2a_map[55] = "7"
set i2a_map[56] = "8"
set i2a_map[57] = "9"
set i2a_map[58] = ":"
set i2a_map[59] = ";"
set i2a_map[60] = "<"
set i2a_map[61] = "="
set i2a_map[62] = ">"
set i2a_map[63] = "?"
set i2a_map[64] = "@"
set i2a_map[65] = "A"
set i2a_map[66] = "B"
set i2a_map[67] = "C"
set i2a_map[68] = "D"
set i2a_map[69] = "E"
set i2a_map[70] = "F"
set i2a_map[71] = "G"
set i2a_map[72] = "H"
set i2a_map[73] = "I"
set i2a_map[74] = "J"
set i2a_map[75] = "K"
set i2a_map[76] = "L"
set i2a_map[77] = "M"
set i2a_map[78] = "N"
set i2a_map[79] = "O"
set i2a_map[80] = "P"
set i2a_map[81] = "Q"
set i2a_map[82] = "R"
set i2a_map[83] = "S"
set i2a_map[84] = "T"
set i2a_map[85] = "U"
set i2a_map[86] = "V"
set i2a_map[87] = "W"
set i2a_map[88] = "X"
set i2a_map[89] = "Y"
set i2a_map[90] = "Z"
set i2a_map[91] = "["
set i2a_map[92] = "\\"
set i2a_map[93] = "]"
set i2a_map[94] = "^"
set i2a_map[95] = "_"
set i2a_map[96] = "`"
set i2a_map[97] = "a"
set i2a_map[98] = "b"
set i2a_map[99] = "c"
set i2a_map[100] = "d"
set i2a_map[101] = "e"
set i2a_map[102] = "f"
set i2a_map[103] = "g"
set i2a_map[104] = "h"
set i2a_map[105] = "i"
set i2a_map[106] = "j"
set i2a_map[107] = "k"
set i2a_map[108] = "l"
set i2a_map[109] = "m"
set i2a_map[110] = "n"
set i2a_map[111] = "o"
set i2a_map[112] = "p"
set i2a_map[113] = "q"
set i2a_map[114] = "r"
set i2a_map[115] = "s"
set i2a_map[116] = "t"
set i2a_map[117] = "u"
set i2a_map[118] = "v"
set i2a_map[119] = "w"
set i2a_map[120] = "x"
set i2a_map[121] = "y"
set i2a_map[122] = "z"
set i2a_map[123] = "{"
set i2a_map[124] = "|"
set i2a_map[125] = "}"
set i2a_map[126] = "~"
endmethod
endmodule
struct i2a_loader_dummy
implement i2a_loader
endstruct
function ModuloInt takes integer h, integer b returns integer
return h - (h / b) * b
endfunction
function sub_I2A takes nothing returns nothing
if i2a_integer < 0x100 then
set i2a_string = i2a_map[ModuloInt(i2a_integer, 0x100)]
else
set i2a_string = i2a_map[i2a_integer / 0x1000000] + /*
*/ i2a_map[ModuloInt(i2a_integer / 0x10000, 0x100)] + /*
*/ i2a_map[ModuloInt(i2a_integer / 0x100, 0x100)] + /*
*/ i2a_map[ModuloInt(i2a_integer, 0x100)]
endif
endfunction
function I2A takes integer i returns string
set i2a_integer = i
call ExecuteFunc("sub_I2A")
return i2a_string
endfunction
endlibrary
globals
integer tms_max_value_index = 0
integer tms_min_value_index = 0
integer tms_iiii = 0
integer tms_jjjj = 0
string tms_array_str = ""
group tms_gggg = CreateGroup()
integer array CHAR2INT
integer tms_copy_of_ith_number = 0
string tms_copy_of_ith_string = ""
unit tms_copy_of_ith_unit = null
item tms_copy_of_ith_item = null
destructable tms_copy_of_ith_destructable = null
// if you need to sort type x then you need
// to modify the next line with the type of the
// variable:
// x tms_copy_of_ith_x = null
// replace the "x"s with your type
endglobals
//! textmacro sort_numbers_asc takes ARRAY_NAME, INDEX_A, INDEX_B
set tms_iiii = $INDEX_A$
loop
exitwhen tms_iiii >= $INDEX_B$
set tms_min_value_index = tms_iiii
set tms_jjjj = tms_iiii + 1
loop
exitwhen tms_jjjj >= $INDEX_B$
if $ARRAY_NAME$[tms_min_value_index] > $ARRAY_NAME$[tms_jjjj] then
set tms_min_value_index = tms_jjjj
endif
set tms_jjjj = tms_jjjj + 1
endloop
set tms_copy_of_ith_number = $ARRAY_NAME$[tms_iiii]
set $ARRAY_NAME$[tms_iiii] = $ARRAY_NAME$[tms_min_value_index]
set $ARRAY_NAME$[tms_min_value_index] = tms_copy_of_ith_number
set tms_iiii = tms_iiii + 1
endloop
//! endtextmacro
//! textmacro sort_numbers_desc takes ARRAY_NAME, INDEX_A, INDEX_B
set tms_iiii = $INDEX_A$
loop
exitwhen tms_iiii >= $INDEX_B$
set tms_max_value_index = tms_iiii
set tms_jjjj = tms_iiii + 1
loop
exitwhen tms_jjjj >= $INDEX_B$
if $ARRAY_NAME$[tms_max_value_index] < $ARRAY_NAME$[tms_jjjj] then
set tms_max_value_index = tms_jjjj
endif
set tms_jjjj = tms_jjjj + 1
endloop
set tms_copy_of_ith_number = $ARRAY_NAME$[tms_iiii]
set $ARRAY_NAME$[tms_iiii] = $ARRAY_NAME$[tms_max_value_index]
set $ARRAY_NAME$[tms_max_value_index] = tms_copy_of_ith_number
set tms_iiii = tms_iiii + 1
endloop
//! endtextmacro
function lexicographical_order takes string s1, string s2 returns boolean
local integer len_s1 = StringLength(s1)
local integer len_s2 = StringLength(s2)
local integer p = 0
local integer shorter = 0
local integer ord_ch1 = 0
local integer ord_ch2 = 0
if len_s1 > len_s2 then
set shorter = len_s2
else
set shorter = len_s1
endif
loop
exitwhen p >= shorter
set ord_ch1 = A2I(SubString(s1, p, p + 1))
set ord_ch2 = A2I(SubString(s2, p, p + 1))
if ord_ch1 > ord_ch2 then
return true
elseif ord_ch1 < ord_ch2 then
return false
endif
set p = p + 1
endloop
if len_s1 == shorter then
return false
endif
return true
endfunction
//! textmacro sort_strings_asc takes ARRAY_NAME, INDEX_A, INDEX_B
set tms_iiii = $INDEX_A$
loop
exitwhen tms_iiii >= $INDEX_B$
set tms_min_value_index = tms_iiii
set tms_jjjj = tms_iiii + 1
loop
exitwhen tms_jjjj >= $INDEX_B$
if lexicographical_order($ARRAY_NAME$[tms_min_value_index], $ARRAY_NAME$[tms_jjjj]) then
set tms_min_value_index = tms_jjjj
endif
set tms_jjjj = tms_jjjj + 1
endloop
set tms_copy_of_ith_string = $ARRAY_NAME$[tms_iiii]
set $ARRAY_NAME$[tms_iiii] = $ARRAY_NAME$[tms_min_value_index]
set $ARRAY_NAME$[tms_min_value_index] = tms_copy_of_ith_string
set tms_iiii = tms_iiii + 1
endloop
//! endtextmacro
//! textmacro sort_strings_desc takes ARRAY_NAME, INDEX_A, INDEX_B
set tms_iiii = $INDEX_A$
loop
exitwhen tms_iiii >= $INDEX_B$
set tms_max_value_index = tms_iiii
set tms_jjjj = tms_iiii + 1
loop
exitwhen tms_jjjj >= $INDEX_B$
if not lexicographical_order($ARRAY_NAME$[tms_max_value_index], $ARRAY_NAME$[tms_jjjj]) then
set tms_max_value_index = tms_jjjj
endif
set tms_jjjj = tms_jjjj + 1
endloop
set tms_copy_of_ith_string = $ARRAY_NAME$[tms_iiii]
set $ARRAY_NAME$[tms_iiii] = $ARRAY_NAME$[tms_max_value_index]
set $ARRAY_NAME$[tms_max_value_index] = tms_copy_of_ith_string
set tms_iiii = tms_iiii + 1
endloop
//! endtextmacro
//! textmacro sort_handles_asc takes ARRAY_NAME, INDEX_A, INDEX_B, COMPARISION_FUNCTION_NAME, HANDLE_TYPE
set tms_iiii = $INDEX_A$
loop
exitwhen tms_iiii >= $INDEX_B$
set tms_min_value_index = tms_iiii
set tms_jjjj = tms_iiii + 1
loop
exitwhen tms_jjjj >= $INDEX_B$
if $COMPARISION_FUNCTION_NAME$($ARRAY_NAME$[tms_min_value_index], $ARRAY_NAME$[tms_jjjj]) then
set tms_min_value_index = tms_jjjj
endif
set tms_jjjj = tms_jjjj + 1
endloop
set tms_copy_of_ith_$HANDLE_TYPE$ = $ARRAY_NAME$[tms_iiii]
set $ARRAY_NAME$[tms_iiii] = $ARRAY_NAME$[tms_min_value_index]
set $ARRAY_NAME$[tms_min_value_index] = tms_copy_of_ith_$HANDLE_TYPE$
set tms_iiii = tms_iiii + 1
endloop
set tms_copy_of_ith_$HANDLE_TYPE$ = null
//! endtextmacro
//! textmacro sort_handles_desc takes ARRAY_NAME, INDEX_A, INDEX_B, COMPARISION_FUNCTION_NAME, HANDLE_TYPE
set tms_iiii = $INDEX_A$
loop
exitwhen tms_iiii >= $INDEX_B$
set tms_max_value_index = tms_iiii
set tms_jjjj = tms_iiii + 1
loop
exitwhen tms_jjjj >= $INDEX_B$
if not $COMPARISION_FUNCTION_NAME$($ARRAY_NAME$[tms_max_value_index], $ARRAY_NAME$[tms_jjjj]) then
set tms_max_value_index = tms_jjjj
endif
set tms_jjjj = tms_jjjj + 1
endloop
set tms_copy_of_ith_$HANDLE_TYPE$ = $ARRAY_NAME$[tms_iiii]
set $ARRAY_NAME$[tms_iiii] = $ARRAY_NAME$[tms_max_value_index]
set $ARRAY_NAME$[tms_max_value_index] = tms_copy_of_ith_$HANDLE_TYPE$
set tms_iiii = tms_iiii + 1
endloop
set tms_copy_of_ith_$HANDLE_TYPE$ = null
//! endtextmacro
// required by the say_a function/textmacro to print arrays of string type =)
function S2S takes string s returns string
return s
endfunction
//! textmacro say_a takes ARRAY_NAME, INDEX_A, INDEX_B, TO_STRING
set tms_array_str = ""
set tms_iiii = $INDEX_A$
loop
exitwhen tms_iiii >= $INDEX_B$
set tms_array_str = tms_array_str + $TO_STRING$($ARRAY_NAME$[tms_iiii]) + ", "
set tms_iiii = tms_iiii + 1
endloop
call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "[" + SubString(tms_array_str, 0, StringLength(tms_array_str) - 2) + "]")
//! endtextmacro
//! textmacro group2array takes GROUP, ARRAY
call GroupClear(tms_gggg)
set tms_iiii = 0
set tms_copy_of_ith_unit = FirstOfGroup($GROUP$)
loop
exitwhen tms_copy_of_ith_unit == null
call GroupAddUnit(tms_gggg, tms_copy_of_ith_unit)
set $ARRAY$[tms_iiii] = tms_copy_of_ith_unit
set tms_iiii = tms_iiii + 1
set tms_copy_of_ith_unit = FirstOfGroup($GROUP$)
endloop
set $GROUP$ = tms_gggg
//! endtextmacro
Example how to use:
JASS:
globals
integer array Arry
unit array Units
string array Lang
endglobals
function comp_unit_hp takes unit u1, unit u2 returns boolean
return GetWidgetLife(u1) > GetWidgetLife(u2)
endfunction
function Trig_Untitled_Trigger_001_Actions takes nothing returns nothing
local integer a_inc = 0
local integer b_excl = 4
local integer lang_count = 25
//! runtextmacro say_a("Arry", "a_inc", "b_excl", "I2S")
//! runtextmacro sort_numbers_asc("Arry", "a_inc", "b_excl")
//! runtextmacro say_a("Arry", "a_inc", "b_excl", "I2S")
//! runtextmacro sort_numbers_desc("Arry", "a_inc", "b_excl")
//! runtextmacro say_a("Arry", "a_inc", "b_excl", "I2S")
//! runtextmacro say_a("Units", "0", "4", "GetUnitName")
//! runtextmacro sort_handles_asc("Units", "0", "4", "comp_unit_hp", "unit")
//! runtextmacro say_a("Units", "0", "4", "GetUnitName")
//! runtextmacro sort_handles_desc("Units", "0", "4", "comp_unit_hp", "unit")
//! runtextmacro say_a("Units", "0", "4", "GetUnitName")
//! runtextmacro say_a("Lang", "0", "lang_count", "S2S")
//! runtextmacro sort_strings_asc("Lang", "0", "lang_count")
//! runtextmacro say_a("Lang", "0", "lang_count", "S2S")
//! runtextmacro sort_strings_desc("Lang", "0", "lang_count")
//! runtextmacro say_a("Lang", "0", "lang_count", "S2S")
endfunction
//===========================================================================
function InitTrig_Untitled_Trigger_001 takes nothing returns nothing
set Arry[0] = 1
set Arry[1] = 0
set Arry[2] = 2
set Arry[3] = 3
set Units[0] = CreateUnit(Player(0), 'hfoo', 0, 0, 0)
set Units[1] = CreateUnit(Player(0), 'ogru', 0, 0, 0)
set Units[2] = CreateUnit(Player(0), 'hpea', 0, 0, 0)
set Units[3] = CreateUnit(Player(0), 'otau', 0, 0, 0)
set Lang[0] = "FORTRAN"
set Lang[1] = "Pascal"
set Lang[2] = "C"
set Lang[3] = "Perl"
set Lang[4] = "C++"
set Lang[5] = "Perl 6 =)"
set Lang[6] = "JavaScript"
set Lang[7] = "C#"
set Lang[8] = "Java"
set Lang[9] = "Delphi"
set Lang[10] = "Phyton"
set Lang[11] = "Galaxy Script"
set Lang[12] = "Andromeda"
set Lang[13] = "vim script"
set Lang[14] = "Bash"
set Lang[15] = "XML"
set Lang[16] = "unrealscript"
set Lang[17] = "E-Prime"
set Lang[18] = "Prolog"
set Lang[19] = "Mathematics"
set Lang[20] = "Hanguel"
set Lang[21] = "Asembly"
set Lang[22] = "vJass"
set Lang[23] = "Php"
set Lang[24] = "Blizzard's Warcraft 3 GUI"
set gg_trg_Untitled_Trigger_001 = CreateTrigger()
call TriggerRegisterPlayerEventEndCinematic( gg_trg_Untitled_Trigger_001, Player(0))
call TriggerAddAction( gg_trg_Untitled_Trigger_001, function Trig_Untitled_Trigger_001_Actions )
call BJDebugMsg(I2S(A2I("A")))
endfunction
Example using the powerfull jass preprocessor called cJass =).
JASS:
#define sort_numbers_asc_(x, a, b) = //! runtextmacro sort_numbers_asc(x, a, b)
#define sort_numbers_desc_(x, a, b) = //! runtextmacro sort_numbers_desc(x, a, b)
#define sort_strings_asc_(x, a, b) = //! runtextmacro sort_strings_asc(x, a, b)
#define sort_strings_desc_(x, a, b) = //! runtextmacro sort_strings_desc(x, a, b)
#define sort_handles_asc_(x, a, b, f, t) = //! runtextmacro sort_handles_asc(x, a, b, f, t)
#define sort_handles_desc_(x, a, b, f, t) = //! runtextmacro sort_handles_desc(x, a, b, f, t)
#define say_a_(x, a, b) = //! runtextmacro say_a(x, a, b)
#define group2array_(g, a) = //! runtextmacro group2array(g, a)
integer array Arry
unit array Units
string array Lang
bool comp_unit_hp(unit u1, unit u2)
{
return GetWidgetLife(u1) > GetWidgetLife(u2)
}
void Trig_Untitled_Trigger_001_Actions()
{
integer a_inc = 0
integer b_excl = 4
integer lang_count = 25
say_a(Arry, a_inc, b_excl, I2S)
sort_numbers_asc(Arry, a_inc, b_excl)
say_a(Arry, a_inc, b_excl, I2S)
sort_numbers_desc(Arry, a_inc, b_excl)
say_a(Arry, a_inc, b_excl, I2S)
say_a(Units, 0, 4, GetUnitName)
sort_handles_asc(Units, 0, 4, comp_unit_hp, unit)
say_a(Units, 0, 4, GetUnitName)
sort_handles_desc(Units, 0, 4, comp_unit_hp, unit)
say_a(Units, 0, 4, GetUnitName)
say_a(Lang, 0, lang_count, S2S)
sort_strings_asc(Lang, 0, lang_count)
say_a(Lang, 0, lang_count, S2S)
sort_strings_desc(Lang, 0, lang_count)
say_a(Lang, 0, lang_count, S2S)
}
//------------------------------------------------------------------
void InitTrig_Untitled_Trigger_001()
{
Arry[0] = 1
Arry[1] = 0
Arry[2] = 2
Arry[3] = 3
Units[0] = CreateUnit(Player(0), 'hfoo', 0, 0, 0)
Units[1] = CreateUnit(Player(0), 'ogru', 0, 0, 0)
Units[2] = CreateUnit(Player(0), 'hpea', 0, 0, 0)
Units[3] = CreateUnit(Player(0), 'otau', 0, 0, 0)
Lang[0] = "FORTRAN"
Lang[1] = "Pascal"
Lang[2] = "C"
Lang[3] = "Perl"
Lang[4] = "C++"
Lang[5] = "Perl 6 =)"
Lang[6] = "JavaScript"
Lang[7] = "C#"
Lang[8] = "Java"
Lang[9] = "Delphi"
Lang[10] = "Phyton"
Lang[11] = "Galaxy Script"
Lang[12] = "Andromeda"
Lang[13] = "vim script"
Lang[14] = "Bash"
Lang[15] = "XML"
Lang[16] = "unrealscript"
Lang[17] = "E-Prime"
Lang[18] = "Prolog"
Lang[19] = "Mathematics"
Lang[20] = "Hanguel"
Lang[21] = "Asembly"
Lang[22] = "vJass"
Lang[23] = "Php"
Lang[24] = "Blizzard's Warcraft 3 GUI"
gg_trg_Untitled_Trigger_001 = CreateTrigger()
TriggerRegisterPlayerEventEndCinematic( gg_trg_Untitled_Trigger_001, Player(0))
TriggerAddAction( gg_trg_Untitled_Trigger_001, function Trig_Untitled_Trigger_001_Actions )
BJDebugMsg(I2S(A2I("A")))
}