Re: [PATCH v3 1/7] prio-queue: add 'peek' operation
- From
Derrick Stolee <stolee@gmail.com>
- Date
- Sep 26, 2018, 19:15 UTC
- Message-ID
- <04c9cd31-81cb-878c-85d4-dfa93220cf8b@gmail.com>
- In-Reply-To
- <cc1ec4c2702d8ba35873600d321015bb0430d92e.1537551564.git.gitgitgadget@gmail.com>
On 9/21/2018 1:39 PM, Derrick Stolee via GitGitGadget wrote:
Show 34 quoted lines
> From: Derrick Stolee <dstolee@microsoft.com>
>
> 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.
>
> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
> ---
> prio-queue.c | 9 +++++++++
> prio-queue.h | 6 ++++++
> t/helper/test-prio-queue.c | 10 +++++++---
> 3 files changed, 22 insertions(+), 3 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;
> +}The second branch here is never run by the test suite, as the only consumers never have compare== NULL. I'll add an ability to test this "stack" behavior into t0009-prio-queue.sh.
-Stolee