Skip to content

Commit 1b9aa7e

Browse files
Migrate to file-scoped namespaces and remove unused global usings (#521)
1 parent 00e9bc4 commit 1b9aa7e

20 files changed

+470
-525
lines changed

Algorithms/Numeric/AdditionWithoutArithmetic.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
using System;
2-
using System.Numerics;
3-
41
namespace Algorithms.Numeric;
52

63
/// <summary>

Algorithms/Numeric/KrishnamurthyNumberChecker.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System;
2-
31
namespace Algorithms.Numeric;
42

53
/// <summary>

Algorithms/Other/Geofence.cs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,30 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
namespace Algorithms.Other;
62

7-
namespace Algorithms.Other
3+
public class Geofence
84
{
9-
public class Geofence
10-
{
11-
public double Latitude { get; set; }
5+
public double Latitude { get; set; }
126

13-
public double Longitude { get; set; }
7+
public double Longitude { get; set; }
148

15-
public double RadiusInMeters { get; set; }
9+
public double RadiusInMeters { get; set; }
1610

17-
public Geofence(double latitude, double longitude, double radiusInMeters)
18-
{
19-
Latitude = latitude;
20-
Longitude = longitude;
21-
RadiusInMeters = radiusInMeters;
22-
}
11+
public Geofence(double latitude, double longitude, double radiusInMeters)
12+
{
13+
Latitude = latitude;
14+
Longitude = longitude;
15+
RadiusInMeters = radiusInMeters;
16+
}
2317

24-
/// <summary>
25-
/// Checks whether the provided user location (latitude and longitude) is within the geofence boundary.
26-
/// The geofence is defined by a center point (latitude, longitude) and a radius in meters.
27-
/// </summary>
28-
/// <param name="userLatitude">The latitude of the user's current location.</param>
29-
/// <param name="userLongitude">The longitude of the user's current location.</param>
30-
/// <returns>Returns true if the user is inside the geofence, otherwise returns false.</returns>
31-
public bool IsInside(double userLatitude, double userLongitude)
32-
{
33-
double distance = GeoLocation.CalculateDistanceFromLatLng(Latitude, Longitude, userLatitude, userLongitude);
34-
return distance <= RadiusInMeters;
35-
}
18+
/// <summary>
19+
/// Checks whether the provided user location (latitude and longitude) is within the geofence boundary.
20+
/// The geofence is defined by a center point (latitude, longitude) and a radius in meters.
21+
/// </summary>
22+
/// <param name="userLatitude">The latitude of the user's current location.</param>
23+
/// <param name="userLongitude">The longitude of the user's current location.</param>
24+
/// <returns>Returns true if the user is inside the geofence, otherwise returns false.</returns>
25+
public bool IsInside(double userLatitude, double userLongitude)
26+
{
27+
double distance = GeoLocation.CalculateDistanceFromLatLng(Latitude, Longitude, userLatitude, userLongitude);
28+
return distance <= RadiusInMeters;
3629
}
3730
}

Algorithms/Other/Geohash.cs

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,79 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
41
using System.Text;
5-
using System.Threading.Tasks;
62

7-
namespace Algorithms.Other
3+
namespace Algorithms.Other;
4+
5+
public static class Geohash
86
{
9-
public static class Geohash
7+
private const string Base32Characters = "0123456789bcdefghjkmnpqrstuvwxyz"; // Convert latitude and longitude coordinates into a concise string
8+
private const int GeohashLength = 12; // ± 1.86 cm
9+
10+
/// <summary>
11+
/// Encodes the provided latitude and longitude coordinates into a Geohash string.
12+
/// Geohashing is a method to encode geographic coordinates (latitude, longitude).
13+
/// into a short string of letters and digits. Each character in the resulting Geohash .
14+
/// string adds more precision to the location. The longer the Geohash, the smaller the area.
15+
/// </summary>
16+
/// <param name="latitude">The latitude of the location to encode. It must be a value between -90 and 90.</param>
17+
/// <param name="longitude">The longitude of the location to encode. It must be a value between -180 and 180.</param>
18+
/// <returns>
19+
/// A Geohash string of length 12 representing the location with high precision.
20+
/// A longer Geohash provides higher precision in terms of geographic area.
21+
/// and a 12-character Geohash can be accurate down to around 1.86 cm.
22+
/// </returns>
23+
public static string Encode(double latitude, double longitude)
1024
{
11-
private const string Base32Characters = "0123456789bcdefghjkmnpqrstuvwxyz"; // Convert latitude and longitude coordinates into a concise string
12-
private const int GeohashLength = 12; // ± 1.86 cm
25+
double[] latitudeRange = new[] { -90.0, 90.0 };
26+
double[] longitudeRange = new[] { -180.0, 180.0 };
27+
bool isEncodingLongitude = true;
28+
int currentBit = 0;
29+
int base32Index = 0;
30+
StringBuilder geohashResult = new StringBuilder();
1331

14-
/// <summary>
15-
/// Encodes the provided latitude and longitude coordinates into a Geohash string.
16-
/// Geohashing is a method to encode geographic coordinates (latitude, longitude).
17-
/// into a short string of letters and digits. Each character in the resulting Geohash .
18-
/// string adds more precision to the location. The longer the Geohash, the smaller the area.
19-
/// </summary>
20-
/// <param name="latitude">The latitude of the location to encode. It must be a value between -90 and 90.</param>
21-
/// <param name="longitude">The longitude of the location to encode. It must be a value between -180 and 180.</param>
22-
/// <returns>
23-
/// A Geohash string of length 12 representing the location with high precision.
24-
/// A longer Geohash provides higher precision in terms of geographic area.
25-
/// and a 12-character Geohash can be accurate down to around 1.86 cm.
26-
/// </returns>
27-
public static string Encode(double latitude, double longitude)
32+
while (geohashResult.Length < GeohashLength)
2833
{
29-
double[] latitudeRange = new[] { -90.0, 90.0 };
30-
double[] longitudeRange = new[] { -180.0, 180.0 };
31-
bool isEncodingLongitude = true;
32-
int currentBit = 0;
33-
int base32Index = 0;
34-
StringBuilder geohashResult = new StringBuilder();
34+
double midpoint;
3535

36-
while (geohashResult.Length < GeohashLength)
36+
if (isEncodingLongitude)
3737
{
38-
double midpoint;
39-
40-
if (isEncodingLongitude)
38+
midpoint = (longitudeRange[0] + longitudeRange[1]) / 2;
39+
if (longitude > midpoint)
4140
{
42-
midpoint = (longitudeRange[0] + longitudeRange[1]) / 2;
43-
if (longitude > midpoint)
44-
{
45-
base32Index |= 1 << (4 - currentBit);
46-
longitudeRange[0] = midpoint;
47-
}
48-
else
49-
{
50-
longitudeRange[1] = midpoint;
51-
}
41+
base32Index |= 1 << (4 - currentBit);
42+
longitudeRange[0] = midpoint;
5243
}
5344
else
5445
{
55-
midpoint = (latitudeRange[0] + latitudeRange[1]) / 2;
56-
if (latitude > midpoint)
57-
{
58-
base32Index |= 1 << (4 - currentBit);
59-
latitudeRange[0] = midpoint;
60-
}
61-
else
62-
{
63-
latitudeRange[1] = midpoint;
64-
}
46+
longitudeRange[1] = midpoint;
6547
}
66-
67-
isEncodingLongitude = !isEncodingLongitude;
68-
69-
if (currentBit < 4)
48+
}
49+
else
50+
{
51+
midpoint = (latitudeRange[0] + latitudeRange[1]) / 2;
52+
if (latitude > midpoint)
7053
{
71-
currentBit++;
54+
base32Index |= 1 << (4 - currentBit);
55+
latitudeRange[0] = midpoint;
7256
}
7357
else
7458
{
75-
geohashResult.Append(Base32Characters[base32Index]);
76-
currentBit = 0;
77-
base32Index = 0;
59+
latitudeRange[1] = midpoint;
7860
}
7961
}
8062

81-
return geohashResult.ToString();
63+
isEncodingLongitude = !isEncodingLongitude;
64+
65+
if (currentBit < 4)
66+
{
67+
currentBit++;
68+
}
69+
else
70+
{
71+
geohashResult.Append(Base32Characters[base32Index]);
72+
currentBit = 0;
73+
base32Index = 0;
74+
}
8275
}
76+
77+
return geohashResult.ToString();
8378
}
8479
}

Algorithms/Other/JulianEaster.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Globalization;
32

43
namespace Algorithms.Other;
54

Algorithms/Other/Triangulator.cs

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,51 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
63

7-
namespace Algorithms.Other
4+
namespace Algorithms.Other;
5+
6+
public class Triangulator
87
{
9-
public class Triangulator
8+
public (double Latitude, double Longitude) CalculatePosition(List<(double Latitude, double Longitude)> baseLocations, List<double> distances)
109
{
11-
public (double Latitude, double Longitude) CalculatePosition(List<(double Latitude, double Longitude)> baseLocations, List<double> distances)
10+
if (baseLocations.Count < 3 || distances.Count < 3)
1211
{
13-
if (baseLocations.Count < 3 || distances.Count < 3)
14-
{
15-
throw new ArgumentException("At least three points and corresponding distances are required.");
16-
}
17-
18-
// Get the coordinates of the three base stations
19-
double lat1 = baseLocations[0].Latitude;
20-
double lon1 = baseLocations[0].Longitude;
21-
double lat2 = baseLocations[1].Latitude;
22-
double lon2 = baseLocations[1].Longitude;
23-
double lat3 = baseLocations[2].Latitude;
24-
double lon3 = baseLocations[2].Longitude;
25-
26-
// Convert coordinates to radians
27-
lat1 = ToRadians(lat1);
28-
lon1 = ToRadians(lon1);
29-
lat2 = ToRadians(lat2);
30-
lon2 = ToRadians(lon2);
31-
lat3 = ToRadians(lat3);
32-
lon3 = ToRadians(lon3);
33-
34-
// Calculate the center point
35-
double centerLat = (lat1 + lat2 + lat3) / 3;
36-
double centerLon = (lon1 + lon2 + lon3) / 3;
37-
38-
// Convert back to degrees
39-
centerLat = ToDegrees(centerLat);
40-
centerLon = ToDegrees(centerLon);
41-
42-
return (centerLat, centerLon);
12+
throw new ArgumentException("At least three points and corresponding distances are required.");
4313
}
4414

45-
private double ToRadians(double degrees)
46-
{
47-
return degrees * Math.PI / 180;
48-
}
15+
// Get the coordinates of the three base stations
16+
double lat1 = baseLocations[0].Latitude;
17+
double lon1 = baseLocations[0].Longitude;
18+
double lat2 = baseLocations[1].Latitude;
19+
double lon2 = baseLocations[1].Longitude;
20+
double lat3 = baseLocations[2].Latitude;
21+
double lon3 = baseLocations[2].Longitude;
22+
23+
// Convert coordinates to radians
24+
lat1 = ToRadians(lat1);
25+
lon1 = ToRadians(lon1);
26+
lat2 = ToRadians(lat2);
27+
lon2 = ToRadians(lon2);
28+
lat3 = ToRadians(lat3);
29+
lon3 = ToRadians(lon3);
30+
31+
// Calculate the center point
32+
double centerLat = (lat1 + lat2 + lat3) / 3;
33+
double centerLon = (lon1 + lon2 + lon3) / 3;
34+
35+
// Convert back to degrees
36+
centerLat = ToDegrees(centerLat);
37+
centerLon = ToDegrees(centerLon);
38+
39+
return (centerLat, centerLon);
40+
}
4941

50-
private double ToDegrees(double radians)
51-
{
52-
return radians * 180 / Math.PI;
53-
}
42+
private double ToRadians(double degrees)
43+
{
44+
return degrees * Math.PI / 180;
45+
}
46+
47+
private double ToDegrees(double radians)
48+
{
49+
return radians * 180 / Math.PI;
5450
}
5551
}

Algorithms/RecommenderSystem/ISimilarityCalculator.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
21
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62

73
namespace Algorithms.RecommenderSystem
84
{

Algorithms/Shufflers/LINQShuffler.cs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
63

7-
namespace Algorithms.Shufflers
4+
namespace Algorithms.Shufflers;
5+
6+
/// <summary>
7+
/// LINQ Shuffle is a simple shuffling algorithm,
8+
/// where the elements within a collection are shuffled using
9+
/// LINQ queries and lambda expressions in C#.
10+
/// </summary>
11+
/// <typeparam name="T">Type array input.</typeparam>
12+
public class LinqShuffler<T>
813
{
914
/// <summary>
10-
/// LINQ Shuffle is a simple shuffling algorithm,
11-
/// where the elements within a collection are shuffled using
12-
/// LINQ queries and lambda expressions in C#.
15+
/// First, it will generate a random value for each element.
16+
/// Next, it will sort the elements based on these generated
17+
/// random numbers using OrderBy.
1318
/// </summary>
14-
/// <typeparam name="T">Type array input.</typeparam>
15-
public class LinqShuffler<T>
19+
/// <param name="array">Array to shuffle.</param>
20+
/// <param name="seed">Random generator seed. Used to repeat the shuffle.</param>
21+
public T[] Shuffle(T[] array, int? seed = null)
1622
{
17-
/// <summary>
18-
/// First, it will generate a random value for each element.
19-
/// Next, it will sort the elements based on these generated
20-
/// random numbers using OrderBy.
21-
/// </summary>
22-
/// <param name="array">Array to shuffle.</param>
23-
/// <param name="seed">Random generator seed. Used to repeat the shuffle.</param>
24-
public T[] Shuffle(T[] array, int? seed = null)
25-
{
26-
var random = seed is null ? new Random() : new Random(seed.Value);
27-
return array.OrderBy(x => random.Next()).ToArray();
28-
}
23+
var random = seed is null ? new Random() : new Random(seed.Value);
24+
return array.OrderBy(x => random.Next()).ToArray();
2925
}
3026
}

0 commit comments

Comments
 (0)