Skip to content

Commit 38c3d6a

Browse files
author
Petr Sramek
committed
updated documenation
1 parent f87d411 commit 38c3d6a

13 files changed

+227
-65
lines changed

src/PolylineAlgorithm/Coordinate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,4 @@ public bool Equals(Coordinate other) {
131131
}
132132

133133
#endregion
134-
}
134+
}

src/PolylineAlgorithm/IPolylineDecoder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ namespace PolylineAlgorithm;
88
using System.Collections.Generic;
99

1010
/// <summary>
11-
/// Defines method to decode a polyline.
11+
/// Defines a method to decode an encoded polyline into a set of coordinates.
1212
/// </summary>
1313
public interface IPolylineDecoder {
1414
/// <summary>
1515
/// Converts an encoded polyline to a set of coordinates.
1616
/// </summary>
1717
/// <param name="polyline">An encoded polyline to decode.</param>
18-
/// <returns>A set of coordinates.</returns>
18+
/// <returns>A set of coordinates represented by the encoded polyline.</returns>
1919
IEnumerable<Coordinate> Decode(ref readonly Polyline polyline);
20-
}
20+
}
21+

src/PolylineAlgorithm/IPolylineEncoder.cs

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

1010
/// <summary>
11-
/// Defines method to encode a set of coordinates.
11+
/// Defines a method to encode a set of coordinates into an encoded polyline.
1212
/// </summary>
1313
public interface IPolylineEncoder {
1414
/// <summary>
1515
/// Converts a set of coordinates to an encoded polyline.
1616
/// </summary>
1717
/// <param name="coordinates">A set of coordinates to encode.</param>
18-
/// <returns>An encoded polyline.</returns>
18+
/// <returns>An encoded polyline representing the set of coordinates.</returns>
1919
Polyline Encode(IEnumerable<Coordinate> coordinates);
20-
}
20+
}

src/PolylineAlgorithm/Internal/PolylineReader.cs

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,44 @@ namespace PolylineAlgorithm.Internal;
88
using System.Runtime.CompilerServices;
99
using System.Runtime.InteropServices;
1010

