pqueue.c 3.3 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
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 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * This file is based on a modified version of the priority queue found
 * in the Apache project and libpqueue library.
 * 
 * https://github.com/vy/libpqueue
 * 
 * Original file notice:
 * 
 * Copyright 2010 Volkan Yazici <volkan.yazici@gmail.com>
 * Copyright 2006-2010 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
28 29 30 31 32
 */

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

Vicent Marti committed
33 34
#define left(i)	((i) << 1)
#define right(i) (((i) << 1) + 1)
35 36 37 38 39 40
#define parent(i) ((i) >> 1)

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

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

Vicent Marti committed
45 46 47
	q->size = 1;
	q->avail = q->step = (n + 1); /* see comment above about n+1 */
	q->cmppri = cmppri;
48

49
	return 0;
50 51 52 53 54
}


void git_pqueue_free(git_pqueue *q)
{
55
	git__free(q->d);
56 57 58
	q->d = NULL;
}

59 60 61 62
void git_pqueue_clear(git_pqueue *q)
{
	q->size = 1;
}
63 64 65

size_t git_pqueue_size(git_pqueue *q)
{
Vicent Marti committed
66 67
	/* queue element 0 exists but doesn't count since it isn't used. */
	return (q->size - 1);
68 69 70 71 72
}


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

Vicent Marti committed
76 77 78 79 80
	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];
	}
81

Vicent Marti committed
82
	q->d[i] = moving_node;
83 84 85 86 87
}


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

Vicent Marti committed
90 91
	if (child_node >= q->size)
		return 0;
92

Vicent Marti committed
93 94 95
	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 */
96

Vicent Marti committed
97
	return child_node;
98 99 100 101 102
}


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

Vicent Marti committed
106 107 108 109 110
	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;
	}
111

Vicent Marti committed
112
	q->d[i] = moving_node;
113 114 115 116 117
}


int git_pqueue_insert(git_pqueue *q, void *d)
{
Vicent Marti committed
118 119 120
	void *tmp;
	size_t i;
	size_t newsize;
121

Vicent Marti committed
122
	if (!q) return 1;
123

Vicent Marti committed
124 125 126
	/* allocate more memory if necessary */
	if (q->size >= q->avail) {
		newsize = q->size + q->step;
127 128
		tmp = git__realloc(q->d, sizeof(void *) * newsize);
		GITERR_CHECK_ALLOC(tmp);
129

Vicent Marti committed
130 131 132
		q->d = tmp;
		q->avail = newsize;
	}
133

Vicent Marti committed
134 135 136 137
	/* insert item */
	i = q->size++;
	q->d[i] = d;
	bubble_up(q, i);
138

139
	return 0;
140 141 142 143 144
}


void *git_pqueue_pop(git_pqueue *q)
{
Vicent Marti committed
145
	void *head;
146

Vicent Marti committed
147 148
	if (!q || q->size == 1)
		return NULL;
149

Vicent Marti committed
150 151 152
	head = q->d[1];
	q->d[1] = q->d[--q->size];
	percolate_down(q, 1);
153

Vicent Marti committed
154
	return head;
155 156 157 158 159
}


void *git_pqueue_peek(git_pqueue *q)
{
Vicent Marti committed
160 161 162
	if (!q || q->size == 1)
		return NULL;
	return q->d[1];
163
}