[Lldb-commits] [PATCH] D63268: Make UniqueCStringMap work with non-default-constructible types and other improvements/cleanups

Pavel Labath via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Jun 13 08:27:03 PDT 2019

labath marked an inline comment as done.
labath added inline comments.

Comment at: include/lldb/Core/UniqueCStringMap.h:87
   T Find(ConstString unique_cstr, T fail_value) const {
-    Entry search_entry(unique_cstr);
-    const_iterator end = m_map.end();
-    const_iterator pos = std::lower_bound(m_map.begin(), end, search_entry);
-    if (pos != end) {
-      if (pos->cstring == unique_cstr)
-        return pos->value;
-    }
+    auto pos = llvm::lower_bound(m_map, unique_cstr, Compare());
+    if (pos != m_map.end() && pos->cstring == unique_cstr)
clayborg wrote:
> What benefits does llvm::lower_bound offer here? With std::lower_bound and std::upper_bound you can write a comparison static functions that take a "const T &" on one side and a ConstString on the other. Lower bound will use one flavor ("static bool operator <(const T&, ConstString);") and upper_bound will use the other ("static bool operator <(ConstString, const T&);"). No need to specify a Compare() object. So the code would be:
> ```
> T Find(ConstString unique_cstr, T fail_value) const {
>   const_iterator end = m_map.end();
>   const_iterator pos = std::lower_bound(m_map.begin(), end, unique_cstr);
>   if (pos != end && pos->cstring == unique_cstr)
>     return pos->value;
>   return fail_value;
> }
> ```
The only benefit of using llvm lower/upper bound is that operates on ranges -- you don't need to pass the begin/end iterator pairs, you can just pass the container. Under the hood, it just delegates to the `std` version.

The issue of using the compare object is completely orthogonal to that. I could define appropriate `operator<`s on the Entry class directly, and thereby avoid the need for comparison objects. The reason I did not do that is because the Entry class is public (and other parts of the code use and manipulate it), and having a (ConstString, Entry) operator< on the Entry class would mean exposing it to the outside world.. It's not a strong reason, it seemed to me like this is the kind of thing that users outside of this class should not be able to do implicitly/accidentally, so I tried to hide it a bit.



More information about the lldb-commits mailing list