Commit 4075e060b45a73834a24684ed835d52f7176d58b

Russell Belfer 2014-02-03T21:02:08

Replace pqueue with code from hashsig heap I accidentally wrote a separate priority queue implementation when I was working on file rename detection as part of the file hash signature calculation code. To simplify licensing terms, I just adapted that to a general purpose priority queue and replace the old priority queue implementation that was borrowed from elsewhere. This also removes parts of the COPYING document that no longer apply to libgit2.

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
diff --git a/COPYING b/COPYING
index f7e9f3a..1817372 100644
--- a/COPYING
+++ b/COPYING
@@ -388,19 +388,7 @@ Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
 
 ----------------------------------------------------------------------
 
-The priority queue implementation is based on code licensed under the
-Apache 2.0 license:
-
-	Copyright 2010 Volkan Yazıcı <volkan.yazici@gmail.com>
-	Copyright 2006-2010 The Apache Software Foundation
-
-The full text of the Apache 2.0 license is available at:
-
-	http://www.apache.org/licenses/LICENSE-2.0
-
-----------------------------------------------------------------------
-
-The Clay framework is licensed under the MIT license:
+The Clar framework is licensed under the MIT license:
 
 Copyright (C) 2011 by Vicent Marti
 
@@ -930,64 +918,3 @@ necessary.  Here is a sample; alter the names:
 That's all there is to it!
 
 ----------------------------------------------------------------------
