-
Notifications
You must be signed in to change notification settings - Fork 223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disjoint Sets primitives #9145
Merged
Merged
Disjoint Sets primitives #9145
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fbfd1da
Disjoint Sets primitives
antonioaversa 60b8f06
Fix code smell
antonioaversa dbe0313
Code review 1
antonioaversa 0652c01
Code review 2
antonioaversa ce53191
Fix alignment
antonioaversa 3f41c0e
Code review 3
antonioaversa c17861b
Code review 4
antonioaversa a023ad7
Code review 5
antonioaversa 571aece
Code review 6
antonioaversa File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
analyzers/src/SonarAnalyzer.Common/Helpers/DisjointSetsPrimitives.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2024 SonarSource SA | ||
* mailto: contact AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
namespace SonarAnalyzer.Helpers; | ||
|
||
public static class DisjointSetsPrimitives | ||
{ | ||
public static void Union(IDictionary<string, string> parents, string from, string to) => | ||
parents[FindRoot(parents, from)] = FindRoot(parents, to); | ||
|
||
public static string FindRoot(IDictionary<string, string> parents, string element) => | ||
parents[element] is var root && root != element ? FindRoot(parents, root) : root; | ||
|
||
public static List<List<string>> DisjointSets(IDictionary<string, string> parents) => | ||
parents.GroupBy(x => FindRoot(parents, x.Key), x => x.Key).Select(x => x.OrderBy(x => x).ToList()).OrderBy(x => x[0]).ToList(); | ||
mary-georgiou-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
70 changes: 70 additions & 0 deletions
70
analyzers/tests/SonarAnalyzer.Test/Helpers/DisjointSetsPrimitivesTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using static SonarAnalyzer.Helpers.DisjointSetsPrimitives; | ||
|
||
namespace SonarAnalyzer.Test.Helpers; | ||
|
||
[TestClass] | ||
public class DisjointSetsPrimitivesTest | ||
{ | ||
[TestMethod] | ||
public void FindRootAndUnion_AreConsistent() | ||
mary-georgiou-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var parents = Enumerable.Range(1, 5).ToDictionary(i => i.ToString(), i => i.ToString()); | ||
foreach (var item in parents.Keys) | ||
{ | ||
FindRoot(parents, item).Should().Be(item); | ||
} | ||
|
||
Union(parents, "1", "1"); | ||
FindRoot(parents, "1").Should().Be("1"); // Reflexivity | ||
Union(parents, "1", "2"); | ||
FindRoot(parents, "1").Should().Be(FindRoot(parents, "2")); // Correctness | ||
Union(parents, "1", "2"); | ||
FindRoot(parents, "1").Should().Be(FindRoot(parents, "2")); // Idempotency | ||
Union(parents, "2", "1"); | ||
FindRoot(parents, "1").Should().Be(FindRoot(parents, "2")); // Symmetry | ||
|
||
FindRoot(parents, "3").Should().Be("3"); | ||
Union(parents, "2", "3"); | ||
FindRoot(parents, "2").Should().Be(FindRoot(parents, "3")); | ||
FindRoot(parents, "1").Should().Be(FindRoot(parents, "3")); // Transitivity | ||
Union(parents, "3", "4"); | ||
FindRoot(parents, "1").Should().Be(FindRoot(parents, "4")); // Double transitivity | ||
Union(parents, "4", "1"); | ||
FindRoot(parents, "4").Should().Be(FindRoot(parents, "1")); // Idempotency after transitivity | ||
gregory-paidis-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
[TestMethod] | ||
public void DisjointSetsAndUnion_AreConsistent() | ||
{ | ||
var parents = Enumerable.Range(1, 5).ToDictionary(i => i.ToString(), i => i.ToString()); | ||
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1"], ["2"], ["3"], ["4"], ["5"]]); // Initial state | ||
Union(parents, "1", "2"); | ||
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2"], ["3"], ["4"], ["5"]]); // Correctness | ||
Union(parents, "1", "2"); | ||
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2"], ["3"], ["4"], ["5"]]); // Idempotency | ||
|
||
Union(parents, "2", "3"); | ||
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2", "3"], ["4"], ["5"]]); | ||
gregory-paidis-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Union(parents, "1", "3"); | ||
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2", "3"], ["4"], ["5"]]); // Idempotency after transitivity | ||
|
||
Union(parents, "4", "5"); | ||
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2", "3"], ["4", "5"]]); // Separated trees | ||
Union(parents, "3", "4"); | ||
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2", "3", "4", "5"]]); // Merged trees | ||
} | ||
|
||
[TestMethod] | ||
public void DisjointSetsAndUnion_OfNestedTrees() | ||
{ | ||
var parents = Enumerable.Range(1, 6).ToDictionary(i => i.ToString(), i => i.ToString()); | ||
Union(parents, "1", "2"); | ||
Union(parents, "3", "4"); | ||
Union(parents, "5", "6"); | ||
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2"], ["3", "4"], ["5", "6"]]); // Merge of 1-height trees | ||
Union(parents, "2", "3"); | ||
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2", "3", "4"], ["5", "6"]]); // Merge of 2-height trees | ||
Union(parents, "4", "5"); | ||
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2", "3", "4", "5", "6"]]); // Merge of 1-height tree and 2-height tree | ||
gregory-paidis-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add a comment here explaining a bit about the purpose of the class and these methods in the context of sonar-dotnet.