Coverage Report

Created: 2025-12-31 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/reftable/pq.c
Line
Count
Source
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-error.h"
12
#include "reftable-record.h"
13
#include "system.h"
14
#include "basics.h"
15
16
int pq_less(struct pq_entry *a, struct pq_entry *b)
17
0
{
18
0
  int cmp, err;
19
20
0
  err = reftable_record_cmp(a->rec, b->rec, &cmp);
21
0
  if (err < 0)
22
0
    return err;
23
24
0
  if (cmp == 0)
25
0
    return a->index > b->index;
26
0
  return cmp < 0;
27
0
}
28
29
int merged_iter_pqueue_remove(struct merged_iter_pqueue *pq, struct pq_entry *out)
30
0
{
31
0
  size_t i = 0;
32
0
  struct pq_entry e = pq->heap[0];
33
0
  pq->heap[0] = pq->heap[pq->len - 1];
34
0
  pq->len--;
35
36
0
  while (i < pq->len) {
37
0
    size_t min = i;
38
0
    size_t j = 2 * i + 1;
39
0
    size_t k = 2 * i + 2;
40
0
    int cmp;
41
42
0
    if (j < pq->len) {
43
0
      cmp = pq_less(&pq->heap[j], &pq->heap[i]);
44
0
      if (cmp < 0)
45
0
        return -1;
46
0
      else if (cmp)
47
0
        min = j;
48
0
    }
49
50
0
    if (k < pq->len) {
51
0
      cmp = pq_less(&pq->heap[k], &pq->heap[min]);
52
0
      if (cmp < 0)
53
0
        return -1;
54
0
      else if (cmp)
55
0
        min = k;
56
0
    }
57
58
0
    if (min == i)
59
0
      break;
60
0
    REFTABLE_SWAP(pq->heap[i], pq->heap[min]);
61
0
    i = min;
62
0
  }
63
64
0
  if (out)
65
0
    *out = e;
66
67
0
  return 0;
68
0
}
69
70
int merged_iter_pqueue_add(struct merged_iter_pqueue *pq, const struct pq_entry *e)
71
0
{
72
0
  size_t i = 0;
73
74
0
  REFTABLE_ALLOC_GROW_OR_NULL(pq->heap, pq->len + 1, pq->cap);
75
0
  if (!pq->heap)
76
0
    return REFTABLE_OUT_OF_MEMORY_ERROR;
77
0
  pq->heap[pq->len++] = *e;
78
79
0
  i = pq->len - 1;
80
0
  while (i > 0) {
81
0
    size_t j = (i - 1) / 2;
82
0
    if (pq_less(&pq->heap[j], &pq->heap[i]))
83
0
      break;
84
0
    REFTABLE_SWAP(pq->heap[j], pq->heap[i]);
85
0
    i = j;
86
0
  }
87
88
0
  return 0;
89
0
}
90
91
void merged_iter_pqueue_release(struct merged_iter_pqueue *pq)
92
0
{
93
  REFTABLE_FREE_AND_NULL(pq->heap);
94
0
  memset(pq, 0, sizeof(*pq));
95
0
}