Coverage Report

Created: 2024-09-08 06:23

/src/git/reftable/pq.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
Copyright 2020 Google LLC
3
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
7
*/
8
9
#include "pq.h"
10
11
#include "reftable-record.h"
12
#include "system.h"
13
#include "basics.h"
14
15
int pq_less(struct pq_entry *a, struct pq_entry *b)
16
0
{
17
0
  int cmp = reftable_record_cmp(a->rec, b->rec);
18
0
  if (cmp == 0)
19
0
    return a->index > b->index;
20
0
  return cmp < 0;
21
0
}
22
23
struct pq_entry merged_iter_pqueue_remove(struct merged_iter_pqueue *pq)
24
0
{
25
0
  size_t i = 0;
26
0
  struct pq_entry e = pq->heap[0];
27
0
  pq->heap[0] = pq->heap[pq->len - 1];
28
0
  pq->len--;
29
30
0
  while (i < pq->len) {
31
0
    size_t min = i;
32
0
    size_t j = 2 * i + 1;
33
0
    size_t k = 2 * i + 2;
34
0
    if (j < pq->len && pq_less(&pq->heap[j], &pq->heap[i]))
35
0
      min = j;
36
0
    if (k < pq->len && pq_less(&pq->heap[k], &pq->heap[min]))
37
0
      min = k;
38
0
    if (min == i)
39
0
      break;
40
0
    SWAP(pq->heap[i], pq->heap[min]);
41
0
    i = min;
42
0
  }
43
44
0
  return e;
45
0
}
46
47
void merged_iter_pqueue_add(struct merged_iter_pqueue *pq, const struct pq_entry *e)
48
0
{
49
0
  size_t i = 0;
50
51
0
  REFTABLE_ALLOC_GROW(pq->heap, pq->len + 1, pq->cap);
52
0
  pq->heap[pq->len++] = *e;
53
54
0
  i = pq->len - 1;
55
0
  while (i > 0) {
56
0
    size_t j = (i - 1) / 2;
57
0
    if (pq_less(&pq->heap[j], &pq->heap[i]))
58
0
      break;
59
0
    SWAP(pq->heap[j], pq->heap[i]);
60
0
    i = j;
61
0
  }
62
0
}
63
64
void merged_iter_pqueue_release(struct merged_iter_pqueue *pq)
65
0
{
66
0
  FREE_AND_NULL(pq->heap);
67
0
  memset(pq, 0, sizeof(*pq));
68
0
}