Skip to content

Commit a63ef7a

Browse files
committed
refactor
1 parent 983d45d commit a63ef7a

File tree

7 files changed

+46
-26
lines changed

7 files changed

+46
-26
lines changed

examples/Sandwych.MapMatchingKit.Examples.HelloWorldApp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static void Main(string[] args)
5858
var heading = cand.Point.Edge.Headeing; // heading
5959
var coord = cand.Point.Coordinate; // GPS position (on the road)
6060
Console.WriteLine("RoadID={0}\t\tFraction={1}", roadId, cand.Point.Fraction);
61-
if (cand.Transition != null)
61+
if (cand.HasTransition)
6262
{
6363
var geom = cand.Transition.Route.ToGeometry(); // path geometry from last matching candidate
6464
Console.WriteLine("fuck");

src/Sandwych.MapMatchingKit/Markov/AbstractStateCandidate.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,41 @@ public abstract class AbstractStateCandidate<TCandidate, TTransition, TSample> :
88
IStateCandidate<TCandidate, TTransition, TSample>
99
where TCandidate : IStateCandidate<TCandidate, TTransition, TSample>
1010
{
11+
private TTransition _transition;
12+
1113
public double Seqprob { get; set; }
1214
public double Filtprob { get; set; }
13-
public TTransition Transition { get; set; }
1415
public TCandidate Predecessor { get; set; }
16+
public bool HasTransition { get; private set; }
17+
18+
public TTransition Transition
19+
{
20+
get
21+
{
22+
if (!this.HasTransition)
23+
{
24+
throw new InvalidOperationException();
25+
}
26+
return _transition;
27+
}
28+
set
29+
{
30+
_transition = value;
31+
this.HasTransition = true;
32+
}
33+
}
1534

1635
public AbstractStateCandidate()
1736
{
1837
}
1938

20-
public abstract bool Equals(TCandidate other);
39+
public virtual bool Equals(TCandidate other)
40+
{
41+
if (object.ReferenceEquals(this, other))
42+
{
43+
return true;
44+
}
45+
return Predecessor.Equals(other.Predecessor) && Transition.Equals(other.Transition);
46+
}
2147
}
2248
}

src/Sandwych.MapMatchingKit/Markov/IStateCandidate.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ public interface IStateCandidate<TCandidate, TTransition, TSample> : IEquatable<
1111
double Filtprob { get; set; }
1212
TCandidate Predecessor { get; set; }
1313
TTransition Transition { get; set; }
14+
bool HasTransition { get; }
1415
}
1516
}

src/Sandwych.MapMatchingKit/Matching/MatcherCandidate.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,26 @@
66

77
namespace Sandwych.MapMatchingKit.Matching
88
{
9-
public sealed class MatcherCandidate : IStateCandidate<MatcherCandidate, MatcherTransition, MatcherSample>
9+
public sealed class MatcherCandidate : AbstractStateCandidate<MatcherCandidate, MatcherTransition, MatcherSample>
1010
{
1111
private readonly RoadPoint _point;
1212

1313
public ref readonly RoadPoint Point => ref _point;
1414

15-
public double Seqprob { get; set; }
16-
public double Filtprob { get; set; }
17-
public MatcherCandidate Predecessor { get; set; }
18-
public MatcherTransition Transition { get; set; }
19-
20-
public MatcherCandidate(in RoadPoint point)
15+
public MatcherCandidate(in RoadPoint point) : base()
2116
{
2217
this._point = point;
2318
}
2419

25-
public override int GetHashCode() =>
26-
(this.Point, this.Predecessor, this.Transition, this.Filtprob, this.Seqprob).GetHashCode();
20+
public override int GetHashCode() => this.Point.GetHashCode();
2721

28-
public bool Equals(MatcherCandidate other)
22+
public override bool Equals(MatcherCandidate other)
2923
{
3024
if (object.ReferenceEquals(this, other))
3125
{
3226
return true;
3327
}
34-
35-
return this.Point.Equals(other.Point) && this.Predecessor.Equals(other.Predecessor) && this.Transition.Equals(other.Transition);
28+
return Point.Equals(other.Point) && Predecessor.Equals(other.Predecessor) && Transition.Equals(other.Transition);
3629
}
3730
}
3831
}

src/Sandwych.MapMatchingKit/Matching/MatcherTransition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Sandwych.MapMatchingKit.Matching
77
{
8-
public class MatcherTransition
8+
public readonly struct MatcherTransition
99
{
1010
public Route Route { get; }
1111

test/Sandwych.MapMatchingKit.Tests/Markov/FilterTest.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Sandwych.MapMatchingKit.Tests.Markov
88
{
99
public class FilterTest
1010
{
11-
public class MockStateTransition
11+
public struct MockStateTransition
1212
{
1313
}
1414

@@ -184,12 +184,12 @@ public void FilterTestInitial()
184184
if (states.Pred(element.Id) == -1)
185185
{
186186
Assert.Null(element.Predecessor);
187-
Assert.Null(element.Transition);
187+
Assert.False(element.HasTransition);
188188
}
189189
else
190190
{
191191
Assert.Equal(states.Pred(element.Id), element.Predecessor.Id);
192-
Assert.Null(element.Transition);
192+
Assert.False(element.HasTransition);
193193
}
194194
}
195195
}
@@ -215,12 +215,12 @@ public void FilterTestSubsequent()
215215
if (states.Pred(element.Id) == -1)
216216
{
217217
Assert.Null(element.Predecessor);
218-
Assert.Null(element.Transition);
218+
Assert.False(element.HasTransition);
219219
}
220220
else
221221
{
222222
Assert.Equal(states.Pred(element.Id), element.Predecessor.Id);
223-
Assert.NotNull(element.Transition);
223+
Assert.True(element.HasTransition);
224224
}
225225
}
226226
}
@@ -246,12 +246,12 @@ public void FilterTestBreakTransition()
246246
if (states.Pred(element.Id) == -1)
247247
{
248248
Assert.Null(element.Predecessor);
249-
Assert.Null(element.Transition);
249+
Assert.False(element.HasTransition);
250250
}
251251
else
252252
{
253253
Assert.Equal(states.Pred(element.Id), element.Predecessor.Id);
254-
Assert.NotNull(element.Transition);
254+
Assert.True(element.HasTransition);
255255
}
256256
}
257257
}
@@ -277,12 +277,12 @@ public void FilterTestBreakCandidates()
277277
if (states.Pred(element.Id) == -1)
278278
{
279279
Assert.Null(element.Predecessor);
280-
Assert.Null(element.Transition);
280+
Assert.False(element.HasTransition);
281281
}
282282
else
283283
{
284284
Assert.Equal(states.Pred(element.Id), element.Predecessor.Id);
285-
Assert.NotNull(element.Transition);
285+
Assert.True(element.HasTransition);
286286
}
287287
}
288288
}

test/Sandwych.MapMatchingKit.Tests/Markov/KStateTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Sandwych.MapMatchingKit.Tests.Markov
99
{
1010
public class KStateTest
1111
{
12-
public class MockStateTransition
12+
public struct MockStateTransition
1313
{
1414
}
1515

0 commit comments

Comments
 (0)