prio-queue: add prio_queue_replace()

Add a function to replace the top element of the queue that basically
does the same as prio_queue_get() followed by prio_queue_put(), but
without the work by prio_queue_get() to rebalance the heap.  It can be
used to optimize loops that get one element and then immediately add
another one.  That's common e.g., with commit history traversal, where
we get out a commit and then put in its parents.

Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe
2025-07-18 11:39:14 +02:00
committed by Junio C Hamano
parent d6ec08788e
commit 3d5091d232
3 changed files with 63 additions and 13 deletions

View File

@@ -52,6 +52,14 @@ void *prio_queue_get(struct prio_queue *);
*/
void *prio_queue_peek(struct prio_queue *);
/*
* Replace the "thing" that compares the smallest with a new "thing",
* like prio_queue_get()+prio_queue_put() would do, but in a more
* efficient way. Does the same as prio_queue_put() if the queue is
* empty.
*/
void prio_queue_replace(struct prio_queue *queue, void *thing);
void clear_prio_queue(struct prio_queue *);
/* Reverse the LIFO elements */