Skip to content

Commit 100b4e7

Browse files
committed
Updated code documentation
1 parent 8e829ab commit 100b4e7

16 files changed

+369
-107
lines changed

src/PolylineAlgorithm/Abstraction/IPolylineDecoder.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ namespace PolylineAlgorithm.Abstraction;
88
using System.Collections.Generic;
99

1010
/// <summary>
11-
/// Defines a method to decode an encoded polyline into a set of coordinates.
11+
/// Defines a contract for decoding an encoded polyline into a collection of geographic coordinates.
1212
/// </summary>
1313
public interface IPolylineDecoder {
1414
/// <summary>
15-
/// Converts an encoded polyline to a set of coordinates.
15+
/// Decodes an encoded polyline string into a collection of geographic coordinates.
1616
/// </summary>
17-
/// <param name="polyline">An encoded polyline to decode.</param>
18-
/// <returns>A set of coordinates represented by the encoded polyline.</returns>
17+
/// <param name="polyline">The <see cref="Polyline"/> instance representing the encoded polyline string.</param>
18+
/// <returns>
19+
/// An <see cref="IEnumerable{T}"/> of <see cref="Coordinate"/> objects representing the decoded geographic coordinates.
20+
/// </returns>
1921
IEnumerable<Coordinate> Decode(Polyline polyline);
20-
}
22+
}

src/PolylineAlgorithm/Abstraction/IPolylineEncoder.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ namespace PolylineAlgorithm.Abstraction;
88
using System.Collections.Generic;
99

1010
/// <summary>
11-
/// Defines a method to encode a set of coordinates into an encoded polyline.
11+
/// Defines a contract for encoding a collection of geographic coordinates into an encoded polyline string.
1212
/// </summary>
1313
public interface IPolylineEncoder {
1414
/// <summary>
15-
/// Converts a set of coordinates to an encoded polyline.
15+
/// Encodes a collection of geographic coordinates into an encoded polyline string.
1616
/// </summary>
17-
/// <param name="coordinates">A set of coordinates to encode.</param>
18-
/// <returns>An encoded polyline representing the set of coordinates.</returns>
17+
/// <param name="coordinates">A collection of <see cref="Coordinate"/> objects to encode.</param>
18+
/// <returns>
19+
/// A <see cref="Polyline"/> instance representing the encoded polyline string.
20+
/// </returns>
1921
Polyline Encode(IEnumerable<Coordinate> coordinates);
20-
}
22+
}

src/PolylineAlgorithm/Extensions/PolylineDecoderExtensions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,21 @@
44
using System;
55
using System.Collections.Generic;
66

