Skip to content

Commit fdfc765

Browse files
authored
Switch to file-scoped namespaces (#431)
1 parent 3d92c39 commit fdfc765

35 files changed

+2031
-2084
lines changed
+52-56
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,74 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
63

7-
namespace Algorithms.Other
4+
namespace Algorithms.Other;
5+
6+
/// <summary>
7+
/// Almost all real complex decision-making task is described by more than one criterion.
8+
/// There are different methods to select the best decisions from the defined set of decisions.
9+
/// This class contains implementations of the popular convolution methods: linear and maxmin.
10+
/// </summary>
11+
public static class DecisionsConvolutions
812
{
913
/// <summary>
10-
/// Almost all real complex decision-making task is described by more than one criterion.
11-
/// There are different methods to select the best decisions from the defined set of decisions.
12-
/// This class contains implementations of the popular convolution methods: linear and maxmin.
14+
/// This method implements the linear method of decision selection. It is based on
15+
/// the calculation of the "value" for each decision and the selection of the most
16+
/// valuable one.
1317
/// </summary>
14-
public static class DecisionsConvolutions
18+
/// <param name="matrix">Contains a collection of the criteria sets.</param>
19+
/// <param name="priorities">Contains a set of priorities for each criterion.</param>
20+
/// <returns>The most effective decision that is represented by a set of criterias.</returns>
21+
public static List<decimal> Linear(List<List<decimal>> matrix, List<decimal> priorities)
1522
{
16-
/// <summary>
17-
/// This method implements the linear method of decision selection. It is based on
18-
/// the calculation of the "value" for each decision and the selection of the most
19-
/// valuable one.
20-
/// </summary>
21-
/// <param name="matrix">Contains a collection of the criteria sets.</param>
22-
/// <param name="priorities">Contains a set of priorities for each criterion.</param>
23-
/// <returns>The most effective decision that is represented by a set of criterias.</returns>
24-
public static List<decimal> Linear(List<List<decimal>> matrix, List<decimal> priorities)
25-
{
26-
var decisionValues = new List<decimal>();
23+
var decisionValues = new List<decimal>();
2724

28-
foreach (var decision in matrix)
25+
foreach (var decision in matrix)
26+
{
27+
decimal sum = 0;
28+
for (int i = 0; i < decision.Count; i++)
2929
{
30-
decimal sum = 0;
31-
for (int i = 0; i < decision.Count; i++)
32-
{
33-
sum += decision[i] * priorities[i];
34-
}
35-
36-
decisionValues.Add(sum);
30+
sum += decision[i] * priorities[i];
3731
}
3832

39-
decimal bestDecisionValue = decisionValues.Max();
40-
int bestDecisionIndex = decisionValues.IndexOf(bestDecisionValue);
41-
42-
return matrix[bestDecisionIndex];
33+
decisionValues.Add(sum);
4334
}
4435

45-
/// <summary>
46-
/// This method implements maxmin method of the decision selection. It is based on
47-
/// the calculation of the least criteria value and comparison of decisions based
48-
/// on the calculated results.
49-
/// </summary>
50-
/// <param name="matrix">Contains a collection of the criteria sets.</param>
51-
/// <param name="priorities">Contains a set of priorities for each criterion.</param>
52-
/// <returns>The most effective decision that is represented by a set of criterias.</returns>
53-
public static List<decimal> MaxMin(List<List<decimal>> matrix, List<decimal> priorities)
54-
{
55-
var decisionValues = new List<decimal>();
36+
decimal bestDecisionValue = decisionValues.Max();
37+
int bestDecisionIndex = decisionValues.IndexOf(bestDecisionValue);
38+
39+
return matrix[bestDecisionIndex];
40+
}
5641

57-
foreach (var decision in matrix)
42+
/// <summary>
43+
/// This method implements maxmin method of the decision selection. It is based on
44+
/// the calculation of the least criteria value and comparison of decisions based
45+
/// on the calculated results.
46+
/// </summary>
47+
/// <param name="matrix">Contains a collection of the criteria sets.</param>
48+
/// <param name="priorities">Contains a set of priorities for each criterion.</param>
49+
/// <returns>The most effective decision that is represented by a set of criterias.</returns>
50+
public static List<decimal> MaxMin(List<List<decimal>> matrix, List<decimal> priorities)
51+
{
52+
var decisionValues = new List<decimal>();
53+
54+
foreach (var decision in matrix)
55+
{
56+
decimal minValue = decimal.MaxValue;
57+
for (int i = 0; i < decision.Count; i++)
5858
{
59-
decimal minValue = decimal.MaxValue;
60-
for (int i = 0; i < decision.Count; i++)
59+
decimal result = decision[i] * priorities[i];
60+
if (result < minValue)
6161
{
62-
decimal result = decision[i] * priorities[i];
63-
if (result < minValue)
64-
{
65-
minValue = result;
66-
}
62+
minValue = result;
6763
}
68-
69-
decisionValues.Add(minValue);
7064
}
7165

72-
decimal bestDecisionValue = decisionValues.Max();
73-
int bestDecisionIndex = decisionValues.IndexOf(bestDecisionValue);
74-
75-
return matrix[bestDecisionIndex];
66+
decisionValues.Add(minValue);
7667
}
68+
69+
decimal bestDecisionValue = decisionValues.Max();
70+
int bestDecisionIndex = decisionValues.IndexOf(bestDecisionValue);
71+
72+
return matrix[bestDecisionIndex];
7773
}
7874
}
+29-30
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
11
using System;
22
using System.Numerics;
33

4-
namespace Algorithms.Other
4+
namespace Algorithms.Other;
5+
6+
/// <summary>
7+
/// Fermat's prime tester https://en.wikipedia.org/wiki/Fermat_primality_test.
8+
/// </summary>
9+
public static class FermatPrimeChecker
510
{
611
/// <summary>
7-
/// Fermat's prime tester https://en.wikipedia.org/wiki/Fermat_primality_test.
12+
/// Checks if input number is a probable prime.
813
/// </summary>
9-
public static class FermatPrimeChecker
14+
/// <param name="numberToTest">Input number.</param>
15+
/// <param name="timesToCheck">Number of times to check.</param>
16+
/// <returns>True if is a prime; False otherwise.</returns>
17+
public static bool IsPrime(int numberToTest, int timesToCheck)
1018
{
11-
/// <summary>
12-
/// Checks if input number is a probable prime.
13-
/// </summary>
14-
/// <param name="numberToTest">Input number.</param>
15-
/// <param name="timesToCheck">Number of times to check.</param>
16-
/// <returns>True if is a prime; False otherwise.</returns>
17-
public static bool IsPrime(int numberToTest, int timesToCheck)
18-
{
19-
// You have to use BigInteger for two reasons:
20-
// 1. The pow operation between two int numbers usually overflows an int
21-
// 2. The pow and modular operation is very optimized
22-
var numberToTestBigInteger = new BigInteger(numberToTest);
23-
var exponentBigInteger = new BigInteger(numberToTest - 1);
19+
// You have to use BigInteger for two reasons:
20+
// 1. The pow operation between two int numbers usually overflows an int
21+
// 2. The pow and modular operation is very optimized
22+
var numberToTestBigInteger = new BigInteger(numberToTest);
23+
var exponentBigInteger = new BigInteger(numberToTest - 1);
2424

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

28-
var iterator = 1;
29-
var prime = true;
28+
var iterator = 1;
29+
var prime = true;
3030

31-
while (iterator < timesToCheck && prime)
31+
while (iterator < timesToCheck && prime)
32+
{
33+
var randomNumber = r.Next(1, numberToTest);
34+
var randomNumberBigInteger = new BigInteger(randomNumber);
35+
if (BigInteger.ModPow(randomNumberBigInteger, exponentBigInteger, numberToTestBigInteger) != 1)
3236
{
33-
var randomNumber = r.Next(1, numberToTest);
34-
var randomNumberBigInteger = new BigInteger(randomNumber);
35-
if (BigInteger.ModPow(randomNumberBigInteger, exponentBigInteger, numberToTestBigInteger) != 1)
36-
{
37-
prime = false;
38-
}
39-
40-
iterator++;
37+
prime = false;
4138
}
4239

43-
return prime;
40+
iterator++;
4441
}
42+
43+
return prime;
4544
}
4645
}

Algorithms/Other/FloodFill.cs

+66-68
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,95 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Drawing;
4-
using System.Numerics;
54

6-
namespace Algorithms.Other
5+
namespace Algorithms.Other;
6+
7+
/// <summary>
8+
/// Flood fill, also called seed fill, is an algorithm that determines and
9+
/// alters the area connected to a given node in a multi-dimensional array with
10+
/// some matching attribute. It is used in the "bucket" fill tool of paint
11+
/// programs to fill connected, similarly-colored areas with a different color.
12+
/// (description adapted from https://en.wikipedia.org/wiki/Flood_fill)
13+
/// (see also: https://www.techiedelight.com/flood-fill-algorithm/).
14+
/// </summary>
15+
public static class FloodFill
716
{
17+
private static readonly List<(int xOffset, int yOffset)> Neighbors = new() { (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1) };
18+
819
/// <summary>
9-
/// Flood fill, also called seed fill, is an algorithm that determines and
10-
/// alters the area connected to a given node in a multi-dimensional array with
11-
/// some matching attribute. It is used in the "bucket" fill tool of paint
12-
/// programs to fill connected, similarly-colored areas with a different color.
13-
/// (description adapted from https://en.wikipedia.org/wiki/Flood_fill)
14-
/// (see also: https://www.techiedelight.com/flood-fill-algorithm/).
20+
/// Implements the flood fill algorithm through a breadth-first approach using a queue.
1521
/// </summary>
16-
public static class FloodFill
22+
/// <param name="bitmap">The bitmap to which the algorithm is applied.</param>
23+
/// <param name="location">The start location on the bitmap.</param>
24+
/// <param name="targetColor">The old color to be replaced.</param>
25+
/// <param name="replacementColor">The new color to replace the old one.</param>
26+
public static void BreadthFirstSearch(Bitmap bitmap, (int x, int y) location, Color targetColor, Color replacementColor)
1727
{
18-
private static readonly List<(int xOffset, int yOffset)> Neighbors = new() { (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1) };
19-
20-
/// <summary>
21-
/// Implements the flood fill algorithm through a breadth-first approach using a queue.
22-
/// </summary>
23-
/// <param name="bitmap">The bitmap to which the algorithm is applied.</param>
24-
/// <param name="location">The start location on the bitmap.</param>
25-
/// <param name="targetColor">The old color to be replaced.</param>
26-
/// <param name="replacementColor">The new color to replace the old one.</param>
27-
public static void BreadthFirstSearch(Bitmap bitmap, (int x, int y) location, Color targetColor, Color replacementColor)
28+
if (location.x < 0 || location.x >= bitmap.Width || location.y < 0 || location.y >= bitmap.Height)
2829
{
29-
if (location.x < 0 || location.x >= bitmap.Width || location.y < 0 || location.y >= bitmap.Height)
30-
{
31-
throw new ArgumentOutOfRangeException(nameof(location), $"{nameof(location)} should point to a pixel within the bitmap");
32-
}
30+
throw new ArgumentOutOfRangeException(nameof(location), $"{nameof(location)} should point to a pixel within the bitmap");
31+
}
3332

34-
var queue = new List<(int x, int y)>();
35-
queue.Add(location);
33+
var queue = new List<(int x, int y)>();
34+
queue.Add(location);
3635

37-
while (queue.Count > 0)
38-
{
39-
BreadthFirstFill(bitmap, location, targetColor, replacementColor, queue);
40-
}
36+
while (queue.Count > 0)
37+
{
38+
BreadthFirstFill(bitmap, location, targetColor, replacementColor, queue);
4139
}
40+
}
4241

43-
/// <summary>
44-
/// Implements the flood fill algorithm through a depth-first approach through recursion.
45-
/// </summary>
46-
/// <param name="bitmap">The bitmap to which the algorithm is applied.</param>
47-
/// <param name="location">The start location on the bitmap.</param>
48-
/// <param name="targetColor">The old color to be replaced.</param>
49-
/// <param name="replacementColor">The new color to replace the old one.</param>
50-
public static void DepthFirstSearch(Bitmap bitmap, (int x, int y) location, Color targetColor, Color replacementColor)
42+
/// <summary>
43+
/// Implements the flood fill algorithm through a depth-first approach through recursion.
44+
/// </summary>
45+
/// <param name="bitmap">The bitmap to which the algorithm is applied.</param>
46+
/// <param name="location">The start location on the bitmap.</param>
47+
/// <param name="targetColor">The old color to be replaced.</param>
48+
/// <param name="replacementColor">The new color to replace the old one.</param>
49+
public static void DepthFirstSearch(Bitmap bitmap, (int x, int y) location, Color targetColor, Color replacementColor)
50+
{
51+
if (location.x < 0 || location.x >= bitmap.Width || location.y < 0 || location.y >= bitmap.Height)
5152
{
52-
if (location.x < 0 || location.x >= bitmap.Width || location.y < 0 || location.y >= bitmap.Height)
53-
{
54-
throw new ArgumentOutOfRangeException(nameof(location), $"{nameof(location)} should point to a pixel within the bitmap");
55-
}
56-
57-
DepthFirstFill(bitmap, location, targetColor, replacementColor);
53+
throw new ArgumentOutOfRangeException(nameof(location), $"{nameof(location)} should point to a pixel within the bitmap");
5854
}
5955

60-
private static void BreadthFirstFill(Bitmap bitmap, (int x, int y) location, Color targetColor, Color replacementColor, List<(int x, int y)> queue)
56+
DepthFirstFill(bitmap, location, targetColor, replacementColor);
57+
}
58+
59+
private static void BreadthFirstFill(Bitmap bitmap, (int x, int y) location, Color targetColor, Color replacementColor, List<(int x, int y)> queue)
60+
{
61+
(int x, int y) currentLocation = queue[0];
62+
queue.RemoveAt(0);
63+
64+
if (bitmap.GetPixel(currentLocation.x, currentLocation.y) == targetColor)
6165
{
62-
(int x, int y) currentLocation = queue[0];
63-
queue.RemoveAt(0);
66+
bitmap.SetPixel(currentLocation.x, currentLocation.y, replacementColor);
6467

65-
if (bitmap.GetPixel(currentLocation.x, currentLocation.y) == targetColor)
68+
for (int i = 0; i < Neighbors.Count; i++)
6669
{
67-
bitmap.SetPixel(currentLocation.x, currentLocation.y, replacementColor);
68-
69-
for (int i = 0; i < Neighbors.Count; i++)
70+
int x = currentLocation.x + Neighbors[i].xOffset;
71+
int y = currentLocation.y + Neighbors[i].yOffset;
72+
if (x >= 0 && x < bitmap.Width && y >= 0 && y < bitmap.Height)
7073
{
71-
int x = currentLocation.x + Neighbors[i].xOffset;
72-
int y = currentLocation.y + Neighbors[i].yOffset;
73-
if (x >= 0 && x < bitmap.Width && y >= 0 && y < bitmap.Height)
74-
{
75-
queue.Add((x, y));
76-
}
74+
queue.Add((x, y));
7775
}
7876
}
7977
}
78+
}
8079

81-
private static void DepthFirstFill(Bitmap bitmap, (int x, int y) location, Color targetColor, Color replacementColor)
80+
private static void DepthFirstFill(Bitmap bitmap, (int x, int y) location, Color targetColor, Color replacementColor)
81+
{
82+
if (bitmap.GetPixel(location.x, location.y) == targetColor)
8283
{
83-
if (bitmap.GetPixel(location.x, location.y) == targetColor)
84-
{
85-
bitmap.SetPixel(location.x, location.y, replacementColor);
84+
bitmap.SetPixel(location.x, location.y, replacementColor);
8685

87-
for (int i = 0; i < Neighbors.Count; i++)
86+
for (int i = 0; i < Neighbors.Count; i++)
87+
{
88+
int x = location.x + Neighbors[i].xOffset;
89+
int y = location.y + Neighbors[i].yOffset;
90+
if (x >= 0 && x < bitmap.Width && y >= 0 && y < bitmap.Height)
8891
{
89-
int x = location.x + Neighbors[i].xOffset;
90-
int y = location.y + Neighbors[i].yOffset;
91-
if (x >= 0 && x < bitmap.Width && y >= 0 && y < bitmap.Height)
92-
{
93-
DepthFirstFill(bitmap, (x, y), targetColor, replacementColor);
94-
}
92+
DepthFirstFill(bitmap, (x, y), targetColor, replacementColor);
9593
}
9694
}
9795
}

0 commit comments

Comments
 (0)