Skip to content

Commit b4b084d

Browse files
siddharthkocharsiriak
authored andcommitted
Refactoring (#124)
* Add new Utility project * Move AddMany to Utility project as an extension method * Use string.IsNullOrEmpty for instead of == * Move ItemNotFoundException to the Utility project
1 parent dbe980e commit b4b084d

File tree

9 files changed

+47
-27
lines changed

9 files changed

+47
-27
lines changed

Algorithms.Tests/Search/LinearSearcherTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Algorithms.Search;
44
using NUnit.Framework;
55
using NUnit.Framework.Internal;
6+
using Utility.Exception;
67

78
namespace Algorithms.Tests.Search
89
{

Algorithms/Algorithms.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@
2323
</PackageReference>
2424
</ItemGroup>
2525

26+
<ItemGroup>
27+
<ProjectReference Include="..\Utility\Utility.csproj" />
28+
</ItemGroup>
29+
2630
</Project>

Algorithms/DataCompression/HuffmanCompressor.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using Algorithms.Sorters.Comparison;
5+
using Utility.Extension;
56

67
namespace Algorithms.DataCompression
78
{
@@ -33,7 +34,7 @@ public HuffmanCompressor(IComparisonSorter<ListNode> sorter, Translator translat
3334
/// <returns>Compressed string and keys to decompress it.</returns>
3435
public (string compressedText, Dictionary<string, string> decompressionKeys) Compress(string uncompressedText)
3536
{
36-
if (uncompressedText == string.Empty)
37+
if (string.IsNullOrEmpty(uncompressedText))
3738
{
3839
return (string.Empty, new Dictionary<string, string>());
3940
}
@@ -53,14 +54,6 @@ public HuffmanCompressor(IComparisonSorter<ListNode> sorter, Translator translat
5354
return (translator.Translate(uncompressedText, compressionKeys), decompressionKeys);
5455
}
5556

56-
private static void AddMany(IDictionary<string, string> keys, IEnumerable<(string key, string value)> enumerable)
57-
{
58-
foreach (var (key, value) in enumerable)
59-
{
60-
keys.Add(key, value);
61-
}
62-
}
63-
6457
/// <summary>
6558
/// Finds frequency for each character in the text.
6659
/// </summary>
@@ -97,15 +90,15 @@ private static ListNode[] GetListNodesFromText(string text)
9790
if (tree.LeftChild != null)
9891
{
9992
var (lsck, lsdk) = GetKeys(tree.LeftChild);
100-
AddMany(compressionKeys, lsck.Select(kvp => (kvp.Key, "0" + kvp.Value)));
101-
AddMany(decompressionKeys, lsdk.Select(kvp => ("0" + kvp.Key, kvp.Value)));
93+
compressionKeys.AddMany(lsck.Select(kvp => (kvp.Key, "0" + kvp.Value)));
94+
decompressionKeys.AddMany(lsdk.Select(kvp => ("0" + kvp.Key, kvp.Value)));
10295
}
10396

10497
if (tree.RightChild != null)
10598
{
10699
var (rsck, rsdk) = GetKeys(tree.RightChild);
107-
AddMany(compressionKeys, rsck.Select(kvp => (kvp.Key, "1" + kvp.Value)));
108-
AddMany(decompressionKeys, rsdk.Select(kvp => ("1" + kvp.Key, kvp.Value)));
100+
compressionKeys.AddMany(rsck.Select(kvp => (kvp.Key, "1" + kvp.Value)));
101+
decompressionKeys.AddMany(rsdk.Select(kvp => ("1" + kvp.Key, kvp.Value)));
109102

110103
return (compressionKeys, decompressionKeys);
111104
}

Algorithms/DataCompression/ShannonFanoCompressor.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using Algorithms.Knapsack;
4+
using Utility.Extension;
45

56
namespace Algorithms.DataCompression
67
{
@@ -32,7 +33,7 @@ public ShannonFanoCompressor(IHeuristicKnapsackSolver<(char symbol, double frequ
3233
/// <returns>Compressed string and keys to decompress it.</returns>
3334
public (string compressedText, Dictionary<string, string> decompressionKeys) Compress(string uncompressedText)
3435
{
35-
if (uncompressedText == string.Empty)
36+
if (string.IsNullOrEmpty(uncompressedText))
3637
{
3738
return (string.Empty, new Dictionary<string, string>());
3839
}
@@ -67,28 +68,20 @@ public ShannonFanoCompressor(IHeuristicKnapsackSolver<(char symbol, double frequ
6768
if (tree.LeftChild != null)
6869
{
6970
var (lsck, lsdk) = GetKeys(tree.LeftChild);
70-
AddMany(compressionKeys, lsck.Select(kvp => (kvp.Key, "0" + kvp.Value)));
71-
AddMany(decompressionKeys, lsdk.Select(kvp => ("0" + kvp.Key, kvp.Value)));
71+
compressionKeys.AddMany(lsck.Select(kvp => (kvp.Key, "0" + kvp.Value)));
72+
decompressionKeys.AddMany(lsdk.Select(kvp => ("0" + kvp.Key, kvp.Value)));
7273
}
7374

7475
if (tree.RightChild != null)
7576
{
7677
var (rsck, rsdk) = GetKeys(tree.RightChild);
77-
AddMany(compressionKeys, rsck.Select(kvp => (kvp.Key, "1" + kvp.Value)));
78-
AddMany(decompressionKeys, rsdk.Select(kvp => ("1" + kvp.Key, kvp.Value)));
78+
compressionKeys.AddMany(rsck.Select(kvp => (kvp.Key, "1" + kvp.Value)));
79+
decompressionKeys.AddMany(rsdk.Select(kvp => ("1" + kvp.Key, kvp.Value)));
7980
}
8081

8182
return (compressionKeys, decompressionKeys);
8283
}
8384

84-
private void AddMany(Dictionary<string, string> keys, IEnumerable<(string key, string value)> enumerable)
85-
{
86-
foreach (var (key, value) in enumerable)
87-
{
88-
keys.Add(key, value);
89-
}
90-
}
91-
9285
private ListNode GenerateShannonFanoTree(ListNode node)
9386
{
9487
if (node.Data.Length == 1)

Algorithms/Search/LinearSearcher.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Utility.Exception;
23

34
namespace Algorithms.Search
45
{

C-Sharp.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionIt
2121
stylecop.ruleset = stylecop.ruleset
2222
EndProjectSection
2323
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utility", "Utility\Utility.csproj", "{3A41157D-296D-4BFC-A34E-91B5ED7F0905}"
25+
EndProject
2426
Global
2527
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2628
Debug|Any CPU = Debug|Any CPU
@@ -43,6 +45,10 @@ Global
4345
{39174100-3A6E-45B2-9AA9-7C69764C0750}.Debug|Any CPU.Build.0 = Debug|Any CPU
4446
{39174100-3A6E-45B2-9AA9-7C69764C0750}.Release|Any CPU.ActiveCfg = Release|Any CPU
4547
{39174100-3A6E-45B2-9AA9-7C69764C0750}.Release|Any CPU.Build.0 = Release|Any CPU
48+
{3A41157D-296D-4BFC-A34E-91B5ED7F0905}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
49+
{3A41157D-296D-4BFC-A34E-91B5ED7F0905}.Debug|Any CPU.Build.0 = Debug|Any CPU
50+
{3A41157D-296D-4BFC-A34E-91B5ED7F0905}.Release|Any CPU.ActiveCfg = Release|Any CPU
51+
{3A41157D-296D-4BFC-A34E-91B5ED7F0905}.Release|Any CPU.Build.0 = Release|Any CPU
4652
EndGlobalSection
4753
GlobalSection(SolutionProperties) = preSolution
4854
HideSolutionNode = FALSE

Algorithms/Search/ItemNotFoundException.cs renamed to Utility/Exception/ItemNotFoundException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace Algorithms.Search
3+
namespace Utility.Exception
44
{
55
/// <summary>
66
/// Signs that sequence doesn't contain any items that one was looking for.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
3+
namespace Utility.Extension
4+
{
5+
public static class DictionaryExtension
6+
{
7+
public static void AddMany<TKey, TValue>(this Dictionary<TKey, TValue> keys, IEnumerable<(TKey, TValue)> enumerable)
8+
{
9+
foreach (var (key, value) in enumerable)
10+
{
11+
keys.Add(key, value);
12+
}
13+
}
14+
}
15+
}

Utility/Utility.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>

0 commit comments

Comments
 (0)