Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CBA_fnc_selectBest function #1244

Merged
merged 2 commits into from
Nov 15, 2019
Merged

Add CBA_fnc_selectBest function #1244

merged 2 commits into from
Nov 15, 2019

Conversation

PabstMirror
Copy link
Contributor

@PabstMirror PabstMirror commented Oct 23, 2019

Select best element from an array

Alternate way, but I found no real speed diff,

_array = _array apply {  [_x call _criteria, _x]; };
_array sort false; 
_array # 0 # 1

@dedmen
Copy link
Contributor

dedmen commented Oct 23, 2019

There should be a speed diff tho 🤔 if/then evaluation and opening a new scope every iteration should be more expensive than creating an array and running a sort.

@commy2
Copy link
Contributor

commy2 commented Oct 23, 2019

I like the alternative in the opening post more for readability alone.
Last line could be:
_array param [0, [0, _default]] select 1
and so handle the default value and the whole script would be 4 lines.

@commy2 commy2 added this to the 3.13 milestone Oct 23, 2019
@PabstMirror
Copy link
Contributor Author

PabstMirror commented Oct 23, 2019

a = {  
    params ["_array", "_criteria", "_return"]; 
    private _bestScore = -1e99; 
    { 
        private _xScore = _x call _criteria; 
        if (_xScore > _bestScore) then { 
            _return = _x; 
            _bestScore = _xScore; 
        }; 
    } forEach _array; 
    _return 
};  
b = {  
    params ["_array", "_criteria", "_default"];  
    _array = _array apply { [_x call _criteria, _x]; };  
    _array sort false;  
    _array param [0, [0, _default]] select 1 
};  
x = [1,2,3,4,5,6,7,8,9,-10];  
c = { abs _this}; 
 
Result: 0.264271 ms
10x [x,c] call a; 

Result: 0.28169 ms
10x [x,c] call b; 

Alt way has problems with undefined in scheduled (Nevermind: Both have similar problem)

[] spawn {[[],{_x}] call b}
Error Undefined variable in expression: _default

You also get _forEachIndex with a

@commy2
Copy link
Contributor

commy2 commented Oct 23, 2019

RETNIL(_var) macro to get around the _default is nil problem.

@commy2
Copy link
Contributor

commy2 commented Nov 6, 2019

One problem I have with this is that the variables _bestScore, __xScore, _array, _criteria, _return leak into the code block.

@commy2 commy2 merged commit e99f079 into master Nov 15, 2019
@commy2 commy2 deleted the selectBest branch November 15, 2019 14:54
@PabstMirror
Copy link
Contributor Author

Just to note, criteria function can return special value of -1e99 and item will be ignored
(e.g. will return default value if no item returns a value higher)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants