Skip to content

Commit 7be6d4e

Browse files
Modernize and refactor C# codebase to leverage latest C# features (#545)
1 parent c6c3aa2 commit 7be6d4e

File tree

5 files changed

+15
-13
lines changed

5 files changed

+15
-13
lines changed

Algorithms/DataCompression/HuffmanCompressor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ public class HuffmanCompressor(IComparisonSorter<HuffmanCompressor.ListNode> sor
2121
{
2222
if (string.IsNullOrEmpty(uncompressedText))
2323
{
24-
return (string.Empty, new Dictionary<string, string>());
24+
return (string.Empty, []);
2525
}
2626

2727
if (uncompressedText.Distinct().Count() == 1)
2828
{
2929
var dict = new Dictionary<string, string>
3030
{
31-
{ "1", uncompressedText[0].ToString() },
31+
["1"] = uncompressedText[0].ToString(),
3232
};
3333
return (new string('1', uncompressedText.Length), dict);
3434
}

Algorithms/Problems/DynamicProgramming/CoinChange/DynamicCoinChangeSolver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static int[] GenerateSingleCoinChanges(int coin, int[] coins)
2121
Array.Sort(coinsArrayCopy);
2222
Array.Reverse(coinsArrayCopy);
2323

24-
var list = new List<int>();
24+
List<int> list = [];
2525

2626
foreach (var item in coinsArrayCopy)
2727
{
@@ -50,7 +50,7 @@ public static int[] GenerateSingleCoinChanges(int coin, int[] coins)
5050
/// <returns>Change dictionary for all values [1,N], where N is the coin.</returns>
5151
public static Dictionary<int, int[]> GenerateChangesDictionary(int coin, int[] coins)
5252
{
53-
var dict = new Dictionary<int, int[]>();
53+
Dictionary<int, int[]> dict = [];
5454
var currentCoin = 1;
5555

5656
while (currentCoin <= coin)

Algorithms/Problems/TravelingSalesman/TravelingSalesmanSolver.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static (int[] Route, double Distance) SolveNearestNeighbor(double[,] dist
7878
}
7979

8080
var visited = new bool[n];
81-
var route = new List<int> { start };
81+
List<int> route = [start];
8282
visited[start] = true;
8383
double totalDistance = 0;
8484
int current = start;
@@ -120,16 +120,15 @@ private static IEnumerable<int[]> Permute(int[] arr)
120120
if (arr.Length == 1)
121121
{
122122
yield return arr;
123+
yield break;
123124
}
124-
else
125+
126+
for (int i = 0; i < arr.Length; i++)
125127
{
126-
for (int i = 0; i < arr.Length; i++)
128+
var rest = arr.Where((_, idx) => idx != i).ToArray();
129+
foreach (var perm in Permute(rest))
127130
{
128-
var rest = arr.Where((_, idx) => idx != i).ToArray();
129-
foreach (var perm in Permute(rest))
130-
{
131-
yield return new[] { arr[i] }.Concat(perm).ToArray();
132-
}
131+
yield return [arr[i], ..perm];
133132
}
134133
}
135134
}

DataStructures/BinarySearchTree/BinarySearchTree.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ private IList<TKey> GetKeysInOrder(BinarySearchTreeNode<TKey>? node)
316316
return [];
317317
}
318318

319+
// Use mutable list approach instead of spread operator for better performance
319320
var result = new List<TKey>();
320321
result.AddRange(GetKeysInOrder(node.Left));
321322
result.Add(node.Key);
@@ -335,6 +336,7 @@ private IList<TKey> GetKeysPreOrder(BinarySearchTreeNode<TKey>? node)
335336
return [];
336337
}
337338

339+
// Use mutable list approach instead of spread operator for better performance
338340
var result = new List<TKey>
339341
{
340342
node.Key,
@@ -356,6 +358,7 @@ private IList<TKey> GetKeysPostOrder(BinarySearchTreeNode<TKey>? node)
356358
return [];
357359
}
358360

361+
// Use mutable list approach instead of spread operator for better performance
359362
var result = new List<TKey>();
360363
result.AddRange(GetKeysPostOrder(node.Left));
361364
result.AddRange(GetKeysPostOrder(node.Right));

DataStructures/Heap/PairingHeap/PairingHeap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ private void Map(T newItem, PairingHeapNode<T> newNode)
223223
}
224224
else
225225
{
226-
mapping[newItem] = new List<PairingHeapNode<T>>(new[] { newNode });
226+
mapping[newItem] = [newNode];
227227
}
228228
}
229229

0 commit comments

Comments
 (0)