#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include "libiberty.h"
#include "fibheap.h"
#define FIBHEAPKEY_MIN LONG_MIN
static void fibheap_ins_root PARAMS ((fibheap_t, fibnode_t));
static void fibheap_rem_root PARAMS ((fibheap_t, fibnode_t));
static void fibheap_consolidate PARAMS ((fibheap_t));
static void fibheap_link PARAMS ((fibheap_t, fibnode_t, fibnode_t));
static void fibheap_cut PARAMS ((fibheap_t, fibnode_t, fibnode_t));
static void fibheap_cascading_cut PARAMS ((fibheap_t, fibnode_t));
static fibnode_t fibheap_extr_min_node PARAMS ((fibheap_t));
static int fibheap_compare PARAMS ((fibheap_t, fibnode_t, fibnode_t));
static int fibheap_comp_data PARAMS ((fibheap_t, fibheapkey_t, void *,
fibnode_t));
static fibnode_t fibnode_new PARAMS ((void));
static void fibnode_insert_after PARAMS ((fibnode_t, fibnode_t));
#define fibnode_insert_before(a, b) fibnode_insert_after (a->left, b)
static fibnode_t fibnode_remove PARAMS ((fibnode_t));
fibheap_t
fibheap_new ()
{
return (fibheap_t) xcalloc (1, sizeof (struct fibheap));
}
static fibnode_t
fibnode_new ()
{
fibnode_t node;
node = (fibnode_t) xcalloc (1, sizeof *node);
node->left = node;
node->right = node;
return node;
}
static inline int
fibheap_compare (heap, a, b)
fibheap_t heap ATTRIBUTE_UNUSED;
fibnode_t a;
fibnode_t b;
{
if (a->key < b->key)
return -1;
if (a->key > b->key)
return 1;
return 0;
}
static inline int
fibheap_comp_data (heap, key, data, b)
fibheap_t heap;
fibheapkey_t key;
void *data;
fibnode_t b;
{
struct fibnode a;
a.key = key;
a.data = data;
return fibheap_compare (heap, &a, b);
}
fibnode_t
fibheap_insert (heap, key, data)
fibheap_t heap;
fibheapkey_t key;
void *data;
{
fibnode_t node;
node = fibnode_new ();
node->data = data;
node->key = key;
fibheap_ins_root (heap, node);
if (heap->min == NULL || node->key < heap->min->key)
heap->min = node;
heap->nodes++;
return node;
}
void *
fibheap_min (heap)
fibheap_t heap;
{
if (heap->min == NULL)
return NULL;
return heap->min->data;
}
fibheapkey_t
fibheap_min_key (heap)
fibheap_t heap;
{
if (heap->min == NULL)
return 0;
return heap->min->key;
}
fibheap_t
fibheap_union (heapa, heapb)
fibheap_t heapa;
fibheap_t heapb;
{
fibnode_t a_root, b_root, temp;
if ((a_root = heapa->root) == NULL)
{
free (heapa);
return heapb;
}
if ((b_root = heapb->root) == NULL)
{
free (heapb);
return heapa;
}
a_root->left->right = b_root;
b_root->left->right = a_root;
temp = a_root->left;
a_root->left = b_root->left;
b_root->left = temp;
heapa->nodes += heapb->nodes;
if (fibheap_compare (heapa, heapb->min, heapa->min) < 0)
heapa->min = heapb->min;
free (heapb);
return heapa;
}
void *
fibheap_extract_min (heap)
fibheap_t heap;
{
fibnode_t z;
void *ret = NULL;
if (heap->min != NULL)
{
z = fibheap_extr_min_node (heap);
ret = z->data;
free (z);
}
return ret;
}
void *
fibheap_replace_key_data (heap, node, key, data)
fibheap_t heap;
fibnode_t node;
fibheapkey_t key;
void *data;
{
void *odata;
int okey;
fibnode_t y;
if (fibheap_comp_data (heap, key, data, node) > 0)
return NULL;
odata = node->data;
okey = node->key;
node->data = data;
node->key = key;
y = node->parent;
if (okey == key)
return odata;
if (y != NULL && fibheap_compare (heap, node, y) <= 0)
{
fibheap_cut (heap, node, y);
fibheap_cascading_cut (heap, y);
}
if (fibheap_compare (heap, node, heap->min) <= 0)
heap->min = node;
return odata;
}
void *
fibheap_replace_data (heap, node, data)
fibheap_t heap;
fibnode_t node;
void *data;
{
return fibheap_replace_key_data (heap, node, node->key, data);
}
fibheapkey_t
fibheap_replace_key (heap, node, key)
fibheap_t heap;
fibnode_t node;
fibheapkey_t key;
{
int okey = node->key;
fibheap_replace_key_data (heap, node, key, node->data);
return okey;
}
void *
fibheap_delete_node (heap, node)
fibheap_t heap;
fibnode_t node;
{
void *ret = node->data;
fibheap_replace_key (heap, node, FIBHEAPKEY_MIN);
fibheap_extract_min (heap);
return ret;
}
void
fibheap_delete (heap)
fibheap_t heap;
{
while (heap->min != NULL)
free (fibheap_extr_min_node (heap));
free (heap);
}
int
fibheap_empty (heap)
fibheap_t heap;
{
return heap->nodes == 0;
}
static fibnode_t
fibheap_extr_min_node (heap)
fibheap_t heap;
{
fibnode_t ret = heap->min;
fibnode_t x, y, orig;
for (x = ret->child, orig = NULL; x != orig && x != NULL; x = y)
{
if (orig == NULL)
orig = x;
y = x->right;
x->parent = NULL;
fibheap_ins_root (heap, x);
}
fibheap_rem_root (heap, ret);
heap->nodes--;
if (heap->nodes == 0)
heap->min = NULL;
else
{
heap->min = ret->right;
fibheap_consolidate (heap);
}
return ret;
}
static void
fibheap_ins_root (heap, node)
fibheap_t heap;
fibnode_t node;
{
if (heap->root == NULL)
{
heap->root = node;
node->left = node;
node->right = node;
return;
}
fibnode_insert_after (heap->root, node);
}
static void
fibheap_rem_root (heap, node)
fibheap_t heap;
fibnode_t node;
{
if (node->left == node)
heap->root = NULL;
else
heap->root = fibnode_remove (node);
}
static void
fibheap_consolidate (heap)
fibheap_t heap;
{
fibnode_t a[1 + 8 * sizeof (long)];
fibnode_t w;
fibnode_t y;
fibnode_t x;
int i;
int d;
int D;
D = 1 + 8 * sizeof (long);
memset (a, 0, sizeof (fibnode_t) * D);
while ((w = heap->root) != NULL)
{
x = w;
fibheap_rem_root (heap, w);
d = x->degree;
while (a[d] != NULL)
{
y = a[d];
if (fibheap_compare (heap, x, y) > 0)
{
fibnode_t temp;
temp = x;
x = y;
y = temp;
}
fibheap_link (heap, y, x);
a[d] = NULL;
d++;
}
a[d] = x;
}
heap->min = NULL;
for (i = 0; i < D; i++)
if (a[i] != NULL)
{
fibheap_ins_root (heap, a[i]);
if (heap->min == NULL || fibheap_compare (heap, a[i], heap->min) < 0)
heap->min = a[i];
}
}
static void
fibheap_link (heap, node, parent)
fibheap_t heap ATTRIBUTE_UNUSED;
fibnode_t node;
fibnode_t parent;
{
if (parent->child == NULL)
parent->child = node;
else
fibnode_insert_before (parent->child, node);
node->parent = parent;
parent->degree++;
node->mark = 0;
}
static void
fibheap_cut (heap, node, parent)
fibheap_t heap;
fibnode_t node;
fibnode_t parent;
{
fibnode_remove (node);
parent->degree--;
fibheap_ins_root (heap, node);
node->parent = NULL;
node->mark = 0;
}
static void
fibheap_cascading_cut (heap, y)
fibheap_t heap;
fibnode_t y;
{
fibnode_t z;
while ((z = y->parent) != NULL)
{
if (y->mark == 0)
{
y->mark = 1;
return;
}
else
{
fibheap_cut (heap, y, z);
y = z;
}
}
}
static void
fibnode_insert_after (a, b)
fibnode_t a;
fibnode_t b;
{
if (a == a->right)
{
a->right = b;
a->left = b;
b->right = a;
b->left = a;
}
else
{
b->right = a->right;
a->right->left = b;
a->right = b;
b->left = a;
}
}
static fibnode_t
fibnode_remove (node)
fibnode_t node;
{
fibnode_t ret;
if (node == node->left)
ret = NULL;
else
ret = node->left;
if (node->parent != NULL && node->parent->child == node)
node->parent->child = ret;
node->right->left = node->left;
node->left->right = node->right;
node->parent = NULL;
node->left = node;
node->right = node;
return ret;
}