2 #include "sha1-lookup.h"
4 static uint32_t take2(const unsigned char *sha1)
6 return ((sha1[0] << 8) | sha1[1]);
10 * Conventional binary search loop looks like this:
13 * int mi = (lo + hi) / 2;
14 * int cmp = "entry pointed at by mi" minus "target";
16 * return (mi is the wanted one)
18 * hi = mi; "mi is larger than target"
20 * lo = mi+1; "mi is smaller than target"
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
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:
34 * - if it is a hit, we are happy.
36 * - if it is strictly higher than the target, we update hi with
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.
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.
49 * The table should contain "nr" elements.
50 * The sha1 of element i (between 0 and nr - 1) should be returned
53 int sha1_pos(const unsigned char *sha1, void *table, size_t nr,
64 size_t lov, hiv, miv, ofs;
66 for (ofs = 0; ofs < 18; ofs += 2) {
67 lov = take2(fn(0, table) + ofs);
68 hiv = take2(fn(nr - 1, table) + ofs);
69 miv = take2(sha1 + ofs);
76 * At this point miv could be equal
77 * to hiv (but sha1 could still be higher);
78 * the invariant of (mi < hi) should be
81 mi = (nr - 1) * (miv - lov) / (hiv - lov);
82 if (lo <= mi && mi < hi)
84 die("BUG: assertion failed in binary search");
91 cmp = hashcmp(fn(mi, table), sha1);