From: Derrick Stolee Date: Wed, 26 Sep 2018 19:15:37 GMT Subject: Re: [PATCH v3 1/7] prio-queue: add 'peek' operation Message-ID: <04c9cd31-81cb-878c-85d4-dfa93220cf8b@gmail.com> In-Reply-To: On 9/21/2018 1:39 PM, Derrick Stolee via GitGitGadget wrote: > From: Derrick Stolee > > 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 > --- > 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