blob: b98ed5e11e8a3af02a99b6d2a977cc804a122414 [file] [log] [blame]
Junio C Hamano628522e2007-12-29 02:05:47 -08001#include "cache.h"
Martin Ågrenbc626922020-12-31 12:56:23 +01002#include "hash-lookup.h"
Junio C Hamano628522e2007-12-29 02:05:47 -08003
Jeff King45ee13b2021-01-28 01:19:42 -05004static uint32_t take2(const struct object_id *oid, size_t ofs)
Christian Couder96beef82009-04-04 22:59:26 +02005{
Jeff King45ee13b2021-01-28 01:19:42 -05006 return ((oid->hash[ofs] << 8) | oid->hash[ofs + 1]);
Christian Couder96beef82009-04-04 22:59:26 +02007}
8
9/*
10 * Conventional binary search loop looks like this:
11 *
12 * do {
Derrick Stolee19716b22017-10-08 14:29:37 -040013 * int mi = lo + (hi - lo) / 2;
Christian Couder96beef82009-04-04 22:59:26 +020014 * int cmp = "entry pointed at by mi" minus "target";
15 * if (!cmp)
16 * return (mi is the wanted one)
17 * if (cmp > 0)
18 * hi = mi; "mi is larger than target"
19 * else
20 * lo = mi+1; "mi is smaller than target"
21 * } while (lo < hi);
22 *
23 * The invariants are:
24 *
25 * - When entering the loop, lo points at a slot that is never
26 * above the target (it could be at the target), hi points at a
27 * slot that is guaranteed to be above the target (it can never
28 * be at the target).
29 *
30 * - We find a point 'mi' between lo and hi (mi could be the same
31 * as lo, but never can be the same as hi), and check if it hits
32 * the target. There are three cases:
33 *
34 * - if it is a hit, we are happy.
35 *
36 * - if it is strictly higher than the target, we update hi with
37 * it.
38 *
39 * - if it is strictly lower than the target, we update lo to be
40 * one slot after it, because we allow lo to be at the target.
41 *
42 * When choosing 'mi', we do not have to take the "middle" but
43 * anywhere in between lo and hi, as long as lo <= mi < hi is
44 * satisfied. When we somehow know that the distance between the
45 * target and lo is much shorter than the target and hi, we could
46 * pick mi that is much closer to lo than the midway.
47 */
48/*
49 * The table should contain "nr" elements.
Jeff King45ee13b2021-01-28 01:19:42 -050050 * The oid of element i (between 0 and nr - 1) should be returned
Christian Couder96beef82009-04-04 22:59:26 +020051 * by "fn(i, table)".
52 */
Jeff King8380dcd2021-01-28 01:20:23 -050053int oid_pos(const struct object_id *oid, const void *table, size_t nr,
Jeff King45ee13b2021-01-28 01:19:42 -050054 oid_access_fn fn)
Christian Couder96beef82009-04-04 22:59:26 +020055{
56 size_t hi = nr;
57 size_t lo = 0;
58 size_t mi = 0;
59
60 if (!nr)
61 return -1;
62
63 if (nr != 1) {
64 size_t lov, hiv, miv, ofs;
65
brian m. carlsone84f3572019-08-18 20:04:14 +000066 for (ofs = 0; ofs < the_hash_algo->rawsz - 2; ofs += 2) {
Jeff King45ee13b2021-01-28 01:19:42 -050067 lov = take2(fn(0, table), ofs);
68 hiv = take2(fn(nr - 1, table), ofs);
69 miv = take2(oid, ofs);
Christian Couder96beef82009-04-04 22:59:26 +020070 if (miv < lov)
71 return -1;
72 if (hiv < miv)
Johannes Schindelinc097b952019-10-04 08:09:26 -070073 return index_pos_to_insert_pos(nr);
Christian Couder96beef82009-04-04 22:59:26 +020074 if (lov != hiv) {
75 /*
76 * At this point miv could be equal
Martin Ågren7a7d9922020-12-31 12:56:22 +010077 * to hiv (but hash could still be higher);
Christian Couder96beef82009-04-04 22:59:26 +020078 * the invariant of (mi < hi) should be
79 * kept.
80 */
81 mi = (nr - 1) * (miv - lov) / (hiv - lov);
82 if (lo <= mi && mi < hi)
83 break;
Johannes Schindelin033abf92018-05-02 11:38:39 +020084 BUG("assertion failed in binary search");
Christian Couder96beef82009-04-04 22:59:26 +020085 }
86 }
Christian Couder96beef82009-04-04 22:59:26 +020087 }
88
89 do {
90 int cmp;
Jeff King45ee13b2021-01-28 01:19:42 -050091 cmp = oidcmp(fn(mi, table), oid);
Christian Couder96beef82009-04-04 22:59:26 +020092 if (!cmp)
93 return mi;
94 if (cmp > 0)
95 hi = mi;
96 else
97 lo = mi + 1;
Derrick Stolee19716b22017-10-08 14:29:37 -040098 mi = lo + (hi - lo) / 2;
Christian Couder96beef82009-04-04 22:59:26 +020099 } while (lo < hi);
Johannes Schindelinc097b952019-10-04 08:09:26 -0700100 return index_pos_to_insert_pos(lo);
Christian Couder96beef82009-04-04 22:59:26 +0200101}
Jonathan Tanb4e00f72018-02-13 10:39:39 -0800102
Martin Ågrenbc626922020-12-31 12:56:23 +0100103int bsearch_hash(const unsigned char *hash, const uint32_t *fanout_nbo,
Jonathan Tanb4e00f72018-02-13 10:39:39 -0800104 const unsigned char *table, size_t stride, uint32_t *result)
105{
106 uint32_t hi, lo;
107
Martin Ågrenbc626922020-12-31 12:56:23 +0100108 hi = ntohl(fanout_nbo[*hash]);
109 lo = ((*hash == 0x0) ? 0 : ntohl(fanout_nbo[*hash - 1]));
Jonathan Tanb4e00f72018-02-13 10:39:39 -0800110
111 while (lo < hi) {
112 unsigned mi = lo + (hi - lo) / 2;
Martin Ågrenbc626922020-12-31 12:56:23 +0100113 int cmp = hashcmp(table + mi * stride, hash);
Jonathan Tanb4e00f72018-02-13 10:39:39 -0800114
115 if (!cmp) {
116 if (result)
117 *result = mi;
118 return 1;
119 }
120 if (cmp > 0)
121 hi = mi;
122 else
123 lo = mi + 1;
124 }
125
126 if (result)
127 *result = lo;
128 return 0;
129}