1
1
using System ;
2
+ using System . Collections . Generic ;
3
+ using Dijkstra . NET . PageRank ;
4
+ using Dijkstra . NET . ShortestPath ;
2
5
3
6
namespace Dijkstra . NET . Graph
4
7
{
5
- public class Node < T , TEdgeCustom > : INode < T , TEdgeCustom > where TEdgeCustom : IEquatable < TEdgeCustom >
8
+ public class Node < T , TEdgeCustom > : IPageRank , IDijkstra , INode < T , TEdgeCustom > where TEdgeCustom : IEquatable < TEdgeCustom >
6
9
{
7
- private Edge < T , TEdgeCustom > [ ] _children ;
10
+ private readonly HashSet < Node < T , TEdgeCustom > > _parents = new HashSet < Node < T , TEdgeCustom > > ( ) ;
11
+ private readonly HashSet < uint > _children = new HashSet < uint > ( ) ;
12
+ private Edge < T , TEdgeCustom > [ ] _edges ;
8
13
9
14
public Node ( uint key , T item )
10
15
{
11
16
Key = key ;
12
17
Item = item ;
13
- _children = new Edge < T , TEdgeCustom > [ 5 ] ;
18
+ _edges = new Edge < T , TEdgeCustom > [ 5 ] ;
14
19
}
15
20
16
21
public uint Key { get ; }
17
22
18
23
public T Item { get ; }
19
24
20
- public int ChildrenCount { get ; internal set ; }
25
+ public int EdgesCount { get ; internal set ; }
26
+
27
+ public int NumberOfEdges => _children . Count ;
21
28
22
- internal void AddChild ( in Edge < T , TEdgeCustom > edge )
29
+ public IEnumerable < IPageRank > Parents => _parents ;
30
+
31
+ public void EachEdge ( Edge edge )
23
32
{
24
- if ( _children . Length == ChildrenCount )
33
+ for ( int i = 0 ; i < EdgesCount ; i ++ )
25
34
{
26
- int newSize = _children . Length ;
35
+ ref Edge < T , TEdgeCustom > e = ref _edges [ i ] ;
27
36
28
- if ( ChildrenCount < NodeConstants . MaxSize )
37
+ edge ( e . Node . Key , e . Cost ) ;
38
+ }
39
+ }
40
+
41
+ internal void AddEdge ( in Edge < T , TEdgeCustom > edge )
42
+ {
43
+ if ( _edges . Length == EdgesCount )
44
+ {
45
+ int newSize = _edges . Length ;
46
+
47
+ if ( EdgesCount < NodeConstants . MaxSize )
29
48
{
30
49
newSize *= 2 ;
31
50
}
@@ -36,21 +55,34 @@ internal void AddChild(in Edge<T, TEdgeCustom> edge)
36
55
newSize = bigSize < Int32 . MaxValue ? ( int ) bigSize : Int32 . MaxValue ;
37
56
}
38
57
39
- Array . Resize ( ref _children , newSize ) ;
58
+ Array . Resize ( ref _edges , newSize ) ;
40
59
}
41
60
42
- _children [ ChildrenCount ] = edge ;
43
- ChildrenCount ++ ;
61
+ _edges [ EdgesCount ] = edge ;
62
+ EdgesCount ++ ;
63
+ _children . Add ( edge . Node . Key ) ;
44
64
}
45
65
46
- public void EachChild ( ChildAction < T , TEdgeCustom > action )
66
+ internal void AddParent ( Node < T , TEdgeCustom > parent )
47
67
{
48
- for ( int i = 0 ; i < ChildrenCount ; i ++ )
49
- {
50
- ref Edge < T , TEdgeCustom > e = ref _children [ i ] ;
68
+ _parents . Add ( parent ) ;
69
+ }
51
70
52
- action ( in e ) ;
53
- }
71
+ public override int GetHashCode ( )
72
+ {
73
+ return Key . GetHashCode ( ) ;
74
+ }
75
+
76
+ public override bool Equals ( object obj )
77
+ {
78
+ var node = obj as INode ;
79
+
80
+ return node ? . Key == Key ;
81
+ }
82
+
83
+ public override string ToString ( )
84
+ {
85
+ return $ "[{ Key } ({ Item ? . ToString ( ) } )]";
54
86
}
55
87
}
56
88
}
0 commit comments