Skip to content

Commit 689fd7d

Browse files
committed
code refactoring
1 parent 4aa7714 commit 689fd7d

File tree

9 files changed

+27
-154
lines changed

9 files changed

+27
-154
lines changed

src/Dijkstra.NET/Dijkstra.NET/Contract/IConcurrentGraph.cs

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

src/Dijkstra.NET/Dijkstra.NET/Contract/IConcurrentNode.cs

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

src/Dijkstra.NET/Dijkstra.NET/Dijkstra.NET.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,10 @@
5858
<Reference Include="System.Xml" />
5959
</ItemGroup>
6060
<ItemGroup>
61-
<Compile Include="Contract\IConcurrentGraph.cs" />
62-
<Compile Include="Contract\IConcurrentNode.cs" />
6361
<Compile Include="Contract\IGraph.cs" />
6462
<Compile Include="Contract\INode.cs" />
6563
<Compile Include="Contract\IShortestPathResult.cs" />
66-
<Compile Include="Model\DijkstraConcurrentResult.cs" />
64+
<Compile Include="Model\BfsConcurrentResult.cs" />
6765
<Compile Include="ShortestPath\Dijkstra.cs" />
6866
<Compile Include="Extensions\SortedSetExtensions.cs" />
6967
<Compile Include="Model\DijkstraResult.cs" />

src/Dijkstra.NET/Dijkstra.NET/Model/DijkstraConcurrentResult.cs renamed to src/Dijkstra.NET/Dijkstra.NET/Model/BfsConcurrentResult.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
using System.Collections.Concurrent;
44
using System.Collections.Generic;
55

6-
public class DijkstraConcurrentResult: DijkstraResult
6+
public class BfsConcurrentResult: DijkstraResult
77
{
88
private readonly ConcurrentDictionary<uint, uint> _path = new ConcurrentDictionary<uint, uint>();
99

10-
public DijkstraConcurrentResult(uint @from, uint to) : base(@from, to)
10+
public BfsConcurrentResult(uint @from, uint to) : base(@from, to)
1111
{
1212
}
1313

1414
public override IDictionary<uint, uint> Path => _path;
15-
internal ConcurrentDictionary<uint, uint> P => _path;
1615
}
1716
}

src/Dijkstra.NET/Dijkstra.NET/Model/Graph.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
using System.Linq;
77
using Contract;
88

