“聪明”的名称是什么?字典(哈希表)?

发布于 2024-08-11 03:45:06 字数 771 浏览 4 评论 0原文

我正在为自定义字典寻找一个好名称,如果所请求的键不存在,它会使用委托自动初始化该值。索引器实现应该有助于理解它的作用:

    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;
        }
    }

我的问题不在于代码,代码工作正常,但我似乎找不到此类的正确名称...我想到了 AutoInitDictionary ,但这听起来不对,并且并没有真正传达“所有键都可以假设存在”的想法。

你会如何命名这样一个类?任何建议将不胜感激。


PS:如何使用它的示例:

var charFrequencies = new AutoInitDictionary<char, int>(key => 0);
foreach(char c in text)
    charFrequencies[c]++;

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]++;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暮年 2024-08-18 03:45:06

Python 正是有这种字典,他们称之为 defaultdict< /代码>

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

神回复 2024-08-18 03:45:06

我建议使用 FactoryDictionary 选项。

I would suggest FactoryDictionary option.

荭秂 2024-08-18 03:45:06

VirtualDictionary 怎么样?

vir·tu·al (vûr'chōō-əl):

虽然不是实际事实、形式,但存在或产生本质或效果

存在于头脑中,尤其是作为想象力的产物

具有这样的力量、力量或效果,尽管实际上或明确不是这样

通过计算机软件临时模拟或扩展

所有 4 个定义在某种程度上与您的实现相关。

编辑:即使是DynamicDictionary也会很好。

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.

神也荒唐 2024-08-18 03:45:06

智能词典怎么样?我认为你的主题足够好。 ;)

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

高速公鹿 2024-08-18 03:45:06

全词典?

FullDictionary ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文