Similar to std::unordered_map::insert_or_assign
Details
Diff Detail
- Repository
- rL LLVM
- Build Status
Buildable 38213 Build 38212: arc lint + arc unit
Event Timeline
It turns out that I need StringMap::insert_or_assign, instead of DenseMap, for D67656.
include/llvm/ADT/StringMap.h | ||
---|---|---|
394 | Please add some description. |
Could we have this as a standalone helper function (STLExtras or the like) rather than implementing it in every container?
We only need it in 2 unordered associative containers: DenseMap, and StringMap. Member methods have the advantage that they have better discoverability and can be easily be completed. This method is simple enough now. Placing it in STLExtras does not save much.
There's also MapVector, I guess, but fair enough - for consistency with the standard containers it's not too much.
unittests/ADT/StringMapTest.cpp | ||
---|---|---|
273–280 | You could use a noisy type here to test that only the appropriate operations were executed (and/or that a type that's not default-constructible is usable here, whereas it wouldn't be if you replaced the implementation with 't[x] = y' for instance) Countable, further down in this test file, could be used to help there. |
You could use a noisy type here to test that only the appropriate operations were executed
Add struct NoDefaultConstructible {...}
include/llvm/ADT/StringMap.h | ||
---|---|---|
397 | Is this API consistent with the C++ standard version? I would guess the c++ standard version takes a pair<K, V>? I'd sort of come back to "why not a free function" if this isn't going to match the standard API & make API interoperability completely transparent. | |
unittests/ADT/StringMapTest.cpp | ||
273–280 | I was thinking/hoping one of the existing test types could be used and might provide more robust checking? One that demonstrates the first insert_or_assign only calls the move constructor and the second only calls the move assignment operator? |
include/llvm/ADT/StringMap.h | ||
---|---|---|
397 | The standard version provides two overloads: template <class M> pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj); template <class M> pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj); This is unnecessary for StringRef, which is cheap to copy (2 words). So I simply add a method that takes a StringRef instead of creating 2 overloads. | |
unittests/ADT/StringMapTest.cpp | ||
273–280 | There is no existing trait-based test types. I can add move constructor/assignment operator to ensure no copy assignment is called if you are strong about this. |
include/llvm/ADT/StringMap.h | ||
---|---|---|
397 | Ah, cool - thanks! | |
unittests/ADT/StringMapTest.cpp | ||
273–280 | I was thinking something like CountCtorCopyAndMove (we might even have a generalized one of these somewhere as a test utility for all of libSupport? Or certainly they've been repeated in a few tests) that distinguishes (move/copy) assignments from (move/copy) constructions. |
Seems good with the test fleshed out more before committing - thanks for your patience!
Please add some description.