authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-06-15 01:41:59 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-06-15 01:41:59 -07:00
log1a725deb03db9738ebb5d1cc7fc065ce16d9f903
tree02da659db575d2fcb91a9f4fe96827071787f406
parent853016b13438cbb980a253714c46500ce4696928
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

rework Tree search again


1 files changed, 34 insertions(+), 8 deletions(-)

git.zig+34-8
...@@ -1393,7 +1393,33 @@ pub const Tree = struct {...@@ -1393,7 +1393,33 @@ pub const Tree = struct {
1393 children: []const Object,1393 children: []const Object,
13941394
1395 pub fn get(self: Tree, name: string, hint: Object.Type) ?Object {1395 pub fn get(self: Tree, name: string, hint: Object.Type) ?Object {
1396 const i = std.sort.binarySearch(Object, self.children, .{ name, hint }, Object.search) orelse return null;1396 _ = hint;
1397 // modified std.sort.binarySearch
1398 const i = blk: {
1399 var low: usize = 0;
1400 var high: usize = self.children.len;
1401 while (low < high) {
1402 const mid = low + (high - low) / 2;
1403 switch (Object.search(name, self.children[mid])) {
1404 .eq => break :blk mid,
1405 .gt => low = mid + 1,
1406 .lt => high = mid,
1407 }
1408 }
1409 for (self.children[low..], 0..) |item, i| {
1410 if (std.mem.startsWith(u8, item.name, name)) {
1411 if (item.name[name.len..].len == 0) {
1412 break :blk low + i;
1413 }
1414 if (std.math.order(item.name[name.len..][0], '/') == .gt) {
1415 return null;
1416 }
1417 continue;
1418 }
1419 break;
1420 }
1421 return null;
1422 };
1397 return self.children[i];1423 return self.children[i];
1398 }1424 }
13991425
...@@ -1424,10 +1450,10 @@ pub const Tree = struct {...@@ -1424,10 +1450,10 @@ pub const Tree = struct {
1424 id: AnyId,1450 id: AnyId,
1425 name: [:0]const u8,1451 name: [:0]const u8,
14261452
1427 fn search(a: struct { []const u8, Type }, b: Object) std.math.Order {1453 fn search(a: []const u8, b: Object) std.math.Order {
1428 if (a[0].ptr != b.name.ptr) {1454 if (a.ptr != b.name.ptr) {
1429 const n = @min(a[0].len, b.name.len);1455 const n = @min(a.len, b.name.len);
1430 for (a[0][0..n], b.name[0..n]) |lhs_elem, rhs_elem| {1456 for (a[0..n], b.name[0..n]) |lhs_elem, rhs_elem| {
1431 switch (std.math.order(lhs_elem, rhs_elem)) {1457 switch (std.math.order(lhs_elem, rhs_elem)) {
1432 .eq => continue,1458 .eq => continue,
1433 .lt => return .lt,1459 .lt => return .lt,
...@@ -1435,9 +1461,9 @@ pub const Tree = struct {...@@ -1435,9 +1461,9 @@ pub const Tree = struct {
1435 }1461 }
1436 }1462 }
1437 }1463 }
1438 return switch (std.math.order(a[0].len, b.name.len)) {1464 return switch (std.math.order(a.len, b.name.len)) {
1439 .lt => if (a[1] == .directory) std.math.order('/', b.name[a[0].len]) else .lt,1465 .lt => .lt,
1440 .gt => if (b.mode.type == .directory) std.math.order(a[0][b.name.len], '/') else .gt,1466 .gt => if (b.mode.type == .directory) std.math.order(a[b.name.len], '/') else .gt,
1441 .eq => .eq,1467 .eq => .eq,
1442 };1468 };
1443 }1469 }