summaryrefslogtreecommitdiff
path: root/src/Places.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-04 22:58:39 -0300
committerGabriel Schneider <[email protected]>2026-11-04 22:58:39 -0300
commite57b26fc1406a2f1e6541558d5ebd35d7ef54b1d (patch)
tree4e22f4b7d475193cec565811a4e5d3961999d3c3 /src/Places.zig
parent7f0e75d6e44e0371e204528792a13f9cac5f2b76 (diff)
downloadpardes-e57b26fc1406a2f1e6541558d5ebd35d7ef54b1d.tar.gz
pardes-e57b26fc1406a2f1e6541558d5ebd35d7ef54b1d.zip
Places: one lineOf for the line a byte is on, std.sort.partitionPoint in place of five hand-written binary searches (firstFrom, paint's, tickOf's, and Query's lineOfOffset and writePlacesRows', the latter two the same search as tickOf's)
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/Places.zig')
-rw-r--r--src/Places.zig42
1 files changed, 20 insertions, 22 deletions
diff --git a/src/Places.zig b/src/Places.zig
index 30c14b46..d5a4bb4d 100644
--- a/src/Places.zig
+++ b/src/Places.zig
@@ -165,13 +165,20 @@ pub fn complete(set: *const Places) bool {
/// The first hit starting at or after `at`.
pub fn firstFrom(hits: []const Hit, at: usize) usize {
- var lo: usize = 0;
- var hi = hits.len;
- while (lo < hi) {
- const mid = lo + (hi - lo) / 2;
- if (hits[mid].start < at) lo = mid + 1 else hi = mid;
- }
- return lo;
+ return std.sort.partitionPoint(Hit, hits, at, struct {
+ fn before(byte: usize, h: Hit) bool {
+ return h.start < byte;
+ }
+ }.before);
+}
+
+/// The line (0-based) that holds byte `at`, by a file's line index.
+pub fn lineOf(index: []const usize, at: usize) usize {
+ return std.sort.partitionPoint(usize, index, at, struct {
+ fn before(byte: usize, start: usize) bool {
+ return start <= byte;
+ }
+ }.before) -| 1;
}
/// Search `pane`'s text for `pattern` from now on, in place of whatever
@@ -700,13 +707,11 @@ pub fn paint(set: *Places, p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect,
const next = File.lineStart(p.gpa, f, @intCast(line + 1));
const le = if (next > ls and next <= f.content.len and f.content[next - 1] == '\n') next - 1 else next;
// the first hit ending past the line's start
- var lo: usize = 0;
- var hi = hits.len;
- while (lo < hi) {
- const mid = lo + (hi - lo) / 2;
- if (hits[mid].end <= ls) lo = mid + 1 else hi = mid;
- }
- var i = lo;
+ var i = std.sort.partitionPoint(Hit, hits, ls, struct {
+ fn before(line_start: usize, h: Hit) bool {
+ return h.end <= line_start;
+ }
+ }.before);
while (i < hits.len and hits[i].start <= le) : (i += 1) {
const a = @max(hits[i].start, ls) - ls;
const b = @min(hits[i].end, le) - ls;
@@ -738,14 +743,7 @@ fn buildTicks(set: *Places, p: *Pardes, f: *File.State) void {
/// The step of the rail holding place `i`.
fn tickOf(set: *Places, p: *Pardes, f: *File.State, i: usize) ?u16 {
const index = File.lineIndex(p.gpa, f) catch return null;
- const at = set.hits.items[i].start;
- var lo: usize = 0;
- var hi = index.len;
- while (lo < hi) {
- const mid = lo + (hi - lo) / 2;
- if (index[mid] <= at) lo = mid + 1 else hi = mid;
- }
- const line = lo -| 1;
+ const line = lineOf(index, set.hits.items[i].start);
return @intCast(@min(tick_scale - 1, line * tick_scale / @max(1, index.len)));
}