有没有实施的关键删除,并获得价值在同一时间? [英] Is there any implementation to Remove by Key and get the Value at the same time?
问题描述
我做了性能关键程序(有点学术的东西),我期待,以优化在可能的情况(不喜欢它证明了这是瓶颈)。
I'm doing a performance critical program (little academic stuff) and I'm looking to optimize wherever possible (not like it proved "this is the" bottleneck).
我有一个自定义字典结构(一个围绕.NET 包装字典<,>
),我会不断地在一个阶段删除项目(由键
值)。我需要的值
的删除的项目。现在我要做的:
I have a custom dictionary structure (a wrapper around .NET Dictionary<,>
) and I would constantly Remove items at one stage (by the Key
value). I need the Value
of the removed items. Right now I have to do:
T t;
if !TryGet(key, out t)
return false;
Remove(key);
这是两个查找。我喜欢这样的:
That's two lookups. I would love this:
public bool Remove(S key, out T value)
{
// implementation
}
我知道有没有在框架中,但有一个实现地方?如果是的话我会改变我的背字典的那一个。
I know there is nothing in the framework, but is there an implementation somewhere? If so I would change my backing dictionary with that one.
编辑:嗯,我知道这两个 TryGetValue
和删除
为O(1) 。只要知道如果有任何馆藏结构,将给予在短短的一个查询相同的效果。正如我所说的,我想优化尽可能多地。只要知道。
Hmm I know both TryGetValue
and Remove
are O(1). Just knowing if there is any collection structure that would give the same effect in just one lookup. As I said I'm trying to optimize as much as possible. Just knowing.
推荐答案
Copenehagen的通用收集库的大学有出现做你想做什么 Dictionary.Remove()
方法:
The University of Copenehagen's Generic Collection Library has a Dictionary.Remove()
method that appears to do what you want:
BOOL删除(K K,OUT V V)
bool Remove(K k, out V v)
返回true,如果字典中包含的项目,其关键字等于ķ 如果是删除该条目,并分配相应的值到v; 否则返回false和T的默认值分配给v。
Returns true if the dictionary contains an entry whose key equals k and if so removes that entry and assigns the associated value to v; otherwise returns false and assigns the default value for T to v.
我还没有使用这个库自己,但我已经看到了这里推荐几十倍的堆栈溢出。它是免费的商业使用,受到这MIT风格许可。
I've not used this library myself, but I've seen it recommended a few times here on Stack Overflow. It's free to use commercially, subject to this MIT-style license.
这篇关于有没有实施的关键删除,并获得价值在同一时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!