11+
/// <summary>
12+
/// Provides functionality to read and decode a polyline.
13+
/// </summary>
1114
[StructLayout(LayoutKind.Auto)]
1215
internal ref struct PolylineReader {
1316
private ReaderState _state = new();
1417
private readonly ReadOnlySpan<char> _polyline;
1518

19+
/// <summary>
20+
/// Creates a new instance of the <see cref="PolylineReader"/> struct with an empty polyline.
21+
/// </summary>
1622
public PolylineReader() {
17-
_polyline = [];
23+
_polyline = ReadOnlySpan<char>.Empty;
1824
}
1925

26+
/// <summary>
27+
/// Creates a new instance of the <see cref="PolylineReader"/> struct with the specified polyline.
28+
/// </summary>
29+
/// <param name="polyline">The polyline to read.</param>
2030
public PolylineReader(ref readonly Polyline polyline) {
2131
_polyline = polyline.Span;
2232
}
2333

34+
/// <summary>
35+
/// Gets a value indicating whether there are more characters to read.
36+
/// </summary>
2437
public readonly bool CanRead => _state.Position < _polyline.Length;
2538

39+
/// <summary>
40+
/// Gets the current position of the reader.
41+
/// </summary>
2642
public readonly int Position => _state.Position;
2743

44+
/// <summary>
45+
/// Reads the next coordinate from the polyline.
46+
/// </summary>
47+
/// <returns>The next <see cref="Coordinate"/>.</returns>
48+
/// <exception cref="InvalidReaderStateException">Thrown when the reader is in an invalid state.</exception>
2849
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2950
public Coordinate Read() {
3051
InvalidReaderStateException.ThrowIfCannotRead(CanRead, Position, _polyline.Length);
@@ -44,23 +65,43 @@ static double Precise(ref int value) {
4465
}
4566
}
4667

68+
/// <summary>
69+
/// Advances the reader to the next character.
70+
/// </summary>
4771
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4872
void Advance() {
4973
_state.Position += 1;
5074
}
5175

76+
/// <summary>
77+
/// Sets the current latitude and longitude.
78+
/// </summary>
79+
/// <param name="latitude">The latitude value.</param>
80+
/// <param name="longitude">The longitude value.</param>
5281
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5382
void SetCurrent(ref readonly int latitude, ref readonly int longitude) {
5483
_state.Latitude = latitude;
5584
_state.Longitude = longitude;
5685
}
5786

87+
/// <summary>
88+
/// Gets the current latitude and longitude.
89+
/// </summary>
90+
/// <param name="latitude">The latitude value.</param>
91+
/// <param name="longitude">The longitude value.</param>
5892
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5993
readonly void GetCurrent(out int latitude, out int longitude) {
6094
latitude = _state.Latitude;
6195
longitude = _state.Longitude;
6296
}
6397

98+
/// <summary>
99+
/// Reads the next value from the polyline.
100+
/// </summary>
101+
/// <param name="value">The current value.</param>
102+
/// <returns>The next value.</returns>
103+
/// <exception cref="InvalidReaderStateException">Thrown when the reader is in an invalid state.</exception>
104+
/// <exception cref="InvalidPolylineException">Thrown when the polyline is malformed.</exception>
64105
[MethodImpl(MethodImplOptions.AggressiveInlining)]
65106
int ReadNext(ref readonly int value) {
66107
int chunk;
@@ -82,17 +123,31 @@ int ReadNext(ref readonly int value) {
82123
return value + ((sum & 1) == 1 ? ~(sum >> 1) : sum >> 1);
83124
}
84125

126+
/// <summary>
127+
/// Represents the state of the reader.
128+
/// </summary>
85129
[StructLayout(LayoutKind.Sequential, Pack = 4, Size = 12)]
86130
private ref struct ReaderState {
131+
/// <summary>
132+
/// Initializes a new instance of the <see cref="ReaderState"/> struct.
133+
/// </summary>
87134
public ReaderState() {
88135
Position = Latitude = Longitude = 0;
89136
}
90137

91-
138+
/// <summary>
139+
/// Gets or sets the current position of the reader.
140+
/// </summary>
92141
public int Position { get; set; }
93142

143+
/// <summary>
144+
/// Gets or sets the current latitude value.
145+
/// </summary>
94146
public int Latitude { get; set; }
95147

148+
/// <summary>
149+
/// Gets or sets the current longitude value.
150+
/// </summary>
96151
public int Longitude { get; set; }
97152
}
98-
}
153+
}

src/PolylineAlgorithm/Internal/PolylineWriter.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,38 @@ namespace PolylineAlgorithm.Internal;
99
using System.Runtime.CompilerServices;
1010
using System.Runtime.InteropServices;
1111

