New argument in getValue() function of map

Hi,
In the current implementation of the map, we have a function getValue() which returns the element or returns the pre-initialized value if parSafe=false, so if the user wants to use map he/she has to create a new condition for fail-safe and this can create some extra work in the use of the map. This can be eliminated if we create a new default argument.

After a small discussion the best solution can be to add an overloaded version of the current getValue() function with new extra argument 'sentinel' that will be returned if a key doesn't exist in the map

proc map.getValue(key) { ... }    // this is the routine as it exists today
proc map.getValue(key, sentinel) { ... }  // returns 'sentinel' if the key is not 

Providing some quick usage and also improve the usage when map is used in parallel programming. This will provide a safe way to add+update values.
eg,

map[key] = map.getValue(key, 0) + 1   // no more need to create extra fail-safe condition 
Link for ref - Issue #17498 (includes discussion)

 


proc getValue(k: keyType, sentinel: valType): valType

If k exists get a copy of the element stored with k else revert back the sentinel value

Arguments:

  • k : keyType -- The key to test for membership
  • sentinel : valType -- The value which will be return in case where no k exists

Returns: Return either a value to the corresponding key or sentinel value

Return type: valType

Is this a good addition to the current map and if so, Is there a better way to implement it. Open to suggestions, if any.

Thanks.

3 Likes