2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @author Christian Wuerdig, Matthias Braun
23 * @brief Priority Queue implementation based on the heap datastructure
32 * Implementation note: It might seem strange that we start indexing at 0
33 * but use 2*i and 2*i+1 to find the left and right sucessor of an index.
34 * The trick is that for index 0 the left successor is 0 again, and the
35 * right successor is 1 in this scheme. For the right successor 1 everything
36 * works like usual. We simply took care in the algorithms that they still
37 * work with the left child of 0 being 0 again. This was possible without
38 * any extra ifs or arithmetic.
39 * Thus we can save the wastage of 1 array position you can see in other
40 * implementations or the ugly (i+1)*2 - 1 and (i+1)*2 for calculating the
41 * left and right child. (At the expense that stuff easily breaks when you make
42 * changes and don't think that the left child of 0 is 0 :-/)
46 typedef struct _pqueue_el_t {
56 * Enforces the heap characteristics if the queue
57 * starting from element at position @p pos.
59 static void pqueue_heapify(pqueue_t *q, unsigned pos) {
60 unsigned len = ARR_LEN(q->elems);
62 while (pos * 2 < len) {
64 unsigned exchange = pos;
66 if (q->elems[exchange].priority < q->elems[pos * 2].priority) {
70 if ((pos * 2 + 1) < len
71 && q->elems[exchange].priority < q->elems[pos * 2 + 1].priority) {
72 exchange = pos * 2 + 1;
79 q->elems[pos] = q->elems[exchange];
80 q->elems[exchange] = tmp;
87 * Sifts up a newly inserted element at position @p pos.
89 static void pqueue_sift_up(pqueue_t *q, unsigned pos) {
90 while(q->elems[pos].priority > q->elems[pos / 2].priority) {
94 q->elems[pos] = q->elems[pos / 2];
95 q->elems[pos / 2] = tmp;
101 pqueue_t *new_pqueue(void) {
102 pqueue_t *res = xmalloc(sizeof(*res));
103 res->elems = NEW_ARR_F(pqueue_el_t, 0);
107 void del_pqueue(pqueue_t *q) {
112 void pqueue_put(pqueue_t *q, void *data, int priority) {
116 el.priority = priority;
118 ARR_APP1(pqueue_el_t, q->elems, el);
120 pqueue_sift_up(q, ARR_LEN(q->elems) - 1);
123 void *pqueue_pop_front(pqueue_t *q) {
124 switch(ARR_LEN(q->elems)) {
126 assert(0 && "Attempt to retrieve element from empty priority queue.");
130 ARR_SHRINKLEN(q->elems, 0);
131 return q->elems[0].data;
134 void *data = q->elems[0].data;
135 int len = ARR_LEN(q->elems) - 1;
137 q->elems[0] = q->elems[len];
138 ARR_SHRINKLEN(q->elems, len);
139 pqueue_heapify(q, 0);
146 int pqueue_length(const pqueue_t *q) {
147 return ARR_LEN(q->elems);
150 int pqueue_empty(const pqueue_t *q) {
151 return ARR_LEN(q->elems) == 0;