authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-11-27 20:48:54+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-11-27 20:48:54+01:00
loge427ba9cd59dc876d27e4a71104b5156460ca618
tree7961791cf75d15df62bb3f463933151f0ec7352d
parent854774d468b0a439def7eef73c544ac8d64f1b62

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

1 files changed, 14 insertions(+), 9 deletions(-)

lib/std/sort.zig+14-9
...@@ -678,18 +678,23 @@ pub fn partitionPoint(...@@ -678,18 +678,23 @@ pub fn partitionPoint(
678 context: anytype,678 context: anytype,
679 comptime predicate: fn (@TypeOf(context), T) bool,679 comptime predicate: fn (@TypeOf(context), T) bool,
680) usize {680) usize {
681 var low: usize = 0;681 var it: usize = 0;
682 var high: usize = items.len;682 var len: usize = items.len;
683683
684 while (low < high) {684 while (len > 1) {
685 const mid = low + (high - low) / 2;685 const half: usize = len / 2;
686 if (predicate(context, items[mid])) {686 len -= half;
687 low = mid + 1;687 if (predicate(context, items[it + half - 1])) {
688 } else {688 @branchHint(.unpredictable);
689 high = mid;689 it += half;
690 }690 }
691 }691 }
692 return low;692
693 if (it < items.len) {
694 it += @intFromBool(predicate(context, items[it]));
695 }
696
697 return it;
693}698}
694699
695test partitionPoint {700test partitionPoint {