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

Added 4 New Array Functions #131

Merged
merged 14 commits into from
Sep 6, 2015
24 changes: 24 additions & 0 deletions addons/arrays/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ class CfgFunctions
description = "Filter each element of an array via a function.";
file = "\x\cba\addons\arrays\fnc_filter.sqf";
};
// CBA_fnc_findNil
class findNil
{
description = "A function that returns the index of the first empty (nil) entry in an array.";
file = "\x\cba\addons\arrays\fnc_findNil.sqf";
};
// CBA_fnc_findTypeName
class findTypeName
{
description = "A function that returns the index of the first entry of a certain type in an array.";
file = "\x\cba\addons\arrays\fnc_findTypeName.sqf";
};
// CBA_fnc_findTypeOf
class findTypeOf
{
description = "A function that returns the index of the first entry of a certain type in an array.";
file = "\x\cba\addons\arrays\fnc_findTypeOf.sqf";
};
// CBA_fnc_findNull
class findNull
{
description = "A function that returns the index of the first null entry in an array.";
file = "\x\cba\addons\arrays\fnc_findNull.sqf";
};
// CBA_fnc_getArrayDiff
class getArrayDiff
{
Expand Down
33 changes: 33 additions & 0 deletions addons/arrays/fnc_findNil.sqf
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
37 changes: 37 additions & 0 deletions addons/arrays/fnc_findNull.sqf
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
48 changes: 48 additions & 0 deletions addons/arrays/fnc_findTypeName.sqf
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. Otherwise 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"transform"

Copy link
Contributor

Choose a reason for hiding this comment

The 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
43 changes: 43 additions & 0 deletions addons/arrays/fnc_findTypeOf.sqf
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 {
_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
2 changes: 1 addition & 1 deletion addons/arrays/test.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define DEBUG_MODE_FULL
#include "script_component.hpp"

#define TESTS ["filter", "inject", "join", "shuffle"]
#define TESTS ["filter", "inject", "join", "shuffle", "findNil", "findNull", "findTypeName", "findTypeOf"]

SCRIPT(test-arrays);

Expand Down
41 changes: 41 additions & 0 deletions addons/arrays/test_findNil.sqf
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 embedded nil
_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;
41 changes: 41 additions & 0 deletions addons/arrays/test_findNull.sqf
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
_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;
Loading