summaryrefslogtreecommitdiffstats
path: root/src/heap.c
blob: e93abe3b15fd61304763b3b2b067841d4538c6de (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
164
165
166
/* heap.c - an array based d-ary heap implementation
 *
 * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 or later as
 * published by the Free Software Foundation.
 *
 * See http://www.gnu.org/ for details.
 */

#include <errno.h>
#include <string.h>
#include <libtf/heap.h>

#define compare_values(a, b) tf_mtime_diff(a, b)

static inline int tf_heap_parent(int index)
{
	return (index - TF_HEAP_ITEM0 - 1) / TF_HEAP_D + TF_HEAP_ITEM0;
}

static inline int tf_heap_first_child(int index)
{
	return TF_HEAP_D * (index - TF_HEAP_ITEM0) + TF_HEAP_ITEM0 + 1;
}

#if 0
static void tf_heap_verify(struct tf_heap_head *head)
{
	int i, count = 0;

	for (i = TF_HEAP_ITEM0; i < head->num_items + TF_HEAP_ITEM0; i++) {
		if (head->item[i].ptr->index != i) {
			printf("Heap item %d is corrupt ptr->index=%d\n", i, head->item[i].ptr->index);
			count++;
		}
	}
	TF_BUG_ON(count);
}
#endif

static inline
void tf_heap_downheap(struct tf_heap_child *heap, int last_index, int index)
{
	struct tf_heap_child he = heap[index];
	struct tf_heap_child *minpos, *pos;
	int c, i, mi;

	while (1) {
		c = tf_heap_first_child(index);
		pos = &heap[c];

		/* find minimum child */
		minpos = pos;
		mi = 0;
		if (likely(c + TF_HEAP_D - 1 < last_index)) {
			for (i = 1, pos++; i < TF_HEAP_D; i++, pos++)
				if (compare_values(pos->val, minpos->val) < 0)
					minpos = pos, mi = i;
		} else if (c < last_index) {
			for (i = 1, pos++; c + i < last_index; i++, pos++)
				if (compare_values(pos->val, minpos->val) < 0)
					minpos = pos, mi = i;
	        } else
			break;

		if (compare_values(he.val, minpos->val) <= 0)
			break;

		heap[index] = *minpos;
		minpos->ptr->index = index;
		index = c + mi;
	}

	heap[index] = he;
	he.ptr->index = index;
}

static inline
void tf_heap_upheap(struct tf_heap_child *heap, int index)
{
	struct tf_heap_child he = heap[index];
	int p = tf_heap_parent(index);

	while (likely(index > TF_HEAP_ITEM0) &&
	       compare_values(heap[p].val, he.val) > 0) {
		heap[index] = heap[p];
		heap[index].ptr->index = index;
		index = p;
		p = tf_heap_parent(p);
	}

	heap[index] = he;
	he.ptr->index = index;
}

static inline
void tf_heap_heapify(struct tf_heap_head *head, int index)
{
	struct tf_heap_child *heap = head->item;

	if (likely(index > TF_HEAP_ITEM0) &&
	    compare_values(heap[index].val, heap[tf_heap_parent(index)].val) <= 0)
		tf_heap_upheap(heap, index);
	else
		tf_heap_downheap(heap, TF_HEAP_ITEM0 + head->num_items, index);
}

int __tf_heap_grow(struct tf_heap_head *head)
{
	void *item;

	if (head->allocated)
		head->allocated *= 2;
	else
		head->allocated = 128;

	item = realloc(head->item, head->allocated * sizeof(head->item[0]));
	if (item == NULL)
		return -ENOMEM;

	head->item = item;
	return 0;
}

void tf_heap_insert(struct tf_heap_node *node, struct tf_heap_head *head,
		    tf_heap_priority val)
{
	int i;

	tf_heap_prealloc(head, head->num_items + 1);

	i = node->index = TF_HEAP_ITEM0 + head->num_items;
	head->num_items++;
	head->item[i].ptr = node;
	head->item[i].val = val;
	tf_heap_upheap(head->item, i);
}

void tf_heap_delete(struct tf_heap_node *node, struct tf_heap_head *head)
{
	int index = node->index;

	if (index == 0)
		return;

	head->num_items--;
	if (likely(index < head->num_items + TF_HEAP_ITEM0)) {
		head->item[index] = head->item[head->num_items+TF_HEAP_ITEM0];
		tf_heap_heapify(head, index);
	}
	node->index = 0;
}

void tf_heap_change(struct tf_heap_node *node, struct tf_heap_head *head,
		    tf_heap_priority val)
{
	if (likely(node->index != 0)) {
		head->item[node->index].val = val;
		tf_heap_heapify(head, node->index);
	} else {
		tf_heap_insert(node, head, val);
	}
}