Coverage Report

Created: 2024-09-08 06:23

/src/git/hash-lookup.c
Line
Count
Source (jump to first uncovered line)
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "hash.h"
5
#include "hash-lookup.h"
6
#include "read-cache-ll.h"
7
8
static uint32_t take2(const struct object_id *oid, size_t ofs)
9
0
{
10
0
  return ((oid->hash[ofs] << 8) | oid->hash[ofs + 1]);
11
0
}
12
13
/*
14
 * Conventional binary search loop looks like this:
15
 *
16
 *      do {
17
 *              int mi = lo + (hi - lo) / 2;
18
 *              int cmp = "entry pointed at by mi" minus "target";
19
 *              if (!cmp)
20
 *                      return (mi is the wanted one)
21
 *              if (cmp > 0)
22
 *                      hi = mi; "mi is larger than target"
23
 *              else
24
 *                      lo = mi+1; "mi is smaller than target"
25
 *      } while (lo < hi);
26
 *
27
 * The invariants are:
28
 *
29
 * - When entering the loop, lo points at a slot that is never
30
 *   above the target (it could be at the target), hi points at a
31
 *   slot that is guaranteed to be above the target (it can never
32
 *   be at the target).
33
 *
34
 * - We find a point 'mi' between lo and hi (mi could be the same
35
 *   as lo, but never can be the same as hi), and check if it hits
36
 *   the target.  There are three cases:
37
 *
38
 *    - if it is a hit, we are happy.
39
 *
40
 *    - if it is strictly higher than the target, we update hi with
41
 *      it.
42
 *
43
 *    - if it is strictly lower than the target, we update lo to be
44
 *      one slot after it, because we allow lo to be at the target.
45
 *
46
 * When choosing 'mi', we do not have to take the "middle" but
47
 * anywhere in between lo and hi, as long as lo <= mi < hi is
48
 * satisfied.  When we somehow know that the distance between the
49
 * target and lo is much shorter than the target and hi, we could
50
 * pick mi that is much closer to lo than the midway.
51
 */
52
/*
53
 * The table should contain "nr" elements.
54
 * The oid of element i (between 0 and nr - 1) should be returned
55
 * by "fn(i, table)".
56
 */
57
int oid_pos(const struct object_id *oid, const void *table, size_t nr,
58
      oid_access_fn fn)
59
0
{
60
0
  size_t hi = nr;
61
0
  size_t lo = 0;
62
0
  size_t mi = 0;
63
64
0
  if (!nr)
65
0
    return -1;
66
67
0
  if (nr != 1) {
68
0
    size_t lov, hiv, miv, ofs;
69
70
0
    for (ofs = 0; ofs < the_hash_algo->rawsz - 2; ofs += 2) {
71
0
      lov = take2(fn(0, table), ofs);
72
0
      hiv = take2(fn(nr - 1, table), ofs);
73
0
      miv = take2(oid, ofs);
74
0
      if (miv < lov)
75
0
        return -1;
76
0
      if (hiv < miv)
77
0
        return index_pos_to_insert_pos(nr);
78
0
      if (lov != hiv) {
79
        /*
80
         * At this point miv could be equal
81
         * to hiv (but hash could still be higher);
82
         * the invariant of (mi < hi) should be
83
         * kept.
84
         */
85
0
        mi = (nr - 1) * (miv - lov) / (hiv - lov);
86
0
        if (lo <= mi && mi < hi)
87
0
          break;
88
0
        BUG("assertion failed in binary search");
89
0
      }
90
0
    }
91
0
  }
92
93
0
  do {
94
0
    int cmp;
95
0
    cmp = oidcmp(fn(mi, table), oid);
96
0
    if (!cmp)
97
0
      return mi;
98
0
    if (cmp > 0)
99
0
      hi = mi;
100
0
    else
101
0
      lo = mi + 1;
102
0
    mi = lo + (hi - lo) / 2;
103
0
  } while (lo < hi);
104
0
  return index_pos_to_insert_pos(lo);
105
0
}
106
107
int bsearch_hash(const unsigned char *hash, const uint32_t *fanout_nbo,
108
     const unsigned char *table, size_t stride, uint32_t *result)
109
0
{
110
0
  uint32_t hi, lo;
111
112
0
  hi = ntohl(fanout_nbo[*hash]);
113
0
  lo = ((*hash == 0x0) ? 0 : ntohl(fanout_nbo[*hash - 1]));
114
115
0
  while (lo < hi) {
116
0
    unsigned mi = lo + (hi - lo) / 2;
117
0
    int cmp = hashcmp(table + mi * stride, hash,
118
0
          the_repository->hash_algo);
119
120
0
    if (!cmp) {
121
0
      if (result)
122
0
        *result = mi;
123
0
      return 1;
124
0
    }
125
0
    if (cmp > 0)
126
0
      hi = mi;
127
0
    else
128
0
      lo = mi + 1;
129
0
  }
130
131
0
  if (result)
132
0
    *result = lo;
133
0
  return 0;
134
0
}