-
-Portions of src/win32/posix_w32.c are derrived from link_win32.c in PHP:
-
--------------------------------------------------------------------- 
-                  The PHP License, version 3.01
-Copyright (c) 1999 - 2012 The PHP Group. All rights reserved.
--------------------------------------------------------------------- 
-
-Redistribution and use in source and binary forms, with or without
-modification, is permitted provided that the following conditions
-are met:
-
-  1. Redistributions of source code must retain the above copyright
-     notice, this list of conditions and the following disclaimer.
- 
-  2. Redistributions in binary form must reproduce the above copyright
-     notice, this list of conditions and the following disclaimer in
-     the documentation and/or other materials provided with the
-     distribution.
- 
-  3. The name "PHP" must not be used to endorse or promote products
-     derived from this software without prior written permission. For
-     written permission, please contact group@php.net.
-  
-  4. Products derived from this software may not be called "PHP", nor
-     may "PHP" appear in their name, without prior written permission
-     from group@php.net.  You may indicate that your software works in
-     conjunction with PHP by saying "Foo for PHP" instead of calling
-     it "PHP Foo" or "phpfoo"
- 
-  5. The PHP Group may publish revised and/or new versions of the
-     license from time to time. Each version will be given a
-     distinguishing version number.
-     Once covered code has been published under a particular version
-     of the license, you may always continue to use it under the terms
-     of that version. You may also choose to use such covered code
-     under the terms of any subsequent version of the license
-     published by the PHP Group. No one other than the PHP Group has
-     the right to modify the terms applicable to covered code created
-     under this License.
-
-  6. Redistributions of any form whatsoever must retain the following
-     acknowledgment:
-     "This product includes PHP software, freely available from
-     <http://www.php.net/software/>".
-
-THIS SOFTWARE IS PROVIDED BY THE PHP DEVELOPMENT TEAM ``AS IS'' AND 
-ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
-THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
-PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE PHP
-DEVELOPMENT TEAM OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
-INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
-(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
-SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
-STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
-OF THE POSSIBILITY OF SUCH DAMAGE.
-
--------------------------------------------------------------------- 
-
diff --git a/src/commit_list.c b/src/commit_list.c
index 64416e5..9db3f56 100644
--- a/src/commit_list.c
+++ b/src/commit_list.c
@@ -11,10 +11,10 @@
 #include "pool.h"
 #include "odb.h"
 
-int git_commit_list_time_cmp(void *a, void *b)
+int git_commit_list_time_cmp(const void *a, const void *b)
 {
-	git_commit_list_node *commit_a = (git_commit_list_node *)a;
-	git_commit_list_node *commit_b = (git_commit_list_node *)b;
+	const git_commit_list_node *commit_a = a;
+	const git_commit_list_node *commit_b = b;
 
 	return (commit_a->time < commit_b->time);
 }
diff --git a/src/commit_list.h b/src/commit_list.h
index d2f54b3..490d841 100644
--- a/src/commit_list.h
+++ b/src/commit_list.h
@@ -39,7 +39,7 @@ typedef struct git_commit_list {
 } git_commit_list;
 
 git_commit_list_node *git_commit_list_alloc_node(git_revwalk *walk);
-int git_commit_list_time_cmp(void *a, void *b);
+int git_commit_list_time_cmp(const void *a, const void *b);
 void git_commit_list_free(git_commit_list **list_p);
 git_commit_list *git_commit_list_insert(git_commit_list_node *item, git_commit_list **list_p);
 git_commit_list *git_commit_list_insert_by_date(git_commit_list_node *item, git_commit_list **list_p);
diff --git a/src/graph.c b/src/graph.c
index f39af5e..96fda7a 100644
--- a/src/graph.c
+++ b/src/graph.c
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (C) the libgit2 contributors. All rights reserved.
  *
@@ -13,9 +12,9 @@
 static int interesting(git_pqueue *list, git_commit_list *roots)
 {
 	unsigned int i;
-	/* element 0 isn't used - we need to start at 1 */
-	for (i = 1; i < list->size; i++) {
-		git_commit_list_node *commit = list->d[i];
+
+	for (i = 0; i < git_pqueue_size(list); i++) {
+		git_commit_list_node *commit = git_pqueue_get(list, i);
 		if ((commit->flags & STALE) == 0)
 			return 1;
 	}
@@ -42,7 +41,7 @@ static int mark_parents(git_revwalk *walk, git_commit_list_node *one,
 		return 0;
 	}
 
-	if (git_pqueue_init(&list, 2, git_commit_list_time_cmp) < 0)
+	if (git_pqueue_init(&list, 0, 2, git_commit_list_time_cmp) < 0)
 		return -1;
 
 	if (git_commit_list_parse(walk, one) < 0)
@@ -59,10 +58,9 @@ static int mark_parents(git_revwalk *walk, git_commit_list_node *one,
 
 	/* as long as there are non-STALE commits */
 	while (interesting(&list, roots)) {
-		git_commit_list_node *commit;
+		git_commit_list_node *commit = git_pqueue_pop(&list);
 		int flags;
 
-		commit = git_pqueue_pop(&list);
 		if (commit == NULL)
 			break;
 
@@ -110,16 +108,16 @@ static int ahead_behind(git_commit_list_node *one, git_commit_list_node *two,
 {
 	git_commit_list_node *commit;
 	git_pqueue pq;
-	int i;
+	int error = 0, i;
 	*ahead = 0;
 	*behind = 0;
 
-	if (git_pqueue_init(&pq, 2, git_commit_list_time_cmp) < 0)
+	if (git_pqueue_init(&pq, 0, 2, git_commit_list_time_cmp) < 0)
 		return -1;
-	if (git_pqueue_insert(&pq, one) < 0)
-		goto on_error;
-	if (git_pqueue_insert(&pq, two) < 0)
-		goto on_error;
+
+	if ((error = git_pqueue_insert(&pq, one)) < 0 ||
+		(error = git_pqueue_insert(&pq, two)) < 0)
+		goto done;
 
 	while ((commit = git_pqueue_pop(&pq)) != NULL) {
 		if (commit->flags & RESULT ||
@@ -132,18 +130,15 @@ static int ahead_behind(git_commit_list_node *one, git_commit_list_node *two,
 
 		for (i = 0; i < commit->out_degree; i++) {
 			git_commit_list_node *p = commit->parents[i];
-			if (git_pqueue_insert(&pq, p) < 0)
-				return -1;
+			if ((error = git_pqueue_insert(&pq, p)) < 0)
+				goto done;
 		}
 		commit->flags |= RESULT;
 	}
 
+done:
 	git_pqueue_free(&pq);
-	return 0;
-
-on_error:
-	git_pqueue_free(&pq);
-	return -1;
+	return error;
 }
 
 int git_graph_ahead_behind(size_t *ahead, size_t *behind, git_repository *repo,
diff --git a/src/merge.c b/src/merge.c
index 20cfc0e..d004554 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -161,10 +161,10 @@ on_error:
 
 static int interesting(git_pqueue *list)
 {
-	unsigned int i;
-	/* element 0 isn't used - we need to start at 1 */
-	for (i = 1; i < list->size; i++) {
-		git_commit_list_node *commit = list->d[i];
+	size_t i;
+
+	for (i = 0; i < git_pqueue_size(list); i++) {
+		git_commit_list_node *commit = git_pqueue_get(list, i);
 		if ((commit->flags & STALE) == 0)
 			return 1;
 	}
@@ -186,7 +186,7 @@ int git_merge__bases_many(git_commit_list **out, git_revwalk *walk, git_commit_l
 			return git_commit_list_insert(one, out) ? 0 : -1;
 	}
 
-	if (git_pqueue_init(&list, twos->length * 2, git_commit_list_time_cmp) < 0)
+	if (git_pqueue_init(&list, 0, twos->length * 2, git_commit_list_time_cmp) < 0)
 		return -1;
 
 	if (git_commit_list_parse(walk, one) < 0)
@@ -205,10 +205,11 @@ int git_merge__bases_many(git_commit_list **out, git_revwalk *walk, git_commit_l
 
 	/* as long as there are non-STALE commits */
 	while (interesting(&list)) {
-		git_commit_list_node *commit;
+		git_commit_list_node *commit = git_pqueue_pop(&list);
 		int flags;
 
-		commit = git_pqueue_pop(&list);
+		if (commit == NULL)
+			break;
 
 		flags = commit->flags & (PARENT1 | PARENT2 | STALE);
 		if (flags == (PARENT1 | PARENT2)) {
diff --git a/src/pqueue.c b/src/pqueue.c
index 7819ed4..ddbad7a 100644
--- a/src/pqueue.c
+++ b/src/pqueue.c
@@ -3,161 +3,95 @@
  *
  * This file is part of libgit2, distributed under the GNU GPL v2 with
  * a Linking Exception. For full terms see the included COPYING file.
- *
- * 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.
  */
 
-#include "common.h"
 #include "pqueue.h"
+#include "util.h"
 
-#define left(i)	((i) << 1)
-#define right(i) (((i) << 1) + 1)
-#define parent(i) ((i) >> 1)
-
-int git_pqueue_init(git_pqueue *q, size_t n, git_pqueue_cmp cmppri)
-{
-	assert(q);
-
-	/* Need to allocate n+1 elements since element 0 isn't used. */
-	q->d = git__malloc((n + 1) * sizeof(void *));
-	GITERR_CHECK_ALLOC(q->d);
-
-	q->size = 1;
-	q->avail = q->step = (n + 1); /* see comment above about n+1 */
-	q->cmppri = cmppri;
-
-	return 0;
-}
-
+#define PQUEUE_LCHILD_OF(I) (((I)<<1)+1)
+#define PQUEUE_RCHILD_OF(I) (((I)<<1)+2)
+#define PQUEUE_PARENT_OF(I) (((I)-1)>>1)
 
-void git_pqueue_free(git_pqueue *q)
+int git_pqueue_init(
+	git_pqueue *pq,
+	uint32_t flags,
+	size_t est_size,
+	git_vector_cmp cmp)
 {
-	git__free(q->d);
-	q->d = NULL;
+	pq->flags = flags;
+	pq->initial_size = est_size;
+	return git_vector_init(&pq->values, est_size, cmp);
 }
 
-void git_pqueue_clear(git_pqueue *q)
+void git_pqueue_free(git_pqueue *pq)
 {
-	q->size = 1;
+	git_vector_free(&pq->values);
 }
 
-size_t git_pqueue_size(git_pqueue *q)
+static void pqueue_up(git_pqueue *pq, size_t el)
 {
-	/* queue element 0 exists but doesn't count since it isn't used. */
-	return (q->size - 1);
-}
+	size_t parent_el = PQUEUE_PARENT_OF(el);
 
+	while (el > 0 && git_vector_cmp_elements(&pq->values, parent_el, el) > 0) {
+		git_vector_swap_elements(&pq->values, el, parent_el);
 
-static void bubble_up(git_pqueue *q, size_t i)
-{
-	size_t parent_node;
-	void *moving_node = q->d[i];
-
-	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];
+		el = parent_el;
+		parent_el = PQUEUE_PARENT_OF(el);
 	}
-
-	q->d[i] = moving_node;
 }
 
-
-static size_t maxchild(git_pqueue *q, size_t i)
+static void pqueue_down(git_pqueue *pq, size_t el)
 {
-	size_t child_node = left(i);
+	size_t last = git_vector_length(&pq->values);
 
-	if (child_node >= q->size)
-		return 0;
+	while (1) {
+		size_t kid = PQUEUE_LCHILD_OF(el), rkid = PQUEUE_RCHILD_OF(el);
+		if (kid >= last)
+			break;
+		if (rkid < last && git_vector_cmp_elements(&pq->values, kid, rkid) > 0)
+			kid = rkid;
 
-	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 */
-
-	return child_node;
-}
+		if (git_vector_cmp_elements(&pq->values, el, kid) < 0)
+			break;
 
-
-static void percolate_down(git_pqueue *q, size_t i)
-{
-	size_t child_node;
-	void *moving_node = q->d[i];
-
-	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;
+		git_vector_swap_elements(&pq->values, el, kid);
+		el = kid;
 	}
-
-	q->d[i] = moving_node;
 }
 
-
-int git_pqueue_insert(git_pqueue *q, void *d)
+int git_pqueue_insert(git_pqueue *pq, void *item)
 {
-	void *tmp;
-	size_t i;
-	size_t newsize;
-
-	if (!q) return 1;
-
-	/* allocate more memory if necessary */
-	if (q->size >= q->avail) {
-		newsize = q->size + q->step;
-		tmp = git__realloc(q->d, sizeof(void *) * newsize);
-		GITERR_CHECK_ALLOC(tmp);
-
-		q->d = tmp;
-		q->avail = newsize;
+	int error = 0;
+
+	/* if heap is full, pop the top element if new one should replace it */
+	if ((pq->flags & GIT_PQUEUE_FIXED_SIZE) != 0 &&
+		pq->values.length >= pq->initial_size)
+	{
+		/* skip item if below min item in heap */
+		if (pq->values._cmp(item, git_vector_get(&pq->values, 0)) <= 0)
+			return 0;
+		(void)git_pqueue_pop(pq);
 	}
 
-	/* insert item */
-	i = q->size++;
-	q->d[i] = d;
-	bubble_up(q, i);
+	error = git_vector_insert(&pq->values, item);
 
-	return 0;
-}
+	if (!error)
+		pqueue_up(pq, pq->values.length - 1);
 
-
-void *git_pqueue_pop(git_pqueue *q)
-{
-	void *head;
-
-	if (!q || q->size == 1)
-		return NULL;
-
-	head = q->d[1];
-	q->d[1] = q->d[--q->size];
-	percolate_down(q, 1);
-
-	return head;
+	return error;
 }
 
-
-void *git_pqueue_peek(git_pqueue *q)
+void *git_pqueue_pop(git_pqueue *pq)
 {
-	if (!q || q->size == 1)
-		return NULL;
-	return q->d[1];
+	void *rval = git_vector_get(&pq->values, 0);
+
+	if (git_vector_length(&pq->values) > 1) {
+		pq->values.contents[0] = git_vector_last(&pq->values);
+		git_vector_pop(&pq->values);
+		pqueue_down(pq, 0);
+	} else {
+		git_vector_pop(&pq->values);
+	}
+
+	return rval;
 }
diff --git a/src/pqueue.h b/src/pqueue.h
index 9061f82..3c977e1 100644
--- a/src/pqueue.h
+++ b/src/pqueue.h
@@ -3,99 +3,74 @@
  *
  * This file is part of libgit2, distributed under the GNU GPL v2 with
  * a Linking Exception. For full terms see the included COPYING file.
- * 
- * 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.
  */
-
 #ifndef INCLUDE_pqueue_h__
 #define INCLUDE_pqueue_h__
 
-/** callback functions to get/set/compare the priority of an element */
-typedef int (*git_pqueue_cmp)(void *a, void *b);
+#include "vector.h"
 
-/** the priority queue handle */
 typedef struct {
-	size_t size, avail, step;
-	git_pqueue_cmp cmppri;
-	void **d;
+	git_vector values;
+	size_t     initial_size;
+	uint32_t   flags;
 } git_pqueue;
 
+enum {
+	GIT_PQUEUE_DEFAULT = 0,
+	GIT_PQUEUE_FIXED_SIZE = (1 << 0), /* don't grow heap, keep highest */
+};
 
 /**
- * initialize the queue
+ * Initialize priority queue
  *
- * @param n the initial estimate of the number of queue items for which memory
- *			should be preallocated
- * @param cmppri the callback function to compare two nodes of the queue
- *
- * @return the handle or NULL for insufficent memory
+ * @param pq The priority queue struct to initialize
+ * @param flags Flags (see above) to control queue behavior
+ * @param est_size The estimated/initial queue size
+ * @param cmp The entry priority comparison function
+ * @return 0 on success, <0 on error
  */
-int git_pqueue_init(git_pqueue *q, size_t n, git_pqueue_cmp cmppri);
-
+extern int git_pqueue_init(
+	git_pqueue *pq,
+	uint32_t flags,
+	size_t est_size,
+	git_vector_cmp cmp);
 
 /**
- * free all memory used by the queue
- * @param q the queue
+ * Free the queue memory
  */
-void git_pqueue_free(git_pqueue *q);
+extern void git_pqueue_free(git_pqueue *pq);
 
 /**
- * clear all the elements in the queue
- * @param q the queue
+ * Get the number of items in the queue
  */
-void git_pqueue_clear(git_pqueue *q);
+GIT_INLINE(size_t) git_pqueue_size(const git_pqueue *pq)
+{
+	return git_vector_length(&pq->values);
+}
 
 /**
- * return the size of the queue.
- * @param q the queue
+ * Get an item in the queue
  */
-size_t git_pqueue_size(git_pqueue *q);
-
+GIT_INLINE(void *) git_pqueue_get(const git_pqueue *pq, size_t pos)
+{
+	return git_vector_get(&pq->values, pos);
+}
 
 /**
- * insert an item into the queue.
- * @param q the queue
- * @param d the item
- * @return 0 on success
- */
-int git_pqueue_insert(git_pqueue *q, void *d);
-
-
-/**
- * pop the highest-ranking item from the queue.
- * @param q the queue
- * @return NULL on error, otherwise the entry
+ * Insert a new item into the queue
+ *
+ * @param pq The priority queue
+ * @param item Pointer to the item data
+ * @return 0 on success, <0 on failure
  */
-void *git_pqueue_pop(git_pqueue *q);
-
+extern int git_pqueue_insert(git_pqueue *pq, void *item);
 
 /**
- * access highest-ranking item without removing it.
- * @param q the queue
- * @return NULL on error, otherwise the entry
+ * Remove the top item in the priority queue
+ *
+ * @param pq The priority queue
+ * @return item from heap on success, NULL if queue is empty
  */
-void *git_pqueue_peek(git_pqueue *q);
-
-#endif /* PQUEUE_H */
-/** @} */
+extern void *git_pqueue_pop(git_pqueue *pq);
 
+#endif
diff --git a/src/revwalk.c b/src/revwalk.c
index c0a0532..d6dc106 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -439,7 +439,8 @@ int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo)
 	walk->commits = git_oidmap_alloc();
 	GITERR_CHECK_ALLOC(walk->commits);
 
-	if (git_pqueue_init(&walk->iterator_time, 8, git_commit_list_time_cmp) < 0 ||
+	if (git_pqueue_init(
+			&walk->iterator_time, 0, 8, git_commit_list_time_cmp) < 0 ||
 		git_vector_init(&walk->twos, 4, NULL) < 0 ||
 		git_pool_init(&walk->commit_pool, 1,
 			git_pool__suggest_items_per_page(COMMIT_ALLOC) * COMMIT_ALLOC) < 0)
@@ -542,7 +543,7 @@ void git_revwalk_reset(git_revwalk *walk)
 		commit->uninteresting = 0;
 		});
 
-	git_pqueue_clear(&walk->iterator_time);
+	git_pqueue_free(&walk->iterator_time);
 	git_commit_list_free(&walk->iterator_topo);
 	git_commit_list_free(&walk->iterator_rand);
 	git_commit_list_free(&walk->iterator_reverse);
diff --git a/src/vector.h b/src/vector.h
index d318463..e8a9678 100644
--- a/src/vector.h
+++ b/src/vector.h
@@ -95,4 +95,14 @@ GIT_INLINE(void) git_vector_set_cmp(git_vector *v, git_vector_cmp cmp)
 	}
 }
 
+/** Swap two elements */
+#define git_vector_swap_elements(V, P1, P2) do { \
+	void *__t = (V)->contents[P1]; \
+	(V)->contents[P1] = (V)->contents[P2]; \
+	(V)->contents[P2] = __t; } while (0)
+
+/** Compare two elements */
+#define git_vector_cmp_elements(V, P1, P2) \
+	(V)->_cmp(git_vector_get(V,P1), git_vector_get(V,P2))
+
 #endif