aboutsummaryrefslogtreecommitdiffstats
path: root/heap.c
blob: 3e192e7417d2b5077331472b02a086e6d3b2255d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * heap.c
 * implementation of a generic heap
 *
 * Erik Liodden
 */

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "structure.h"
#include "misc.h"

#define ceil(a, b) 	(a % b) ? a/b + 1 : a/b
#define parent(i) 	ceil(i, 2) - 1
#define right(i) 	2*i + 1
#define left(i) 	2*i + 2

static void grow_heap(struct heap *h)
{
	h->alloc_length *= 2;
	h->elems = realloc(h->elems, h->alloc_length * h->elem_size);
	assert(h->elems != NULL);
}

static void heap_init_from_array(struct heap *h, void *elems, int nel, size_t
		elem_size, void (*freefn)(void *), int (*compar)(const void *,
			const void *))
{
	h->elem_size = elem_size;
	h->log_length = nel-1;
	h->alloc_length = nel-1;
	h->elems = elems;
	h->freefn = freefn;
	h->compar = compar;
}

void heap_init(struct heap *h, size_t elem_size, void (*freefn)(void *),
		int (*compar)(const void *, const void *))
{
	h->elem_size = elem_size;
	h->log_length = 0;
	h->alloc_length = 4;
	h->elems = allocate(h->alloc_length * elem_size);
	h->freefn = freefn;
	h->compar = compar;
}

void heap_dispose(struct heap *h)
{
	int i;
	if (h->freefn) {
		for (i = 0; i < h->log_length; i++)
			h->freefn((char *)h->elems + (i * h->elem_size));
	}
	free(h->elems);
}

void heap_max_heapify(struct heap *h, int i)
{
	int largest;
	int l = left(i);
	int r = right(i);

	if (l <= h->log_length && h->compar((char *)h->elems + l*h->elem_size,
				(char *)h->elems + i*h->elem_size) > 0)
		largest = l;
	else
		largest = i;
	if (r <= h->log_length && h->compar((char *)h->elems + r*h->elem_size,
				(char *)h->elems + largest*h->elem_size) > 0)
		largest = r;
	if (largest != i) {
		swap((char *)h->elems + i*h->elem_size, (char *)h->elems +
				largest*h->elem_size, h->elem_size);
		heap_max_heapify(h, largest);
	}
}

void heap_build_max_heap(struct heap *h)
{
	int i;
	for (i = h->log_length/2; i >= 0; i--)
		heap_max_heapify(h, i);
}

void heap_maximum(struct heap *h, void *elem_addr)
{
	if (h->log_length >= 0)
		memcpy(elem_addr, h->elems, h->elem_size);
}

void heap_extract_max(struct heap *h, void *elem_addr)
{
	if (h->log_length <= 0) {
		fprintf(stderr, "error: underflow. heap empty\n");
		exit(EXIT_FAILURE);
	}
	heap_maximum(h, elem_addr);
	memcpy(h->elems, (char *)h->elems + (h->log_length-1)*h->elem_size,
			h->elem_size);
	h->log_length--;
	heap_max_heapify(h, 0);
}

void heap_increase_key(struct heap *h, int i, void *key)
{
	int p;
	if (h->compar(key, (char *)h->elems + i*h->elem_size) < 0) {
		fprintf(stderr, "new key is smaller than previos key\n");
		exit(EXIT_FAILURE);
	}
	memcpy((char *)h->elems + i*h->elem_size, key, h->elem_size);
	p = parent(i);
	while (i > 0 && h->compar((char *)h->elems + p*h->elem_size,
				(char *)h->elems + i*h->elem_size) < 0) {
		swap((char *)h->elems + i*h->elem_size, (char *)h->elems +
				p*h->elem_size, h->elem_size);
		i = parent(i);
		p = parent(i);
	}
}

void heap_max_heap_insert(struct heap *h, void *key)
{
	int i, p;
	if (h->log_length == h->alloc_length)
		grow_heap(h);
	void *target = (char *)h->elems + (h->log_length * h->elem_size);
	memcpy(target, key, h->elem_size);
	i = h->log_length;
	p = parent(i);
	while (i > 0 && h->compar((char *)h->elems + p*h->elem_size,
				(char *)h->elems + i*h->elem_size) < 0) {
		swap((char *)h->elems + i*h->elem_size, (char *)h->elems +
				p*h->elem_size, h->elem_size);
		i = parent(i);
		p = parent(i);
	}
	h->log_length++;
}

int heap_length(struct heap *h)
{
	return h->log_length;
}

void heap_sort(void *base, size_t nel, size_t width,
		int (*compar)(const void *, const void *))
{
	int i;
	struct heap h;

	heap_init_from_array(&h, base, nel, width, NULL, compar);
	heap_build_max_heap(&h);
	for (i = nel-1; i > 0; i--) {
		swap((char *)base, (char *)base + i*width, width);
		h.log_length--;
		heap_max_heapify(&h, 0);
	}
}