2 Copyright 2020 Google LLC
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file or at
6 https://developers.google.com/open-source/licenses/bsd
11 #include "reftable-record.h"
15 int pq_less(struct pq_entry
*a
, struct pq_entry
*b
)
17 struct strbuf ak
= STRBUF_INIT
;
18 struct strbuf bk
= STRBUF_INIT
;
20 reftable_record_key(&a
->rec
, &ak
);
21 reftable_record_key(&b
->rec
, &bk
);
23 cmp
= strbuf_cmp(&ak
, &bk
);
29 return a
->index
> b
->index
;
34 struct pq_entry
merged_iter_pqueue_top(struct merged_iter_pqueue pq
)
39 int merged_iter_pqueue_is_empty(struct merged_iter_pqueue pq
)
44 struct pq_entry
merged_iter_pqueue_remove(struct merged_iter_pqueue
*pq
)
47 struct pq_entry e
= pq
->heap
[0];
48 pq
->heap
[0] = pq
->heap
[pq
->len
- 1];
56 if (j
< pq
->len
&& pq_less(&pq
->heap
[j
], &pq
->heap
[i
])) {
59 if (k
< pq
->len
&& pq_less(&pq
->heap
[k
], &pq
->heap
[min
])) {
67 SWAP(pq
->heap
[i
], pq
->heap
[min
]);
74 void merged_iter_pqueue_add(struct merged_iter_pqueue
*pq
, const struct pq_entry
*e
)
78 if (pq
->len
== pq
->cap
) {
79 pq
->cap
= 2 * pq
->cap
+ 1;
80 pq
->heap
= reftable_realloc(pq
->heap
,
81 pq
->cap
* sizeof(struct pq_entry
));
84 pq
->heap
[pq
->len
++] = *e
;
88 if (pq_less(&pq
->heap
[j
], &pq
->heap
[i
])) {
92 SWAP(pq
->heap
[j
], pq
->heap
[i
]);
98 void merged_iter_pqueue_release(struct merged_iter_pqueue
*pq
)
101 for (i
= 0; i
< pq
->len
; i
++) {
102 reftable_record_release(&pq
->heap
[i
].rec
);
104 FREE_AND_NULL(pq
->heap
);
105 pq
->len
= pq
->cap
= 0;