7+
/// <summary>
8+
/// Provides extension methods for the <see cref="IPolylineDecoder"/> interface to simplify decoding operations.
9+
/// </summary>
710
public static class PolylineDecoderExtensions {
11+
/// <summary>
12+
/// Decodes an encoded polyline string into a collection of geographic coordinates.
13+
/// </summary>
14+
/// <param name="decoder">The <see cref="IPolylineDecoder"/> instance used to decode the polyline.</param>
15+
/// <param name="polyline">The encoded polyline string to decode.</param>
16+
/// <returns>
17+
/// An <see cref="IEnumerable{T}"/> of <see cref="Coordinate"/> objects representing the decoded geographic coordinates.
18+
/// </returns>
19+
/// <exception cref="ArgumentNullException">
20+
/// Thrown if the <paramref name="decoder"/> is <see langword="null"/>.
21+
/// </exception>
822
public static IEnumerable<Coordinate> Decode(this IPolylineDecoder decoder, string polyline) {
923
if (decoder is null) {
1024
throw new ArgumentNullException(nameof(decoder));
@@ -13,6 +27,17 @@ public static IEnumerable<Coordinate> Decode(this IPolylineDecoder decoder, stri
1327
return decoder.Decode(Polyline.FromString(polyline));
1428
}
1529

30+
/// <summary>
31+
/// Decodes an encoded polyline represented as a character array into a collection of geographic coordinates.
32+
/// </summary>
33+
/// <param name="decoder">The <see cref="IPolylineDecoder"/> instance used to decode the polyline.</param>
34+
/// <param name="polyline">The encoded polyline represented as a character array to decode.</param>
35+
/// <returns>
36+
/// An <see cref="IEnumerable{T}"/> of <see cref="Coordinate"/> objects representing the decoded geographic coordinates.
37+
/// </returns>
38+
/// <exception cref="ArgumentNullException">
39+
/// Thrown if the <paramref name="decoder"/> is <see langword="null"/>.
40+
/// </exception>
1641
public static IEnumerable<Coordinate> Decode(this IPolylineDecoder decoder, char[] polyline) {
1742
if (decoder is null) {
1843
throw new ArgumentNullException(nameof(decoder));

src/PolylineAlgorithm/Extensions/PolylineEncoderExtensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,22 @@
44
using System;
55
using System.Collections.Generic;
66

7+
/// <summary>
8+
/// Provides extension methods for the <see cref="IPolylineEncoder"/> interface to simplify encoding operations.
9+
/// </summary>
710
public static class PolylineEncoderExtensions {
11+
/// <summary>
12+
/// Encodes a collection of geographic coordinates into an encoded polyline string.
13+
/// </summary>
14+
/// <typeparam name="Coordinate">The type representing a geographic coordinate.</typeparam>
15+
/// <param name="encoder">The <see cref="IPolylineEncoder"/> instance used to encode the coordinates.</param>
16+
/// <param name="coordinates">A collection of coordinates to encode.</param>
17+
/// <returns>
18+
/// A <see cref="Polyline"/> instance representing the encoded polyline string.
19+
/// </returns>
20+
/// <exception cref="ArgumentNullException">
21+
/// Thrown if the <paramref name="encoder"/> is <see langword="null"/>.
22+
/// </exception>
823
public static Polyline Encode<Coordinate>(this IPolylineEncoder encoder, ICollection<Coordinate> coordinates) {
924
if (encoder is null) {
1025
throw new ArgumentNullException(nameof(encoder));
@@ -13,6 +28,17 @@ public static Polyline Encode<Coordinate>(this IPolylineEncoder encoder, ICollec
1328
return encoder.Encode(coordinates);
1429
}
1530

31+
/// <summary>
32+
/// Encodes an array of geographic coordinates into an encoded polyline string.
33+
/// </summary>
34+
/// <param name="encoder">The <see cref="IPolylineEncoder"/> instance used to encode the coordinates.</param>
35+
/// <param name="coordinates">An array of coordinates to encode.</param>
36+
/// <returns>
37+
/// A <see cref="Polyline"/> instance representing the encoded polyline string.
38+
/// </returns>
39+
/// <exception cref="ArgumentNullException">
40+
/// Thrown if the <paramref name="encoder"/> is <see langword="null"/>.
41+
/// </exception>
1642
public static Polyline Encode(this IPolylineEncoder encoder, Coordinate[] coordinates) {
1743
if (encoder is null) {
1844
throw new ArgumentNullException(nameof(encoder));

src/PolylineAlgorithm/Internal/CoordinateVariance.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,40 @@
55
using System.Runtime.CompilerServices;
66
using System.Runtime.InteropServices;
77

8+
/// <summary>
9+
/// Represents the variance between consecutive geographic coordinates (latitude and longitude).
10+
/// This struct is used to calculate and store the differences between coordinate values.
11+
/// </summary>
812
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
913
[StructLayout(LayoutKind.Auto)]
1014
internal struct CoordinateVariance {
1115
private (int Latitude, int Longitude) _current = (0, 0);
1216

17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="CoordinateVariance"/> struct with the specified latitude and longitude values.
19+
/// </summary>
20+
/// <param name="latitude">The initial latitude value.</param>
21+
/// <param name="longitude">The initial longitude value.</param>
1322
private CoordinateVariance(int latitude, int longitude) {
1423
Latitude = latitude;
1524
Longitude = longitude;
1625
}
1726

27+
/// <summary>
28+
/// Gets the variance in latitude between the current and previous coordinates.
29+
/// </summary>
1830
public int Latitude { get; private set; }
1931

32+
/// <summary>
33+
/// Gets the variance in longitude between the current and previous coordinates.
34+
/// </summary>
2035
public int Longitude { get; private set; }
2136

37+
/// <summary>
38+
/// Updates the variance based on the next set of latitude and longitude values.
39+
/// </summary>
40+
/// <param name="latitude">The next latitude value.</param>
41+
/// <param name="longitude">The next longitude value.</param>
2242
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2343
public void Next(int latitude, int longitude) {
2444
Latitude = Variance(_current.Latitude, latitude);
@@ -27,6 +47,12 @@ public void Next(int latitude, int longitude) {
2747
_current = (latitude, longitude);
2848
}
2949

50+
/// <summary>
51+
/// Calculates the variance between two coordinate values.
52+
/// </summary>
53+
/// <param name="initial">The initial coordinate value.</param>
54+
/// <param name="next">The next coordinate value.</param>
55+
/// <returns>The calculated variance between the two values.</returns>
3056
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3157
private static int Variance(int initial, int next) => (initial, next) switch {
3258
(0, 0) => 0,
@@ -38,6 +64,10 @@ public void Next(int latitude, int longitude) {
3864
( > 0, > 0) => next - initial,
3965
};
4066

67+
/// <summary>
68+
/// Returns a string representation of the coordinate variance.
69+
/// </summary>
70+
/// <returns>A string in the format: Variance: { Latitude: [int], Longitude: [int] }.</returns>
4171
public override readonly string ToString()
4272
=> $"Variance: {{ Latitude: {Latitude}, Longitude: {Longitude} }}";
4373
}

src/PolylineAlgorithm/Internal/Defaults.cs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,33 @@ namespace PolylineAlgorithm.Internal;
99
using System.Diagnostics.CodeAnalysis;
1010

1111
/// <summary>
12-
/// Provides default values used in the Polyline Algorithm.
12+
/// Provides default values and constants used throughout the Polyline Algorithm.
13+
/// This class contains nested static classes for organizing defaults related to the algorithm,
14+
/// polyline encoding, and geographic coordinates.
1315
/// </summary>
1416
[ExcludeFromCodeCoverage]
1517
internal static class Defaults {
1618
/// <summary>
17-
/// Contains default values related to the algorithm.
19+
/// Contains default values and constants related to the polyline encoding algorithm.
1820
/// </summary>
1921
public static class Algorithm {
2022
/// <summary>
21-
/// The coordinate rounding precision.
23+
/// The coordinate rounding precision used in the polyline encoding algorithm.
2224
/// </summary>
2325
public const int Precision = 100_000;
2426

2527
/// <summary>
26-
/// The length of the shift used in the algorithm.
28+
/// The bit shift length used in the polyline encoding algorithm.
2729
/// </summary>
2830
public const byte ShiftLength = 5;
2931

3032
/// <summary>
31-
/// The ASCII value for the question mark character.
33+
/// The ASCII value for the question mark character ('?').
3234
/// </summary>
3335
public const byte QuestionMark = 63;
3436

3537
/// <summary>
36-
/// The ASCII value for the space character.
38+
/// The ASCII value for the space character (' ').
3739
/// </summary>
3840
public const byte Space = 32;
3941

@@ -44,36 +46,41 @@ public static class Algorithm {
4446
}
4547

4648
/// <summary>
47-
/// Contains default values related to polyline.
49+
/// Contains default values and constants related to polyline encoding.
4850
/// </summary>
4951
public static class Polyline {
52+
/// <summary>
53+
/// An array of delimiters used in the polyline encoding process.
54+
/// Each delimiter is derived by adding the ASCII value of the question mark ('?') to a range of integers.
55+
/// </summary>
5056
public static readonly byte[] Delimiters = [.. Enumerable.Range(0, 32).Select(n => (byte)(n + Defaults.Algorithm.QuestionMark))];
5157

5258
/// <summary>
53-
/// The minimum length of an encoded coordinate.
59+
/// The minimum length of an encoded coordinate in the polyline format.
5460
/// </summary>
5561
public const int MinEncodedCoordinateLength = 2;
62+
5663
/// <summary>
57-
/// The maximum length of an encoded coordinate.
64+
/// The maximum length of an encoded coordinate in the polyline format.
5865
/// </summary>
5966
public const int MaxEncodedCoordinateLength = 12;
6067
}
6168

6269
/// <summary>
63-
/// Contains default values related to coordinates.
70+
/// Contains default values and constants related to geographic coordinates.
6471
/// </summary>
6572
public static class Coordinate {
6673
/// <summary>
6774
/// Contains default values related to latitude.
6875
/// </summary>
6976
public static class Latitude {
7077
/// <summary>
71-
/// The minimum value for latitude.
78+
/// The minimum valid value for latitude, in degrees.
7279
/// </summary>
7380
public const sbyte Min = -Max;
7481

7582
/// <summary>
76-
/// The maximum value for latitude.
83+
/// The maximum valid value for latitude, in degrees.
7784
/// </summary>
7885
public const byte Max = 90;
7986
}
@@ -83,29 +90,29 @@ public static class Latitude {
8390
/// </summary>
8491
public static class Longitude {
8592
/// <summary>
86-
/// The minimum value for longitude.
93+
/// The minimum valid value for longitude, in degrees.
8794
/// </summary>
8895
public const short Min = -Max;
8996

9097
/// <summary>
91-
/// The maximum value for longitude.
98+
/// The maximum valid value for longitude, in degrees.
9299
/// </summary>
93100
public const byte Max = 180;
94101
}
95102

96103
/// <summary>
97-
/// Contains default ranges for latitude and longitude.
104+
/// Contains default ranges for validating latitude and longitude values.
98105
/// </summary>
99106
public static class Range {
100107
/// <summary>
101-
/// The validation range for latitude.
108+
/// The default validation range for latitude values.
102109
/// </summary>
103110
public static readonly CoordinateRange Latitude = new(Coordinate.Latitude.Min, Coordinate.Latitude.Max);
104111

105112
/// <summary>
106-
/// The validation range for longitude.
113+
/// The default validation range for longitude values.
107114
/// </summary>
108115
public static readonly CoordinateRange Longitude = new(Coordinate.Longitude.Min, Coordinate.Longitude.Max);
109116
}
110117
}
111-
}
118+
}

src/PolylineAlgorithm/Internal/PolylineBuilder.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,23 @@
33
using System;
44
using System.Runtime.InteropServices;
55

6+
/// <summary>
7+
/// Provides functionality to build a polyline from multiple segments of characters.
8+
/// This struct is used to efficiently construct a <see cref="Polyline"/> instance.
9+
/// </summary>
610
[StructLayout(LayoutKind.Auto)]
711
internal struct PolylineBuilder {
812
private PolylineSegment? _initial;
913
private PolylineSegment? _last;
1014

15+
/// <summary>
16+
/// Appends a new segment of characters to the polyline being built.
17+
/// </summary>
18+
/// <param name="value">The segment of characters to append.</param>
19+
/// <remarks>
20+
/// This method creates a new <see cref="PolylineSegment"/> for the provided memory
21+
/// and links it to the existing chain of segments.
22+
/// </remarks>
1123
public void Append(ReadOnlyMemory<char> value) {
1224
var current = new PolylineSegment(value);
1325

@@ -17,11 +29,18 @@ public void Append(ReadOnlyMemory<char> value) {
1729
_last = current;
1830
}
1931

32+
/// <summary>
33+
/// Builds the final <see cref="Polyline"/> instance from the appended segments.
34+
/// </summary>
35+
/// <returns>
36+
/// A <see cref="Polyline"/> instance representing the concatenated segments.
37+
/// If no segments were appended, an empty <see cref="Polyline"/> is returned.
38+
/// </returns>
2039
public readonly Polyline Build() {
2140
if (_initial is null) {
2241
return Polyline.FromMemory(ReadOnlyMemory<char>.Empty);
2342
}
2443

2544
return Polyline.FromSequence(new(_initial, 0, _last, _last!.Memory.Length));
2645
}
27-
}
46+
}

src/PolylineAlgorithm/Internal/PolylineSegment.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,35 @@
22

33
using System.Buffers;
44

5+
/// <summary>
6+
/// Represents a segment of a polyline, implemented as a linked list of memory segments.
7+
/// This class extends <see cref="ReadOnlySequenceSegment{T}"/> to support efficient handling of polyline data.
8+
/// </summary>
59
internal class PolylineSegment : ReadOnlySequenceSegment<char> {
10+
/// <summary>
11+
/// Initializes a new instance of the <see cref="PolylineSegment"/> class with the specified memory and running index.
12+
/// </summary>
13+
/// <param name="memory">The memory segment to associate with this polyline segment.</param>
14+
/// <param name="runningIndex">
15+
/// The cumulative index of this segment within the polyline. Defaults to 0.
16+
/// </param>
617
public PolylineSegment(ReadOnlyMemory<char> memory, long runningIndex = 0) {
718
Memory = memory;
819
RunningIndex = runningIndex;
920
}
1021

22+
/// <summary>
23+
/// Appends a new memory segment to the current polyline segment.
24+
/// </summary>
25+
/// <param name="memory">The memory segment to append.</param>
1126
public void Append(ReadOnlyMemory<char> memory) {
1227
Append(new PolylineSegment(memory));
1328
}
1429

30+
/// <summary>
31+
/// Appends another <see cref="PolylineSegment"/> to the current segment, forming a linked list.
32+
/// </summary>
33+
/// <param name="next">The next <see cref="PolylineSegment"/> to append.</param>
1534
public void Append(PolylineSegment next) {
1635
next.RunningIndex = RunningIndex + Memory.Length;
1736
Next = next;

0 commit comments

Comments
 (0)