From fed66854927cec5de843734d0fa61f64ab70afbd Mon Sep 17 00:00:00 2001 From: Killswitch Date: Sun, 16 Oct 2016 10:47:00 +0200 Subject: [PATCH] Add function to get size of hashes. Fixes #528 --- addons/hashes/CfgFunctions.hpp | 6 ++++++ addons/hashes/fnc_hashSize.sqf | 37 ++++++++++++++++++++++++++++++++++ addons/hashes/test_hashes.sqf | 31 ++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 addons/hashes/fnc_hashSize.sqf diff --git a/addons/hashes/CfgFunctions.hpp b/addons/hashes/CfgFunctions.hpp index 3ddece4c5..b0fd79ea8 100644 --- a/addons/hashes/CfgFunctions.hpp +++ b/addons/hashes/CfgFunctions.hpp @@ -50,6 +50,12 @@ class CfgFunctions description = "Sets a value for a given key in a Hash."; file = "\x\cba\addons\hashes\fnc_hashSet.sqf"; }; + // CBA_fnc_hashSize + class hashSize + { + description = "Get number of elements in a Hash."; + file = "\x\cba\addons\hashes\fnc_hashSize.sqf"; + }; // CBA_fnc_isHash class isHash { diff --git a/addons/hashes/fnc_hashSize.sqf b/addons/hashes/fnc_hashSize.sqf new file mode 100644 index 000000000..04534fa88 --- /dev/null +++ b/addons/hashes/fnc_hashSize.sqf @@ -0,0 +1,37 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_hashSize + +Description: + Get number of elements in a Hash. + +Parameters: + _hash - Hash to check size of [Array which is a Hash structure] + +Returns: + Size of the Hash or -1 if the argument is not a Hash [Number] + +Examples: +(begin code) + _emptyHash = [] call CBA_fnc_hashCreate; + [_emptyHash] call CBA_fnc_hashSize; // => 0 + + _animalCounts = [[["frog", 12], ["fish", 9]]] call CBA_fnc_hashCreate; + [_animalCounts] call CBA_fnc_hashSize; // => 2 +(end code) + +Author: + Killswitch +---------------------------------------------------------------------------- */ + +#include "script_component.hpp" +#include "script_hashes.hpp" + +SCRIPT(hashSize); + +params ["_hash"]; + +if ([_hash] call CBA_fnc_isHash) then { + count (_hash select HASH_KEYS) +} else { + -1 +}; diff --git a/addons/hashes/test_hashes.sqf b/addons/hashes/test_hashes.sqf index 93e4e3d2e..4741a9edf 100644 --- a/addons/hashes/test_hashes.sqf +++ b/addons/hashes/test_hashes.sqf @@ -6,16 +6,17 @@ SCRIPT(test_hashes); // ---------------------------------------------------------------------------- #define DEBUG_MODE_FULL -private ["_hash", "_expected", "_result"]; +private ["_hash", "_expected", "_result", "_size"]; LOG("Testing Hashes"); -// UNIT TESTS (initStrings.sqf - stringJoin) +// UNIT TESTS TEST_DEFINED("CBA_fnc_hashCreate",""); TEST_DEFINED("CBA_fnc_hashGet",""); TEST_DEFINED("CBA_fnc_hashSet",""); TEST_DEFINED("CBA_fnc_hashHasKey",""); TEST_DEFINED("CBA_fnc_isHash",""); +TEST_DEFINED("CBA_fnc_hashSize",""); TEST_FALSE([[]] call CBA_fnc_isHash,"CBA_fnc_isHash"); _hash = [5, [4], [1], 2]; // Not a real hash. @@ -88,4 +89,30 @@ TEST_OP(_result,==,1,"hashSet/Get"); _result = [_hash, "frog"] call CBA_fnc_hashGet; TEST_TRUE(isNil "_result","hashSet/Get"); +// Empty hash size +_hash = [] call CBA_fnc_hashCreate; +_size = [_hash] call CBA_fnc_hashSize; +TEST_OP(_size,==,0,"hashSize"); + +// Add one element +[_hash, "aaa", 1] call CBA_fnc_hashSet; +_size = [_hash] call CBA_fnc_hashSize; +TEST_OP(_size,==,1,"hashSize"); + +// Add two elements +[_hash, "bbb", 2] call CBA_fnc_hashSet; +[_hash, "ccc", 3] call CBA_fnc_hashSet; +_size = [_hash] call CBA_fnc_hashSize; +TEST_OP(_size,==,3,"hashSize"); + +// Remove the second element added +[_hash, "bbb"] call CBA_fnc_hashRem; +_size = [_hash] call CBA_fnc_hashSize; +TEST_OP(_size,==,2,"hashSize"); + +// Check size of something that is not a hash +_hash = [5, [4], [1], 2]; +_size = [_hash] call CBA_fnc_hashSize; +TEST_OP(_size,==,-1,"hashSize"); + nil;