First of all big thanks to Andrey for another donation!

всегда с интересом читаю твои статьи
wbr, f0rt

Большое спасибо, Андрей!

Today I’m going to share a couple of array functions I made (maybe not in the same post). I briefly looked through available BIS functions and haven’t found one that can reverse array so here it is, KK_fnc_arrayReverse:

KK_fnc_arrayReverse = { private ["_i","_j","_tmp"]; for [ {_i = 0; _j = count _this - 1}, {_i < _j}, {_i = _i + 1; _j = _j - 1} ] do { _tmp = _this select _i; _this set [_i, _this select _j]; _this set [_j, _tmp]; }; _this };

The algorithm is pretty simple, take one element from the beginning and one from the end of array and swap them around, then take the 2nd element and the penultimate element and swap them around and so forth until you reach the middle of array. Because of this you only need to iterate through half of the array which should give boost in performance. I tested this on 10 element array, it took the function 0.089 ms, 20 elements took 0.16 ms for example. Since for [{},{},{}] do {} construct allows for some extended evaluation control, in the end it comes out pretty neat. It can even be ported to C++ with ease, let’s see if I can convince BIS people to add a dedicated command for array reversing. Examples of use:

//reverse array by reference (modify original) _arr = [0,1,2,3,4,5,6,7,8,9]; _arr call KK_fnc_arrayReverse; hint str _arr; //[9,8,7,6,5,4,3,2,1,0]

As you have noticed, by default the original array will get modified. If you don’t want this, pass a copy of original array rather than its reference (+ operator):

//reverse and return copy of array (do not modify original) _arr = [0,1,2,3,4,5,6,7,8,9]; _newArr = +_arr call KK_fnc_arrayReverse; hint str _newArr; //[9,8,7,6,5,4,3,2,1,0] hint str _arr; //[0,1,2,3,4,5,6,7,8,9]
//reverse on the fly array and return it _arr = [0,1,2,3,4,5,6,7,8,9] call KK_fnc_arrayReverse; hint str _arr; //[9,8,7,6,5,4,3,2,1,0]

Just thought of another very simple function, while typing this, which might be somewhat useful, KK_fnc_XYZtoXZY:

KK_fnc_XYZtoXZY = { [_this select 0, _this select 2, _this select 1] };

positionCameraToWorld for example is in [x,z,y] format, and so are the coordinates in mission.sqm, while everything else seems to be in [x,y,z] format. It is easy to convert it forth and back:

hint str ([1,2,3] call KK_fnc_XYZtoXZY); //[1,3,2]

Enjoy,
KK

UPDATE: From today, dedicated command reverse is available on DEV branch. Just to compare, 10 element array is reversed in 0.0027 ms compared to 0.089 ms when done with above SQF function. Thank you, Jan!