Skip to content

Commit 3b15130

Browse files
Updates to Network.cs.
1 parent 1735e37 commit 3b15130

1 file changed

Lines changed: 27 additions & 18 deletions

File tree

  • 4.8.1/Numerics/Mathematics/Optimization/Dynamic

4.8.1/Numerics/Mathematics/Optimization/Dynamic/Network.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,46 +41,55 @@ namespace Numerics.Mathematics.Optimization
4141
/// </summary>
4242
public class Network
4343
{
44-
private readonly List<Edge>[] _outgoingEdges;
45-
private readonly List<Edge>[] _incomingEdges;
44+
private readonly Dictionary<int, List<Edge>> _outgoingEdges;
45+
private readonly Dictionary<int, List<Edge>> _incomingEdges;
4646
private readonly int _nodeCount;
4747
private readonly int[] _destinationIndices;
48-
//private readonly RoadSegment[] _segments;
4948
private readonly Edge[] _edges;
5049

51-
//public RoadSegment[] Segments { get => _segments; }
5250
public int[] DestinationIndices { get => _destinationIndices; }
51+
public Dictionary<int,List<Edge>> IncomingEdges { get => _incomingEdges; }
52+
public Dictionary<int, List<Edge>> OutgoingEdges { get => _outgoingEdges; }
5353

54-
public List<Edge>[] IncomingEdges { get => _incomingEdges; }
55-
public List<Edge>[] OutgoingEdges { get => _outgoingEdges; }
56-
57-
public Network(Edge[] edges, int[] destinationIndices)
54+
public Network(Edge[] edges, int[] destinationIndices, int nodeCount = -1)
5855
{
59-
//_segments = roadSegments;
60-
_edges = edges;//new Edge[edges.Length];
56+
_edges = edges;
6157
_destinationIndices = destinationIndices;
62-
_nodeCount = 0;
58+
59+
60+
if (nodeCount != -1)
61+
{
62+
_nodeCount = nodeCount;
63+
}
64+
else
65+
{
66+
_nodeCount = 0;
67+
// Calculate the number of unique nodes
68+
}
69+
6370

6471
//for (int i = 0; i < roadSegments.Length; i++)
6572
//{
6673
// _edges[i] = new Edge(roadSegments[i].HeadIndex, roadSegments[i].TailIndex, (float)roadSegments[i].Source.TravelTimeSeconds, i);
6774
// if (_edges[i].FromIndex > _nodeCount) { _nodeCount = _edges[i].FromIndex; }
6875
// if (_edges[i].ToIndex > _nodeCount) { _nodeCount = _edges[i].ToIndex; }
6976
//}
70-
var max1 = edges.Select(x=>x.FromIndex).Max();
71-
var max2 = edges.Select(x => x.ToIndex).Max();
72-
var max = Math.Max(max1, max2);
77+
//var max1 = edges.Select(x=>x.FromIndex).Max();
78+
//var max2 = edges.Select(x => x.ToIndex).Max();
79+
//var max = Math.Max(max1, max2);
7380
//// Add one to the count for the index offset.
7481
//_nodeCount += 1;
75-
_incomingEdges = new List<Edge>[max];
76-
_outgoingEdges = new List<Edge>[max];
82+
_incomingEdges = new Dictionary<int, List<Edge>>();
83+
_outgoingEdges = new Dictionary<int, List<Edge>>();
7784
//
7885
for (int i = 0; i < _edges.Length; i++)
7986
{
80-
if (_incomingEdges[_edges[i].ToIndex] == null) { _incomingEdges[_edges[i].ToIndex] = new List<Edge>(); }
87+
if(_incomingEdges.ContainsKey(_edges[i].ToIndex)==false) { _incomingEdges.Add(_edges[i].ToIndex, new List<Edge>()); }
88+
//if (_incomingEdges[_edges[i].ToIndex] == null) { _incomingEdges[_edges[i].ToIndex] = new List<Edge>(); }
8189
_incomingEdges[_edges[i].ToIndex].Add(_edges[i]);
8290

83-
if (_outgoingEdges[_edges[i].FromIndex] == null) { _outgoingEdges[_edges[i].FromIndex] = new List<Edge>(); }
91+
if (_outgoingEdges.ContainsKey(_edges[i].FromIndex) == false) { _outgoingEdges.Add(_edges[i].FromIndex, new List<Edge>()); }
92+
//if (_outgoingEdges[_edges[i].FromIndex] == null) { _outgoingEdges[_edges[i].FromIndex] = new List<Edge>(); }
8493
_outgoingEdges[_edges[i].FromIndex].Add(_edges[i]);
8594
}
8695

0 commit comments

Comments
 (0)