git

Форк
0
/
hash-lookup.c 
134 строки · 3.3 Кб
1
#define USE_THE_REPOSITORY_VARIABLE
2

3
#include "git-compat-util.h"
4
#include "hash.h"
5
#include "hash-lookup.h"
6
#include "read-cache-ll.h"
7

8
static uint32_t take2(const struct object_id *oid, size_t ofs)
9
{
10
	return ((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
 */
57
int oid_pos(const struct object_id *oid, const void *table, size_t nr,
58
	    oid_access_fn fn)
59
{
60
	size_t hi = nr;
61
	size_t lo = 0;
62
	size_t mi = 0;
63

64
	if (!nr)
65
		return -1;
66

67
	if (nr != 1) {
68
		size_t lov, hiv, miv, ofs;
69

70
		for (ofs = 0; ofs < the_hash_algo->rawsz - 2; ofs += 2) {
71
			lov = take2(fn(0, table), ofs);
72
			hiv = take2(fn(nr - 1, table), ofs);
73
			miv = take2(oid, ofs);
74
			if (miv < lov)
75
				return -1;
76
			if (hiv < miv)
77
				return index_pos_to_insert_pos(nr);
78
			if (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
				 */
85
				mi = (nr - 1) * (miv - lov) / (hiv - lov);
86
				if (lo <= mi && mi < hi)
87
					break;
88
				BUG("assertion failed in binary search");
89
			}
90
		}
91
	}
92

93
	do {
94
		int cmp;
95
		cmp = oidcmp(fn(mi, table), oid);
96
		if (!cmp)
97
			return mi;
98
		if (cmp > 0)
99
			hi = mi;
100
		else
101
			lo = mi + 1;
102
		mi = lo + (hi - lo) / 2;
103
	} while (lo < hi);
104
	return index_pos_to_insert_pos(lo);
105
}
106

107
int bsearch_hash(const unsigned char *hash, const uint32_t *fanout_nbo,
108
		 const unsigned char *table, size_t stride, uint32_t *result)
109
{
110
	uint32_t hi, lo;
111

112
	hi = ntohl(fanout_nbo[*hash]);
113
	lo = ((*hash == 0x0) ? 0 : ntohl(fanout_nbo[*hash - 1]));
114

115
	while (lo < hi) {
116
		unsigned mi = lo + (hi - lo) / 2;
117
		int cmp = hashcmp(table + mi * stride, hash,
118
				  the_repository->hash_algo);
119

120
		if (!cmp) {
121
			if (result)
122
				*result = mi;
123
			return 1;
124
		}
125
		if (cmp > 0)
126
			hi = mi;
127
		else
128
			lo = mi + 1;
129
	}
130

131
	if (result)
132
		*result = lo;
133
	return 0;
134
}
135

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.