sha1_lookup.c 641 Bytes
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7
 */

8 9
#include "sha1_lookup.h"

10 11
#include <stdio.h>

12
#include "oid.h"
13

14 15 16 17 18
int sha1_position(const void *table,
			size_t stride,
			unsigned lo, unsigned hi,
			const unsigned char *key)
{
19 20
	const unsigned char *base = table;

21
	while (lo < hi) {
22
		unsigned mi = (lo + hi) / 2;
23
		int cmp = git_oid__hashcmp(base + mi * stride, key);
24 25 26 27 28 29 30 31

		if (!cmp)
			return mi;

		if (cmp > 0)
			hi = mi;
		else
			lo = mi+1;
32
	}
33 34 35

	return -((int)lo)-1;
}