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 int cmp
= reftable_record_cmp(a
->rec
, b
->rec
);
19 return a
->index
> b
->index
;
23 struct pq_entry
merged_iter_pqueue_remove(struct merged_iter_pqueue
*pq
)
26 struct pq_entry e
= pq
->heap
[0];
27 pq
->heap
[0] = pq
->heap
[pq
->len
- 1];
35 if (j
< pq
->len
&& pq_less(&pq
->heap
[j
], &pq
->heap
[i
])) {
38 if (k
< pq
->len
&& pq_less(&pq
->heap
[k
], &pq
->heap
[min
])) {
46 SWAP(pq
->heap
[i
], pq
->heap
[min
]);
53 void merged_iter_pqueue_add(struct merged_iter_pqueue
*pq
, const struct pq_entry
*e
)
57 REFTABLE_ALLOC_GROW(pq
->heap
, pq
->len
+ 1, pq
->cap
);
58 pq
->heap
[pq
->len
++] = *e
;
63 if (pq_less(&pq
->heap
[j
], &pq
->heap
[i
])) {
67 SWAP(pq
->heap
[j
], pq
->heap
[i
]);
73 void merged_iter_pqueue_release(struct merged_iter_pqueue
*pq
)
75 FREE_AND_NULL(pq
->heap
);
76 memset(pq
, 0, sizeof(*pq
));