fix an issue in findSegment

This commit is contained in:
falsycat 2025-03-30 08:56:14 +09:00
parent a1ede8d1a0
commit b9153e2708

View File

@ -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));
}
}