[LLVMdev] Adding DenseMap::FindAndConstruct with a default value

Matthijs Kooijman matthijs at stdin.nl
Thu Jun 5 07:49:41 PDT 2008


Hi All,

I've been fiddling around with a DenseMap to store cached copies of some
result. In short, I'm doing the following:

It = Map.find(Key)
if (It != Map.end() && It->second != Unknown)
	Return It->second;

// do_stuff

return Map[Key] = Result;

However, I this requires two lookups in the hash table, which is not so nice.
Currently, there is no way to write this down so you only do one lookup.
Intuitively, you'd write:

ValueT &Value = Map[Key];
if (Value != Unknown)
	return Value;

// do_stuff

return Value = Result;

However, I'm using an enum as a map value, so when you do Map[Key] while Key
is not yet in the map, the new value will be unitialized (I can't define a
constructor on an enum, right?).

So, to solve this, I propose adding a second version of
DenseMap::FindAndConstruct, which has an extra argument for the default value.
So, it would do the same thing as the original (return a reference to the
value from the map, adding it if it's not already in), but instead of using
ValueT() as a default value, it would use an argument.

Any objections to adding this? Suggestions for alternatives?

Gr.

Matthijs
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20080605/d8bfe66a/attachment.sig>


More information about the llvm-dev mailing list