|
| 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.SensorData.Sample; |
| 7 | + |
| 8 | +using PolylineAlgorithm.Abstraction; |
| 9 | +using System.Buffers; |
| 10 | +using System.Threading; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Encodes a sequence of <see cref="SensorReading"/> values into a compact polyline string |
| 14 | +/// using the polyline delta-encoding algorithm applied to the <see cref="SensorReading.Temperature"/> field. |
| 15 | +/// </summary> |
| 16 | +/// <remarks> |
| 17 | +/// <para> |
| 18 | +/// This class demonstrates implementing <see cref="IPolylineEncoder{TValue,TPolyline}"/> for a custom |
| 19 | +/// scalar type, following the same structural pattern as <see cref="AbstractPolylineEncoder{TCoordinate,TPolyline}"/>. |
| 20 | +/// </para> |
| 21 | +/// <para> |
| 22 | +/// Because sensor readings carry only a single numeric dimension (temperature), the base class designed |
| 23 | +/// for two-dimensional coordinate pairs is not used. Instead, <see cref="PolylineEncoding"/> static |
| 24 | +/// helpers are called directly to perform normalisation, delta computation, and character-level encoding. |
| 25 | +/// </para> |
| 26 | +/// <para> |
| 27 | +/// Only <see cref="SensorReading.Temperature"/> values are encoded. Timestamps are not encoded and |
| 28 | +/// will not be recovered on decoding. |
| 29 | +/// </para> |
| 30 | +/// </remarks> |
| 31 | +public sealed class SensorDataEncoder : IPolylineEncoder<SensorReading, string> { |
| 32 | + /// <summary> |
| 33 | + /// Initializes a new instance of the <see cref="SensorDataEncoder"/> class with default encoding options. |
| 34 | + /// </summary> |
| 35 | + public SensorDataEncoder() |
| 36 | + : this(new PolylineEncodingOptions()) { } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Initializes a new instance of the <see cref="SensorDataEncoder"/> class with the specified encoding options. |
| 40 | + /// </summary> |
| 41 | + /// <param name="options"> |
| 42 | + /// The <see cref="PolylineEncodingOptions"/> to use for encoding operations. |
| 43 | + /// </param> |
| 44 | + /// <exception cref="ArgumentNullException"> |
| 45 | + /// Thrown when <paramref name="options"/> is <see langword="null"/>. |
| 46 | + /// </exception> |
| 47 | + public SensorDataEncoder(PolylineEncodingOptions options) { |
| 48 | + if (options is null) { |
| 49 | + throw new ArgumentNullException(nameof(options)); |
| 50 | + } |
| 51 | + |
| 52 | + Options = options; |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Gets the encoding options used by this encoder. |
| 57 | + /// </summary> |
| 58 | + public PolylineEncodingOptions Options { get; } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Encodes a sequence of <see cref="SensorReading"/> values into a polyline string. |
| 62 | + /// </summary> |
| 63 | + /// <param name="coordinates"> |
| 64 | + /// The sensor readings whose <see cref="SensorReading.Temperature"/> values are to be encoded. |
| 65 | + /// Must contain at least one element. |
| 66 | + /// </param> |
| 67 | + /// <param name="cancellationToken"> |
| 68 | + /// A <see cref="CancellationToken"/> that can be used to cancel the encoding operation. |
| 69 | + /// </param> |
| 70 | + /// <returns> |
| 71 | + /// A polyline-encoded string representing the delta-compressed temperature series. |
| 72 | + /// </returns> |
| 73 | + /// <exception cref="ArgumentException"> |
| 74 | + /// Thrown when <paramref name="coordinates"/> is empty. |
| 75 | + /// </exception> |
| 76 | + /// <exception cref="OperationCanceledException"> |
| 77 | + /// Thrown when <paramref name="cancellationToken"/> requests cancellation. |
| 78 | + /// </exception> |
| 79 | + public string Encode(ReadOnlySpan<SensorReading> coordinates, CancellationToken cancellationToken = default) { |
| 80 | + if (coordinates.Length < 1) { |
| 81 | + throw new ArgumentException("Sequence must contain at least one element.", nameof(coordinates)); |
| 82 | + } |
| 83 | + |
| 84 | + // Maximum number of ASCII characters required to encode a single 32-bit delta value |
| 85 | + // using the polyline algorithm (ceil(32 bits / 5 bits per chunk) + sign bit = 7). |
| 86 | + const int MaxEncodedCharsPerValue = 7; |
| 87 | + |
| 88 | + int previousNormalized = 0; |
| 89 | + int position = 0; |
| 90 | + int length = coordinates.Length * MaxEncodedCharsPerValue; |
| 91 | + |
| 92 | + char[]? temp = length <= Options.StackAllocLimit |
| 93 | + ? null |
| 94 | + : ArrayPool<char>.Shared.Rent(length); |
| 95 | + |
| 96 | + Span<char> buffer = temp is null ? stackalloc char[length] : temp.AsSpan(0, length); |
| 97 | + |
| 98 | + try { |
| 99 | + for (int i = 0; i < coordinates.Length; i++) { |
| 100 | + cancellationToken.ThrowIfCancellationRequested(); |
| 101 | + |
| 102 | + int normalized = PolylineEncoding.Normalize(coordinates[i].Temperature, Options.Precision); |
| 103 | + int delta = normalized - previousNormalized; |
| 104 | + |
| 105 | + if (!PolylineEncoding.TryWriteValue(delta, buffer, ref position)) { |
| 106 | + throw new InvalidOperationException("Encoding buffer is too small to hold the encoded value."); |
| 107 | + } |
| 108 | + |
| 109 | + previousNormalized = normalized; |
| 110 | + } |
| 111 | + |
| 112 | + return buffer[..position].ToString(); |
| 113 | + } finally { |
| 114 | + if (temp is not null) { |
| 115 | + ArrayPool<char>.Shared.Return(temp); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments