Skip to content

Commit a5ee525

Browse files
author
Petr Sramek
committed
itermediate commit
1 parent 03fc8eb commit a5ee525

File tree

11 files changed

+110
-224
lines changed

11 files changed

+110
-224
lines changed

PolylineAlgorithm.sln

-70
This file was deleted.

PolylineAlgorithm.slnx

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Solution>
2+
<Folder Name="/benchmarks/">
3+
<Project Path="benchmarks/PolylineAlgorithm.Benchmarks/PolylineAlgorithm.Benchmarks.csproj" />
4+
<Project Path="benchmarks/PolylineAlgorithm.Comparison.Benchmarks/PolylineAlgorithm.Comparison.Benchmarks.csproj" />
5+
</Folder>
6+
<Folder Name="/samples/" />
7+
<Folder Name="/src/">
8+
<Project Path="src/PolylineAlgorithm.Abstraction/PolylineAlgorithm.Abstraction.csproj" />
9+
<Project Path="src/PolylineAlgorithm.IO.Pipelines/PolylineAlgorithm.IO.Pipelines.csproj" />
10+
<Project Path="src/PolylineAlgorithm/PolylineAlgorithm.csproj" />
11+
</Folder>
12+
<Folder Name="/tests/">
13+
<Project Path="tests/PolylineAlgorithm.Tests/PolylineAlgorithm.Tests.csproj" />
14+
</Folder>
15+
</Solution>

benchmarks/PolylineAlgorithm.Benchmarks/PolylineBenchmark.cs

+29-29
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace PolylineAlgorithm.Benchmarks;
1212
using System.Text;
1313

