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).
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.
Edit: 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.
Get
andRemove
in theDictionary
have the amortized complexity ofO(1)
, so callingGet
+Remove
will still give youO(1)
... – SheepDictionary<,>
implementation. (alternatively, you can grab the Mono Dictionary<,> implementation from its source code repository...) – Jankey