From b9153e2708f5e32d3f5f5d0ba3142827042eb3cf Mon Sep 17 00:00:00 2001 From: falsycat Date: Sun, 30 Mar 2025 08:56:14 +0900 Subject: [PATCH] fix an issue in findSegment --- src/hncore/Digraph.zig | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/hncore/Digraph.zig b/src/hncore/Digraph.zig index 24c5bcb..44ec4df 100644 --- a/src/hncore/Digraph.zig +++ b/src/hncore/Digraph.zig @@ -72,6 +72,12 @@ pub fn Digraph(comptime T: type, comptime lessThanFn: LessThanFunc(T)) type { } + /// + pub fn getChildrenOf(self: *const @This(), from: T) []const Conn { + const begin, const end = self.findSegment(from); + return self.map.items[begin..end]; + } + fn findConnectionInSegment(self: *const @This(), from: T, to: T, begin: usize, end: usize) ?usize { for (self.map.items[begin..end], begin..) |v, idx| { if ((v.from == from) and (v.to == to)) { @@ -93,14 +99,14 @@ pub fn Digraph(comptime T: type, comptime lessThanFn: LessThanFunc(T)) type { var end : usize = undefined; if (baseFrom < from) { begin = baseIdx; - while ((begin < n) and (self.map.items[begin].from != from)) { begin += 1; } + while ((begin < n) and (self.map.items[begin].from < from)) { begin += 1; } end = begin; while ((end < n) and (self.map.items[end].from == from)) { end += 1; } } else if (baseFrom > from) { end = baseIdx; - while ((end > 0) and (self.map.items[end-1].from != from)) { end -= 1; } + while ((end > 0) and (self.map.items[end-1].from > from)) { end -= 1; } begin = end; while ((begin > 0) and (self.map.items[begin-1].from == from)) { begin -= 1; } @@ -256,20 +262,23 @@ test "chaotic operation" { defer sut.deinit(); const N = 100; + const P = 109; + const Q = 113; + const R = 127; for (0..N) |v| { const x: Sut.Node = @intCast(v); - try sut.connect(x*%7, x*%13); + try sut.connect((x*Q)%P, (x*R)%P); } for (N/2..N) |v| { const x: Sut.Node = @intCast(v); - try sut.disconnect(x*%7, x*%13); + try sut.disconnect((x*Q)%P, (x*R)%P); } for (0..N/2) |v| { const x: Sut.Node = @intCast(v); - try std.testing.expect(sut.isConnected(x*%7, x*%13)); + try std.testing.expect(sut.isConnected((x*Q)%P, (x*R)%P)); } for (N/2..N) |v| { const x: Sut.Node = @intCast(v); - try std.testing.expect(!sut.isConnected(x*%7, x*%13)); + try std.testing.expect(!sut.isConnected((x*Q)%P, (x*R)%P)); } }