Which name for a "smart" dictionary (hashtable)?
I'm looking for a good name for a custom dictionary which automatically initializes the value for a requested key if it doesn't exist, using a delegate. The indexer implementation should help understand what it does :
public V this[K ke开发者_StackOverflowy]
{
get
{
V value;
if (!_dictionary.TryGetValue(key, out value))
{
value = _defaultValueGenerator(key);
_dictionary[key] = value;
}
return value;
}
set
{
_dictionary[key] = value;
}
}
My problem is not about the code, which works fine, but I can't seem to find a proper name for this class... I thought about AutoInitDictionary
, but it doesn't sound right, and doesn't really conveys the idea that "all keys can be assumed to exist".
How would you name such a class ? Any suggestion would be appreciated.
PS: an example of how it could be used :
var charFrequencies = new AutoInitDictionary<char, int>(key => 0);
foreach(char c in text)
charFrequencies[c]++;
Python has exactly this kind of dictionary, and they call it a defaultdict
.
I would suggest FactoryDictionary option.
What about VirtualDictionary
?
vir·tu·al (vûr'chōō-əl):
Existing or resulting in essence or effect though not in actual fact, form
Existing in the mind, especially as a product of the imagination
Being such in power, force, or effect, though not actually or expressly such
Temporarily simulated or extended by computer software
All 4 definitions somewhat relate to your implementation.
EDIT: Even DynamicDictionary
will be good.
How about SmartDictionary? I thought your subject was good enough. ;)
FullDictionary ?
精彩评论