12+
/// <summary>
13+
/// Provides functionality to write and encode a polyline.
14+
/// </summary>
1215
[StructLayout(LayoutKind.Auto)]
1316
internal ref struct PolylineWriter {
1417
private WriterState _state = new();
1518
private Memory<char> _buffer;
1619

20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="PolylineWriter"/> struct with the specified buffer.
22+
/// </summary>
23+
/// <param name="buffer">The buffer to write to.</param>
1724
public PolylineWriter(ref readonly Memory<char> buffer) {
1825
_buffer = buffer;
1926
}
2027

28+
/// <summary>
29+
/// Gets a value indicating whether there is space left in the buffer to write.
30+
/// </summary>
2131
public readonly bool CanWrite => _state.Position < _buffer.Length;
2232

33+
/// <summary>
34+
/// Gets the current position of the writer.
35+
/// </summary>
2336
public readonly int Position => _state.Position;
2437

38+
/// <summary>
39+
/// Writes the specified coordinate to the buffer.
40+
/// </summary>
41+
/// <param name="coordinate">The coordinate to write.</param>
42+
/// <exception cref="InvalidCoordinateException">Thrown when the coordinate is invalid.</exception>
43+
/// <exception cref="InvalidWriterStateException">Thrown when the writer is in an invalid state.</exception>
2544
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2645
public void Write(ref readonly Coordinate coordinate) {
2746
InvalidCoordinateException.ThrowIfNotValid(coordinate);
@@ -41,6 +60,11 @@ static void Imprecise(double value, out int result) {
4160
}
4261
}
4362

63+
/// <summary>
64+
/// Writes the next value to the buffer.
65+
/// </summary>
66+
/// <param name="value">The value to write.</param>
67+
/// <exception cref="InvalidWriterStateException">Thrown when the writer is in an invalid state.</exception>
4468
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4569
void WriteNext(ref readonly int value) {
4670
int rem = value << 1;
@@ -57,6 +81,11 @@ void WriteNext(ref readonly int value) {
5781
WriteChar(Convert.ToChar(rem + Defaults.Algorithm.QuestionMark));
5882
}
5983

84+
/// <summary>
85+
/// Writes a character to the buffer.
86+
/// </summary>
87+
/// <param name="value">The character to write.</param>
88+
/// <exception cref="InvalidWriterStateException">Thrown when the writer is in an invalid state.</exception>
6089
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6190
void WriteChar(char value) {
6291
InvalidWriterStateException.ThrowIfCannotWrite(CanWrite, Position, _buffer.Length);
@@ -65,6 +94,13 @@ void WriteChar(char value) {
6594
_state.Position += 1;
6695
}
6796

97+
/// <summary>
98+
/// Updates the current latitude and longitude values and calculates their differences.
99+
/// </summary>
100+
/// <param name="latitude">The latitude value.</param>
101+
/// <param name="longitude">The longitude value.</param>
102+
/// <param name="latDiff">The difference in latitude.</param>
103+
/// <param name="longDiff">The difference in longitude.</param>
68104
[MethodImpl(MethodImplOptions.AggressiveInlining)]
69105
void UpdateCurrent(ref readonly int latitude, ref readonly int longitude, out int latDiff, out int longDiff) {
70106
latDiff = latitude - _state.Latitude;
@@ -74,22 +110,41 @@ void UpdateCurrent(ref readonly int latitude, ref readonly int longitude, out in
74110
_state.Longitude = longitude;
75111
}
76112

113+
/// <summary>
114+
/// Converts the buffer to a <see cref="Polyline"/> instance.
115+
/// </summary>
116+
/// <returns>The <see cref="Polyline"/> instance.</returns>
77117
public readonly Polyline ToPolyline() {
78118
ReadOnlyMemory<char> buffer = _buffer[.._state.Position];
79119
var polyline = Polyline.FromMemory(buffer);
80120
return polyline;
81121
}
82122

123+
/// <summary>
124+
/// Represents the state of the writer.
125+
/// </summary>
83126
[StructLayout(LayoutKind.Sequential, Pack = 4, Size = 12)]
84127
private ref struct WriterState {
128+
/// <summary>
129+
/// Initializes a new instance of the <see cref="WriterState"/> struct.
130+
/// </summary>
85131
public WriterState() {
86132
Position = Latitude = Longitude = 0;
87133
}
88134

135+
/// <summary>
136+
/// Gets or sets the current position of the writer.
137+
/// </summary>
89138
public int Position { get; set; }
90139

140+
/// <summary>
141+
/// Gets or sets the current latitude value.
142+
/// </summary>
91143
public int Latitude { get; set; }
92144

145+
/// <summary>
146+
/// Gets or sets the current longitude value.
147+
/// </summary>
93148
public int Longitude { get; set; }
94149
}
95-
}
150+
}

src/PolylineAlgorithm/InvalidCoordinateException.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,36 @@ namespace PolylineAlgorithm;
1111
using System.Diagnostics.CodeAnalysis;
1212

1313
/// <summary>
14-
/// Represents error that is caused by invalid coordinate.
14+
/// Represents an error that is caused by an invalid coordinate.
1515
/// </summary>
1616
[SuppressMessage("Design", "CA1032:Implement standard exception constructors", Justification = "Internal use only.")]
1717
[DebuggerDisplay($"{nameof(InvalidCoordinateException)}: {{ToString()}}")]
1818
public sealed class InvalidCoordinateException : Exception {
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="InvalidCoordinateException"/> class with the specified coordinate and error message.
21+
/// </summary>
22+
/// <param name="coordinate">The coordinate that caused the exception.</param>
23+
/// <param name="message">The error message that explains the reason for the exception.</param>
1924
private InvalidCoordinateException(Coordinate coordinate, string message)
2025
: base(message) {
2126
Coordinate = coordinate;
2227
}
2328

2429
/// <summary>
25-
/// Coordinate that caused the exception.
30+
/// Gets the coordinate that caused the exception.
2631
/// </summary>
2732
public Coordinate Coordinate { get; }
2833

34+
/// <summary>
35+
/// Throws an <see cref="InvalidCoordinateException"/> if the specified coordinate is not valid.
36+
/// </summary>
37+
/// <param name="coordinate">The coordinate to validate.</param>
38+
/// <exception cref="InvalidCoordinateException">Thrown when the coordinate is invalid.</exception>
2939
internal static void ThrowIfNotValid(Coordinate coordinate) {
3040
if (coordinate.IsValid) {
3141
return;
3242
}
3343

3444
throw new InvalidCoordinateException(coordinate, string.Format(ExceptionMessageResource.CoordinateIsOutOfRangeMessageFormat, coordinate.Latitude, coordinate.Longitude));
3545
}
36-
}
46+
}

src/PolylineAlgorithm/InvalidPolylineException.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,24 @@ namespace PolylineAlgorithm;
1212
using System.Diagnostics.CodeAnalysis;
1313

1414
/// <summary>
15-
/// Represents error that is caused by malformed polyline.
15+
/// Represents an error that is caused by a malformed polyline.
1616
/// </summary>
1717
[SuppressMessage("Design", "CA1032:Implement standard exception constructors", Justification = "Internal use only.")]
1818
[DebuggerDisplay($"{nameof(InvalidPolylineException)}: {{ToString()}}")]
1919
public sealed class InvalidPolylineException : Exception {
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="InvalidPolylineException"/> class with the specified error message.
22+
/// </summary>
23+
/// <param name="message">The error message that explains the reason for the exception.</param>
2024
private InvalidPolylineException(string message)
2125
: base(message) { }
2226

27+
/// <summary>
28+
/// Throws an <see cref="InvalidPolylineException"/> with a message indicating the position of the error in the polyline.
29+
/// </summary>
30+
/// <param name="position">The position in the polyline where the error occurred.</param>
31+
/// <exception cref="InvalidPolylineException">Always thrown to indicate a malformed polyline.</exception>
2332
internal static void Throw(int position) {
2433
throw new InvalidPolylineException(string.Format(ExceptionMessageResource.PolylineStringIsMalformedMessage, position));
2534
}
26-
}
35+
}

