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 function to get size of hashes. Fixes #528 #531

Merged
merged 1 commit into from
Oct 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions addons/hashes/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
37 changes: 37 additions & 0 deletions addons/hashes/fnc_hashSize.sqf
Original file line number Diff line number Diff line change
@@ -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
};
31 changes: 29 additions & 2 deletions addons/hashes/test_hashes.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;