patch-ids: use the new generic "sha1_pos" function to lookup sha1
instead of the specific one from which the new one has been copied. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Christian Couder committed
Apr 4, 2009 at 22:59 UTC
5289bae17f24805cc8507129e21d794b0b56264c
1 file changed
+5
-88
patch-ids.c
+5
-88
@@ -1,6 +1,7 @@
1
#include "cache.h"
2
#include "diff.h"
3
#include "commit.h"
4
+#include "sha1-lookup.h"
5
#include "patch-ids.h"
6
7
static int commit_patch_id(struct commit *commit, struct diff_options *options,
@@ -15,99 +16,15 @@ static int commit_patch_id(struct commit *commit, struct diff_options *options,
16
return diff_flush_patch_id(options, sha1);
17
}
18
18
-static uint32_t take2(const unsigned char *id)
19
+static const unsigned char *patch_id_access(size_t index, void *table)
20
{
20
- return ((id[0] << 8) | id[1]);
21
+ struct patch_id **id_table = table;
22
+ return id_table[index]->patch_id;
23
}
24
23
-/*
24
- * Conventional binary search loop looks like this:
25
- *
26
- * do {
27
- * int mi = (lo + hi) / 2;
28
- * int cmp = "entry pointed at by mi" minus "target";
29
- * if (!cmp)
30
- * return (mi is the wanted one)
31
- * if (cmp > 0)
32
- * hi = mi; "mi is larger than target"
33
- * else
34
- * lo = mi+1; "mi is smaller than target"
35
- * } while (lo < hi);
36
- *
37
- * The invariants are:
38
- *
39
- * - When entering the loop, lo points at a slot that is never
40
- * above the target (it could be at the target), hi points at a
41
- * slot that is guaranteed to be above the target (it can never
42
- * be at the target).
43
- *
44
- * - We find a point 'mi' between lo and hi (mi could be the same
45
- * as lo, but never can be the same as hi), and check if it hits
46
- * the target. There are three cases:
47
- *
48
- * - if it is a hit, we are happy.
49
- *
50
- * - if it is strictly higher than the target, we update hi with
51
- * it.
52
- *
53
- * - if it is strictly lower than the target, we update lo to be
54
- * one slot after it, because we allow lo to be at the target.
55
- *
56
- * When choosing 'mi', we do not have to take the "middle" but
57
- * anywhere in between lo and hi, as long as lo <= mi < hi is
58
- * satisfied. When we somehow know that the distance between the
59
- * target and lo is much shorter than the target and hi, we could
60
- * pick mi that is much closer to lo than the midway.
61
- */
25
static int patch_pos(struct patch_id **table, int nr, const unsigned char *id)
26
{
64
- int hi = nr;
65
- int lo = 0;
66
- int mi = 0;
67
-
68
- if (!nr)
69
- return -1;
70
-
71
- if (nr != 1) {
72
- unsigned lov, hiv, miv, ofs;
73
-
74
- for (ofs = 0; ofs < 18; ofs += 2) {
75
- lov = take2(table[0]->patch_id + ofs);
76
- hiv = take2(table[nr-1]->patch_id + ofs);
77
- miv = take2(id + ofs);
78
- if (miv < lov)
79
- return -1;
80
- if (hiv < miv)
81
- return -1 - nr;
82
- if (lov != hiv) {
83
- /*
84
- * At this point miv could be equal
85
- * to hiv (but id could still be higher);
86
- * the invariant of (mi < hi) should be
87
- * kept.
88
- */
89
- mi = (nr-1) * (miv - lov) / (hiv - lov);
90
- if (lo <= mi && mi < hi)
91
- break;
92
- die("oops");
93
- }
94
- }
95
- if (18 <= ofs)
96
- die("cannot happen -- lo and hi are identical");
97
- }
98
-
99
- do {
100
- int cmp;
101
- cmp = hashcmp(table[mi]->patch_id, id);
102
- if (!cmp)
103
- return mi;
104
- if (cmp > 0)
105
- hi = mi;
106
- else
107
- lo = mi + 1;
108
- mi = (hi + lo) / 2;
109
- } while (lo < hi);
110
- return -lo-1;
27
+ return sha1_pos(id, table, nr, patch_id_access);
28
}
29
30
#define BUCKET_SIZE 190 /* 190 * 21 = 3990, with slop close enough to 4K */