Skip to content

Commit 6d63baa

Browse files
peffgitster
authored andcommitted
prio-queue: factor out compare and swap operations
When manipulating the priority queue's heap, we frequently have to compare and swap heap entries. As we are storing only void pointers right now, this is quite easy to do inline in a few lines. However, when we start using a more complicated heap entry in a future patch, that will get longer. Factoring out these operations lets us make future changes in one place. It also makes the code a little shorter and more readable. Note that we actually accept indices into the queue array instead of pointers. This is slightly less flexible than passing pointers-to-pointers (we could not swap items from unrelated arrays, but we would not want to), but will make further refactoring simpler (and lets us avoid repeating "queue->array" at each callsite, which led to some long lines). And finally, note that we are cleaning up an accidental use of a "struct commit" pointer to hold a temporary entry during swap. Even though we currently only use this code for commits, it is supposed to be type-agnostic. In practice this didn't matter anyway because we never dereferenced the commit pointer (and on most systems, the pointer values themselves are interchangeable between types). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 66f467c commit 6d63baa

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

prio-queue.c

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
#include "cache.h"
2-
#include "commit.h"
32
#include "prio-queue.h"
43

4+
static inline int compare(struct prio_queue *queue, int i, int j)
5+
{
6+
int cmp = queue->compare(queue->array[i], queue->array[j],
7+
queue->cb_data);
8+
return cmp;
9+
}
10+
11+
static inline void swap(struct prio_queue *queue, int i, int j)
12+
{
13+
void *tmp = queue->array[i];
14+
queue->array[i] = queue->array[j];
15+
queue->array[j] = tmp;
16+
}
17+
518
void prio_queue_reverse(struct prio_queue *queue)
619
{
720
int i, j;
821

922
if (queue->compare != NULL)
1023
die("BUG: prio_queue_reverse() on non-LIFO queue");
11-
for (i = 0; i <= (j = (queue->nr - 1) - i); i++) {
12-
struct commit *swap = queue->array[i];
13-
queue->array[i] = queue->array[j];
14-
queue->array[j] = swap;
15-
}
24+
for (i = 0; i <= (j = (queue->nr - 1) - i); i++)
25+
swap(queue, i, j);
1626
}
1727

1828
void clear_prio_queue(struct prio_queue *queue)
@@ -25,37 +35,32 @@ void clear_prio_queue(struct prio_queue *queue)
2535

2636
void prio_queue_put(struct prio_queue *queue, void *thing)
2737
{
28-
prio_queue_compare_fn compare = queue->compare;
2938
int ix, parent;
3039

3140
/* Append at the end */
3241
ALLOC_GROW(queue->array, queue->nr + 1, queue->alloc);
3342
queue->array[queue->nr++] = thing;
34-
if (!compare)
43+
if (!queue->compare)
3544
return; /* LIFO */
3645

3746
/* Bubble up the new one */
3847
for (ix = queue->nr - 1; ix; ix = parent) {
3948
parent = (ix - 1) / 2;
40-
if (compare(queue->array[parent], queue->array[ix],
41-
queue->cb_data) <= 0)
49+
if (compare(queue, parent, ix) <= 0)
4250
break;
4351

44-
thing = queue->array[parent];
45-
queue->array[parent] = queue->array[ix];
46-
queue->array[ix] = thing;
52+
swap(queue, parent, ix);
4753
}
4854
}
4955

5056
void *prio_queue_get(struct prio_queue *queue)
5157
{
52-
void *result, *swap;
58+
void *result;
5359
int ix, child;
54-
prio_queue_compare_fn compare = queue->compare;
5560

5661
if (!queue->nr)
5762
return NULL;
58-
if (!compare)
63+
if (!queue->compare)
5964
return queue->array[--queue->nr]; /* LIFO */
6065

6166
result = queue->array[0];
@@ -67,18 +72,14 @@ void *prio_queue_get(struct prio_queue *queue)
6772
/* Push down the one at the root */
6873
for (ix = 0; ix * 2 + 1 < queue->nr; ix = child) {
6974
child = ix * 2 + 1; /* left */
70-
if ((child + 1 < queue->nr) &&
71-
(compare(queue->array[child], queue->array[child + 1],
72-
queue->cb_data) >= 0))
75+
if (child + 1 < queue->nr &&
76+
compare(queue, child, child + 1) >= 0)
7377
child++; /* use right child */
7478

75-
if (compare(queue->array[ix], queue->array[child],
76-
queue->cb_data) <= 0)
79+
if (compare(queue, ix, child) <= 0)
7780
break;
7881

79-
swap = queue->array[child];
80-
queue->array[child] = queue->array[ix];
81-
queue->array[ix] = swap;
82+
swap(queue, child, ix);
8283
}
8384
return result;
8485
}

0 commit comments

Comments
 (0)