Skip to content

Commit fa3ce54

Browse files
committed
Translated comments for DisjointSets
1 parent 871a7a8 commit fa3ce54

File tree

3 files changed

+53
-45
lines changed

3 files changed

+53
-45
lines changed

Main/src/Collections/DisjointSets.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
namespace CodeJam.Collections
22
{
3-
/// <summary>Реализация классической структуры Disjoint sets</summary>
3+
/// <summary>Disjoint sets without payload</summary>
44
/// <remarks>
5-
/// http://en.wikipedia.org/wiki/Disjoint-set_data_structure
5+
/// See http://en.wikipedia.org/wiki/Disjoint-set_data_structure
66
/// </remarks>
77
public sealed class DisjointSets : DisjointSetsBase<BasicNode>
88
{
9-
/// <summary>Создает пустой DS</summary>
9+
/// <summary>Creates an empty Disjoint sets</summary>
1010
public DisjointSets() { }
1111

12-
/// <summary>Создает DS с заданным количеством элементов</summary>
13-
/// <param name="count">Количество элементов</param>
12+
/// <summary>Creates a Disjoint sets with the given number of elements</summary>
13+
/// <param name="count">The initial number of elements</param>
1414
public DisjointSets(int count)
1515
{
1616
Add(count);
1717
}
1818

19-
/// <summary>Добавление заданного количества элементов</summary>
20-
/// <param name="count">Количество элементов</param>
19+
/// <summary>Appends the given number of new elements</summary>
20+
/// <param name="count">The number of elements to add</param>
2121
public void Add(int count)
2222
{
2323
for (var i = 0; i < count; ++i)

Main/src/Collections/DisjointSetsBase.cs

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,35 @@
22

33
namespace CodeJam.Collections
44
{
5-
/// <summary>Базовый класс для реализации Disjoint sets</summary>
5+
/// <summary>Disjoint sets implementation base</summary>
66
/// <remarks>
7-
/// http://en.wikipedia.org/wiki/Disjoint-set_data_structure
7+
/// See http://en.wikipedia.org/wiki/Disjoint-set_data_structure
88
/// </remarks>
99
public class DisjointSetsBase<T> where T : BasicNode
1010
{
11-
/// <summary>Список всех узлов</summary>
11+
/// <summary>All nodes</summary>
1212
protected readonly List<T> Nodes_ = new List<T>();
1313

14-
/// <summary>Создает пустой DS</summary>
14+
/// <summary>Creates an empty base</summary>
1515
protected DisjointSetsBase() { }
1616

17-
/// <summary>Число элементов</summary>
17+
/// <summary>The number of nodes</summary>
1818
public int Count => Nodes_.Count;
1919

20-
/// <summary>Число несвязанных множеств</summary>
20+
/// <summary>The number of disjoint sets</summary>
2121
public int SetsCount { get; protected set; }
2222

23-
/// <summary>Поиск идентификатора множества, которому принадлежит элемент по заданному индексу</summary>
24-
/// <param name="index">Индекс элемента</param>
25-
/// <returns>Идентификатор множества, которому принадлежит элемент</returns>
26-
/// <remarks>Идентификатор множества - это индекс элемента, представляющего множество</remarks>
23+
/// <summary>Finds a set identifier for the element</summary>
24+
/// <param name="index">The element index</param>
25+
/// <returns>The identifier of the containing set</returns>
26+
/// <remarks>
27+
/// The set identifier is the index of a single element representing the set.
28+
/// The Union operation may lead to a choice of a different representative for a set.
29+
/// In this case IndexToSetId(oldSetId) may be called to get the new set id.
30+
/// </remarks>
2731
public int IndexToSetId(int index)
2832
{
29-
// сначала ищем индекс корневого элемента дерева, к которому принадлежит наш узел
33+
// First, find a root element of a tree containing the passed element
3034
var rootIndex = index;
3135
for (;;)
3236
{
@@ -38,7 +42,8 @@ public int IndexToSetId(int index)
3842
rootIndex = parentIndex;
3943
}
4044

41-
// компрессия пути - идем от нашего элемента вверх к корню, обновляя ParentIndex на rootIndex
45+
// Then, do the path compression:
46+
// walk from the passed element upto the root replacing the the ParentIndex with the root index
4247
while (index != rootIndex)
4348
{
4449
var node = Nodes_[index];
@@ -48,48 +53,52 @@ public int IndexToSetId(int index)
4853
return rootIndex;
4954
}
5055

51-
/// <summary>Объединение двух множеств в одно</summary>
52-
/// <param name="elementOfSet1Index">Индекс какого-то элемента первого множества</param>
53-
/// <param name="elementOfSet2Index">Индекс какого-то элемента второго можества</param>
56+
/// <summary>Combines to distjoint sets into a single set</summary>
57+
/// <param name="elementOfSet1Index">Index of an element of the first set</param>
58+
/// <param name="elementOfSet2Index">Index of an element of the second set</param>
5459
public void Union(int elementOfSet1Index, int elementOfSet2Index)
5560
{
5661
elementOfSet1Index = IndexToSetId(elementOfSet1Index);
5762
elementOfSet2Index = IndexToSetId(elementOfSet2Index);
5863
if (elementOfSet1Index == elementOfSet2Index)
5964
{
60-
return; // уже одно множество
65+
return; // Already the single set
6166
}
6267

6368
var set1Root = Nodes_[elementOfSet1Index];
6469
var set2Root = Nodes_[elementOfSet2Index];
6570
var rankDifference = set1Root.Rank - set2Root.Rank;
66-
// Цепляем дерево с меньшим рангом к корню дерева с большим. В этом случае ранг получившегося дерева равен большему, кроме случая, когда ранги равны (тогда будет +1)
67-
if (rankDifference > 0) // у 1-го ранг больше
71+
// Attach the tree with a smaller rank to the tree with a higher rank.
72+
// The resulting tree rank is equal to the higher rank
73+
// except the case when initial ranks are equal.
74+
// In the latter case the new rank will be increased by 1
75+
if (rankDifference > 0) // 1st has higher rank
6876
{
6977
set2Root.ParentIndex = elementOfSet1Index;
7078
}
71-
else if (rankDifference < 0) // у 2-го больше
79+
else if (rankDifference < 0) // 2nd has the higher rank
7280
{
7381
set1Root.ParentIndex = elementOfSet2Index;
7482
}
75-
else // ранги равны. пофигу что к чему цеплять, но нужно увеличить ранг того, к чему прицепили
83+
else // ranks are equal and the new root choice is arbitrary
7684
{
7785
set2Root.ParentIndex = elementOfSet1Index;
7886
++set1Root.Rank;
7987
}
8088

81-
// поскольку слили 2 в одно, уменьшаем число сетов
89+
// we have joined 2 sets, so we have to decrease the count
8290
--SetsCount;
8391
}
8492
}
8593

86-
/// <summary>Базовый класс узла дерева</summary>
94+
/// <summary>Node base class</summary>
8795
public class BasicNode
8896
{
89-
/// <summary>Индекс родителя (после компрессии пути указывает на корень)</summary>
97+
/// <summary>Parent node index</summary>
98+
/// <remarks>Points to the root after a path compression</remarks>
9099
public int ParentIndex;
91100

92-
/// <summary>Примерный уровень ноды в дереве (с несжатыми путями), считая от корня</summary>
101+
/// <summary>Estimated height of the tree (i.e. maximum length of the path from the root to a node. Path compression is not taken into account)</summary>
93102
public int Rank;
94103
}
95104
}

Main/src/Collections/DisjointSetsT.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,48 @@
44
namespace CodeJam.Collections
55
{
66
/// <summary>
7-
/// Реализация классической структуры Disjoint sets
8-
/// Generic - версия, позволяющая хранить данные внутри
7+
/// Generic implementation of the Disjoint sets
98
/// </summary>
109
/// <remarks>
11-
/// http://en.wikipedia.org/wiki/Disjoint-set_data_structure
10+
/// See http://en.wikipedia.org/wiki/Disjoint-set_data_structure
1211
/// </remarks>
1312
public sealed class DisjointSets<T> : DisjointSetsBase<DisjointSets<T>.Node>
1413
{
15-
/// <summary>Создает пустой DS</summary>
14+
/// <summary>Creates an empty Disjoint sets</summary>
1615
public DisjointSets() { }
1716

18-
/// <summary>Создает DS со значениями из перечисления</summary>
19-
/// <param name="values">Перечисление значений для добавления</param>
17+
/// <summary>Creates a Disjoint sets with the passed values</summary>
18+
/// <param name="values">The values to store</param>
2019
public DisjointSets(IEnumerable<T> values)
2120
{
2221
Add(values);
2322
}
2423

25-
/// <summary>Получение значения элемента по индексу</summary>
26-
/// <param name="index">Индекс элемента</param>
24+
/// <summary>Gets an element by its index</summary>
25+
/// <param name="index">Elmement's index</param>
2726
public T this[int index] => Nodes_[index].Value;
2827

29-
/// <summary>Добавление перечисления элементов</summary>
30-
/// <param name="values">Элементы</param>
28+
/// <summary>Appends a list of values</summary>
29+
/// <param name="values">The values to append</param>
3130
public void Add(IEnumerable<T> values)
3231
{
3332
var initialNodesCount = Nodes_.Count;
3433
Nodes_.AddRange(values.Select(_ => new Node { Value = _, ParentIndex = -1, Rank = 0 }));
3534
SetsCount += Nodes_.Count - initialNodesCount;
3635
}
3736

38-
/// <summary>Добавление одного элемента</summary>
39-
/// <param name="value">Элемент</param>
37+
/// <summary>Appends a single element</summary>
38+
/// <param name="value">The value to append</param>
4039
public void Add(T value)
4140
{
4241
Nodes_.Add(new Node { Value = value, ParentIndex = -1, Rank = 0 });
4342
++SetsCount;
4443
}
4544

46-
/// <summary>Узел дерева</summary>
45+
/// <summary>A sets node</summary>
4746
public class Node : BasicNode
4847
{
49-
/// <summary>Собственно полезные данные</summary>
48+
/// <summary>The node data</summary>
5049
public T Value;
5150
}
5251
}

0 commit comments

Comments
 (0)