views:

83

answers:

5

Hi,

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 key]
    {
        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]++;
+2  A: 

Python has exactly this kind of dictionary, and they call it a defaultdict.

Mark Rushakoff
I like it... to follow the .NET naming style, it would be something like `DefaultDictionary`. The only thing that bothers me is that it sounds like it is "the default kind of dictionary"...
Thomas Levesque
OK, I think that's the most sensible answer... thanks !
Thomas Levesque
+1  A: 

I would suggest FactoryDictionary option.

Alex Ustinov
Not bad... however it describes the implementation, rather than the behavior
Thomas Levesque
+1  A: 

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.

Yogesh
A: 

How about SmartDictionary? I thought your subject was good enough. ;)

jrista
yes, but it doesn't explain in what way it is smart...
Thomas Levesque
SmartValueDictionary
jrista
A: 

FullDictionary ?

David B