]>
git.proxmox.com Git - libgit2.git/blob - src/sha1_lookup.c
2 * Copyright (C) 2009-2012 the libgit2 contributors
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
10 #include "sha1_lookup.h"
14 * Conventional binary search loop looks like this:
18 * unsigned mi = (lo + hi) / 2;
19 * int cmp = "entry pointed at by mi" minus "target";
21 * return (mi is the wanted one)
23 * hi = mi; "mi is larger than target"
25 * lo = mi+1; "mi is smaller than target"
30 * - When entering the loop, lo points at a slot that is never
31 * above the target (it could be at the target), hi points at a
32 * slot that is guaranteed to be above the target (it can never
35 * - We find a point 'mi' between lo and hi (mi could be the same
36 * as lo, but never can be as same as hi), and check if it hits
37 * the target. There are three cases:
39 * - if it is a hit, we are happy.
41 * - if it is strictly higher than the target, we set it to hi,
42 * and repeat the search.
44 * - if it is strictly lower than the target, we update lo to
45 * one slot after it, because we allow lo to be at the target.
47 * If the loop exits, there is no matching entry.
49 * When choosing 'mi', we do not have to take the "middle" but
50 * anywhere in between lo and hi, as long as lo <= mi < hi is
51 * satisfied. When we somehow know that the distance between the
52 * target and lo is much shorter than the target and hi, we could
53 * pick mi that is much closer to lo than the midway.
55 * Now, we can take advantage of the fact that SHA-1 is a good hash
56 * function, and as long as there are enough entries in the table, we
57 * can expect uniform distribution. An entry that begins with for
58 * example "deadbeef..." is much likely to appear much later than in
59 * the midway of the table. It can reasonably be expected to be near
60 * 87% (222/256) from the top of the table.
62 * However, we do not want to pick "mi" too precisely. If the entry at
63 * the 87% in the above example turns out to be higher than the target
64 * we are looking for, we would end up narrowing the search space down
65 * only by 13%, instead of 50% we would get if we did a simple binary
66 * search. So we would want to hedge our bets by being less aggressive.
68 * The table at "table" holds at least "nr" entries of "elem_size"
69 * bytes each. Each entry has the SHA-1 key at "key_offset". The
70 * table is sorted by the SHA-1 key of the entries. The caller wants
71 * to find the entry with "key", and knows that the entry at "lo" is
72 * not higher than the entry it is looking for, and that the entry at
73 * "hi" is higher than the entry it is looking for.
75 int sha1_entry_pos(const void *table
,
78 unsigned lo
, unsigned hi
, unsigned nr
,
79 const unsigned char *key
)
81 const unsigned char *base
= (const unsigned char*)table
;
82 const unsigned char *hi_key
, *lo_key
;
91 hi_key
= base
+ elem_size
* hi
+ key_offset
;
92 lo_key
= base
+ elem_size
* lo
+ key_offset
;
97 unsigned ofs
, mi
, range
;
98 unsigned lov
, hiv
, kyv
;
99 const unsigned char *mi_key
;
103 for (ofs
= ofs_0
; ofs
< 20; ofs
++)
104 if (lo_key
[ofs
] != hi_key
[ofs
])
108 * byte 0 thru (ofs-1) are the same between
109 * lo and hi; ofs is the first byte that is
114 hiv
= (hiv
<< 8) | hi_key
[ofs_0
+1];
123 lov
= (lov
<< 8) | lo_key
[ofs_0
+1];
124 kyv
= (kyv
<< 8) | key
[ofs_0
+1];
134 * Even if we know the target is much closer to 'hi'
135 * than 'lo', if we pick too precisely and overshoot
136 * (e.g. when we know 'mi' is closer to 'hi' than to
137 * 'lo', pick 'mi' that is higher than the target), we
138 * end up narrowing the search space by a smaller
139 * amount (i.e. the distance between 'mi' and 'hi')
140 * than what we would have (i.e. about half of 'lo'
141 * and 'hi'). Hedge our bets to pick 'mi' less
142 * aggressively, i.e. make 'mi' a bit closer to the
143 * middle than we would otherwise pick.
145 kyv
= (kyv
* 6 + lov
+ hiv
) / 8;
152 mi
= (range
- 1) * (kyv
- lov
) / (hiv
- lov
) + lo
;
154 #ifdef INDEX_DEBUG_LOOKUP
155 printf("lo %u hi %u rg %u mi %u ", lo
, hi
, range
, mi
);
156 printf("ofs %u lov %x, hiv %x, kyv %x\n",
157 ofs_0
, lov
, hiv
, kyv
);
160 if (!(lo
<= mi
&& mi
< hi
)) {
161 giterr_set(GITERR_INVALID
, "Assertion failure. Binary search invariant is false");
165 mi_key
= base
+ elem_size
* mi
+ key_offset
;
166 cmp
= memcmp(mi_key
+ ofs_0
, key
+ ofs_0
, 20 - ofs_0
);
174 lo_key
= mi_key
+ elem_size
;