Jump to content
Sign in to follow this  
LoonyWarrior

select max value from array aka select team with highest score

Recommended Posts

/************************************************************

Find Greatest Number

By Andrew Barron

Parameters: [number set]

Returns: number

Returns the highest number out of the passed set.

Example: [1,5,10] call BIS_fnc_greatestNum

returns 10

************************************************************/

Share this post


Link to post
Share on other sites

Header for the fn_sortBy.sqf function that BIS supply:

/*

Author: Jiri Wainar (Warka)

Description:

Sorts an array according to given algorithm.

Parameter(s):

_this select 0: any unsorted array (Array)

- array can contain any types (object/numbers/strings ..)

_this select 1: sorted algorithm (Code) [optional: default {_x}]

- code needs to return a scalar

- variable _x refers to array item

_this select 2: sort direction (String) [optional: default "ASCEND"]

"ASCEND": sorts array in ascending direction (from lowest value to highest)

"DESCEND": sorts array in descending direction

_this select 3: filter (Code) [optional: default {true}]

- code that needs to evaluate true for the array item to be sorted, otherwise item is removed

Returns:

Array

Examples:

_sortedNumbers = [[1,-80,0,480,15,-40],{_x},"ASCEND"] call BIS_fnc_sortBy;

_closestHelicopters = [[_heli1,_heli2,_heli3],{player distance _x},"ASCEND"] call BIS_fnc_sortBy;

_furtherstEnemy = [[_enemy1,_enemy2,_enemy3],{player distance _x},"DESCEND",{canMove _x}] call BIS_fnc_sortBy;

*/

Bolded is of particular interest to you. (Although I suggest you use "DESCEND" and select the first element :))

Share this post


Link to post
Share on other sites
/************************************************************

Find Greatest Number

By Andrew Barron

Parameters: [number set]

Returns: number

Returns the highest number out of the passed set.

Example: [1,5,10] call BIS_fnc_greatestNum

returns 10

************************************************************/

Just tested this vs plain max command, what a waste of time tbh.

_t = diag_tickTime;
for "_i" from 1 to 100000 do {
_k = 1 max 5 max 10;
};
hint str (diag_tickTime - _t);
//0.32 seconds

_t = diag_tickTime;
for "_i" from 1 to 100000 do {
_k = [1, 5, 10] call BIS_fnc_greatestNum;
};
hint str (diag_tickTime - _t);
//4.57 seconds

Share this post


Link to post
Share on other sites

Aye was easy to point to though. Also sod writing out that max line with a 100 index :P

Strange enough BIS_fnc_greatestNum in its raw form

_nums = [1,5,10];
_t = diag_tickTime;
for "_i" from 1 to 100000 do {

_highest = -1e9;
{
if(_x > _highest) then
{
	_highest = _x;
};
} foreach _nums;

};
hint str (diag_tickTime - _t);
//1.5 around abouts

nums = [1,5,10];
highest = -1e9;
t = diag_tickTime;
for "_i" from 1 to 100000 do {
for "_n" from 0 to ((count nums)-1) do {
	highest = highest max (nums select _n);
};
};
hint str (diag_tickTime - t);
//1.3 around abouts

nums = [1,5,10];
highest = -1e9;
t = diag_tickTime;
for "_i" from 1 to 100000 do {
{
	highest = highest max _x;
}forEach nums;
};
hint str (diag_tickTime - t);
//1.05 around abouts

Edited by Larrow

Share this post


Link to post
Share on other sites
Aye was easy to point to though. Also sod writing out that max line with a 100 index :P

Strange enough BIS_fnc_greatestNum in its raw form

_nums = [1,5,10];
_t = diag_tickTime;
for "_i" from 1 to 100000 do {

_highest = -1e9;
{
if(_x > _highest) then
{
	_highest = _x;
};
} foreach _nums;

};
hint str (diag_tickTime - _t);
//1.5 around abouts

Yeah noticed that, not great. Pretty sure if you need to deal with long arrays there could be another way. In most cases though custom approach is the best.

Share this post


Link to post
Share on other sites

nums = [1,5,10];
highest = -1e9;
t = diag_tickTime;
for "_i" from 1 to 100000 do {
{
	highest = highest max _x;
}forEach nums;
};
hint str (diag_tickTime - t);
//1.05 around abouts

i just found the winner... :D

Share this post


Link to post
Share on other sites
Also sod writing out that max line with a 100 index :P

This got me thinking, if only Arma had support for regular expressions... But C++ has! So I wrote 2 extensions regex_match and regex_replace. And this is how it could be done with lightning speed

_array = [10,50,45,33];

_max = call compile (
"regex_replace" callExtension "/,/ max /" + (
	"regex_replace" callExtension "/\[|\]//" + str _array
)
);

hint str _max;

I'm not kidding about lightning speed, matching a word in a string is 100x faster with RegExp than trying to solve the same problem with sqf. Gonna release both dlls soon.

Edited by Killzone_Kid

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×