Skip to content

Commit

Permalink
Fixed code style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rameel committed Apr 10, 2016
1 parent 372a464 commit 0322c18
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Main/src/Collections/DisjointSets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace CodeJam.Collections
{
/// <summary>Disjoint sets without payload</summary>
/// <remarks>
/// <remarks>
/// See http://en.wikipedia.org/wiki/Disjoint-set_data_structure
/// </remarks>
[PublicAPI]
Expand All @@ -25,7 +25,7 @@ public void Add(int count)
{
for (var i = 0; i < count; ++i)
{
Nodes_.Add(new BasicNode { ParentIndex = -1, Rank = 0 });
Nodes.Add(new BasicNode { ParentIndex = -1, Rank = 0 });
}
SetsCount += count;
}
Expand Down
14 changes: 8 additions & 6 deletions Main/src/Collections/DisjointSetsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ namespace CodeJam.Collections
public class DisjointSetsBase<T> where T : BasicNode
{
/// <summary>All nodes</summary>
protected readonly List<T> Nodes_ = new List<T>();
protected readonly List<T> Nodes = new List<T>();

/// <summary>Creates an empty base</summary>
protected DisjointSetsBase() { }

/// <summary>The number of nodes</summary>
public int Count => Nodes_.Count;
public int Count => Nodes.Count;

/// <summary>The number of disjoint sets</summary>
public int SetsCount { get; protected set; }
Expand All @@ -34,7 +34,7 @@ public int IndexToSetId(int index)
var rootIndex = index;
for (;;)
{
var parentIndex = Nodes_[rootIndex].ParentIndex;
var parentIndex = Nodes[rootIndex].ParentIndex;
if (parentIndex == -1)
{
break;
Expand All @@ -46,7 +46,7 @@ public int IndexToSetId(int index)
// walk from the passed element upto the root replacing the the ParentIndex with the root index
while (index != rootIndex)
{
var node = Nodes_[index];
var node = Nodes[index];
index = node.ParentIndex;
node.ParentIndex = rootIndex;
}
Expand All @@ -60,14 +60,16 @@ public void Union(int elementOfSet1Index, int elementOfSet2Index)
{
elementOfSet1Index = IndexToSetId(elementOfSet1Index);
elementOfSet2Index = IndexToSetId(elementOfSet2Index);

if (elementOfSet1Index == elementOfSet2Index)
{
return; // Already the single set
}

var set1Root = Nodes_[elementOfSet1Index];
var set2Root = Nodes_[elementOfSet2Index];
var set1Root = Nodes[elementOfSet1Index];
var set2Root = Nodes[elementOfSet2Index];
var rankDifference = set1Root.Rank - set2Root.Rank;

// Attach the tree with a smaller rank to the tree with a higher rank.
// The resulting tree rank is equal to the higher rank
// except the case when initial ranks are equal.
Expand Down
10 changes: 5 additions & 5 deletions Main/src/Collections/DisjointSetsT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ public DisjointSets(IEnumerable<T> values)

/// <summary>Gets an element by its index</summary>
/// <param name="index">Elmement's index</param>
public T this[int index] => Nodes_[index].Value;
public T this[int index] => Nodes[index].Value;

/// <summary>Appends a list of values</summary>
/// <param name="values">The values to append</param>
public void Add(IEnumerable<T> values)
{
var initialNodesCount = Nodes_.Count;
Nodes_.AddRange(values.Select(_ => new Node { Value = _, ParentIndex = -1, Rank = 0 }));
SetsCount += Nodes_.Count - initialNodesCount;
var initialNodesCount = Nodes.Count;
Nodes.AddRange(values.Select(_ => new Node { Value = _, ParentIndex = -1, Rank = 0 }));
SetsCount += Nodes.Count - initialNodesCount;
}

/// <summary>Appends a single element</summary>
/// <param name="value">The value to append</param>
public void Add(T value)
{
Nodes_.Add(new Node { Value = value, ParentIndex = -1, Rank = 0 });
Nodes.Add(new Node { Value = value, ParentIndex = -1, Rank = 0 });
++SetsCount;
}

Expand Down
14 changes: 7 additions & 7 deletions Main/tests/Collections/DisjointSetsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ namespace CodeJam
[TestFixture]
public class DisjointSetsTest
{
private readonly Random random_ = new Random();
private readonly Random _random = new Random();
private const int ElementsNumber = 10000;
private readonly List<int> seq_ = Enumerable.Range(0, ElementsNumber).ToList();
private readonly List<int> _seq = Enumerable.Range(0, ElementsNumber).ToList();

[Test]
public void Test01NonGeneric()
{
for (var i = 1; i <= ElementsNumber; i += 1 + i / (10 + random_.Next(0, 10)))
for (var i = 1; i <= ElementsNumber; i += 1 + i / (10 + _random.Next(0, 10)))
{
Console.WriteLine($"i = {i}");
var djs = new DisjointSets(ElementsNumber);
foreach (var el in RandomShuffle(seq_))
foreach (var el in RandomShuffle(_seq))
{
djs.Union(el, el % i);
}
Expand All @@ -33,10 +33,10 @@ public void Test01NonGeneric()
[Test]
public void Test02Generic()
{
for (var i = 1; i <= ElementsNumber; i += 1 + i / (10 + random_.Next(0, 10)))
for (var i = 1; i <= ElementsNumber; i += 1 + i / (10 + _random.Next(0, 10)))
{
Console.WriteLine($"i = {i}");
var rs = RandomShuffle(seq_).ToList();
var rs = RandomShuffle(_seq).ToList();
var djs = new DisjointSets<int>(rs);
foreach (var el in rs)
{
Expand All @@ -60,6 +60,6 @@ private static void VerifySets<T>(DisjointSetsBase<T> djs, int mod) where T : Ba
}
}

private IEnumerable<T> RandomShuffle<T>(IEnumerable<T> en) => en.OrderBy(x => random_.Next());
private IEnumerable<T> RandomShuffle<T>(IEnumerable<T> en) => en.OrderBy(x => _random.Next());
}
}

0 comments on commit 0322c18

Please sign in to comment.