Remove the unused parameter const arch_env_t *arch_env from be_reload().
[libfirm] / ir / adt / pqueue.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @author  Christian Wuerdig, Matthias Braun
23  * @brief   Priority Queue implementation based on the heap datastructure
24  * @version $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "array.h"
31 #include "pqueue.h"
32
33 /*
34  * Implements a heap.
35  *
36  * Implementation note: It might seem strange that we start indexing at 0
37  * but use 2*i and 2*i+1 to find the left and right sucessor of an index.
38  * The trick is that for index 0 the left successor is 0 again, and the
39  * right successor is 1 in this scheme. For the right successor 1 everything
40  * works like usual. We simply took care in the algorithms that they still
41  * work with the left child of 0 being 0 again. This was possible without
42  * any extra ifs or arithmetic.
43  * Thus we can save the wastage of 1 array position you can see in other
44  * implementations or the ugly (i+1)*2 - 1 and (i+1)*2 for calculating the
45  * left and right child. (At the expense that stuff easily breaks when you make
46  * changes and don't think that the left child of 0 is 0 :-/)
47  *
48  */
49
50 typedef struct _pqueue_el_t {
51         void *data;
52         int  priority;
53 } pqueue_el_t;
54
55 struct _pqueue_t {
56         pqueue_el_t *elems;
57 };
58
59 /**
60  * Enforces the heap characteristics if the queue
61  * starting from element at position @p pos.
62  */
63 static void pqueue_heapify(pqueue_t *q, unsigned pos) {
64         unsigned len = ARR_LEN(q->elems);
65
66         while (pos * 2 < len) {
67                 pqueue_el_t tmp;
68                 unsigned    exchange = pos;
69
70                 if (q->elems[exchange].priority < q->elems[pos * 2].priority) {
71                         exchange = pos * 2;
72                 }
73
74                 if ((pos * 2 + 1) < len
75                                 && q->elems[exchange].priority < q->elems[pos * 2 + 1].priority) {
76                         exchange = pos * 2 + 1;
77                 }
78
79                 if (exchange == pos)
80                         break;
81
82                 tmp                = q->elems[pos];
83                 q->elems[pos]      = q->elems[exchange];
84                 q->elems[exchange] = tmp;
85
86                 pos = exchange;
87         }
88 }
89
90 /**
91  * Sifts up a newly inserted element at position @p pos.
92  */
93 static void pqueue_sift_up(pqueue_t *q, unsigned pos) {
94         while(q->elems[pos].priority > q->elems[pos / 2].priority) {
95                 pqueue_el_t tmp;
96
97                 tmp               = q->elems[pos];
98                 q->elems[pos]     = q->elems[pos / 2];
99                 q->elems[pos / 2] = tmp;
100
101                 pos /= 2;
102         }
103 }
104
105 pqueue_t *new_pqueue(void) {
106         pqueue_t *res = XMALLOC(pqueue_t);
107         res->elems = NEW_ARR_F(pqueue_el_t, 0);
108         return res;
109 }
110
111 void del_pqueue(pqueue_t *q) {
112         DEL_ARR_F(q->elems);
113         free(q);
114 }
115
116 void pqueue_put(pqueue_t *q, void *data, int priority) {
117         pqueue_el_t el;
118
119         el.data     = data;
120         el.priority = priority;
121
122         ARR_APP1(pqueue_el_t, q->elems, el);
123
124         pqueue_sift_up(q, ARR_LEN(q->elems) - 1);
125 }
126
127 void *pqueue_pop_front(pqueue_t *q) {
128         switch(ARR_LEN(q->elems)) {
129                 case 0:
130                         assert(0 && "Attempt to retrieve element from empty priority queue.");
131                         return NULL;
132                         break;
133                 case 1:
134                         ARR_SHRINKLEN(q->elems, 0);
135                         return q->elems[0].data;
136                         break;
137                 default: {
138                         void *data = q->elems[0].data;
139                         int  len   = ARR_LEN(q->elems) - 1;
140
141                         q->elems[0] = q->elems[len];
142                         ARR_SHRINKLEN(q->elems, len);
143                         pqueue_heapify(q, 0);
144
145                         return data;
146                 }
147         }
148 }
149
150 int pqueue_length(const pqueue_t *q) {
151         return ARR_LEN(q->elems);
152 }
153
154 int pqueue_empty(const pqueue_t *q) {
155         return ARR_LEN(q->elems) == 0;
156 }