Browse Source

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>
maint
Derrick Stolee 6 years ago committed by Junio C Hamano
parent
commit
aca4240f6a
  1. 9
      prio-queue.c
  2. 6
      prio-queue.h
  3. 26
      t/helper/test-prio-queue.c
  4. 14
      t/t0009-prio-queue.sh

9
prio-queue.c

@ -85,3 +85,12 @@ void *prio_queue_get(struct prio_queue *queue)
} }
return result; 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;
}

6
prio-queue.h

@ -46,6 +46,12 @@ extern void prio_queue_put(struct prio_queue *, void *thing);
*/ */
extern void *prio_queue_get(struct prio_queue *); extern void *prio_queue_get(struct prio_queue *);


/*
* Gain access to the "thing" that would be returned by
* prio_queue_get, but do not remove it from the queue.
*/
extern void *prio_queue_peek(struct prio_queue *);

extern void clear_prio_queue(struct prio_queue *); extern void clear_prio_queue(struct prio_queue *);


/* Reverse the LIFO elements */ /* Reverse the LIFO elements */

26
t/helper/test-prio-queue.c

@ -22,14 +22,24 @@ int cmd__prio_queue(int argc, const char **argv)
struct prio_queue pq = { intcmp }; struct prio_queue pq = { intcmp };


while (*++argv) { while (*++argv) {
if (!strcmp(*argv, "get")) if (!strcmp(*argv, "get")) {
show(prio_queue_get(&pq)); void *peek = prio_queue_peek(&pq);
else if (!strcmp(*argv, "dump")) { void *get = prio_queue_get(&pq);
int *v; if (peek != get)
while ((v = prio_queue_get(&pq))) BUG("peek and get results do not match");
show(v); show(get);
} } else if (!strcmp(*argv, "dump")) {
else { void *peek;
void *get;
while ((peek = prio_queue_peek(&pq))) {
get = prio_queue_get(&pq);
if (peek != get)
BUG("peek and get results do not match");
show(get);
}
} else if (!strcmp(*argv, "stack")) {
pq.compare = NULL;
} else {
int *v = malloc(sizeof(*v)); int *v = malloc(sizeof(*v));
*v = atoi(*argv); *v = atoi(*argv);
prio_queue_put(&pq, v); prio_queue_put(&pq, v);

14
t/t0009-prio-queue.sh

@ -47,4 +47,18 @@ test_expect_success 'notice empty queue' '
test_cmp expect actual test_cmp expect actual
' '


cat >expect <<'EOF'
3
2
6
4
5
1
8
EOF
test_expect_success 'stack order' '
test-tool prio-queue stack 8 1 5 4 6 2 3 dump >actual &&
test_cmp expect actual
'

test_done test_done

Loading…
Cancel
Save