English 中文(简体)
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 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]++;
最佳回答

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 ?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签