-
Notifications
You must be signed in to change notification settings - Fork 156
Added 4 New Array Functions #131
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
Changes from 9 commits
3adfb0e
058409d
e1f58c3
1a9ae13
232a7e6
52d47a0
9129ce8
0dfc4a2
50c75f6
43e8ec4
f4d2a80
6816ee4
aa136b5
1f22847
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* ---------------------------------------------------------------------------- | ||
| Function: CBA_fnc_findNil | ||
|
|
||
| Description: | ||
| A function that returns the index of the first empty (nil) entry in an array. | ||
|
|
||
| Parameters: | ||
| The array to search in. | ||
|
|
||
| Example: | ||
| (begin example) | ||
| _index = ["", Player, "test", nil, VARIABLE, nil] call CBA_fnc_findNil | ||
| (end) | ||
|
|
||
| Returns: | ||
| Index of the first nil entry in the array. If there is no nil entry, the function returns -1 | ||
|
|
||
| Author: | ||
| joko // Jonas | ||
| ---------------------------------------------------------------------------- */ | ||
| #include "script_component.hpp" | ||
|
|
||
| if !(IS_ARRAY(_this)) exitWith {-1}; | ||
|
|
||
| scopeName "main"; | ||
|
|
||
| { | ||
| if (isNil "_x") then { | ||
| _forEachIndex breakOut "main"; | ||
| }; | ||
| } forEach _this; | ||
|
|
||
| -1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* ---------------------------------------------------------------------------- | ||
| Function: CBA_fnc_findNull | ||
|
|
||
| Description: | ||
| A function that returns the index of the first null entry in an array. | ||
|
|
||
| Parameters: | ||
| The array to search in. | ||
|
|
||
| Example: | ||
| (begin example) | ||
| _index = ["", Player, "test", objNull, VARIABLE] call CBA_fnc_findNull | ||
| (end) | ||
|
|
||
| Returns: | ||
| Index of the first null entry in the array. If there is no null entry, the function returns -1 | ||
|
|
||
| Author: | ||
| joko // Jonas | ||
| ---------------------------------------------------------------------------- */ | ||
| #include "script_component.hpp" | ||
|
|
||
| if !(IS_ARRAY(_this)) exitWith {-1}; | ||
|
|
||
| scopeName "main"; | ||
|
|
||
| private "_checkableTypes"; | ||
|
|
||
| _checkableTypes = ["OBJECT", "CONTROL", "DISPLAY", "GROUP", "LOCATION", "TASK", "SCRIPT"]; | ||
|
|
||
| { | ||
| if (typeName _x in _checkableTypes && {isNull _x}) then { | ||
| _forEachIndex breakOut "main"; | ||
| }; | ||
| } forEach _this; | ||
|
|
||
| -1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* ---------------------------------------------------------------------------- | ||
| Function: CBA_fnc_findTypeName | ||
|
|
||
| Description: | ||
| A function that returns the index of the first entry of the given type in an array. | ||
|
|
||
| Parameters: | ||
| 0: Array | ||
| 1: TypeName, if parameter is a string, that contains a case insensitive typename, it will be used. Otherwies typename of the variable will be used. | ||
|
|
||
| Example: | ||
| (begin example) | ||
| _index = ["OBJECT",["", Player, "test", nil, VARIABLE, nil]] call CBA_fnc_findTypeName | ||
| (end) | ||
|
|
||
| Returns: | ||
| Index of the first entry of the indicated type in the array or -1 if no entry of the type could be found. | ||
|
|
||
| Author: | ||
| joko // Jonas | ||
| ---------------------------------------------------------------------------- */ | ||
| #include "script_component.hpp" | ||
|
|
||
| #define TYPENAMES ["ARRAY", "BOOL", "CODE", "CONFIG", "CONTROL", "DISPLAY", "GROUP", "LOCATION", "OBJECT", "SCALAR", "SCRIPT", "SIDE", "STRING", "TASK", "TEXT", "TEAM_MEMBER", "NAMESPACE"] | ||
|
|
||
| scopeName "main"; | ||
|
|
||
| params [["_array", [], [[]]], "_typeName"]; | ||
|
|
||
| if (isNil "_typeName" || {_array isEqualTo []}) exitWith {-1}; | ||
|
|
||
| // If a string is given, tansform to uppercase for type matching | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "transform"
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A better word for "transform" in this context is to use "convert" |
||
| if (IS_STRING(_typeName)) then { | ||
| _typeName = toUpper _typeName; | ||
| }; | ||
|
|
||
| // If _typeName is not a typename description, use the type of that value | ||
| if !(_typeName in TYPENAMES) then { | ||
| _typeName = typeName _typeName; | ||
| }; | ||
|
|
||
| { | ||
| if (typeName _x == _typeName) then { | ||
| _forEachIndex breakOut "main"; | ||
| }; | ||
| } forEach _array; | ||
|
|
||
| -1 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* ---------------------------------------------------------------------------- | ||
| Function: CBA_fnc_findTypeOf | ||
|
|
||
| Description: | ||
| A function that returns the index of the first entry (either object or class name string) of the given type in an array. | ||
|
|
||
| Parameters: | ||
| 0: Array | ||
| 1: Entry type, can be either Object or class name string (as returned by typeOf) | ||
|
|
||
| Example: | ||
| (begin example) | ||
| _index = [["", Player, "test", nil, VARIABLE, nil], player] call CBA_fnc_findTypeOf | ||
| (end) | ||
|
|
||
| Returns: | ||
| Index of the first entry of the indicated type in the array or -1 if no entry of the type could be found. | ||
|
|
||
| Author: | ||
| joko // Jonas | ||
| ---------------------------------------------------------------------------- */ | ||
| #include "script_component.hpp" | ||
|
|
||
| scopeName "main"; | ||
|
|
||
| params [["_array", [], [[]]], ["_typeOf", nil, [objNull, ""]]]; | ||
|
|
||
| if (isNil "_typeOf" || {_array isEqualTo []}) exitWith {-1}; | ||
|
|
||
| if (IS_OBJECT(_typeOf) then { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that this hasn't been tested in-game at all. |
||
| _typeOf = typeOf _typeOf; | ||
| }; | ||
|
|
||
| { | ||
| if (IS_OBJECT(_x) && {typeOf _x == _typeOf}) then { | ||
| _forEachIndex breakOut "main"; | ||
| }; | ||
| if (IS_STRING(_x) && {_x == _typeOf}) then { | ||
| _forEachIndex breakOut "main"; | ||
| }; | ||
| } forEach _array; | ||
|
|
||
| -1 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // ---------------------------------------------------------------------------- | ||
| #define DEBUG_MODE_FULL | ||
| #include "script_component.hpp" | ||
|
|
||
| SCRIPT(test_findNil); | ||
|
|
||
| // ---------------------------------------------------------------------------- | ||
|
|
||
| private ["_expected", "_result", "_fn"]; | ||
|
|
||
| _fn = "CBA_fnc_findNil"; | ||
| LOG("Testing " + _fn); | ||
|
|
||
| TEST_DEFINED("CBA_fnc_findNil",""); | ||
|
|
||
| // Use of embeded nil | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "embedded" |
||
| _result = ["", 5, objNull, nil, player] call CBA_fnc_findNil; | ||
| _expected = 3; | ||
| TEST_OP(_result,==,_expected,_fn); | ||
|
|
||
| // Only find the first nil | ||
| _result = ["", 5, objNull, player, nil, nil] call CBA_fnc_findNil; | ||
| _expected = 4; | ||
| TEST_OP(_result,==,_expected,_fn); | ||
|
|
||
| // Return a not found when there is no nil | ||
| _result = ["", 5, objNull, displayNull, player] call CBA_fnc_findNil; | ||
| _expected = -1; | ||
| TEST_OP(_result,==,_expected,_fn); | ||
|
|
||
| // Return a not found when empty array is given | ||
| _result = [] call CBA_fnc_findNil; | ||
| _expected = -1; | ||
| TEST_OP(_result,==,_expected,_fn); | ||
|
|
||
| // Return a not found when a non array is given | ||
| _result = "not an array" call CBA_fnc_findNil; | ||
| _expected = -1; | ||
| TEST_OP(_result,==,_expected,_fn); | ||
|
|
||
| nil; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // ---------------------------------------------------------------------------- | ||
| #define DEBUG_MODE_FULL | ||
| #include "script_component.hpp" | ||
|
|
||
| SCRIPT(test_findNull); | ||
|
|
||
| // ---------------------------------------------------------------------------- | ||
|
|
||
| private ["_original", "_expected", "_result", "_fn"]; | ||
|
|
||
| _fn = "CBA_fnc_findNull"; | ||
| LOG("Testing " + _fn); | ||
|
|
||
| TEST_DEFINED("CBA_fnc_findNull",""); | ||
|
|
||
| // Use of embedded null | ||
| _result = ["", objNull, player, 3] call CBA_fnc_findNull; | ||
| _expected = 1; | ||
| TEST_OP(_result,==,_expected,_fn); | ||
|
|
||
| // Only find first Null | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "null", not "Null" |
||
| _result = ["", player, objNull, displayNull, 4] call CBA_fnc_findNull; | ||
| _expected = 2; | ||
| TEST_OP(_result,==,_expected,_fn); | ||
|
|
||
| // Return not found if there is no null | ||
| _result = ["", player, 5] call CBA_fnc_findNull; | ||
| _expected = -1; | ||
| TEST_OP(_result,==,_expected,_fn); | ||
|
|
||
| // Return a not found if empty array is given | ||
| _result = [] call CBA_fnc_findNull; | ||
| _expected = -1; | ||
| TEST_OP(_result,==,_expected,_fn); | ||
|
|
||
| // Return a not found when a non array is given | ||
| _result = "not an array" call CBA_fnc_findNull; | ||
| _expected = -1; | ||
| TEST_OP(_result,==,_expected,_fn); | ||
|
|
||
| nil; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Otherwise"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
God dammit :D