mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 05:44:20 +00:00
std.sort.partitionPoint: faster implementation (#30005)
Migrated from https://github.com/ziglang/zig/pull/21419 Co-authored-by: Jonathan Hallstrom <lmj.hallstrom@gmail.com> Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30005
This commit is contained in:
parent
854774d468
commit
e427ba9cd5
1 changed files with 14 additions and 9 deletions
|
|
@ -678,18 +678,23 @@ pub fn partitionPoint(
|
||||||
context: anytype,
|
context: anytype,
|
||||||
comptime predicate: fn (@TypeOf(context), T) bool,
|
comptime predicate: fn (@TypeOf(context), T) bool,
|
||||||
) usize {
|
) usize {
|
||||||
var low: usize = 0;
|
var it: usize = 0;
|
||||||
var high: usize = items.len;
|
var len: usize = items.len;
|
||||||
|
|
||||||
while (low < high) {
|
while (len > 1) {
|
||||||
const mid = low + (high - low) / 2;
|
const half: usize = len / 2;
|
||||||
if (predicate(context, items[mid])) {
|
len -= half;
|
||||||
low = mid + 1;
|
if (predicate(context, items[it + half - 1])) {
|
||||||
} else {
|
@branchHint(.unpredictable);
|
||||||
high = mid;
|
it += half;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return low;
|
|
||||||
|
if (it < items.len) {
|
||||||
|
it += @intFromBool(predicate(context, items[it]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
test partitionPoint {
|
test partitionPoint {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue