Skip to content

Commit 50031b7

Browse files
author
Petr Sramek
committed
interim checkin
1 parent 5eb3113 commit 50031b7

File tree

11 files changed

+117
-207
lines changed

11 files changed

+117
-207
lines changed

benchmarks/PolylineAlgorithm.Benchmarks/PolylineDecoderBenchmark.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace PolylineAlgorithm.Benchmarks;
1414
/// Benchmarks for the <see cref="PolylineDecoder"/> class.
1515
/// </summary>
1616
[RankColumn]
17-
[ShortRunJob]
1817
public class PolylineDecoderBenchmark {
1918
private readonly Consumer _consumer = new();
2019

benchmarks/PolylineAlgorithm.Benchmarks/PolylineEncoderBenchmark.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ namespace PolylineAlgorithm.Benchmarks;
1515
/// Benchmarks for the <see cref="PolylineEncoder"/> class.
1616
/// </summary>
1717
[RankColumn]
18-
[ShortRunJob]
1918
public class PolylineEncoderBenchmark {
2019
[Params(1, 10, 100, 1_000, 10_000, 100_000, 1_000_000)]
2120
public int N;

benchmarks/PolylineAlgorithm.Benchmarks/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ internal class Program {
1616
/// </summary>
1717
/// <param name="args">The command-line arguments.</param>
1818
static void Main(string[] args) {
19-
Directory.Delete("C:\\temp\\benchmark", true);
19+
//Directory.Delete("C:\\temp\\benchmark", true);
2020

2121
BenchmarkSwitcher
2222
.FromAssembly(typeof(Program).Assembly)
23-
.Run(args,/* new DebugInProcessConfig()*/);
23+
.Run(args/*, new DebugInProcessConfig()*/);
2424
}
2525
}

src/PolylineAlgorithm.IO.Pipelines/Internal/Defaults.cs

Lines changed: 0 additions & 109 deletions
This file was deleted.

src/PolylineAlgorithm.IO.Pipelines/PolylineDecoder.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public async Task DecodeAsync(PipeReader reader, PipeWriter writer, Cancellation
5656
position = Process(result, ref longitude, ref temp, ref buffer);
5757
reader.AdvanceTo(position);
5858

59+
await Formatter
60+
.TryWriteAsync(writer, new Coordinate(latitude, longitude), cancellation)
61+
.ConfigureAwait(false);
62+
5963
if (result.IsCompleted) {
6064
break;
6165
}
@@ -64,11 +68,11 @@ public async Task DecodeAsync(PipeReader reader, PipeWriter writer, Cancellation
6468
await CompleteAsync(reader, writer)
6569
.ConfigureAwait(false);
6670

67-
static SequencePosition Process(ReadResult result, ref int latitude, ref Memory<byte> temp, ref Memory<char> buffer) {
71+
static SequencePosition Process(ReadResult result, ref int value, ref Memory<byte> temp, ref Memory<char> buffer) {
6872
result.Buffer.Slice(0, 6).CopyTo(temp.Span);
6973
Encoding.UTF8.GetChars(temp.Span, buffer.Span);
7074

71-
long consumed = Decode(buffer.Span, ref latitude);
75+
long consumed = PolylineEncoding.Default.GetNextValue(buffer.Span, ref value);
7276

7377
return result.Buffer.GetPosition(consumed);
7478
}

src/PolylineAlgorithm.IO.Pipelines/PolylineEncoder.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace PolylineAlgorithm.IO.Pipelines;
77

88
using PolylineAlgorithm.Internal;
9-
using PolylineAlgorithm.IO.Pipelines.Internal;
109
using System;
1110
using System.IO.Pipelines;
1211
using System.Text;
@@ -49,7 +48,7 @@ public async Task EncodeAsync(PipeReader reader, PipeWriter writer, Cancellation
4948
static void Process(PipeWriter writer, int value, Memory<char> buffer) {
5049
Span<char> temp = buffer.Span;
5150

52-
int length = Encode(ref temp, value);
51+
int length = PolylineEncoding.Default.GetChars(value, ref temp);
5352

5453
Encoding.UTF8.GetBytes(temp[..length], writer.GetSpan(length));
5554

@@ -66,24 +65,6 @@ await writer
6665
}
6766
}
6867

69-
private static int Encode(ref Span<char> buffer, int difference) {
70-
int index = 0;
71-
int rem = difference << 1;
72-
73-
if (difference < 0) {
74-
rem = ~rem;
75-
}
76-
77-
while (rem >= Defaults.Algorithm.Space) {
78-
buffer[index++] = Convert.ToChar((Defaults.Algorithm.Space | rem & Defaults.Algorithm.UnitSeparator) + Defaults.Algorithm.QuestionMark);
79-
rem >>= Defaults.Algorithm.ShiftLength;
80-
}
81-
82-
buffer[index++] = Convert.ToChar(rem + Defaults.Algorithm.QuestionMark);
83-
84-
return index;
85-
}
86-
8768
public static int GetRequiredCharCount(int difference) => difference switch {
8869
// DO NOT CHANGE THE ORDER. We are skipping inside exclusive ranges as those are covered by previous statements.
8970
>= -16 and <= +15 => 1,

src/PolylineAlgorithm/Polyline.cs

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,27 @@
66
namespace PolylineAlgorithm;
77

88
using System;
9+
using System.Buffers;
910
using System.Collections.Generic;
1011
using System.Diagnostics;
1112
using System.Diagnostics.CodeAnalysis;
13+
using System.Globalization;
1214
using System.Runtime.InteropServices;
15+
using System.Text;
1316

1417
/// <summary>
1518
/// Represents a readonly encoded polyline string.
1619
/// </summary>
1720
[StructLayout(LayoutKind.Auto)]
1821
[DebuggerDisplay("Value: {ToString()}, IsEmpty: {IsEmpty}, Length: {Length}")]
1922
public readonly struct Polyline : IEquatable<Polyline> {
20-
private readonly ReadOnlyMemory<char> _value;
23+
private readonly ReadOnlySequence<char> _value;
2124

2225
/// <summary>
2326
/// Creates a new <see cref="Polyline"/> structure that is empty.
2427
/// </summary>
2528
public Polyline() {
26-
_value = ReadOnlyMemory<char>.Empty;
29+
_value = ReadOnlySequence<char>.Empty;
2730
}
2831

2932
/// <summary>
@@ -36,7 +39,7 @@ public Polyline(string value) {
3639
throw new ArgumentNullException(nameof(value));
3740
}
3841

39-
_value = value.AsMemory();
42+
_value = new ReadOnlySequence<char>(value.AsMemory());
4043
}
4144

4245
/// <summary>
@@ -49,21 +52,21 @@ public Polyline(char[] value) {
4952
throw new ArgumentNullException(nameof(value));
5053
}
5154

52-
_value = _value = value.AsMemory();
55+
_value = new ReadOnlySequence<char>(value.AsMemory());
5356
}
5457

5558
/// <summary>
5659
/// Creates a new <see cref="Polyline"/> structure that contains the specified readonly memory region.
5760
/// </summary>
5861
/// <param name="value">The readonly memory region to initialize the polyline with.</param>
5962
public Polyline(ReadOnlyMemory<char> value) {
60-
_value = value;
63+
_value = new ReadOnlySequence<char>(value);
6164
}
6265

6366
/// <summary>
6467
/// Gets the span of characters in the polyline.
6568
/// </summary>
66-
internal readonly ReadOnlyMemory<char> Value => _value;
69+
internal readonly ReadOnlySequence<char> Value => _value;
6770

6871
/// <summary>
6972
/// Gets a value indicating whether this <see cref="Polyline" /> is empty.
@@ -79,24 +82,57 @@ public Polyline(ReadOnlyMemory<char> value) {
7982
/// Copies the characters in this instance to a Unicode character array.
8083
/// </summary>
8184
/// <returns>A Unicode character array.</returns>
82-
public char[] ToCharArray() => Value.ToArray();
85+
public char[] CopyTo(char[] destination) {
86+
if (destination is null) {
87+
throw new ArgumentNullException(nameof(destination));
88+
}
8389

84-
/// <summary>
85-
/// Returns a string representation of the value of this instance.
86-
/// </summary>
87-
/// <returns>The string value of this <see cref="Polyline"/> object.</returns>
88-
public override string ToString() => Value.ToString();
90+
if(Length != destination.Length) {
91+
throw new ArgumentException("", paramName: nameof(destination));
92+
}
93+
94+
_value.CopyTo(destination);
8995

90-
public ReadOnlyMemory<char> AsMemory() => Value;
96+
return destination;
97+
}
98+
99+
public ReadOnlySequence<char> AsSequence() => Value;
100+
101+
public override string ToString() {
102+
if (IsEmpty) {
103+
return string.Empty;
104+
}
91105

92-
public Polyline Append(ReadOnlyMemory<char> value) {
93-
if (value.IsEmpty) {
94-
return this;
106+
if (Value.IsSingleSegment) {
107+
return Value.FirstSpan.ToString();
108+
}
109+
110+
var sb = Value.Length <= int.MaxValue ? new StringBuilder(Convert.ToInt32(Value.Length)) : new StringBuilder();
111+
var enumerator = Value.GetEnumerator();
112+
113+
while (true) {
114+
if (!enumerator.MoveNext()) {
115+
break;
116+
}
117+
118+
if (enumerator.Current.IsEmpty) {
119+
continue;
120+
}
121+
122+
sb.Append(enumerator.Current);
123+
}
124+
125+
return sb.ToString();
126+
}
127+
128+
public bool Equals(Polyline other) {
129+
if ((IsEmpty != other.IsEmpty) || (Length != other.Length)) {
130+
return false;
95131
}
96132

97133
long position = 0;
98134
var enumerator = Value.GetEnumerator();
99-
var right = new SequenceReader<byte>(other.Value);
135+
var right = new SequenceReader<char>(other.Value);
100136

101137
while (true) {
102138
if (!enumerator.MoveNext()) {
@@ -117,6 +153,22 @@ public Polyline Append(ReadOnlyMemory<char> value) {
117153
return position == right.Length;
118154
}
119155

156+
public override bool Equals(object obj) {
157+
return obj is Polyline other && Equals(other);
158+
}
159+
160+
public override int GetHashCode() {
161+
return Value.GetHashCode();
162+
}
163+
164+
public static bool operator ==(Polyline left, Polyline right) {
165+
return left.Equals(right);
166+
}
167+
168+
public static bool operator !=(Polyline left, Polyline right) {
169+
return !(left == right);
170+
}
171+
120172
#region Factory methods
121173

122174
/// <summary>
@@ -140,10 +192,6 @@ public Polyline Append(ReadOnlyMemory<char> value) {
140192
/// <returns>The <see cref="Polyline"/> value that corresponds to the specified string value.</returns>
141193
public static Polyline FromString(string polyline) => new(polyline);
142194

143-
internal void Append(IEnumerable<char> latitude, IEnumerable<char> longitude) {
144-
throw new NotImplementedException();
145-
}
146-
147195
#endregion
148196

149197
#region Explicit conversions

src/PolylineAlgorithm/PolylineAlgorithm.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
6666
<_Parameter1>PolylineAlgorithm.Tests</_Parameter1>
6767
</AssemblyAttribute>
68+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
69+
<_Parameter1>PolylineAlgorithm.IO.Pipelines</_Parameter1>
70+
</AssemblyAttribute>
6871
</ItemGroup>
6972

7073
<ItemGroup>

0 commit comments

Comments
 (0)