11using System ;
2+ using System . Collections . Generic ;
3+ using Dijkstra . NET . PageRank ;
4+ using Dijkstra . NET . ShortestPath ;
25
36namespace Dijkstra . NET . Graph
47{
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 >
69 {
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 ;
813
914 public Node ( uint key , T item )
1015 {
1116 Key = key ;
1217 Item = item ;
13- _children = new Edge < T , TEdgeCustom > [ 5 ] ;
18+ _edges = new Edge < T , TEdgeCustom > [ 5 ] ;
1419 }
1520
1621 public uint Key { get ; }
1722
1823 public T Item { get ; }
1924
20- public int ChildrenCount { get ; internal set ; }
25+ public int EdgesCount { get ; internal set ; }
26+
27+ public int NumberOfEdges => _children . Count ;
2128
22- internal void AddChild ( in Edge < T , TEdgeCustom > edge )
29+ public IEnumerable < IPageRank > Parents => _parents ;
30+
31+ public void EachEdge ( Edge edge )
2332 {
24- if ( _children . Length == ChildrenCount )
33+ for ( int i = 0 ; i < EdgesCount ; i ++ )
2534 {
26- int newSize = _children . Length ;
35+ ref Edge < T , TEdgeCustom > e = ref _edges [ i ] ;
2736
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 )
2948 {
3049 newSize *= 2 ;
3150 }
@@ -36,21 +55,34 @@ internal void AddChild(in Edge<T, TEdgeCustom> edge)
3655 newSize = bigSize < Int32 . MaxValue ? ( int ) bigSize : Int32 . MaxValue ;
3756 }
3857
39- Array . Resize ( ref _children , newSize ) ;
58+ Array . Resize ( ref _edges , newSize ) ;
4059 }
4160
42- _children [ ChildrenCount ] = edge ;
43- ChildrenCount ++ ;
61+ _edges [ EdgesCount ] = edge ;
62+ EdgesCount ++ ;
63+ _children . Add ( edge . Node . Key ) ;
4464 }
4565
46- public void EachChild ( ChildAction < T , TEdgeCustom > action )
66+ internal void AddParent ( Node < T , TEdgeCustom > parent )
4767 {
48- for ( int i = 0 ; i < ChildrenCount ; i ++ )
49- {
50- ref Edge < T , TEdgeCustom > e = ref _children [ i ] ;
68+ _parents . Add ( parent ) ;
69+ }
5170
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 ( ) } )]";
5486 }
5587 }
5688}
0 commit comments