From 2ba98d0d49b0d18eb732044b5dcbd0f70a5ac0bc Mon Sep 17 00:00:00 2001 From: beck-thompson Date: Tue, 11 Feb 2025 18:30:01 -0800 Subject: [PATCH 1/3] Fix --- .../Prototypes/MultiRootInheritanceGraph.cs | 24 +++++++++---------- .../Shared/Prototypes/MultiRootGraphTest.cs | 7 ++++++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Robust.Shared/Prototypes/MultiRootInheritanceGraph.cs b/Robust.Shared/Prototypes/MultiRootInheritanceGraph.cs index c7a797dd586..95d5efa1337 100644 --- a/Robust.Shared/Prototypes/MultiRootInheritanceGraph.cs +++ b/Robust.Shared/Prototypes/MultiRootInheritanceGraph.cs @@ -35,6 +35,18 @@ public bool TryGetParents(T id, [NotNullWhen(true)] out T[]? parents) public void Add(T id, params T[] parents) { + _rootNodes.Remove(id); + + foreach (var parent in parents) + { + var edges = _edges.GetOrNew(parent); + edges.Add(id); + _parents[id] = parents; + + if (!_parents.ContainsKey(parent)) + _rootNodes.Add(parent); + } + //check for circular inheritance foreach (var parent in parents) { @@ -57,18 +69,6 @@ public void Add(T id, params T[] parents) } } } - - _rootNodes.Remove(id); - - foreach (var parent in parents) - { - var edges = _edges.GetOrNew(parent); - edges.Add(id); - _parents[id] = parents; - - if (!_parents.ContainsKey(parent)) - _rootNodes.Add(parent); - } } public bool Remove(T id, bool force = false) diff --git a/Robust.UnitTesting/Shared/Prototypes/MultiRootGraphTest.cs b/Robust.UnitTesting/Shared/Prototypes/MultiRootGraphTest.cs index a907c1d795d..adfa7d0f0e8 100644 --- a/Robust.UnitTesting/Shared/Prototypes/MultiRootGraphTest.cs +++ b/Robust.UnitTesting/Shared/Prototypes/MultiRootGraphTest.cs @@ -104,4 +104,11 @@ public void CircleCheckTest() Assert.Throws(() => graph.Add(Id3, new[] { Id1 })); } + + [Test] + public void IsOwnParentTest() + { + var graph = new MultiRootInheritanceGraph(); + Assert.Throws(() => graph.Add(Id1, new []{Id1})); + } } From b1c7757f417decf6e478a4444703cb196d80a7ff Mon Sep 17 00:00:00 2001 From: beck-thompson Date: Thu, 13 Feb 2025 10:25:33 -0800 Subject: [PATCH 2/3] This is better! --- .../Prototypes/MultiRootInheritanceGraph.cs | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/Robust.Shared/Prototypes/MultiRootInheritanceGraph.cs b/Robust.Shared/Prototypes/MultiRootInheritanceGraph.cs index 95d5efa1337..bff7e5d6e19 100644 --- a/Robust.Shared/Prototypes/MultiRootInheritanceGraph.cs +++ b/Robust.Shared/Prototypes/MultiRootInheritanceGraph.cs @@ -35,21 +35,12 @@ public bool TryGetParents(T id, [NotNullWhen(true)] out T[]? parents) public void Add(T id, params T[] parents) { - _rootNodes.Remove(id); - - foreach (var parent in parents) - { - var edges = _edges.GetOrNew(parent); - edges.Add(id); - _parents[id] = parents; - - if (!_parents.ContainsKey(parent)) - _rootNodes.Add(parent); - } - //check for circular inheritance foreach (var parent in parents) { + if (EqualityComparer.Default.Equals(parent,id)) + throw new InvalidOperationException($"Circular Inheritance detected for id \"{id}\" and parent \"{parent}\""); + var parentsL1 = GetParents(parent); if(parentsL1 == null) continue; @@ -69,6 +60,18 @@ public void Add(T id, params T[] parents) } } } + + _rootNodes.Remove(id); + + foreach (var parent in parents) + { + var edges = _edges.GetOrNew(parent); + edges.Add(id); + _parents[id] = parents; + + if (!_parents.ContainsKey(parent)) + _rootNodes.Add(parent); + } } public bool Remove(T id, bool force = false) From 278815661e7613060a56bfe3d7b20dc922d3e48d Mon Sep 17 00:00:00 2001 From: beck-thompson Date: Thu, 13 Feb 2025 10:47:46 -0800 Subject: [PATCH 3/3] Fixes --- Robust.Shared/Prototypes/MultiRootInheritanceGraph.cs | 4 ++-- Robust.UnitTesting/Shared/Prototypes/MultiRootGraphTest.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Robust.Shared/Prototypes/MultiRootInheritanceGraph.cs b/Robust.Shared/Prototypes/MultiRootInheritanceGraph.cs index bff7e5d6e19..66cdc5d6d2f 100644 --- a/Robust.Shared/Prototypes/MultiRootInheritanceGraph.cs +++ b/Robust.Shared/Prototypes/MultiRootInheritanceGraph.cs @@ -38,8 +38,8 @@ public void Add(T id, params T[] parents) //check for circular inheritance foreach (var parent in parents) { - if (EqualityComparer.Default.Equals(parent,id)) - throw new InvalidOperationException($"Circular Inheritance detected for id \"{id}\" and parent \"{parent}\""); + if (EqualityComparer.Default.Equals(parent, id)) + throw new InvalidOperationException($"Self Inheritance detected for id \"{id}\"!"); var parentsL1 = GetParents(parent); if(parentsL1 == null) continue; diff --git a/Robust.UnitTesting/Shared/Prototypes/MultiRootGraphTest.cs b/Robust.UnitTesting/Shared/Prototypes/MultiRootGraphTest.cs index adfa7d0f0e8..8344f276ec3 100644 --- a/Robust.UnitTesting/Shared/Prototypes/MultiRootGraphTest.cs +++ b/Robust.UnitTesting/Shared/Prototypes/MultiRootGraphTest.cs @@ -109,6 +109,6 @@ public void CircleCheckTest() public void IsOwnParentTest() { var graph = new MultiRootInheritanceGraph(); - Assert.Throws(() => graph.Add(Id1, new []{Id1})); + Assert.Throws(() => graph.Add(Id1, new []{ Id1 })); } }