Skip to content

Commit e9202bc

Browse files
authored
Fix warnings in VS and index (#92)
* Fix warnings in VS * Fix redundant boolean literal
1 parent 2538d5b commit e9202bc

20 files changed

+101
-202
lines changed

Algorithms.Tests/Compressors/HuffmanCompressorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Algorithms.Compressors;
1+
using Algorithms.DataCompression;
22
using Algorithms.Sorters;
33
using NUnit.Framework;
44
using NUnit.Framework.Internal;

Algorithms.Tests/Compressors/ShannonFanoCompressorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Algorithms.Compressors;
1+
using Algorithms.DataCompression;
22
using Algorithms.Knapsack;
33
using NUnit.Framework;
44
using NUnit.Framework.Internal;

Algorithms.Tests/Compressors/TranslatorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Collections.Generic;
2-
using Algorithms.Compressors;
2+
using Algorithms.DataCompression;
33
using NUnit.Framework;
44

55
namespace Algorithms.Tests.Compressors

Algorithms.Tests/Sorters/BucketSortTests.cs renamed to Algorithms.Tests/Sorters/BucketSorterTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
namespace Algorithms.Tests.Sorters
77
{
8-
public class BucketSortTests
8+
public class BucketSorterTests
99
{
1010
[Test]
1111
public void ArraySorted([Random(0, 1000, 100, Distinct = true)]int n)
1212
{
1313
// Arrange
14-
var sorter = new BucketSort();
14+
var sorter = new BucketSorter();
1515
var intComparer = new IntComparer();
1616
var (correctArray, testArray) = RandomHelper.GetArrays(n);
1717

Algorithms/Compressors/HuffmanCompressor.cs renamed to Algorithms/DataCompression/HuffmanCompressor.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Linq;
44
using Algorithms.Sorters;
55

6-
namespace Algorithms.Compressors
6+
namespace Algorithms.DataCompression
77
{
88
/// <summary>
99
/// Greedy lossless compression algorithm.
@@ -53,6 +53,14 @@ public HuffmanCompressor(ISorter<ListNode> sorter, Translator translator)
5353
return (translator.Translate(uncompressedText, compressionKeys), decompressionKeys);
5454
}
5555

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+
5664
private (Dictionary<string, string> compressionKeys, Dictionary<string, string> decompressionKeys) GetKeys(ListNode tree)
5765
{
5866
var compressionKeys = new Dictionary<string, string>();
@@ -84,14 +92,6 @@ public HuffmanCompressor(ISorter<ListNode> sorter, Translator translator)
8492
return (compressionKeys, decompressionKeys);
8593
}
8694

87-
private static void AddMany(IDictionary<string, string> keys, IEnumerable<(string key, string value)> enumerable)
88-
{
89-
foreach (var (key, value) in enumerable)
90-
{
91-
keys.Add(key, value);
92-
}
93-
}
94-
9595
private ListNode GenerateHuffmanTree(ListNode[] nodes)
9696
{
9797
var comparer = new ListNodeComparer();
@@ -137,16 +137,6 @@ private ListNode[] GetListNodesFromText(string text)
137137
/// </summary>
138138
public class ListNode
139139
{
140-
public char Data { get; }
141-
142-
public bool HasData { get; }
143-
144-
public double Frequency { get; }
145-
146-
public ListNode RightChild { get; }
147-
148-
public ListNode LeftChild { get; }
149-
150140
public ListNode(char data, double frequency)
151141
{
152142
HasData = true;
@@ -160,6 +150,16 @@ public ListNode(ListNode leftChild, ListNode rightChild)
160150
RightChild = rightChild;
161151
Frequency = leftChild.Frequency + rightChild.Frequency;
162152
}
153+
154+
public char Data { get; }
155+
156+
public bool HasData { get; }
157+
158+
public double Frequency { get; }
159+
160+
public ListNode RightChild { get; }
161+
162+
public ListNode LeftChild { get; }
163163
}
164164

165165
private class ListNodeComparer : IComparer<ListNode>

Algorithms/Compressors/ShannonFano.cs renamed to Algorithms/DataCompression/ShannonFanoCompressor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Linq;
33
using Algorithms.Knapsack;
44

5-
namespace Algorithms.Compressors
5+
namespace Algorithms.DataCompression
66
{
77
/// <summary>
88
/// Greedy lossless compression algorithm.
@@ -22,7 +22,7 @@ public ShannonFanoCompressor(IHeuristicKnapsackSolver<(char symbol, double frequ
2222
/// Given an input string, returns a new compressed string
2323
/// using Shannon-Fano enconding.
2424
/// </summary>
25-
/// <param name="inputText">Text message to compress.</param>
25+
/// <param name="uncompressedText">Text message to compress.</param>
2626
/// <returns>Compressed string and keys to decompress it.</returns>
2727
public (string compressedText, Dictionary<string, string> decompressionKeys) Compress(string uncompressedText)
2828
{
@@ -126,13 +126,13 @@ private ListNode GetListNodeFromText(string text)
126126
/// </summary>
127127
public class ListNode
128128
{
129+
public ListNode((char symbol, double frequency)[] data) => Data = data;
130+
129131
public (char symbol, double frequency)[] Data { get; }
130132

131133
public ListNode RightChild { get; set; }
132134

133135
public ListNode LeftChild { get; set; }
134-
135-
public ListNode((char symbol, double frequency)[] data) => Data = data;
136136
}
137137
}
138138
}

Algorithms/Compressors/Translator.cs renamed to Algorithms/DataCompression/Translator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections.Generic;
22
using System.Text;
33

4-
namespace Algorithms.Compressors
4+
namespace Algorithms.DataCompression
55
{
66
public class Translator
77
{

Algorithms/Encoders/HillEncoder.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,6 @@ public string Decode(string text, double[,] key)
6464
/// <returns>Message.</returns>
6565
private static string BuildStringFromArray(double[] arr) => new string(arr.Select(c => (char)c).ToArray());
6666

67-
/// <summary>
68-
/// Given a list of vectors, returns a single array of elements.
69-
/// </summary>
70-
/// <param name="list">List of ciphered arrays.</param>
71-
/// <returns>unidimensional list.</returns>
72-
private double[] MergeArrayList(double[][] list)
73-
{
74-
var merged = new double[list.Length * 3];
75-
76-
for (var i = 0; i < list.Length; i++)
77-
{
78-
Array.Copy(list[i], 0, merged, i * 3, list[0].Length);
79-
}
80-
81-
return merged;
82-
}
83-
8467
/// <summary>
8568
/// Multiplies the key for the given scalar.
8669
/// </summary>
@@ -102,6 +85,23 @@ private static double[] MatrixCipher(double[] vector, double[,] key)
10285
return multiplied;
10386
}
10487

88+
/// <summary>
89+
/// Given a list of vectors, returns a single array of elements.
90+
/// </summary>
91+
/// <param name="list">List of ciphered arrays.</param>
92+
/// <returns>unidimensional list.</returns>
93+
private double[] MergeArrayList(double[][] list)
94+
{
95+
var merged = new double[list.Length * 3];
96+
97+
for (var i = 0; i < list.Length; i++)
98+
{
99+
Array.Copy(list[i], 0, merged, i * 3, list[0].Length);
100+
}
101+
102+
return merged;
103+
}
104+
105105
/// <summary>
106106
/// Splits the input text message as chunks of words.
107107
/// </summary>

Algorithms/Numeric/GaussJordanElimination.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,7 @@ public bool Solve(double[,] matrix)
3838
/// <param name="matrix">Multidimensional array matrix.</param>
3939
/// <returns>True: if algorithm can be use for given matrix;
4040
/// False: Otherwise. </returns>
41-
private bool CanMatrixBeUsed(double[,] matrix)
42-
{
43-
if (matrix == null || matrix.Length != RowCount * (RowCount + 1))
44-
{
45-
return false;
46-
}
47-
48-
return RowCount > 1;
49-
}
41+
private bool CanMatrixBeUsed(double[,] matrix) => matrix?.Length == RowCount * (RowCount + 1) && RowCount > 1;
5042

5143
/// <summary>
5244
/// To prepare given matrix by pivoting rows.

Algorithms/Other/fermat_prime_checker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ private static void Main()
2525
var exponentBigInteger = new BigInteger(numberToTest - 1);
2626

2727
// Create a random number generator using the current time as seed
28-
var r = new Random(new DateTime().Millisecond);
28+
var r = new Random(default(DateTime).Millisecond);
2929

3030
var iterator = 1;
3131
var prime = true;

Algorithms/Search/AStar/Location.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ namespace AStar
44
{
55
public class Location : IEquatable<Location>
66
{
7+
// Constructors
8+
public Location(int a, int b, Location p)
9+
{
10+
X = a;
11+
Y = b;
12+
Parent = p;
13+
14+
G = p == null ? 0 : p.G + 1;
15+
16+
H = Program.ComputeHScore(X, Y);
17+
F = G + H;
18+
}
19+
720
// Properties
821
public int F
922
{
@@ -38,19 +51,6 @@ public int Y
3851
get;
3952
}
4053

41-
// Constructors
42-
public Location(int a, int b, Location p)
43-
{
44-
X = a;
45-
Y = b;
46-
Parent = p;
47-
48-
G = p == null ? 0 : p.G + 1;
49-
50-
H = Program.ComputeHScore(X, Y);
51-
F = G + H;
52-
}
53-
5454
// Methods
5555
public bool Equals(Location other) => X == other.X && Y == other.Y;
5656
}

Algorithms/Sorters/BinaryInsertionSorter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public void Sort(T[] array, IComparer<T> comparer)
3030

3131
/// <summary>Implementation of Binary Search using an iterative approach.</summary>
3232
/// <param name="array">An array of values sorted in ascending order between the index values left and right to search through.</param>
33-
/// <param name="left">Left index to search from (inclusive).</param>
34-
/// <param name="right">Right index to search to (inclusive).</param>
33+
/// <param name="from">Left index to search from (inclusive).</param>
34+
/// <param name="to">Right index to search to (inclusive).</param>
3535
/// <param name="target">The value to find placefor in the provided array.</param>
3636
/// <returns>The index where to insert target value.</returns>
3737
private static int BinarySearch(T[] array, int from, int to, T target, IComparer<T> comparer)

Algorithms/Sorters/BucketSort.cs renamed to Algorithms/Sorters/BucketSorter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Algorithms.Sorters
77
/// <summary>
88
/// Class that implements the BuckerSort Algorithm.
99
/// </summary>
10-
public class BucketSort : ISorter<int>
10+
public class BucketSorter : ISorter<int>
1111
{
1212
private const int NumOfDigitsInBase10 = 10;
1313

Algorithms/Sorters/ISorter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33
namespace Algorithms.Sorters
44
{
5+
/// <summary>
6+
/// Sorts array in ascending order.
7+
/// </summary>
8+
/// <typeparam name="T">Type of array item.</typeparam>
59
public interface ISorter<T>
610
{
11+
/// <summary>
12+
/// Sorts array in ascending order.
13+
/// </summary>
14+
/// <param name="array">Array to sort.</param>
15+
/// <param name="comparer">Comparer to compare items of <paramref name="array"/>.</param>
716
void Sort(T[] array, IComparer<T> comparer);
817
}
918
}

Algorithms/Sorters/cocktail_sort.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,7 @@
1-
using System;
2-
3-
namespace cocktail_sort
1+
namespace cocktail_sort
42
{
53
internal class Program
64
{
7-
private static void Main()
8-
{
9-
Console.WriteLine("Please enter some integers, separated by spaces:");
10-
11-
var input = Console.ReadLine();
12-
var integers = input.Split(' ');
13-
var data = new int[integers.Length];
14-
15-
for (var i = 0; i < data.Length; i++)
16-
{
17-
data[i] = int.Parse(integers[i]);
18-
}
19-
20-
Console.WriteLine("Unsorted: {0}", string.Join(" ", data));
21-
22-
CocktailSort(data);
23-
24-
Console.WriteLine("Sorted: {0}", string.Join(" ", data));
25-
26-
_ = Console.ReadKey();
27-
}
28-
295
public static void CocktailSort(int[] intArray)
306
{
317
var swapped = true;

Algorithms/Sorters/cycle_sort.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,7 @@
1-
using System;
2-
31
namespace cycle_sort
42
{
53
internal class Program
64
{
7-
private static void Main()
8-
{
9-
Console.WriteLine("Please enter some integers, separated by spaces:");
10-
var input = Console.ReadLine();
11-
var integers = input.Split(' ');
12-
var data = new int[integers.Length];
13-
for (var i = 0; i < data.Length; i++)
14-
{
15-
data[i] = int.Parse(integers[i]);
16-
}
17-
18-
Console.Write("\nUnsorted: ");
19-
for (var i = 0; i < data.Length; i++)
20-
{
21-
Console.Write(data[i] + " ");
22-
}
23-
24-
CycleSort(data);
25-
Console.Write("\nSorted: ");
26-
for (var i = 0; i < data.Length; i++)
27-
{
28-
Console.Write(data[i] + " ");
29-
}
30-
31-
_ = Console.ReadKey();
32-
}
33-
345
public static void CycleSort(int[] data)
356
{
367
for (var cycleStart = 0; cycleStart <= data.Length - 2; cycleStart++)

0 commit comments

Comments
 (0)