<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Sep 8, 2014 at 2:39 AM, Frédéric Riss <span dir="ltr"><<a href="mailto:friss@apple.com" target="_blank">friss@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">><br>
> ================<br>
> Comment at: lib/DebugInfo/DWARFContext.cpp:401<br>
> @@ +400,3 @@<br>
> +                  const std::unique_ptr<UnitType> &RHS) const {<br>
> +    return LHS->getNextUnitOffset() <= RHS->getNextUnitOffset();<br>
> +  }<br>
> ----------------<br>
> I have a sneaking suspicion that <= ordering would violate the requirements of lower_bound...<br>
><br>
> I /suspect/ the right answer is:<br>
><br>
> Off < RHS->getOffset()<br>
><br>
> LHS->getNextUnitOffset() <= Off<br>
><br>
> LHS->getNextUnitOffset() <= RHS->getOffset()<br>
><br>
> That should be a proper "less than" for a half open range [offset, next unit offset), I think... maybe?<br>
<br>
</span>I think you’re generally right. But all the documentation I can find about lower_bound tells me that only the comp(Unit, Offset) will be used by lower_bound. The other 2 functions make it adhere to the Compare concept, but I find it strange to implement unused functions just for that. Would it be legal to just pass a function accepting a Unit and an Offset?<br>
<br>
BTW, is there an authoritative freely available source regarding these sort of STL usage details?<br>
<span class=""><br>
><br>
> On 05 Sep 2014, at 22:46, Alexey Samsonov <<a href="mailto:vonosmas@gmail.com">vonosmas@gmail.com</a>> wrote:<br>
><br>
> IIRC, originally getCompileUnitForOffset() was supposed to work only for the exact Offsets (which are returned by DWARFUnit::getOffset()). Now that you're changing it to work for arbitrary offsets, I think it makes more sense to rewrite it as follows:<br>
<br>
</span>Then the comment of the function in the header was wrong.<br></blockquote><div><br></div><div>Yes, aparently it is wrong. Otherwise function implementation is plain wrong.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class=""><br>
> if (CUs.empty()) return nullptr;<br>
> auto It = std::upper_bound(CUs.begin(), CUs.end(), Offset, OffsetComparator());<br>
> if (It == CUs.begin()) return nullptr;<br>
> --It;<br>
> return (It->getOffset <= Offset && Offset < It->getNextUnitOffset()) ? It->get() : nullptr;<br>
><br>
> and leave the comparator as is. I would rather not play with non-strict comparisons.<br>
<br>
</span>I could even keep using lower_bound without modifying the comparator, and when it returns CUs.end(), check --CUs.end() for the given offset. It would be a bit more efficient than the above I believe.<br></blockquote><div><br></div><div>I'm not sure I follow. std::lower_bound() returns an element which is greater than or equal than the provided value. If you want to find the CU with the largest offset, which is still less than the offset</div><div>you provide, you should always decrement the std::lower_bound(). But than there is the special case for equal offset... that's why you should better use upper_bound, which returns the element</div><div>strictly greater than the provided value, and decrement the result unconditionally.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
So should I use David’s variation, remove the seemingly unused comparison functions, don’t touch the comparator object and rewrite the function? What do you prefer?<br>
<br>
Fred</blockquote></div><br><br clear="all"><div><br></div>-- <br><div dir="ltr">Alexey Samsonov<br><a href="mailto:vonosmas@gmail.com" target="_blank">vonosmas@gmail.com</a></div>
</div></div>