using System.Collections; using System.Collections.Generic; using System; namespace Rokojori { public class Graph:GraphWalker where N:class { List _nodes = []; public Graph( List nodes ) { _nodes = nodes; } List> _connections = []; MapList _fromConnections = new MapList(); MapList _toConnections = new MapList(); public void Connect( N from, N to ) { var c = new GraphConnection(); c.from = from; c.to = to; _connections.Add( c ); _toConnections.Add( from, to ); _fromConnections.Add( to, from ); } public override int NumConnectedTo( N n ) { if ( ! _toConnections.ContainsKey( n ) ) { return 0; } return _toConnections[ n ].Count; } public override N GetConnectedTo( N n, int index ) { if ( ! _toConnections.ContainsKey( n ) ) { return null; } return _toConnections[ n ][ index ] ; } public override int NumConnectedFrom( N n ) { if ( ! _fromConnections.ContainsKey( n ) ) { return 0; } return _fromConnections[ n ].Count; } public override N GetConnectedFrom( N n, int index ) { if ( ! _fromConnections.ContainsKey( n ) ) { return null; } return _fromConnections[ n ][ index ] ; } public List ComputeOrder() { return ComputeOrder( _nodes ); } } }