Pap Lőrinc created GROOVY-7802:
----------------------------------
Summary: `MapWithDefault` shouldn't store its default value
Key: GROOVY-7802
URL: https://issues.apache.org/jira/browse/GROOVY-7802
Project: Groovy
Issue Type: Bug
Components: groovy-jdk
Affects Versions: 2.4.6
Reporter: Pap Lőrinc
As described in https://github.com/apache/groovy/pull/267, a Groovy map with a
default value stores all the accessed elements, even if they weren't added
explicitly, just queried.
In certain algorithms (e.g.
https://github.com/careercup/CtCI-6th-Edition-Groovy/blob/d116d65469bdf17d1e215e89f3e76ac3a97660a9/src/main/groovy/Ch04_TreesAndGraphs/_04_12_PathsWithSum.groovy#L21)
the complexity would automatically be reduced, if default values would be
deleted (i.e. a default of 0 that is incremented and decremented in a recursive
algorithm).
Topics of discussion:
* Currently a `getAt` will `add` also, therefore `containsKey` isn't the same
as `getAt(...) != null`
* After the change `keySet()/entrySet()` wouldn't include default values
I proposed alternatives:
* `removeIfDefault(Object)` // removes current key, if value is equal to default
* `consolidate()` // removes all default values
* constructor parameter `STORE_DEFAULT storeDefaultValues =
STORE_DEFAULT.FALSE`, or maybe with a `boolean`, though I find primitive
parameters confusing
others:
* `containsKey` should always return `true`, or throw an exception, since its
behavior is inconsistent
Waiting for your thoughts :)
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)