fn_addToPairs.sqf 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Author: Jiri Wainar
  3. Modified by: Gundy
  4. Description:
  5. Adds given value to pair array, stored under unique key.
  6. If value is found:
  7. a) both values are scalars: values are added and stored as a single scalar
  8. b) one or both values are array: values are added and stored as a single array
  9. c) anything else: an array is created and both values are stored there
  10. Remarks:
  11. Function by default modifies the input array. It returns the new value that has been added.
  12. Syntax:
  13. _pairs:array = [_pairs:array,_key:string,_value:number] call BIS_fnc_addToPairs;
  14. Example:
  15. [["apple",3],["pear",12]] = [[["apple",3],["pear",2]],"pear",10] call BIS_fnc_addToPairs;
  16. [["apple",1],["pear",2]] = [[["apple",3],["pear",2]],"apple",-2] call BIS_fnc_addToPairs;
  17. [["greetings",["Hello!","Hi!"]],["rudewords",""]] = [[["greetings","Hello!"],["rudewords",""]],"greetings","Hi!"] call BIS_fnc_addToPairs;
  18. */
  19. #define SCALAR (typeName 123)
  20. #define ARRAY (typeName [])
  21. private ["_pairs","_key","_add","_copy","_index","_value","_tValue","_tAdd","_return"];
  22. _pairs = [_this,0,[],[[]]] call bis_fnc_param;
  23. _key = [_this,1,"",[""]] call bis_fnc_param;
  24. _add = [_this,2,1] call bis_fnc_param;
  25. _index = -1;
  26. _return = "";
  27. {
  28. if (typeName _x != typeName []) exitWith
  29. {
  30. ["Every item in the pair array must be an array!"] call BIS_fnc_error;
  31. _index = -2;
  32. };
  33. if (count _x != 2) exitWith
  34. {
  35. ["Pair array must contain exactly 2 items, key and value!"] call BIS_fnc_error;
  36. _index = -2;
  37. };
  38. if (isNil{_x select 0}) exitWith
  39. {
  40. ["Key cannot be nil!"] call BIS_fnc_error;
  41. _index = -2;
  42. };
  43. if (typeName(_x select 0) != typeName "") exitWith
  44. {
  45. ["Key must be a string!"] call BIS_fnc_error;
  46. _index = -2;
  47. };
  48. if (isNil{_x select 1}) exitWith
  49. {
  50. ["Value cannot be nil!"] call BIS_fnc_error;
  51. _index = -2;
  52. };
  53. if (_key == (_x select 0)) exitWith
  54. {
  55. _index = _forEachIndex;
  56. _value = _x select 1;
  57. };
  58. }
  59. forEach _pairs;
  60. //error occured, exit
  61. if (_index == -2) exitWith {nil};
  62. //key not found, add the new entry
  63. if (_index == -1) then
  64. {
  65. _pairs pushBack [_key, _add];
  66. _return = _add;
  67. }
  68. //key found, add the value to existing entry
  69. else
  70. {
  71. _tValue = typeName _value;
  72. _tAdd = typeName _add;
  73. switch (true) do
  74. {
  75. case (_tValue == SCALAR && _tAdd == SCALAR):
  76. {
  77. _return = _value + _add;
  78. };
  79. case (_tValue == ARRAY && _tAdd == ARRAY):
  80. {
  81. _return = _value + _add;
  82. };
  83. case (_tValue == ARRAY):
  84. {
  85. _return = _value + [_add];
  86. };
  87. case (_tAdd == ARRAY):
  88. {
  89. _return = _add + [_value];
  90. };
  91. //none of the items are of array, so create one and store them there
  92. default
  93. {
  94. _return = _value + [_add];
  95. };
  96. };
  97. _pairs set [_index, [_key, _return]];
  98. };
  99. _return