src/PolylineAlgorithm/InvalidReaderStateException.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,27 @@ namespace PolylineAlgorithm;
1212
using System.Diagnostics.CodeAnalysis;
1313

1414
/// <summary>
15-
/// Represents error that is caused by invalid reader state.
15+
/// Represents an error that is caused by an invalid reader state.
1616
/// </summary>
1717
[SuppressMessage("Design", "CA1032:Implement standard exception constructors", Justification = "Internal use only.")]
1818
[DebuggerDisplay($"{nameof(InvalidReaderStateException)}: {{ToString()}}")]
1919
public sealed class InvalidReaderStateException : Exception {
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="InvalidReaderStateException"/> class with the specified error message.
22+
/// </summary>
23+
/// <param name="message">The error message that explains the reason for the exception.</param>
2024
private InvalidReaderStateException(string message)
2125
: base(message) { }
2226

27+
/// <summary>
28+
/// Throws an <see cref="InvalidReaderStateException"/> if the reader cannot read from the polyline.
29+
/// </summary>
30+
/// <param name="canRead">A value indicating whether the reader can read from the polyline.</param>
31+
/// <param name="readerPosition">The current position of the reader.</param>
32+
/// <param name="polylineLength">The length of the polyline.</param>
33+
/// <exception cref="InvalidReaderStateException">
34+
/// Thrown when the reader cannot read from the polyline because the polyline is either empty or the reader has reached the end of the polyline.
35+
/// </exception>
2336
internal static void ThrowIfCannotRead(bool canRead, int readerPosition, int polylineLength) {
2437
if (canRead) {
2538
return;
@@ -31,4 +44,5 @@ internal static void ThrowIfCannotRead(bool canRead, int readerPosition, int pol
3144
throw new InvalidReaderStateException(string.Format(ExceptionMessageResource.UnableToReadPolylineAtPositionMessageFormat, readerPosition, polylineLength));
3245
}
3346
}
34-
}
47+
}
48+

0 commit comments

Comments
 (0)