Name
dict_inc_or_put — Creates or increments an integer counter for a given key and a dictionary.
Synopsis
dict_inc_or_put
(
|
inout dict dictionary , |
in key any , | |
in
value_increment
integer
) ; |
Description
The function checks whether
dict
contains
key
.
If it isn't so then the function checks the datatype of the value associated with the key.
An error 42000 is signalled in case of non-integer value or a negative integer value.
If the value is positive then value_decrement
is added to it and the result become the new value associated with key
in dict
.
If key is not in the dictionary then a new item is added to the
dict
in order to associate the
key
with
value_increment
.
Parameters
dict
Dictionary of counters. If the value is NULL then the function immediately returns zero.
key
Key of a dictionary item to process.
value decrement
A nonnegative integer (typically 1) that is added to the value associated with
key
or used as a starting value of a newly created counter.
Return Types
The function returns zero (for NULL
dict
) or the changed (or the added) value associated with the
key
.
Example
Example 24.91. Simple Use
The function is convenient to deal with multisets, i.e., sets with repeating elements.
In this case the dictionary contains distinct items as keys and counts of duplicates as associated values.
dict_inc_or_add
is to add a member,
dict_dec_or_remove
is to remove.
The following example gets an array of multisets and return the sum of them.
create function DB.DBA.SUM_MULTISETS (inout msets any) returns any { declare sum_of_msets any; sum_of_msets := dict_new (17); foreach (any mset in msets) do { declare iter any; declare memb any; declare dup_count integer; iter := mset; --- unlike dict_duplicate() this does not make copy of mset so it's fast. dict_iter_rewind (iter); while (dict_iter_next (iter, memb, dup_count)) dict_inc_or_put (sum_of_msets, memb, dup_count); } return sum_of_msets; };