-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPolylineDecoder.cs
More file actions
144 lines (125 loc) · 6.1 KB
/
PolylineDecoder.cs
File metadata and controls
144 lines (125 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Copyright © Pete Sramek. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
namespace PolylineAlgorithm;
using Microsoft.Extensions.Logging;
using PolylineAlgorithm.Abstraction;
using PolylineAlgorithm.Internal;
using PolylineAlgorithm.Internal.Diagnostics;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
/// <summary>
/// Decodes encoded polyline representations into sequences of values.
/// </summary>
/// <typeparam name="TPolyline">The type that represents the encoded polyline input.</typeparam>
/// <typeparam name="TValue">The type that represents a decoded value.</typeparam>
/// <remarks>
/// Pass a <see cref="PolylineOptions{TValue, TPolyline}"/> that carries a
/// <see cref="IPolylineFormatter{TValue, TPolyline}"/> to the constructor. The formatter handles
/// all type-specific concerns; no subclassing is required.
/// </remarks>
public class PolylineDecoder<TPolyline, TValue> : IPolylineDecoder<TPolyline, TValue> {
private readonly IPolylineFormatter<TValue, TPolyline> _formatter;
private readonly ILogger<PolylineDecoder<TPolyline, TValue>> _logger;
/// <summary>
/// Initializes a new instance of <see cref="PolylineDecoder{TPolyline, TValue}"/>.
/// </summary>
/// <param name="options">
/// A <see cref="PolylineOptions{TValue, TPolyline}"/> that carries the formatter and settings.
/// Must not be <see langword="null"/>.
/// </param>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="options"/> is <see langword="null"/>.
/// </exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "Null is verified before use via ExceptionGuard.ThrowArgumentNull, which is annotated [DoesNotReturn]. CA1062 does not recognise custom [DoesNotReturn] helpers as null guards.")]
public PolylineDecoder(PolylineOptions<TValue, TPolyline> options) {
if (options is null) {
ExceptionGuard.ThrowArgumentNull(nameof(options));
}
_formatter = options.Formatter;
_logger = options.LoggerFactory.CreateLogger<PolylineDecoder<TPolyline, TValue>>();
}
/// <summary>
/// Decodes an encoded <typeparamref name="TPolyline"/> into a sequence of
/// <typeparamref name="TValue"/> instances, applying per-call <paramref name="options"/> to
/// seed the accumulated-delta state. Use this overload to decode polylines that were produced by
/// chunked encoding.
/// </summary>
/// <param name="polyline">The encoded polyline to decode. Must not be <see langword="null"/>.</param>
/// <param name="options">
/// Per-call options that control the accumulated-delta seed. Pass <see langword="null"/> or an
/// instance with <see cref="PolylineDecodingOptions{TValue}.Previous"/> set to
/// <see langword="null"/> to start from zero (same as calling
/// <see cref="Decode(TPolyline, CancellationToken)"/>).
/// </param>
/// <param name="cancellationToken">A token that can be used to cancel the operation.</param>
/// <returns>
/// An <see cref="IEnumerable{T}"/> of <typeparamref name="TValue"/> representing the decoded
/// values.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="polyline"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="InvalidPolylineException">
/// Thrown when the polyline format is invalid or malformed.
/// </exception>
/// <exception cref="OperationCanceledException">
/// Thrown when <paramref name="cancellationToken"/> is canceled during decoding.
/// </exception>
public IEnumerable<TValue> Decode(
TPolyline polyline,
PolylineDecodingOptions<TValue>? options = null,
CancellationToken cancellationToken = default) {
const string OperationName = nameof(Decode);
_logger.LogOperationStartedDebug(OperationName);
if (polyline is null) {
_logger.LogNullArgumentWarning(nameof(polyline));
ExceptionGuard.ThrowArgumentNull(nameof(polyline));
}
ReadOnlyMemory<char> sequence = _formatter.Read(polyline);
if (sequence.Length < Defaults.Polyline.Block.Length.Min) {
_logger.LogOperationFailedDebug(OperationName);
_logger.LogPolylineCannotBeShorterThanWarning(sequence.Length, Defaults.Polyline.Block.Length.Min);
ExceptionGuard.ThrowInvalidPolylineLength(sequence.Length, Defaults.Polyline.Block.Length.Min);
}
try {
PolylineEncoding.ValidateFormat(sequence.Span);
} catch (ArgumentException ex) {
_logger.LogInvalidPolylineFormatWarning(ex);
throw;
}
int width = _formatter.Width;
long[] accumulated = new long[width];
int position = 0;
SeedAccumulated(accumulated, options);
try {
while (position < sequence.Length) {
cancellationToken.ThrowIfCancellationRequested();
for (int j = 0; j < width; j++) {
if (!PolylineEncoding.TryReadValue(ref accumulated[j], sequence, ref position)) {
_logger.LogOperationFailedDebug(OperationName);
_logger.LogInvalidPolylineWarning(position);
ExceptionGuard.ThrowInvalidPolylineFormat(position);
}
}
yield return _formatter.CreateItem(accumulated.AsSpan());
}
} finally {
_logger.LogOperationFinishedDebug(OperationName);
}
}
private void SeedAccumulated(long[] accumulated, PolylineDecodingOptions<TValue>? options) {
if (options is not { HasPrevious: true }) {
return;
}
int width = _formatter.Width;
long[] scaled = new long[width];
_formatter.GetValues(options.Previous, scaled.AsSpan());
for (int j = 0; j < width; j++) {
accumulated[j] = scaled[j] - _formatter.GetBaseline(j);
}
}
}