“聪明”的名称是什么?字典(哈希表)?
我正在为自定义字典寻找一个好名称,如果所请求的键不存在,它会使用委托自动初始化该值。索引器实现应该有助于理解它的作用:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Python 正是有这种字典,他们称之为
defaultdict< /代码>
。
Python has exactly this kind of dictionary, and they call it a
defaultdict
.我建议使用 FactoryDictionary 选项。
I would suggest FactoryDictionary option.
VirtualDictionary
怎么样?所有 4 个定义在某种程度上与您的实现相关。
编辑:即使是
DynamicDictionary
也会很好。What about
VirtualDictionary
?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 ?