pqueue.c 2.44 KB
Newer Older
1
/*
schu committed
2
 * Copyright (C) 2009-2012 the libgit2 contributors
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7 8 9 10
 */

#include "common.h"
#include "pqueue.h"

Vicent Marti committed
11 12
#define left(i)	((i) << 1)
#define right(i) (((i) << 1) + 1)
13 14 15 16 17 18
#define parent(i) ((i) >> 1)

int git_pqueue_init(git_pqueue *q, size_t n, git_pqueue_cmp cmppri)
{
	assert(q);

Vicent Marti committed
19
	/* Need to allocate n+1 elements since element 0 isn't used. */
20 21
	q->d = git__malloc((n + 1) * sizeof(void *));
	GITERR_CHECK_ALLOC(q->d);
22

Vicent Marti committed
23 24 25
	q->size = 1;
	q->avail = q->step = (n + 1); /* see comment above about n+1 */
	q->cmppri = cmppri;
26

27
	return 0;
28 29 30 31 32
}


void git_pqueue_free(git_pqueue *q)
{
33
	git__free(q->d);
34 35 36
	q->d = NULL;
}

37 38 39 40
void git_pqueue_clear(git_pqueue *q)
{
	q->size = 1;
}
41 42 43

size_t git_pqueue_size(git_pqueue *q)
{
Vicent Marti committed
44 45
	/* queue element 0 exists but doesn't count since it isn't used. */
	return (q->size - 1);
46 47 48 49 50
}


static void bubble_up(git_pqueue *q, size_t i)
{
Vicent Marti committed
51 52
	size_t parent_node;
	void *moving_node = q->d[i];
53

Vicent Marti committed
54 55 56 57 58
	for (parent_node = parent(i);
			((i > 1) && q->cmppri(q->d[parent_node], moving_node));
			i = parent_node, parent_node = parent(i)) {
		q->d[i] = q->d[parent_node];
	}
59

Vicent Marti committed
60
	q->d[i] = moving_node;
61 62 63 64 65
}


static size_t maxchild(git_pqueue *q, size_t i)
{
Vicent Marti committed
66
	size_t child_node = left(i);
67

Vicent Marti committed
68 69
	if (child_node >= q->size)
		return 0;
70

Vicent Marti committed
71 72 73
	if ((child_node + 1) < q->size &&
		q->cmppri(q->d[child_node], q->d[child_node + 1]))
		child_node++; /* use right child instead of left */
74

Vicent Marti committed
75
	return child_node;
76 77 78 79 80
}


static void percolate_down(git_pqueue *q, size_t i)
{
Vicent Marti committed
81 82
	size_t child_node;
	void *moving_node = q->d[i];
83

Vicent Marti committed
84 85 86 87 88
	while ((child_node = maxchild(q, i)) != 0 &&
			q->cmppri(moving_node, q->d[child_node])) {
		q->d[i] = q->d[child_node];
		i = child_node;
	}
89

Vicent Marti committed
90
	q->d[i] = moving_node;
91 92 93 94 95
}


int git_pqueue_insert(git_pqueue *q, void *d)
{
Vicent Marti committed
96 97 98
	void *tmp;
	size_t i;
	size_t newsize;
99

Vicent Marti committed
100
	if (!q) return 1;
101

Vicent Marti committed
102 103 104
	/* allocate more memory if necessary */
	if (q->size >= q->avail) {
		newsize = q->size + q->step;
105 106
		tmp = git__realloc(q->d, sizeof(void *) * newsize);
		GITERR_CHECK_ALLOC(tmp);
107

Vicent Marti committed
108 109 110
		q->d = tmp;
		q->avail = newsize;
	}
111

Vicent Marti committed
112 113 114 115
	/* insert item */
	i = q->size++;
	q->d[i] = d;
	bubble_up(q, i);
116

117
	return 0;
118 119 120 121 122
}


void *git_pqueue_pop(git_pqueue *q)
{
Vicent Marti committed
123
	void *head;
124

Vicent Marti committed
125 126
	if (!q || q->size == 1)
		return NULL;
127

Vicent Marti committed
128 129 130
	head = q->d[1];
	q->d[1] = q->d[--q->size];
	percolate_down(q, 1);
131

Vicent Marti committed
132
	return head;
133 134 135 136 137
}


void *git_pqueue_peek(git_pqueue *q)
{
Vicent Marti committed
138 139 140
	if (!q || q->size == 1)
		return NULL;
	return q->d[1];
141
}