[libcxx-commits] [PATCH] D57734: priority_queue::replace_top(x)

Arthur O'Dwyer via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Feb 4 20:12:31 PST 2019


Quuxplusone created this revision.
Quuxplusone added reviewers: Leandros, mclow.lists.
Quuxplusone added a project: libc++.
Herald added subscribers: libcxx-commits, ldionne, christof.

Add a fast `replace_top` method to `std::priority_queue`.

Also `std::poke_heap(first, last)`, which assumes that the given range is in max-heap order *except* for the first element, which needs to be put into its proper place. This is just like `push_heap`, except that `push_heap` assumes the *last* element is out of order, whereas `poke_heap` assumes the *first* element is out of order.

The changes in `<queue>` could use `_VSTD::poke_heap` instead of `__sift_down`, but they do not, in order to keep the `<queue>` changes usable as a single-file patch (in case someone wants to take the `<queue>` changes without the `<algorithm>` changes).

The `poke_heap` approach would replace the last line of each new method with this line instead:

  _VSTD::poke_heap(c.begin(), c.end(), comp);

----

I did this patch a while back for a blog post, and initially didn't intend to do anything else with it. Just recently I learned that people (well, Simon Brand and @Leandros) apparently want it enough to tweet about the existence of the patch
https://twitter.com/ArvidGerstmann/status/1071033081719676928
so I figure I should take the next step and actually post it for review.
(The blog post in question was https://quuxplusone.github.io/blog/2018/04/27/pq-replace-top/ )


Repository:
  rCXX libc++

https://reviews.llvm.org/D57734

Files:
  include/algorithm
  include/queue
  test/libcxx/algorithms/alg.sorting/alg.heap.operations/poke.heap/poke_heap.pass.cpp
  test/libcxx/algorithms/alg.sorting/alg.heap.operations/poke.heap/poke_heap_comp.pass.cpp
  test/libcxx/containers/container.adaptors/priority.queue/priqueue.members/reemplace_top.pass.cpp
  test/libcxx/containers/container.adaptors/priority.queue/priqueue.members/replace_top.pass.cpp
  test/libcxx/containers/container.adaptors/priority.queue/priqueue.members/replace_top_rvalue.pass.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D57734.185223.patch
Type: text/x-patch
Size: 11157 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20190205/13794119/attachment-0001.bin>


More information about the libcxx-commits mailing list