Skip to content

Commit f95e27c

Browse files
Copilotpetesramek
andcommitted
Changes before error encountered
Co-authored-by: petesramek <2333452+petesramek@users.noreply.github.com> Agent-Logs-Url: https://github.com/petesramek/polyline-algorithm-csharp/sessions/dffc0779-5a05-4dc6-bfc5-93a43db4236e
1 parent a71090d commit f95e27c

6 files changed

Lines changed: 92 additions & 14 deletions

File tree

src/PolylineAlgorithm/Abstraction/AbstractPolylineDecoder.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@ namespace PolylineAlgorithm.Abstraction;
1111
using PolylineAlgorithm.Internal.Logging;
1212
using PolylineAlgorithm.Properties;
1313
using System;
14+
using System.Collections.Generic;
1415
using System.Diagnostics.CodeAnalysis;
16+
using System.Runtime.CompilerServices;
17+
using System.Threading;
18+
using System.Threading.Tasks;
1519

1620
/// <summary>
1721
/// Decodes encoded polyline strings into sequences of geographic coordinates.
18-
/// Implements the <see cref="IPolylineDecoder{TPolyline, TCoordinate}"/> interface.
22+
/// Implements the <see cref="IPolylineDecoder{TPolyline, TCoordinate}"/> and <see cref="IAsyncPolylineDecoder{TPolyline, TCoordinate}"/> interfaces.
1923
/// </summary>
2024
/// <remarks>
2125
/// This abstract class provides a base implementation for decoding polylines, allowing subclasses to define how to handle specific polyline formats.
2226
/// </remarks>
23-
public abstract class AbstractPolylineDecoder<TPolyline, TCoordinate> : IPolylineDecoder<TPolyline, TCoordinate> {
27+
public abstract class AbstractPolylineDecoder<TPolyline, TCoordinate> : IPolylineDecoder<TPolyline, TCoordinate>, IAsyncPolylineDecoder<TPolyline, TCoordinate> {
2428
/// <summary>
2529
/// Initializes a new instance of the <see cref="AbstractPolylineDecoder{TPolyline, TCoordinate}"/> class with default encoding options.
2630
/// </summary>
@@ -151,4 +155,25 @@ static void ValidateEmptySequence(ILogger<AbstractPolylineDecoder<TPolyline, TCo
151155
/// A coordinate instance of type <typeparamref name="TCoordinate"/>.
152156
/// </returns>
153157
protected abstract TCoordinate CreateCoordinate(double latitude, double longitude);
158+
159+
/// <summary>
160+
/// Asynchronously decodes an encoded <typeparamref name="TPolyline"/> into a sequence of <typeparamref name="TCoordinate"/> instances.
161+
/// </summary>
162+
/// <param name="polyline">
163+
/// The <typeparamref name="TPolyline"/> instance containing the encoded polyline string to decode.
164+
/// </param>
165+
/// <param name="cancellationToken">
166+
/// A <see cref="CancellationToken"/> to observe while waiting for the task to complete.
167+
/// </param>
168+
/// <returns>
169+
/// An <see cref="IAsyncEnumerable{T}"/> of <typeparamref name="TCoordinate"/> representing the decoded latitude and longitude pairs.
170+
/// </returns>
171+
public async IAsyncEnumerable<TCoordinate> DecodeAsync(TPolyline polyline, [EnumeratorCancellation] CancellationToken cancellationToken = default) {
172+
foreach (var coordinate in Decode(polyline)) {
173+
cancellationToken.ThrowIfCancellationRequested();
174+
yield return coordinate;
175+
}
176+
177+
await Task.CompletedTask.ConfigureAwait(false);
178+
}
154179
}

src/PolylineAlgorithm/Abstraction/AbstractPolylineEncoder.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ namespace PolylineAlgorithm.Abstraction;
1414
using System.Collections;
1515
using System.Collections.Generic;
1616
using System.Diagnostics;
17+
using System.Threading;
18+
using System.Threading.Tasks;
1719

1820
/// <summary>
1921
/// Provides functionality to encode a collection of geographic coordinates into an encoded polyline string.
20-
/// Implements the <see cref="IPolylineEncoder{TCoordinate, TPolyline}"/> interface.
22+
/// Implements the <see cref="IPolylineEncoder{TCoordinate, TPolyline}"/> and <see cref="IAsyncPolylineEncoder{TCoordinate, TPolyline}"/> interfaces.
2123
/// </summary>
2224
/// <remarks>
2325
/// This abstract class serves as a base for specific polyline encoders, allowing customization of the encoding process.
2426
/// </remarks>
25-
public abstract class AbstractPolylineEncoder<TCoordinate, TPolyline> : IPolylineEncoder<TCoordinate, TPolyline> {
27+
public abstract class AbstractPolylineEncoder<TCoordinate, TPolyline> : IPolylineEncoder<TCoordinate, TPolyline>, IAsyncPolylineEncoder<TCoordinate, TPolyline> {
2628
/// <summary>
2729
/// Initializes a new instance of the <see cref="AbstractPolylineEncoder{TCoordinate, TPolyline}"/> class with default encoding options.
2830
/// </summary>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Copyright © Pete Sramek. All rights reserved.
3+
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace PolylineAlgorithm.Abstraction;
7+
8+
using System.Collections.Generic;
9+
using System.Runtime.CompilerServices;
10+
using System.Threading;
11+
12+
/// <summary>
13+
/// Defines a contract for asynchronously decoding an encoded polyline into a sequence of geographic coordinates.
14+
/// </summary>
15+
public interface IAsyncPolylineDecoder<TPolyline, TCoordinate> {
16+
/// <summary>
17+
/// Asynchronously decodes the specified encoded polyline into a sequence of geographic coordinates.
18+
/// </summary>
19+
/// <param name="polyline">
20+
/// The <typeparamref name="TPolyline"/> instance containing the encoded polyline string to decode.
21+
/// </param>
22+
/// <param name="cancellationToken">
23+
/// A <see cref="CancellationToken"/> to observe while waiting for the task to complete.
24+
/// </param>
25+
/// <returns>
26+
/// An <see cref="IAsyncEnumerable{T}"/> of <typeparamref name="TCoordinate"/> representing the decoded latitude and longitude pairs.
27+
/// </returns>
28+
IAsyncEnumerable<TCoordinate> DecodeAsync(TPolyline polyline, [EnumeratorCancellation] CancellationToken cancellationToken = default);
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Copyright © Pete Sramek. All rights reserved.
3+
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace PolylineAlgorithm.Abstraction;
7+
8+
using System.Collections.Generic;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
12+
/// <summary>
13+
/// Defines a contract for asynchronously encoding a sequence of geographic coordinates into an encoded polyline string.
14+
/// </summary>
15+
public interface IAsyncPolylineEncoder<TCoordinate, TPolyline> {
16+
/// <summary>
17+
/// Asynchronously encodes a sequence of geographic coordinates into an encoded polyline representation.
18+
/// </summary>
19+
/// <param name="coordinates">
20+
/// The asynchronous collection of <typeparamref name="TCoordinate"/> instances to encode into a polyline.
21+
/// </param>
22+
/// <param name="cancellationToken">
23+
/// A <see cref="CancellationToken"/> to observe while waiting for the task to complete.
24+
/// </param>
25+
/// <returns>
26+
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation, containing a <typeparamref name="TPolyline"/>
27+
/// with the encoded polyline string that represents the input coordinates.
28+
/// </returns>
29+
ValueTask<TPolyline> EncodeAsync(IAsyncEnumerable<TCoordinate> coordinates, CancellationToken cancellationToken = default);
30+
}

src/PolylineAlgorithm/Internal/CoordinateVariance.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,7 @@ public void Next(int latitude, int longitude) {
5959
/// <param name="next">The next coordinate value.</param>
6060
/// <returns>The computed variance between <paramref name="initial"/> and <paramref name="next"/>.</returns>
6161

62-
private static int Variance(int initial, int next) => (initial, next) switch {
63-
(0, 0) => 0,
64-
(0, _) => next,
65-
(_, 0) => -initial,
66-
( < 0, < 0) => -(Math.Abs(next) - Math.Abs(initial)),
67-
( < 0, > 0) => next + Math.Abs(initial),
68-
( > 0, < 0) => -(Math.Abs(next) + initial),
69-
( > 0, > 0) => next - initial,
70-
};
62+
private static int Variance(int initial, int next) => next - initial;
7163

7264
/// <summary>
7365
/// Returns a string representation of the current coordinate variance.

src/PolylineAlgorithm/PolylineEncoding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public static double Denormalize(int value, CoordinateValueType type) {
104104
return 0.0;
105105
}
106106

107-
return Math.Truncate((double)value) / Defaults.Algorithm.Precision;
107+
return (double)value / Defaults.Algorithm.Precision;
108108
}
109109

110110
/// <summary>

0 commit comments

Comments
 (0)