[PATCH] [libc++] Fix std::make_heap's worst case time complexity

David Majnemer david.majnemer at gmail.com
Fri Jul 18 17:07:32 PDT 2014


This should address both Richard and Marshall's review comments, many
thanks to both!


On Fri, Jul 18, 2014 at 6:51 AM, Marshall Clow <mclow.lists at gmail.com>
wrote:

>
> On Jul 15, 2014, at 12:18 PM, David Majnemer <david.majnemer at gmail.com>
> wrote:
>
> > Ping.
> >
> > On Wed, Jul 9, 2014 at 3:14 AM, David Majnemer <david.majnemer at gmail.com>
> wrote:
> >> std::make_heap is currently implemented by iteratively applying a
> >> sift_up-type algorithm.
> >> Since sift-up is O(ln n), this gives std::make_heap a worst case time
> >> complexity of O(n ln n).  Since the C++ standard mandates that
> >> std::make_heap make no more than O(3n) comparisons, this makes our
> >> std::make_heap out of spec.
> >>
> >> Fix this by introducing an implementation of __sift_down and switch
> >> std::make_heap to create the heap using it.  This gives std::make_heap
> >> linear time complexity in the worst case.
> >>
> >> This fixes PR20161.
>
> I added some more bits to the tests.
> Your change to “make_heap.pass.cpp” is not right because:
> 1) It passes a comparison function to make_heap, but that test is testing
> the version that does not take a comparison predicate.
> 2) It uses a lambda, which fails when the tests are run with -std=c++03
>
> Revised test attached.
>
> — Marshall
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140718/681cd8c4/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: updated.patch
Type: text/x-patch
Size: 9132 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140718/681cd8c4/attachment.bin>


More information about the cfe-commits mailing list