]> git.proxmox.com Git - libgit2.git/blob - src/sha1_lookup.c
pkt-line: parse other-ref lines
[libgit2.git] / src / sha1_lookup.c
1 /*
2 * This file is basically taken from git code.
3 * This file is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License, version 2,
5 * as published by the Free Software Foundation.
6 *
7 * In addition to the permissions in the GNU General Public License,
8 * the authors give you unlimited permission to link the compiled
9 * version of this file into combinations with other programs,
10 * and to distribute those combinations without any restriction
11 * coming from the use of this file. (The General Public License
12 * restrictions do apply in other respects; for example, they cover
13 * modification of the file, and distribution when not linked into
14 * a combined executable.)
15 *
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
25 */
26
27 #include <stdio.h>
28
29 #include "sha1_lookup.h"
30 #include "common.h"
31
32 /*
33 * Conventional binary search loop looks like this:
34 *
35 * unsigned lo, hi;
36 * do {
37 * unsigned mi = (lo + hi) / 2;
38 * int cmp = "entry pointed at by mi" minus "target";
39 * if (!cmp)
40 * return (mi is the wanted one)
41 * if (cmp > 0)
42 * hi = mi; "mi is larger than target"
43 * else
44 * lo = mi+1; "mi is smaller than target"
45 * } while (lo < hi);
46 *
47 * The invariants are:
48 *
49 * - When entering the loop, lo points at a slot that is never
50 * above the target (it could be at the target), hi points at a
51 * slot that is guaranteed to be above the target (it can never
52 * be at the target).
53 *
54 * - We find a point 'mi' between lo and hi (mi could be the same
55 * as lo, but never can be as same as hi), and check if it hits
56 * the target. There are three cases:
57 *
58 * - if it is a hit, we are happy.
59 *
60 * - if it is strictly higher than the target, we set it to hi,
61 * and repeat the search.
62 *
63 * - if it is strictly lower than the target, we update lo to
64 * one slot after it, because we allow lo to be at the target.
65 *
66 * If the loop exits, there is no matching entry.
67 *
68 * When choosing 'mi', we do not have to take the "middle" but
69 * anywhere in between lo and hi, as long as lo <= mi < hi is
70 * satisfied. When we somehow know that the distance between the
71 * target and lo is much shorter than the target and hi, we could
72 * pick mi that is much closer to lo than the midway.
73 *
74 * Now, we can take advantage of the fact that SHA-1 is a good hash
75 * function, and as long as there are enough entries in the table, we
76 * can expect uniform distribution. An entry that begins with for
77 * example "deadbeef..." is much likely to appear much later than in
78 * the midway of the table. It can reasonably be expected to be near
79 * 87% (222/256) from the top of the table.
80 *
81 * However, we do not want to pick "mi" too precisely. If the entry at
82 * the 87% in the above example turns out to be higher than the target
83 * we are looking for, we would end up narrowing the search space down
84 * only by 13%, instead of 50% we would get if we did a simple binary
85 * search. So we would want to hedge our bets by being less aggressive.
86 *
87 * The table at "table" holds at least "nr" entries of "elem_size"
88 * bytes each. Each entry has the SHA-1 key at "key_offset". The
89 * table is sorted by the SHA-1 key of the entries. The caller wants
90 * to find the entry with "key", and knows that the entry at "lo" is
91 * not higher than the entry it is looking for, and that the entry at
92 * "hi" is higher than the entry it is looking for.
93 */
94 int sha1_entry_pos(const void *table,
95 size_t elem_size,
96 size_t key_offset,
97 unsigned lo, unsigned hi, unsigned nr,
98 const unsigned char *key)
99 {
100 const unsigned char *base = (const unsigned char*)table;
101 const unsigned char *hi_key, *lo_key;
102 unsigned ofs_0;
103
104 if (!nr || lo >= hi)
105 return -1;
106
107 if (nr == hi)
108 hi_key = NULL;
109 else
110 hi_key = base + elem_size * hi + key_offset;
111 lo_key = base + elem_size * lo + key_offset;
112
113 ofs_0 = 0;
114 do {
115 int cmp;
116 unsigned ofs, mi, range;
117 unsigned lov, hiv, kyv;
118 const unsigned char *mi_key;
119
120 range = hi - lo;
121 if (hi_key) {
122 for (ofs = ofs_0; ofs < 20; ofs++)
123 if (lo_key[ofs] != hi_key[ofs])
124 break;
125 ofs_0 = ofs;
126 /*
127 * byte 0 thru (ofs-1) are the same between
128 * lo and hi; ofs is the first byte that is
129 * different.
130 */
131 hiv = hi_key[ofs_0];
132 if (ofs_0 < 19)
133 hiv = (hiv << 8) | hi_key[ofs_0+1];
134 } else {
135 hiv = 256;
136 if (ofs_0 < 19)
137 hiv <<= 8;
138 }
139 lov = lo_key[ofs_0];
140 kyv = key[ofs_0];
141 if (ofs_0 < 19) {
142 lov = (lov << 8) | lo_key[ofs_0+1];
143 kyv = (kyv << 8) | key[ofs_0+1];
144 }
145 assert(lov < hiv);
146
147 if (kyv < lov)
148 return -1 - lo;
149 if (hiv < kyv)
150 return -1 - hi;
151
152 /*
153 * Even if we know the target is much closer to 'hi'
154 * than 'lo', if we pick too precisely and overshoot
155 * (e.g. when we know 'mi' is closer to 'hi' than to
156 * 'lo', pick 'mi' that is higher than the target), we
157 * end up narrowing the search space by a smaller
158 * amount (i.e. the distance between 'mi' and 'hi')
159 * than what we would have (i.e. about half of 'lo'
160 * and 'hi'). Hedge our bets to pick 'mi' less
161 * aggressively, i.e. make 'mi' a bit closer to the
162 * middle than we would otherwise pick.
163 */
164 kyv = (kyv * 6 + lov + hiv) / 8;
165 if (lov < hiv - 1) {
166 if (kyv == lov)
167 kyv++;
168 else if (kyv == hiv)
169 kyv--;
170 }
171 mi = (range - 1) * (kyv - lov) / (hiv - lov) + lo;
172
173 #ifdef INDEX_DEBUG_LOOKUP
174 printf("lo %u hi %u rg %u mi %u ", lo, hi, range, mi);
175 printf("ofs %u lov %x, hiv %x, kyv %x\n",
176 ofs_0, lov, hiv, kyv);
177 #endif
178
179 if (!(lo <= mi && mi < hi)) {
180 return git__throw(GIT_ERROR, "Assertion failure. Binary search invariant is false");
181 }
182
183 mi_key = base + elem_size * mi + key_offset;
184 cmp = memcmp(mi_key + ofs_0, key + ofs_0, 20 - ofs_0);
185 if (!cmp)
186 return mi;
187 if (cmp > 0) {
188 hi = mi;
189 hi_key = mi_key;
190 } else {
191 lo = mi + 1;
192 lo_key = mi_key + elem_size;
193 }
194 } while (lo < hi);
195 return -((int)lo)-1;
196 }