allow specification of names for in parameters in spec file
[libfirm] / ir / adt / pqueue.c
index e8495b0..b721921 100644 (file)
@@ -1,7 +1,32 @@
+/*
+ * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
+ *
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
+ */
+
+/**
+ * @file
+ * @author  Christian Wuerdig, Matthias Braun
+ * @brief   Priority Queue implementation based on the heap datastructure
+ * @version $Id$
+ */
 #include "array.h"
 #include "pqueue.h"
 
-/**
+/*
  * Implements a heap.
  *
  * Implementation note: It might seem strange that we start indexing at 0
@@ -15,7 +40,6 @@
  * implementations or the ugly (i+1)*2 - 1 and (i+1)*2 for calculating the
  * left and right child. (At the expense that stuff easily breaks when you make
  * changes and don't think that the left child of 0 is 0 :-/)
- * @author matze
  *
  */
 
@@ -32,12 +56,12 @@ struct _pqueue_t {
  * Enforces the heap characteristics if the queue
  * starting from element at position @p pos.
  */
-static void pqueue_heapify(pqueue *q, int pos) {
-       int len = ARR_LEN(q->elems);
+static void pqueue_heapify(pqueue *q, unsigned pos) {
+       unsigned len = ARR_LEN(q->elems);
 
        while (pos * 2 < len) {
                pqueue_el_t tmp;
-               int         exchange = pos;
+               unsigned    exchange = pos;
 
                if (q->elems[exchange].key < q->elems[pos * 2].key) {
                        exchange = pos * 2;
@@ -61,7 +85,7 @@ static void pqueue_heapify(pqueue *q, int pos) {
 /**
  * Sifts up a newly inserted element at position @p pos.
  */
-static void pqueue_sift_up(pqueue *q, int pos) {
+static void pqueue_sift_up(pqueue *q, unsigned pos) {
        while(q->elems[pos].key > q->elems[pos / 2].key) {
                pqueue_el_t tmp;
 
@@ -77,7 +101,7 @@ static void pqueue_sift_up(pqueue *q, int pos) {
  * Creates a new priority queue.
  * @return A priority queue of initial length 0.
  */
-pqueue *new_pqueue() {
+pqueue *new_pqueue(void) {
        pqueue *res = xmalloc(sizeof(*res));
        res->elems = NEW_ARR_F(pqueue_el_t, 0);
        return res;
@@ -106,7 +130,7 @@ void pqueue_put(pqueue *q, void *data, int key) {
 
        ARR_APP1(pqueue_el_t, q->elems, el);
 
-       pqueue_sift_up(q, ARR_LEN(q->elems));
+       pqueue_sift_up(q, ARR_LEN(q->elems) - 1);
 }
 
 /**