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