diff options
Diffstat (limited to 'core/Names.zig')
| -rw-r--r-- | core/Names.zig | 92 |
1 files changed, 82 insertions, 10 deletions
diff --git a/core/Names.zig b/core/Names.zig index 47165f52..d063bd17 100644 --- a/core/Names.zig +++ b/core/Names.zig @@ -29,6 +29,10 @@ query_buf: [max_query]u8 = undefined, query_len: u8 = 0, /// Bumped whenever `hits` change: the rows are made again only then. gen: u32 = 0, +/// How many of `hits` are in order, best first: as many as a list shows +/// (`max_rows`), or as far as its cursor went (`reachTo`); the matches after +/// them are counted, in no order. +reach: u32 = max_rows, /// The query is `query_buf[0..query_len]`: what lies past it is undefined /// (0xAA in Debug, not in a release build), so no digest reads it. @@ -81,6 +85,7 @@ pub fn reset(n: *Names) void { n.pending.clearRetainingCapacity(); n.pending_at = 0; n.scored = 0; + n.reach = max_rows; n.gen +%= 1; } @@ -124,6 +129,7 @@ pub fn typed(n: *Names, gpa: std.mem.Allocator, text: []const u8) void { const grew = n.query_len > 0 and std.mem.startsWith(u8, q, n.queryText()) and std.mem.indexOfScalar(u8, q[n.query_len..], ' ') == null; n.query_len = @intCast(q.len); @memcpy(n.query_buf[0..q.len], q); + n.reach = max_rows; if (grew) { const pending = n.pending.items[n.pending_at..]; std.mem.copyForwards(u32, n.pending.items[0..pending.len], pending); @@ -165,19 +171,65 @@ pub fn rescore(n: *Names, gpa: std.mem.Allocator) void { if (!empty and left == 0) break; } // An empty query keeps the source's order (the walk's, recency): no - // row moves as the walk goes on, it only grows at the foot. - if (!empty) std.sort.pdq(Hit, n.hits.items, n.cands.items, struct { - fn lt(cands: []const Cand, a: Hit, b: Hit) bool { - if (a.score != b.score) return a.score > b.score; - const la = cands[a.cand].key_len; - const lb = cands[b.cand].key_len; - if (la != lb) return la < lb; - return a.cand < b.cand; - } - }.lt); + // row moves as the walk goes on, it only grows at the foot. Else the + // best `reach` first: a sort of every hit was most of a first key's + // 8 ms over 50,000 paths. + if (!empty) bestFirst(n.hits.items, n.cands.items, n.reach); if (n.hits.items.len != had or n.scored != was_scored or count > 0) n.gen +%= 1; } +/// Hits sorted as far as row `rows`, for a cursor gone past the ones that +/// were (an empty query's are in the source's order already). +pub fn reachTo(n: *Names, rows: usize) void { + if (rows <= n.reach or std.mem.trim(u8, n.queryText(), " ").len == 0) return; + n.reach = @intCast(@min(rows, max_cands)); + bestFirst(n.hits.items, n.cands.items, n.reach); + n.gen +%= 1; +} + +/// Hit `a` goes before `b`: the higher score, then the shorter key, then +/// the candidate added first. +fn before(cands: []const Cand, a: Hit, b: Hit) bool { + if (a.score != b.score) return a.score > b.score; + const la = cands[a.cand].key_len; + const lb = cands[b.cand].key_len; + if (la != lb) return la < lb; + return a.cand < b.cand; +} + +/// `hits`' best `k` to the front, in order, the rest after them in no +/// order: the best so far kept as a heap whose root is the worst of them, +/// then sorted. O(n log k). +fn bestFirst(hits: []Hit, cands: []const Cand, k_in: usize) void { + const k = @min(k_in, hits.len); + if (k == 0) return; + const heap = hits[0..k]; + var i = k / 2; + while (i > 0) { + i -= 1; + sink(heap, cands, i); + } + for (hits[k..]) |*h| if (before(cands, h.*, heap[0])) { + std.mem.swap(Hit, h, &heap[0]); + sink(heap, cands, 0); + }; + std.sort.pdq(Hit, heap, cands, before); +} + +/// Heap entry `at` taken down past the worse of its children while it is +/// better than that child. +fn sink(heap: []Hit, cands: []const Cand, from: usize) void { + var at = from; + while (true) { + const l = 2 * at + 1; + if (l >= heap.len) return; + const c = if (l + 1 < heap.len and before(cands, heap[l], heap[l + 1])) l + 1 else l; + if (!before(cands, heap[at], heap[c])) return; + std.mem.swap(Hit, &heap[at], &heap[c]); + at = c; + } +} + /// The row of hit `i` and, in `marks`, the bytes of it the query matched. pub fn row(n: *const Names, i: usize, marks: []u16, nmarks: *usize) []const u8 { const c = n.cands.items[n.hits.items[i].cand]; @@ -363,3 +415,23 @@ test "Names ranks real paths as the pick prototype did" { try std.testing.expectEqualStrings("src/gui/Post.zig", topFor("gui/po")); try std.testing.expectEqualStrings("src/Output.zig", topFor("Out")); } + +test "Names keeps its best `reach` hits first in a full sort's order, the rest after them, and sorts further as a cursor goes past them" { + const gpa = std.testing.allocator; + var n: Names = .{}; + defer n.deinit(gpa); + var buf: [64]u8 = undefined; + for (0..3000) |i| { + const path = try std.fmt.bufPrint(&buf, "dir{d}/sub{d}/file_{d}.zig", .{ i % 7, i % 13, i }); + try n.add(gpa, path, 0, path.len, baseOf(path), 0); + } + n.typed(gpa, "f1"); + while (n.busy()) n.rescore(gpa); + const all = try gpa.dupe(Hit, n.hits.items); + defer gpa.free(all); + std.sort.pdq(Hit, all, n.cands.items, before); + try std.testing.expect(all.len > max_rows); + try std.testing.expectEqualSlices(Hit, all[0..max_rows], n.hits.items[0..max_rows]); + n.reachTo(max_rows + 500); + try std.testing.expectEqualSlices(Hit, all[0 .. max_rows + 500], n.hits.items[0 .. max_rows + 500]); +} |
