Skip to content

Commit e03b8c3

Browse files
committed
refact
1 parent f880b31 commit e03b8c3

6 files changed

Lines changed: 48 additions & 62 deletions

File tree

src/PolylineAlgorithm.Abstraction/AbstractPolylineDecoder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ public IEnumerable<TCoordinate> Decode(TPolyline polyline) {
7878

7979
ReadOnlyMemory<char> sequence = GetReadOnlyMemory(polyline);
8080

81-
if (sequence.Length < LibraryDefaults.Polyline.Block.Length.Min) {
81+
if (sequence.Length < Defaults.Polyline.Block.Length.Min) {
8282
logger
83-
.LogPolylineCannotBeShorterThanWarning(nameof(sequence), sequence.Length, LibraryDefaults.Polyline.Block.Length.Min);
83+
.LogPolylineCannotBeShorterThanWarning(nameof(sequence), sequence.Length, Defaults.Polyline.Block.Length.Min);
8484
logger.
8585
LogOperationFailedInfo(nameof(Decode));
8686

src/PolylineAlgorithm.Abstraction/AbstractPolylineEncoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static int GetRemainingBufferSize(int position, int length) {
161161
int GetBufferLength(int count) {
162162
Debug.Assert(count > 0, "Count must be greater than zero.");
163163

164-
int requestedBufferLength = count * LibraryDefaults.Polyline.Block.Length.Max;
164+
int requestedBufferLength = count * Defaults.Polyline.Block.Length.Max;
165165

166166
Debug.Assert(Options.MaxBufferLength > 0, "Max buffer length must be greater than zero.");
167167
Debug.Assert(requestedBufferLength > 0, "Requested buffer length must be greater than zero.");

src/PolylineAlgorithm.Abstraction/Internal/LibraryDefaults.cs renamed to src/PolylineAlgorithm.Abstraction/Internal/Defaults.cs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,129 +11,129 @@ namespace PolylineAlgorithm.Abstraction.Internal;
1111
/// Organizes defaults for algorithm parameters, polyline encoding, and geographic coordinates into nested static classes.
1212
/// </summary>
1313
[ExcludeFromCodeCoverage]
14-
internal static class LibraryDefaults {
14+
internal static class Defaults {
1515
/// <summary>
1616
/// Contains default values and constants specific to the polyline encoding algorithm.
1717
/// </summary>
18-
public static class Algorithm {
18+
internal static class Algorithm {
1919
/// <summary>
2020
/// The precision factor used to round coordinate values during polyline encoding.
2121
/// </summary>
22-
public const int Precision = 100_000;
22+
internal const int Precision = 100_000;
2323

2424
/// <summary>
2525
/// The number of bits to shift during polyline encoding.
2626
/// </summary>
27-
public const byte ShiftLength = 5;
27+
internal const byte ShiftLength = 5;
2828

2929
/// <summary>
3030
/// The ASCII value for the question mark character ('?').
3131
/// </summary>
32-
public const byte QuestionMark = 63;
32+
internal const byte QuestionMark = 63;
3333

3434
/// <summary>
3535
/// The ASCII value for the space character (' ').
3636
/// </summary>
37-
public const byte Space = 32;
37+
internal const byte Space = 32;
3838

3939
/// <summary>
4040
/// The ASCII value for the unit separator character.
4141
/// </summary>
42-
public const byte UnitSeparator = 31;
42+
internal const byte UnitSeparator = 31;
4343
}
4444

45-
public static class Coordinate {
45+
internal static class Coordinate {
4646
/// <summary>
4747
/// Provides constants representing latitude values, including the default, minimum, and maximum valid values.
4848
/// </summary>
49-
public static class Latitude {
49+
internal static class Latitude {
5050
/// <summary>
5151
/// The default value for latitude, representing the equator.
5252
/// </summary>
53-
public const double Default = 0.00000;
53+
internal const double Default = 0.00000;
5454
/// <summary>
5555
/// The minimum valid latitude value.
5656
/// </summary>
57-
public const double Min = -90.00000;
57+
internal const double Min = -90.00000;
5858
/// <summary>
5959
/// The maximum valid latitude value.
6060
/// </summary>
61-
public const double Max = 90.00000;
61+
internal const double Max = 90.00000;
6262

6363
/// <summary>
6464
/// Contains constants related to normalized latitude values.
6565
/// </summary>
66-
public static class Normalized {
66+
internal static class Normalized {
6767
/// <summary>
6868
/// The minimum normalized latitude value.
6969
/// </summary>
70-
public const int Min = (int)(Latitude.Min * Algorithm.Precision);
70+
internal const int Min = (int)(Latitude.Min * Algorithm.Precision);
7171
/// <summary>
7272
/// The maximum normalized latitude value.
7373
/// </summary>
74-
public const int Max = (int)(Latitude.Max * Algorithm.Precision);
74+
internal const int Max = (int)(Latitude.Max * Algorithm.Precision);
7575
}
7676
}
7777

7878
/// <summary>
7979
/// Provides constants representing longitude values, including the default, minimum, and maximum valid values.
8080
/// </summary>
81-
public static class Longitude {
81+
internal static class Longitude {
8282
/// <summary>
8383
/// The default value for longitude, representing the equator.
8484
/// </summary>
85-
public const double Default = 0.00000;
85+
internal const double Default = 0.00000;
8686
/// <summary>
8787
/// The minimum valid longitude value.
8888
/// </summary>
89-
public const double Min = -180.00000;
89+
internal const double Min = -180.00000;
9090
/// <summary>
9191
/// The maximum valid longitude value.
9292
/// </summary>
93-
public const double Max = 180.00000;
93+
internal const double Max = 180.00000;
9494

9595
/// <summary>
9696
/// Contains constants related to normalized longitude values.
9797
/// </summary>
98-
public static class Normalized {
98+
internal static class Normalized {
9999
/// <summary>
100100
/// The minimum normalized latitude value.
101101
/// </summary>
102-
public const int Min = (int)(Longitude.Min * Algorithm.Precision);
102+
internal const int Min = (int)(Longitude.Min * Algorithm.Precision);
103103
/// <summary>
104104
/// The maximum normalized latitude value.
105105
/// </summary>
106-
public const int Max = (int)(Longitude.Max * Algorithm.Precision);
106+
internal const int Max = (int)(Longitude.Max * Algorithm.Precision);
107107
}
108108
}
109109
}
110110

111111
/// <summary>
112112
/// Contains default values and constants related to polyline.
113113
/// </summary>
114-
public static class Polyline {
114+
internal static class Polyline {
115115
/// <summary>
116116
/// Contains constants related to the polyline blocks.
117117
/// </summary>
118-
public static class Block {
118+
internal static class Block {
119119
/// <summary>
120120
/// An array of delimiter byte values used in polyline encoding, derived by adding the ASCII value of the question mark ('?') to a range of integers.
121121
/// </summary>
122-
public static readonly byte[] Delimiters = [.. Enumerable.Range(0, 32).Select(n => (byte)(n + Algorithm.QuestionMark))];
122+
internal static readonly byte[] Delimiters = [.. Enumerable.Range(0, 32).Select(n => (byte)(n + Algorithm.QuestionMark))];
123123

124124
/// <summary>
125125
/// Contains constants related to the length of encoded coordinates in polyline encoding.
126126
/// </summary>
127-
public static class Length {
127+
internal static class Length {
128128
/// <summary>
129129
/// The minimum number of characters required to represent an encoded coordinate.
130130
/// </summary>
131-
public const int Min = 2;
131+
internal const int Min = 2;
132132

133133
/// <summary>
134134
/// The maximum number of characters allowed to represent an encoded coordinate.
135135
/// </summary>
136-
public const int Max = 12;
136+
internal const int Max = 12;
137137
}
138138
}
139139
}

src/PolylineAlgorithm.Abstraction/PolylineEncoding.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,20 @@ public static bool TryReadValue(ref int variance, ref ReadOnlyMemory<char> buffe
5353

5454
// Read characters from the buffer until a termination condition is met or the end of the buffer is reached.
5555
while (position < buffer.Length) {
56-
chunk = span[position++] - LibraryDefaults.Algorithm.QuestionMark;
57-
sum |= (chunk & LibraryDefaults.Algorithm.UnitSeparator) << shifter;
58-
shifter += LibraryDefaults.Algorithm.ShiftLength;
56+
chunk = span[position++] - Defaults.Algorithm.QuestionMark;
57+
sum |= (chunk & Defaults.Algorithm.UnitSeparator) << shifter;
58+
shifter += Defaults.Algorithm.ShiftLength;
5959

6060
// If the chunk is less than the space character, it indicates the end of the value.
61-
if (chunk < LibraryDefaults.Algorithm.Space) {
61+
if (chunk < Defaults.Algorithm.Space) {
6262
break;
6363
}
6464
}
6565

6666
variance += (sum & 1) == 1 ? ~(sum >> 1) : sum >> 1;
6767

6868
// If the end of the buffer was reached without reading a complete value, return false.
69-
return chunk < LibraryDefaults.Algorithm.Space;
69+
return chunk < Defaults.Algorithm.Space;
7070
}
7171

7272
/// <summary>
@@ -106,7 +106,7 @@ public static double Denormalize(int value, CoordinateValueType type) {
106106
return 0.0;
107107
}
108108

109-
return Math.Truncate((double)value) / LibraryDefaults.Algorithm.Precision;
109+
return Math.Truncate((double)value) / Defaults.Algorithm.Precision;
110110
}
111111

112112
/// <summary>
@@ -146,13 +146,13 @@ public static bool TryWriteValue(int variance, ref Span<char> buffer, ref int po
146146
}
147147

148148
// Write the value to the buffer in a way that encodes it using the specified algorithm.
149-
while (rem >= LibraryDefaults.Algorithm.Space) {
150-
buffer[position++] = (char)((LibraryDefaults.Algorithm.Space | rem & LibraryDefaults.Algorithm.UnitSeparator) + LibraryDefaults.Algorithm.QuestionMark);
151-
rem >>= LibraryDefaults.Algorithm.ShiftLength;
149+
while (rem >= Defaults.Algorithm.Space) {
150+
buffer[position++] = (char)((Defaults.Algorithm.Space | rem & Defaults.Algorithm.UnitSeparator) + Defaults.Algorithm.QuestionMark);
151+
rem >>= Defaults.Algorithm.ShiftLength;
152152
}
153153

154154
// Write the final character, which is less than the space character.
155-
buffer[position++] = (char)(rem + LibraryDefaults.Algorithm.QuestionMark);
155+
buffer[position++] = (char)(rem + Defaults.Algorithm.QuestionMark);
156156

157157
return true;
158158
}
@@ -200,7 +200,7 @@ public static int Normalize(double value, CoordinateValueType type) {
200200
return 0;
201201
}
202202

203-
return (int)Math.Round(value * LibraryDefaults.Algorithm.Precision);
203+
return (int)Math.Round(value * Defaults.Algorithm.Precision);
204204
}
205205

206206
/// <summary>
@@ -245,10 +245,10 @@ public static int Normalize(double value, CoordinateValueType type) {
245245
/// name="type"/>; otherwise, <see langword="false"/>.
246246
/// </returns>
247247
private static bool ValidateValue<T>(T value, CoordinateValueType type) => (type, value) switch {
248-
(CoordinateValueType.Latitude, int normalized) when normalized >= LibraryDefaults.Coordinate.Latitude.Normalized.Min && normalized <= LibraryDefaults.Coordinate.Latitude.Normalized.Max => true,
249-
(CoordinateValueType.Longitude, int normalized) when normalized >= LibraryDefaults.Coordinate.Longitude.Normalized.Min && normalized <= LibraryDefaults.Coordinate.Longitude.Normalized.Max => true,
250-
(CoordinateValueType.Latitude, double denormalized) when denormalized >= LibraryDefaults.Coordinate.Latitude.Min && denormalized <= LibraryDefaults.Coordinate.Latitude.Max => true,
251-
(CoordinateValueType.Longitude, double denormalized) when denormalized >= LibraryDefaults.Coordinate.Longitude.Min && denormalized <= LibraryDefaults.Coordinate.Longitude.Max => true,
248+
(CoordinateValueType.Latitude, int normalized) when normalized >= Defaults.Coordinate.Latitude.Normalized.Min && normalized <= Defaults.Coordinate.Latitude.Normalized.Max => true,
249+
(CoordinateValueType.Longitude, int normalized) when normalized >= Defaults.Coordinate.Longitude.Normalized.Min && normalized <= Defaults.Coordinate.Longitude.Normalized.Max => true,
250+
(CoordinateValueType.Latitude, double denormalized) when denormalized >= Defaults.Coordinate.Latitude.Min && denormalized <= Defaults.Coordinate.Latitude.Max => true,
251+
(CoordinateValueType.Longitude, double denormalized) when denormalized >= Defaults.Coordinate.Longitude.Min && denormalized <= Defaults.Coordinate.Longitude.Max => true,
252252
_ => false,
253253
};
254254
}

src/PolylineAlgorithm.Abstraction/PolylineEncodingOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public sealed class PolylineEncodingOptions {
2121
/// Gets the maximum buffer size for encoding operations.
2222
/// </summary>
2323
/// <remarks>
24-
/// The default buffer size is 64,000 bytes (64 KB). This can be adjusted based on the expected size of the polyline data.
24+
/// The default buffer size is 2,048 bytes (2 KB). This can be adjusted based on the expected size of the polyline data.
2525
/// </remarks>
26-
public int BufferSizeInBytes { get; internal set; } = 64_000;
26+
public int BufferSizeInBytes { get; internal set; } = 2_048;
2727

2828
/// <summary>
2929
/// Gets or sets the precision for encoding coordinates.

src/PolylineAlgorithm/Polyline.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,6 @@ namespace PolylineAlgorithm;
2121
public readonly struct Polyline : IEquatable<Polyline> {
2222
private readonly ReadOnlyMemory<char> _value;
2323

24-
///// <summary>
25-
///// Initializes a new instance of the <see cref="Polyline"/> struct with the specified character sequence.
26-
///// </summary>
27-
///// <param name="value">
28-
///// A read-only span of characters representing an encoded polyline.
29-
///// </param>
30-
//public Polyline(ReadOnlySpan<char> value) {
31-
// if (value.IsEmpty) {
32-
// _value = ReadOnlyMemory<char>.Empty;
33-
// } else {
34-
// _value = new ReadOnlyMemory<char>(value.ToArray());
35-
// }
36-
//}
37-
3824
/// <summary>
3925
/// Initializes a new, empty instance of the <see cref="Polyline"/> struct.
4026
/// </summary>

0 commit comments

Comments
 (0)