blob: 4f9a37e6bee5b537c10458c9ec18792cfd5de348 [file] [log] [blame]
Junio C Hamanob4b594a2013-06-06 19:13:50 -07001#ifndef PRIO_QUEUE_H
2#define PRIO_QUEUE_H
3
4/*
5 * A priority queue implementation, primarily for keeping track of
6 * commits in the 'date-order' so that we process them from new to old
7 * as they are discovered, but can be used to hold any pointer to
8 * struct. The caller is responsible for supplying a function to
9 * compare two "things".
10 *
11 * Alternatively, this data structure can also be used as a LIFO stack
12 * by specifying NULL as the comparison function.
13 */
14
15/*
16 * Compare two "things", one and two; the third parameter is cb_data
17 * in the prio_queue structure. The result is returned as a sign of
18 * the return value, being the same as the sign of the result of
19 * subtracting "two" from "one" (i.e. negative if "one" sorts earlier
20 * than "two").
21 */
22typedef int (*prio_queue_compare_fn)(const void *one, const void *two, void *cb_data);
23
Jeff Kinge8f91e32014-07-14 01:51:59 -040024struct prio_queue_entry {
25 unsigned ctr;
26 void *data;
27};
28
Junio C Hamanob4b594a2013-06-06 19:13:50 -070029struct prio_queue {
30 prio_queue_compare_fn compare;
Jeff Kinge8f91e32014-07-14 01:51:59 -040031 unsigned insertion_ctr;
Junio C Hamanob4b594a2013-06-06 19:13:50 -070032 void *cb_data;
33 int alloc, nr;
Jeff Kinge8f91e32014-07-14 01:51:59 -040034 struct prio_queue_entry *array;
Junio C Hamanob4b594a2013-06-06 19:13:50 -070035};
36
37/*
38 * Add the "thing" to the queue.
39 */
Denton Liu55454422019-04-29 04:28:14 -040040void prio_queue_put(struct prio_queue *, void *thing);
Junio C Hamanob4b594a2013-06-06 19:13:50 -070041
42/*
43 * Extract the "thing" that compares the smallest out of the queue,
44 * or NULL. If compare function is NULL, the queue acts as a LIFO
45 * stack.
46 */
Denton Liu55454422019-04-29 04:28:14 -040047void *prio_queue_get(struct prio_queue *);
Junio C Hamanob4b594a2013-06-06 19:13:50 -070048
Derrick Stoleeaca42402018-11-01 13:46:17 +000049/*
50 * Gain access to the "thing" that would be returned by
51 * prio_queue_get, but do not remove it from the queue.
52 */
Denton Liu55454422019-04-29 04:28:14 -040053void *prio_queue_peek(struct prio_queue *);
Derrick Stoleeaca42402018-11-01 13:46:17 +000054
Denton Liu55454422019-04-29 04:28:14 -040055void clear_prio_queue(struct prio_queue *);
Junio C Hamanob4b594a2013-06-06 19:13:50 -070056
Junio C Hamanoda24b102013-06-06 21:58:12 -070057/* Reverse the LIFO elements */
Denton Liu55454422019-04-29 04:28:14 -040058void prio_queue_reverse(struct prio_queue *);
Junio C Hamanoda24b102013-06-06 21:58:12 -070059
Junio C Hamanob4b594a2013-06-06 19:13:50 -070060#endif /* PRIO_QUEUE_H */