9-
public class Graph<T, TEdgeCustom>: IConcurrentGraph<T, TEdgeCustom>, IEnumerable<INode<T, TEdgeCustom>>, ICloneable where TEdgeCustom : IEquatable<TEdgeCustom>
9+
public class Graph<T, TEdgeCustom>: IGraph<T, TEdgeCustom>, IEnumerable<INode<T, TEdgeCustom>>, ICloneable where TEdgeCustom : IEquatable<TEdgeCustom>
1010
{
11-
private readonly IDictionary<uint, IConcurrentNode<T, TEdgeCustom>> _nodes = new Dictionary<uint, IConcurrentNode<T, TEdgeCustom>>();
11+
private readonly IDictionary<uint, INode<T, TEdgeCustom>> _nodes = new Dictionary<uint, INode<T, TEdgeCustom>>();
1212

1313
public void AddNode(T item)
1414
{
@@ -52,7 +52,6 @@ public void Reset()
5252
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
5353

5454
public INode<T, TEdgeCustom> this[uint node] => _nodes[node];
55-
public IConcurrentNode<T, TEdgeCustom> GetConccurentNode(uint node) => _nodes[node];
5655

5756
/// <summary>
5857
/// Deep copy of graph

src/Dijkstra.NET/Dijkstra.NET/Model/Node.cs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,19 @@
22
{
33
using System;
44
using System.Collections.Generic;
5-
using System.Threading;
65
using Contract;
7-
public class Node<T, TEdgeCustom>: IConcurrentNode<T, TEdgeCustom> where TEdgeCustom: IEquatable<TEdgeCustom>
6+
public class Node<T, TEdgeCustom>: INode<T, TEdgeCustom> where TEdgeCustom: IEquatable<TEdgeCustom>
87
{
9-
private int _distance;
10-
118
public Node(uint key, T item)
129
{
1310
Key = key;
1411
Item = item;
15-
_distance = Int32.MaxValue;
12+
Distance = Int32.MaxValue;
1613
}
1714

1815
public IList<Edge<T, TEdgeCustom>> Children { get; } = new List<Edge<T, TEdgeCustom>>();
1916
public uint Key { get; }
2017
public T Item { get; }
21-
22-
public int Distance
23-
{
24-
get { return _distance; }
25-
set { _distance = value; }
26-
}
27-
28-
/// <summary>
29-
/// Thread safe add distance.
30-
/// </summary>
31-
/// <param name="distance">New value</param>
32-
/// <param name="comparator">Old value. If old value is equal then distance was replaced successfully.</param>
33-
/// <returns>If true distance was replaced successfully.</returns>
34-
public bool TrySetDistance(int distance, int comparator)
35-
{
36-
return Interlocked.CompareExchange(ref _distance, distance, comparator) == comparator;
37-
}
18+
public int Distance { get; set; }
3819
}
3920
}
Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,27 @@
11
namespace Dijkstra.NET.ShortestPath
22
{
33
using System;
4-
using System.Diagnostics;
5-
using System.Threading;
64
using Contract;
75
using Model;
86
using NET.Model;
97
using Utility;
108

11-
public class BfsParallel<T, TEdgeCustom> : Dijkstra<T, TEdgeCustom>, IDisposable where TEdgeCustom : IEquatable<TEdgeCustom>
9+
public class BfsParallel<T, TEdgeCustom> : Dijkstra<T, TEdgeCustom> where TEdgeCustom : IEquatable<TEdgeCustom>
1210
{
1311
private readonly ProducerConsumer<T, TEdgeCustom> _table;
14-
private readonly IConcurrentGraph<T, TEdgeCustom> _graph;
1512

16-
private DijkstraConcurrentResult _result;
13+
private BfsConcurrentResult _result;
1714

18-
private bool _disposed;
19-
20-
public BfsParallel(IConcurrentGraph<T, TEdgeCustom> graph) : base(graph)
15+
public BfsParallel(IGraph<T, TEdgeCustom> graph) : base(graph)
2116
{
22-
_graph = graph;
2317
_table = new ProducerConsumer<T, TEdgeCustom>();
2418
}
2519

26-
public BfsParallel(IConcurrentGraph<T, TEdgeCustom> graph, double guardInterval) : base(graph)
27-
{
28-
_graph = graph;
29-
_table = new ProducerConsumer<T, TEdgeCustom>(guardInterval);
30-
}
31-
3220
public override IShortestPathResult Process(uint @from, uint to)
3321
{
34-
_result = new DijkstraConcurrentResult(from, to);
22+
_result = new BfsConcurrentResult(from, to);
3523

36-
IConcurrentNode<T, TEdgeCustom> nodeFrom = _graph.GetConccurentNode(from);
24+
INode<T, TEdgeCustom> nodeFrom = Graph[from];
3725
nodeFrom.Distance = 0;
3826

3927
_table.Initialise = () => _table.Produce(nodeFrom);
@@ -52,32 +40,18 @@ public override IShortestPathResult Process(uint @from, uint to)
5240

5341
_table.Consuming = job =>
5442
{
55-
if (Reduce(job.From,_graph.GetConccurentNode(job.To), job.Distance))
56-
{
57-
Debug.WriteLineIf(job.To == 3, $"Update ({job.From})->({job.To}) [{job.Distance}]");
58-
//_result.P.AddOrUpdate(job.To, job.From, (u, u1) =>
59-
//{
60-
// return job.From;
61-
//});
62-
63-
_table.Produce(_graph.GetConccurentNode(job.To));
64-
}
43+
if (Reduce(job.From, Graph[job.To], job.Distance))
44+
_table.Produce(Graph[job.To]);
6545
};
6646

6747
_table.Work();
6848

69-
_result.Distance = _graph[to].Distance;
49+
_result.Distance = Graph[to].Distance;
7050

7151
return _result;
7252
}
73-
private object _locker = new object();
74-
private object _locker2 = new object();
75-
public void SetInsurance(int insurance)
76-
{
77-
_table.Insurance = insurance;
78-
}
7953

80-
private bool Reduce(uint from, IConcurrentNode<T, TEdgeCustom> to, int distance)
54+
private bool Reduce(uint from, INode<T, TEdgeCustom> to, int distance)
8155
{
8256
lock (to)
8357
{
@@ -92,21 +66,5 @@ private bool Reduce(uint from, IConcurrentNode<T, TEdgeCustom> to, int distance)
9266
return false;
9367
}
9468
}
95-
96-
public void Dispose()
97-
{
98-
if (_disposed)
99-
return;
100-
101-
Dispose(true);
102-
GC.SuppressFinalize(this);
103-
_disposed = true;
104-
}
105-
106-
protected virtual void Dispose(bool disposing)
107-
{
108-
if (disposing)
109-
_table.Dispose();
110-
}
11169
}
11270
}

src/Dijkstra.NET/Dijkstra.NET/ShortestPath/Dijkstra.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
public class Dijkstra<T, TEdgeCustom> where TEdgeCustom : IEquatable<TEdgeCustom>
1111
{
12-
private readonly IGraph<T, TEdgeCustom> _graph;
12+
protected readonly IGraph<T, TEdgeCustom> Graph;
1313

1414
public Dijkstra(IGraph<T, TEdgeCustom> graph)
1515
{
16-
_graph = graph;
16+
Graph = graph;
1717
}
1818

1919
/// <summary>
@@ -25,8 +25,8 @@ public Dijkstra(IGraph<T, TEdgeCustom> graph)
2525
public virtual IShortestPathResult Process(uint from, uint to)
2626
{
2727
var result = new DijkstraResult(from, to);
28-
_graph[from].Distance = 0;
29-
var q = new SortedSet<INode<T, TEdgeCustom>>(new[] {_graph[from]}, new NodeComparer<T, TEdgeCustom>());
28+
Graph[from].Distance = 0;
29+
var q = new SortedSet<INode<T, TEdgeCustom>>(new[] { Graph[from]}, new NodeComparer<T, TEdgeCustom>());
3030

3131
while (q.Count > 0)
3232
{

src/Dijkstra.NET/Dijkstra.NET/ShortestPath/Utility/ProducerConsumer.cs

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,22 @@
11
namespace Dijkstra.NET.ShortestPath.Utility
22
{
33
using System;
4-
using System.Diagnostics;
54
using System.Threading;
65
using System.Threading.Tasks;
76
using Contract;
87
using Model;
9-
using Timer = System.Timers.Timer;
108

11-
internal sealed class ProducerConsumer<T, TEdgeCustom> : IDisposable where TEdgeCustom : IEquatable<TEdgeCustom>
9+
internal sealed class ProducerConsumer<T, TEdgeCustom> where TEdgeCustom : IEquatable<TEdgeCustom>
1210
{
1311
private readonly Collection<Emitter> _table = new Collection<Emitter>();
14-
private readonly Timer _guardTimer;
15-
16-
private bool _isDisposed;
17-
private int _guardIsWorking;
1812

1913
private int _currentJobs;
2014

21-
private int _counter;
22-
23-
public ProducerConsumer(double guardInterval = 5)
24-
{
25-
_guardTimer = new Timer(guardInterval);
26-
27-
_guardTimer.Elapsed += (sender, args) =>
28-
{
29-
if (Interlocked.CompareExchange(ref _counter, Insurance, Insurance) == Insurance)
30-
{
31-
_guardTimer.Stop();
32-
Complete();
33-
}
34-
else if (IsNotWorking)
35-
Interlocked.Increment(ref _counter);
36-
else
37-
Interlocked.Exchange(ref _counter, 0);
38-
};
39-
}
40-
41-
public int Insurance { get; set; } = 20;
42-
43-
public Action<IConcurrentNode<T, TEdgeCustom>> Producing { get; set; }
15+
public Action<INode<T, TEdgeCustom>> Producing { get; set; }
4416
public Action<MapReduceJob> Consuming { get; set; }
4517
public Action Initialise { get; set; }
4618

47-
public void Produce(IConcurrentNode<T, TEdgeCustom> product)
19+
public void Produce(INode<T, TEdgeCustom> product)
4820
{
4921
Emit(new Emitter(product));
5022
}
@@ -84,36 +56,20 @@ public void Work()
8456

8557
private void NotifyGuard()
8658
{
87-
//if (Interlocked.Exchange(ref _guardIsWorking, 1) == 0)
88-
// _guardTimer.Start();
89-
//else
90-
// Interlocked.Exchange(ref _counter, 0);
91-
9259
if (IsNotWorking)
93-
{
9460
Complete();
95-
}
9661
}
9762

9863
private bool IsNotWorking => _table.Count == 0 && Interlocked.CompareExchange(ref _currentJobs, 0, 0) == 0;
9964

100-
public void Dispose()
101-
{
102-
if (!_isDisposed)
103-
{
104-
_guardTimer.Dispose();
105-
_isDisposed = true;
106-
}
107-
}
108-
10965
private class Emitter
11066
{
111-
private readonly IConcurrentNode<T, TEdgeCustom> _mapper;
67+
private readonly INode<T, TEdgeCustom> _mapper;
11268
private readonly MapReduceJob _reducer;
11369

11470
public bool IsMapper { get; private set; }
11571

116-
public Emitter(IConcurrentNode<T, TEdgeCustom> node)
72+
public Emitter(INode<T, TEdgeCustom> node)
11773
{
11874
_mapper = node;
11975
IsMapper = true;
@@ -124,7 +80,7 @@ public Emitter(MapReduceJob job)
12480
_reducer = job;
12581
}
12682

127-
public IConcurrentNode<T, TEdgeCustom> Mapper => _mapper;
83+
public INode<T, TEdgeCustom> Mapper => _mapper;
12884
public MapReduceJob Reducer => _reducer;
12985
}
13086
}

0 commit comments

Comments
 (0)