-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPolylineEncoder.cs
More file actions
240 lines (197 loc) · 9.98 KB
/
PolylineEncoder.cs
File metadata and controls
240 lines (197 loc) · 9.98 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// 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.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
/// <summary>
/// Encodes sequences of values into encoded polyline representations.
/// </summary>
/// <typeparam name="TValue">The type that represents a value to encode.</typeparam>
/// <typeparam name="TPolyline">The type that represents the encoded polyline output.</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 PolylineEncoder<TValue, TPolyline> : IPolylineEncoder<TValue, TPolyline> {
private readonly IPolylineFormatter<TValue, TPolyline> _formatter;
private readonly PolylineOptions<TValue, TPolyline> _options;
private readonly ILogger<PolylineEncoder<TValue, TPolyline>> _logger;
/// <summary>
/// Initializes a new instance of <see cref="PolylineEncoder{TValue, TPolyline}"/>.
/// </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 PolylineEncoder(PolylineOptions<TValue, TPolyline> options) {
if (options is null) {
ExceptionGuard.ThrowArgumentNull(nameof(options));
}
_options = options;
_formatter = options.Formatter;
_logger = options.LoggerFactory.CreateLogger<PolylineEncoder<TValue, TPolyline>>();
}
/// <summary>
/// Encodes a collection of <typeparamref name="TValue"/> instances into an encoded
/// <typeparamref name="TPolyline"/>.
/// </summary>
/// <param name="coordinates">The collection of values to encode.</param>
/// <param name="cancellationToken">A token that can be used to cancel the operation.</param>
/// <returns>
/// An instance of <typeparamref name="TPolyline"/> representing the encoded values.
/// </returns>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="coordinates"/> is empty.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Thrown when the internal encoding buffer cannot accommodate the encoded value.
/// </exception>
/// <exception cref="OperationCanceledException">
/// Thrown when <paramref name="cancellationToken"/> is canceled.
/// </exception>
public TPolyline Encode(ReadOnlySpan<TValue> coordinates, CancellationToken cancellationToken = default) {
const string OperationName = nameof(Encode);
_logger.LogOperationStartedDebug(OperationName);
Debug.Assert(coordinates.Length >= 0, "Count must be non-negative.");
if (coordinates.Length < 1) {
_logger.LogOperationFailedDebug(OperationName);
_logger.LogEmptyArgumentWarning(nameof(coordinates));
ExceptionGuard.ThrowArgumentCannotBeEmptyEnumerationMessage(nameof(coordinates));
}
int width = _formatter.Width;
int length = GetMaxBufferLength(coordinates.Length, width);
char[]? temp = length <= _options.StackAllocLimit
? null
: ArrayPool<char>.Shared.Rent(length);
Span<char> buffer = temp is null ? stackalloc char[length] : temp.AsSpan(0, length);
int position = 0;
long[] previous = new long[width];
long[] values = new long[width];
SeedPrevious(previous, null);
try {
for (int i = 0; i < coordinates.Length; i++) {
cancellationToken.ThrowIfCancellationRequested();
_formatter.GetValues(coordinates[i], values.AsSpan());
for (int j = 0; j < width; j++) {
long current = values[j];
long delta = current - previous[j];
previous[j] = current;
if (!PolylineEncoding.TryWriteValue(delta, buffer, ref position)) {
_logger.LogOperationFailedDebug(OperationName);
_logger.LogCannotWriteValueToBufferWarning(position, i);
ExceptionGuard.ThrowCouldNotWriteEncodedValueToBuffer();
}
}
}
// Convert to string inside the try block so the buffer is still valid.
string encodedResult = buffer[..position].ToString();
_logger.LogOperationFinishedDebug(OperationName);
return _formatter.Write(encodedResult.AsMemory());
} finally {
if (temp is not null) {
ArrayPool<char>.Shared.Return(temp);
}
}
}
/// <summary>
/// Encodes a collection of <typeparamref name="TValue"/> instances into an encoded
/// <typeparamref name="TPolyline"/>, applying per-call <paramref name="options"/> to control the
/// delta baseline. Use this overload to encode large sequences in independent chunks that can be
/// concatenated into a single valid polyline.
/// </summary>
/// <param name="coordinates">The collection of values to encode.</param>
/// <param name="options">
/// Per-call options that control the starting delta baseline. Pass <see langword="null"/> or an
/// instance with <see cref="PolylineEncodingOptions{TValue}.Previous"/> set to
/// <see langword="null"/> to use the formatter's default baseline (same as calling
/// <see cref="Encode(ReadOnlySpan{TValue}, CancellationToken)"/>).
/// </param>
/// <param name="cancellationToken">A token that can be used to cancel the operation.</param>
/// <returns>
/// An instance of <typeparamref name="TPolyline"/> representing the encoded values.
/// </returns>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="coordinates"/> is empty.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Thrown when the internal encoding buffer cannot accommodate the encoded value.
/// </exception>
/// <exception cref="OperationCanceledException">
/// Thrown when <paramref name="cancellationToken"/> is canceled.
/// </exception>
public TPolyline Encode(ReadOnlySpan<TValue> coordinates, PolylineEncodingOptions<TValue>? options, CancellationToken cancellationToken) {
const string OperationName = nameof(Encode);
_logger.LogOperationStartedDebug(OperationName);
Debug.Assert(coordinates.Length >= 0, "Count must be non-negative.");
if (coordinates.Length < 1) {
_logger.LogOperationFailedDebug(OperationName);
_logger.LogEmptyArgumentWarning(nameof(coordinates));
ExceptionGuard.ThrowArgumentCannotBeEmptyEnumerationMessage(nameof(coordinates));
}
int width = _formatter.Width;
int length = GetMaxBufferLength(coordinates.Length, width);
char[]? temp = length <= _options.StackAllocLimit
? null
: ArrayPool<char>.Shared.Rent(length);
Span<char> buffer = temp is null ? stackalloc char[length] : temp.AsSpan(0, length);
int position = 0;
long[] previous = new long[width];
long[] values = new long[width];
SeedPrevious(previous, options);
try {
for (int i = 0; i < coordinates.Length; i++) {
cancellationToken.ThrowIfCancellationRequested();
_formatter.GetValues(coordinates[i], values.AsSpan());
for (int j = 0; j < width; j++) {
long current = values[j];
long delta = current - previous[j];
previous[j] = current;
if (!PolylineEncoding.TryWriteValue(delta, buffer, ref position)) {
_logger.LogOperationFailedDebug(OperationName);
_logger.LogCannotWriteValueToBufferWarning(position, i);
ExceptionGuard.ThrowCouldNotWriteEncodedValueToBuffer();
}
}
}
string encodedResult = buffer[..position].ToString();
_logger.LogOperationFinishedDebug(OperationName);
return _formatter.Write(encodedResult.AsMemory());
} finally {
if (temp is not null) {
ArrayPool<char>.Shared.Return(temp);
}
}
}
private void SeedPrevious(long[] previous, PolylineEncodingOptions<TValue>? options) {
int width = _formatter.Width;
if (options is { HasPrevious: true }) {
_formatter.GetValues(options.Previous, previous.AsSpan());
} else {
for (int j = 0; j < width; j++) {
previous[j] = _formatter.GetBaseline(j);
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetMaxBufferLength(int count, int valuesPerItem) {
Debug.Assert(count > 0, "Count must be greater than zero.");
Debug.Assert(valuesPerItem > 0, "Values per item must be greater than zero.");
int requestedBufferLength = count * valuesPerItem * Defaults.Polyline.Block.Length.Max;
Debug.Assert(requestedBufferLength > 0, "Requested buffer length must be greater than zero.");
return requestedBufferLength;
}
}