mirror of
https://github.com/facebook/sapling.git
synced 2024-10-11 09:17:30 +03:00
fc8c0d61a1
Summary: This allows us to use fastmanifest as a directory to drop in the python module. Test Plan: compiles, passes existing tests. Reviewers: lcharignon Reviewed By: lcharignon Subscribers: mitrandir, mjpieters Differential Revision: https://phabricator.intern.facebook.com/D3351021 Signature: t1:3351021:1464284417:6cbcde514ab1fd7b5caa6c83cb5577f3502dbc58
43 lines
880 B
C
43 lines
880 B
C
// Copyright 2016-present Facebook. All Rights Reserved.
|
|
//
|
|
// bsearch.c: binary search implementation with context-aware callback.
|
|
//
|
|
// no-check-code
|
|
|
|
#include <stdio.h>
|
|
#include <stddef.h>
|
|
|
|
#include "bsearch.h"
|
|
|
|
size_t bsearch_between(const void *needle,
|
|
const void *base, const size_t nel, const size_t width,
|
|
int (*compare)(const void *needle,
|
|
const void *fromarray,
|
|
const void *context),
|
|
const void *context) {
|
|
ptrdiff_t start = 0;
|
|
ptrdiff_t end = nel;
|
|
|
|
while (start < end) {
|
|
ptrdiff_t midpoint = start + ((end - start) / 2);
|
|
|
|
if (midpoint == nel) {
|
|
return nel;
|
|
}
|
|
|
|
const void *ptr = base + (midpoint * width);
|
|
|
|
int cmp = compare(needle, ptr, context);
|
|
|
|
if (cmp == 0) {
|
|
return midpoint;
|
|
} else if (cmp < 0) {
|
|
end = midpoint;
|
|
} else {
|
|
start = midpoint + 1;
|
|
}
|
|
}
|
|
|
|
return start;
|
|
}
|