git
/
hash-lookup.c
134 строки · 3.3 Кб
1#define USE_THE_REPOSITORY_VARIABLE2
3#include "git-compat-util.h"4#include "hash.h"5#include "hash-lookup.h"6#include "read-cache-ll.h"7
8static uint32_t take2(const struct object_id *oid, size_t ofs)9{
10return ((oid->hash[ofs] << 8) | oid->hash[ofs + 1]);11}
12
13/*
14* Conventional binary search loop looks like this:
15*
16* do {
17* int mi = lo + (hi - lo) / 2;
18* int cmp = "entry pointed at by mi" minus "target";
19* if (!cmp)
20* return (mi is the wanted one)
21* if (cmp > 0)
22* hi = mi; "mi is larger than target"
23* else
24* lo = mi+1; "mi is smaller than target"
25* } while (lo < hi);
26*
27* The invariants are:
28*
29* - When entering the loop, lo points at a slot that is never
30* above the target (it could be at the target), hi points at a
31* slot that is guaranteed to be above the target (it can never
32* be at the target).
33*
34* - We find a point 'mi' between lo and hi (mi could be the same
35* as lo, but never can be the same as hi), and check if it hits
36* the target. There are three cases:
37*
38* - if it is a hit, we are happy.
39*
40* - if it is strictly higher than the target, we update hi with
41* it.
42*
43* - if it is strictly lower than the target, we update lo to be
44* one slot after it, because we allow lo to be at the target.
45*
46* When choosing 'mi', we do not have to take the "middle" but
47* anywhere in between lo and hi, as long as lo <= mi < hi is
48* satisfied. When we somehow know that the distance between the
49* target and lo is much shorter than the target and hi, we could
50* pick mi that is much closer to lo than the midway.
51*/
52/*
53* The table should contain "nr" elements.
54* The oid of element i (between 0 and nr - 1) should be returned
55* by "fn(i, table)".
56*/
57int oid_pos(const struct object_id *oid, const void *table, size_t nr,58oid_access_fn fn)59{
60size_t hi = nr;61size_t lo = 0;62size_t mi = 0;63
64if (!nr)65return -1;66
67if (nr != 1) {68size_t lov, hiv, miv, ofs;69
70for (ofs = 0; ofs < the_hash_algo->rawsz - 2; ofs += 2) {71lov = take2(fn(0, table), ofs);72hiv = take2(fn(nr - 1, table), ofs);73miv = take2(oid, ofs);74if (miv < lov)75return -1;76if (hiv < miv)77return index_pos_to_insert_pos(nr);78if (lov != hiv) {79/*80* At this point miv could be equal
81* to hiv (but hash could still be higher);
82* the invariant of (mi < hi) should be
83* kept.
84*/
85mi = (nr - 1) * (miv - lov) / (hiv - lov);86if (lo <= mi && mi < hi)87break;88BUG("assertion failed in binary search");89}90}91}92
93do {94int cmp;95cmp = oidcmp(fn(mi, table), oid);96if (!cmp)97return mi;98if (cmp > 0)99hi = mi;100else101lo = mi + 1;102mi = lo + (hi - lo) / 2;103} while (lo < hi);104return index_pos_to_insert_pos(lo);105}
106
107int bsearch_hash(const unsigned char *hash, const uint32_t *fanout_nbo,108const unsigned char *table, size_t stride, uint32_t *result)109{
110uint32_t hi, lo;111
112hi = ntohl(fanout_nbo[*hash]);113lo = ((*hash == 0x0) ? 0 : ntohl(fanout_nbo[*hash - 1]));114
115while (lo < hi) {116unsigned mi = lo + (hi - lo) / 2;117int cmp = hashcmp(table + mi * stride, hash,118the_repository->hash_algo);119
120if (!cmp) {121if (result)122*result = mi;123return 1;124}125if (cmp > 0)126hi = mi;127else128lo = mi + 1;129}130
131if (result)132*result = lo;133return 0;134}
135