diff options
author | Derrick Stolee <stolee@gmail.com> | 2018-11-01 13:46:17 +0000 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2018-11-02 12:14:21 +0900 |
commit | aca4240f6a32423df5f95de6a9354b524fe57ec5 (patch) | |
tree | 06a2b1efc6f6d9d0a66088e13965238bfb36e86b /prio-queue.c | |
parent | 2d3b1c576c85b7f5db1f418907af00ab88e0c303 (diff) | |
download | git-aca4240f6a32423df5f95de6a9354b524fe57ec5.tar.gz |
prio-queue: add 'peek' operation
When consuming a priority queue, it can be convenient to inspect
the next object that will be dequeued without actually dequeueing
it. Our existing library did not have such a 'peek' operation, so
add it as prio_queue_peek().
Add a reference-level comparison in t/helper/test-prio-queue.c
so this method is exercised by t0009-prio-queue.sh. Further, add
a test that checks the behavior when the compare function is NULL
(i.e. the queue becomes a stack).
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'prio-queue.c')
-rw-r--r-- | prio-queue.c | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/prio-queue.c b/prio-queue.c index a078451872..d3f488cb05 100644 --- a/prio-queue.c +++ b/prio-queue.c @@ -85,3 +85,12 @@ void *prio_queue_get(struct prio_queue *queue) } return result; } + +void *prio_queue_peek(struct prio_queue *queue) +{ + if (!queue->nr) + return NULL; + if (!queue->compare) + return queue->array[queue->nr - 1].data; + return queue->array[0].data; +} |