1414
/// <summary>
15-
/// Benchmarks for the <see cref="Polyline"/> struct.
15+
/// Benchmarks for the <see cref="PolylineValue"/> struct.
1616
/// </summary>
1717
[RankColumn]
1818
public class PolylineBenchmark {
@@ -23,24 +23,24 @@ public class PolylineBenchmark {
2323

2424
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
2525
/// <summary>
26-
/// Gets the string value representing the encoded polyline.
26+
/// Gets the character array representing the encoded polyline.
2727
/// </summary>
28-
public string StringValue { get; private set; }
28+
public char[] CharArrayValue { get; private set; }
2929

3030
/// <summary>
31-
/// Gets the character array representing the encoded polyline.
31+
/// Gets the read-only memory representing the encoded polyline.
3232
/// </summary>
33-
public char[] CharArray { get; private set; }
33+
public ReadOnlyMemory<char> MemoryValue { get; private set; }
3434

3535
/// <summary>
3636
/// Gets the read-only memory representing the encoded polyline.
3737
/// </summary>
38-
public ReadOnlyMemory<char> Memory { get; private set; }
38+
public Polyline PolylineValue { get; private set; }
3939

4040
/// <summary>
41-
/// Gets the read-only memory representing the encoded polyline.
41+
/// Gets the string value representing the encoded polyline.
4242
/// </summary>
43-
public Polyline Polyline { get; private set; }
43+
public string StringValue { get; private set; }
4444

4545
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
4646

@@ -50,10 +50,10 @@ public class PolylineBenchmark {
5050
/// </summary>
5151
[GlobalSetup]
5252
public void SetupData() {
53-
Polyline = ValueProvider.GetPolyline(Length);
54-
StringValue = Polyline.ToString();
55-
CharArray = StringValue.ToArray();
56-
Memory = CharArray.AsMemory();
53+
PolylineValue = ValueProvider.GetPolyline(Length);
54+
StringValue = PolylineValue.ToString();
55+
CharArrayValue = StringValue.ToArray();
56+
MemoryValue = CharArrayValue.AsMemory();
5757
}
5858

5959
/// <summary>
@@ -75,7 +75,7 @@ public Polyline Polyline_FromString() {
7575
[Benchmark]
7676
public Polyline Polyline_FromCharArray() {
7777
var polyline = Polyline
78-
.FromCharArray(CharArray);
78+
.FromCharArray(CharArrayValue);
7979

8080
return polyline;
8181
}
@@ -87,7 +87,7 @@ public Polyline Polyline_FromCharArray() {
8787
[Benchmark]
8888
public Polyline Polyline_FromMemory() {
8989
var polyline = Polyline
90-
.FromMemory(Memory);
90+
.FromMemory(MemoryValue);
9191

9292
return polyline;
9393
}
@@ -98,23 +98,23 @@ public Polyline Polyline_FromMemory() {
9898
/// <returns>The encoded polyline.</returns>
9999
[Benchmark]
100100
public string Polyline_ToString() {
101-
var stringValue = Polyline
101+
var stringValue = PolylineValue
102102
.ToString();
103103

104104
return stringValue;
105105
}
106106

107-
/// <summary>
108-
/// Benchmarks the encoding of an enumeration of coordinates into a polyline.
109-
/// </summary>
110-
/// <returns>The encoded polyline.</returns>
111-
[Benchmark]
112-
public long Polyline_GetCoordinateCount() {
113-
var coordinateCount = Polyline
114-
.GetCoordinateCount();
107+
///// <summary>
108+
///// Benchmarks the encoding of an enumeration of coordinates into a polyline.
109+
///// </summary>
110+
///// <returns>The encoded polyline.</returns>
111+
//[Benchmark]
112+
//public long Polyline_GetCoordinateCount() {
113+
// var coordinateCount = PolylineValue
114+
// .GetCoordinateCount();
115115

116-
return coordinateCount;
117-
}
116+
// return coordinateCount;
117+
//}
118118

119119

120120
/// <summary>
@@ -123,9 +123,9 @@ public long Polyline_GetCoordinateCount() {
123123
/// <returns>The encoded polyline.</returns>
124124
[Benchmark]
125125
public void Polyline_CopyTo() {
126-
var destination = new char[Polyline.Length];
126+
var destination = new char[PolylineValue.Length];
127127

128-
Polyline
128+
PolylineValue
129129
.CopyTo(destination);
130130

131131
destination
@@ -138,8 +138,8 @@ public void Polyline_CopyTo() {
138138
/// <returns>The encoded polyline.</returns>
139139
[Benchmark]
140140
public bool Polyline_Equals() {
141-
var equals = Polyline
142-
.Equals(Polyline);
141+
var equals = PolylineValue
142+
.Equals(PolylineValue);
143143

144144
return equals;
145145
}

benchmarks/PolylineAlgorithm.Comparison.Benchmarks/PolylineEncoderBenchmark.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace PolylineAlgorithm.Comparison.Benchmarks;
1919
/// </summary>
2020
[RankColumn]
2121
public class PolylineEncoderBenchmark {
22-
[Params(1, 10, 100, 1_000, 10_000, 100_000, 1_000_000)]
22+
[Params(1, 10, 100, 250, 500, 1_000, 2_500, 5_000, 7_500, 10_000, 15_000, 20_000, 25_000, 50_000, 75_000, 100_000, 250_000, 500_000, 750_000, 1_000_000)]
2323
public int N;
2424

2525
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.

src/PolylineAlgorithm.IO.Pipelines/PolylineEncoderPipe.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@ public async Task EncodeAsync(PipeReader reader, PipeWriter writer, Cancellation
3636
throw new ArgumentNullException(nameof(writer));
3737
}
3838

39-
CoordinateVariance variance;
40-
Coordinate current = Coordinate.Default;
39+
CoordinateVariance variance = new();
4140

42-
while (await Formatter.TryReadAsync(reader, out Coordinate next, cancellation).ConfigureAwait(false)) {
43-
variance = CoordinateVariance.Calculate(current, next);
41+
while (await Formatter.TryReadAsync(reader, out Coordinate coordinate, cancellation).ConfigureAwait(false)) {
42+
(int Latitude, int Longitude) next = (PolylineEncoding.Default.Normalize(coordinate.Latitude), PolylineEncoding.Default.Normalize(coordinate.Longitude));
43+
44+
variance
45+
.Next(next);
4446

4547
Process(writer, variance.Latitude, _buffer);
4648
Process(writer, variance.Longitude, _buffer);
4749

4850
await writer
4951
.FlushAsync(cancellation)
5052
.ConfigureAwait(false);
51-
52-
current = next;
5353
}
5454

5555
await CompleteAsync(reader, writer)

src/PolylineAlgorithm/Abstraction/IAsyncPolylineDecoder.cs

-20
This file was deleted.

src/PolylineAlgorithm/Abstraction/IAsyncPolylineEncoder.cs

-21
This file was deleted.

src/PolylineAlgorithm/Internal/CoordinateVariance.cs

+13-14
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,29 @@
22

33
using System;
44
using System.Diagnostics;
5+
using System.Runtime.InteropServices;
56

67
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
7-
public readonly ref struct CoordinateVariance {
8+
[StructLayout(LayoutKind.Sequential)]
9+
public struct CoordinateVariance {
10+
private (int Latitude, int Longitude) _current = (0, 0);
11+
812
private CoordinateVariance(int latitude, int longitude) {
913
Latitude = latitude;
1014
Longitude = longitude;
1115
}
1216

13-
public int Latitude { get; }
14-
public int Longitude { get; }
17+
public int Latitude { get; private set; }
1518

16-
public static CoordinateVariance Create(int latitude, int longitude) {
17-
return new CoordinateVariance(latitude, longitude);
18-
}
19+
public int Longitude { get; private set; }
1920

20-
public static CoordinateVariance Calculate(Coordinate initial, Coordinate next) {
21-
int latitude = Variance(Round(initial.Latitude), Round(next.Latitude));
22-
int longitude = Variance(Round(initial.Longitude), Round(next.Longitude));
21+
public void Next(in (int Latitude, int Longitude) next) {
22+
Latitude = Variance(_current.Latitude, next.Latitude);
23+
Longitude = Variance(_current.Longitude, next.Longitude);
2324

24-
return new CoordinateVariance(latitude, longitude);
25+
_current = next;
2526
}
2627

27-
public override readonly string ToString()
28-
=> $"Variance: {{ Latitude: {Latitude}, Longitude: {Longitude} }}";
29-
3028
private static int Variance(int initial, int next) => (initial, next) switch {
3129
(0, 0) => 0,
3230
(0, _) => next,
@@ -37,5 +35,6 @@ public override readonly string ToString()
3735
( > 0, > 0) => next - initial,
3836
};
3937

40-
private static int Round(double value) => (int)Math.Round(value * Defaults.Algorithm.Precision);
38+
public override readonly string ToString()
39+
=> $"Variance: {{ Latitude: {Latitude}, Longitude: {Longitude} }}";
4140
}

0 commit comments

Comments
 (0)