PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/Graph#/GraphHideHelper.cs

#
C# | 492 lines | 390 code | 85 blank | 17 comment | 28 complexity | c8967ea87dccf85c9d7a5a144acdad6c MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using QuickGraph;
  5. namespace GraphSharp
  6. {
  7. internal class GraphHideHelper<TVertex, TEdge> : ISoftMutableGraph<TVertex, TEdge>
  8. where TEdge : IEdge<TVertex>
  9. {
  10. private readonly IMutableBidirectionalGraph<TVertex, TEdge> graph;
  11. #region Helper Types
  12. protected class HiddenCollection
  13. {
  14. public List<TVertex> hiddenVertices = new List<TVertex>();
  15. public List<TEdge> hiddenEdges = new List<TEdge>();
  16. }
  17. #endregion
  18. #region Properties, fields, events
  19. private readonly List<TVertex> hiddenVertices = new List<TVertex>();
  20. private readonly List<TEdge> hiddenEdges = new List<TEdge>();
  21. private readonly IDictionary<string, HiddenCollection> hiddenCollections = new Dictionary<string, HiddenCollection>();
  22. private readonly IDictionary<TVertex, List<TEdge>> hiddenEdgesOf = new Dictionary<TVertex, List<TEdge>>();
  23. public event EdgeAction<TVertex, TEdge> EdgeHidden;
  24. public event EdgeAction<TVertex, TEdge> EdgeUnhidden;
  25. public event VertexAction<TVertex> VertexHidden;
  26. public event VertexAction<TVertex> VertexUnhidden;
  27. #endregion
  28. public GraphHideHelper( IMutableBidirectionalGraph<TVertex, TEdge> managedGraph )
  29. {
  30. graph = managedGraph;
  31. }
  32. #region Event handlers, helper methods
  33. /// <summary>
  34. /// Returns every edge connected with the vertex <code>v</code>.
  35. /// </summary>
  36. /// <param name="v">The vertex.</param>
  37. /// <returns>Edges, adjacent to the vertex <code>v</code>.</returns>
  38. protected IEnumerable<TEdge> EdgesFor( TVertex v )
  39. {
  40. return graph.InEdges( v ).Concat( graph.OutEdges( v ) );
  41. }
  42. protected HiddenCollection GetHiddenCollection( string tag )
  43. {
  44. HiddenCollection h;
  45. if ( !hiddenCollections.TryGetValue( tag, out h ) )
  46. {
  47. h = new HiddenCollection();
  48. hiddenCollections[tag] = h;
  49. }
  50. return h;
  51. }
  52. protected void OnEdgeHidden( TEdge e )
  53. {
  54. if ( EdgeHidden != null )
  55. EdgeHidden( e );
  56. }
  57. protected void OnEdgeUnhidden( TEdge e )
  58. {
  59. if ( EdgeUnhidden != null )
  60. EdgeUnhidden( e );
  61. }
  62. protected void OnVertexHidden( TVertex v )
  63. {
  64. if ( VertexHidden != null )
  65. VertexHidden( v );
  66. }
  67. protected void OnVertexUnhidden( TVertex v )
  68. {
  69. if ( VertexUnhidden != null )
  70. VertexUnhidden( v );
  71. }
  72. #endregion
  73. #region ISoftMutableGraph<TVertex,TEdge> Members
  74. public IEnumerable<TVertex> HiddenVertices
  75. {
  76. get { return hiddenVertices; }
  77. }
  78. public IEnumerable<TEdge> HiddenEdges
  79. {
  80. get { return hiddenEdges; }
  81. }
  82. /// <summary>
  83. /// Hides the vertex <code>v</code>.
  84. /// </summary>
  85. /// <param name="v">The vertex to hide.</param>
  86. public bool HideVertex( TVertex v )
  87. {
  88. if ( graph.ContainsVertex( v ) && !hiddenVertices.Contains( v ) )
  89. {
  90. HideEdges( EdgesFor( v ) );
  91. //hide the vertex
  92. graph.RemoveVertex( v );
  93. hiddenVertices.Add( v );
  94. OnVertexHidden( v );
  95. return true;
  96. }
  97. return false;
  98. }
  99. /// <summary>
  100. /// Hides a lot of vertices.
  101. /// </summary>
  102. /// <param name="vertices">The vertices to hide.</param>
  103. public void HideVertices( IEnumerable<TVertex> vertices )
  104. {
  105. var verticesToHide = new List<TVertex>( vertices );
  106. foreach ( TVertex v in verticesToHide )
  107. {
  108. HideVertex( v );
  109. }
  110. }
  111. public bool HideVertex( TVertex v, string tag )
  112. {
  113. HiddenCollection h = GetHiddenCollection( tag );
  114. var eeh = new EdgeAction<TVertex, TEdge>( e => h.hiddenEdges.Add( e ) );
  115. var veh = new VertexAction<TVertex>( vertex => h.hiddenVertices.Add( vertex ) );
  116. EdgeHidden += eeh;
  117. VertexHidden += veh;
  118. bool ret = HideVertex( v );
  119. EdgeHidden -= eeh;
  120. VertexHidden -= veh;
  121. return ret;
  122. }
  123. public void HideVertices( IEnumerable<TVertex> vertices, string tag )
  124. {
  125. foreach ( TVertex v in vertices )
  126. {
  127. HideVertex( v, tag );
  128. }
  129. }
  130. public void HideVerticesIf( Func<TVertex, bool> predicate, string tag )
  131. {
  132. var verticesToHide = new List<TVertex>();
  133. foreach ( var v in graph.Vertices )
  134. {
  135. if ( predicate( v ) )
  136. verticesToHide.Add( v );
  137. }
  138. HideVertices( verticesToHide, tag );
  139. }
  140. public bool IsHiddenVertex( TVertex v )
  141. {
  142. return ( !graph.ContainsVertex( v ) && hiddenVertices.Contains( v ) );
  143. }
  144. public bool UnhideVertex( TVertex v )
  145. {
  146. //if v not hidden, it's an error
  147. if ( !IsHiddenVertex( v ) )
  148. return false;
  149. //unhide the vertex
  150. graph.AddVertex( v );
  151. hiddenVertices.Remove( v );
  152. OnVertexUnhidden( v );
  153. return true;
  154. }
  155. public void UnhideVertexAndEdges( TVertex v )
  156. {
  157. UnhideVertex( v );
  158. List<TEdge> hiddenEdgesList;
  159. hiddenEdgesOf.TryGetValue( v, out hiddenEdgesList );
  160. if ( hiddenEdgesList != null )
  161. UnhideEdges( hiddenEdgesList );
  162. }
  163. public bool HideEdge( TEdge e )
  164. {
  165. if ( graph.ContainsEdge( e ) && !hiddenEdges.Contains( e ) )
  166. {
  167. graph.RemoveEdge( e );
  168. hiddenEdges.Add( e );
  169. GetHiddenEdgeListOf( e.Source ).Add( e );
  170. GetHiddenEdgeListOf( e.Target ).Add( e );
  171. OnEdgeHidden( e );
  172. return true;
  173. }
  174. return false;
  175. }
  176. private List<TEdge> GetHiddenEdgeListOf( TVertex v )
  177. {
  178. List<TEdge> hiddenEdgeList;
  179. hiddenEdgesOf.TryGetValue( v, out hiddenEdgeList );
  180. if ( hiddenEdgeList == null )
  181. {
  182. hiddenEdgeList = new List<TEdge>();
  183. hiddenEdgesOf[v] = hiddenEdgeList;
  184. }
  185. return hiddenEdgeList;
  186. }
  187. public IEnumerable<TEdge> HiddenEdgesOf( TVertex v )
  188. {
  189. return GetHiddenEdgeListOf( v );
  190. }
  191. public int HiddenEdgeCountOf( TVertex v )
  192. {
  193. return GetHiddenEdgeListOf( v ).Count;
  194. }
  195. public bool HideEdge( TEdge e, string tag )
  196. {
  197. var h = GetHiddenCollection( tag );
  198. var eeh = new EdgeAction<TVertex, TEdge>( edge => h.hiddenEdges.Add( edge ) );
  199. EdgeHidden += eeh;
  200. bool ret = HideEdge( e );
  201. EdgeHidden -= eeh;
  202. return ret;
  203. }
  204. public void HideEdges( IEnumerable<TEdge> edges )
  205. {
  206. var edgesToHide = new List<TEdge>( edges );
  207. foreach ( var e in edgesToHide )
  208. {
  209. HideEdge( e );
  210. }
  211. }
  212. public void HideEdges( IEnumerable<TEdge> edges, string tag )
  213. {
  214. var edgesToHide = new List<TEdge>( edges );
  215. foreach ( var e in edgesToHide )
  216. {
  217. HideEdge( e, tag );
  218. }
  219. }
  220. public void HideEdgesIf( Func<TEdge, bool> predicate, string tag )
  221. {
  222. var edgesToHide = new List<TEdge>();
  223. foreach ( var e in graph.Edges )
  224. {
  225. if ( predicate( e ) )
  226. edgesToHide.Add( e );
  227. }
  228. HideEdges( edgesToHide, tag );
  229. }
  230. public bool IsHiddenEdge( TEdge e )
  231. {
  232. return ( !graph.ContainsEdge( e ) && hiddenEdges.Contains( e ) );
  233. }
  234. public bool UnhideEdge( TEdge e )
  235. {
  236. if ( IsHiddenVertex( e.Source ) || IsHiddenVertex( e.Target ) || !IsHiddenEdge( e ) )
  237. return false;
  238. //unhide the edge
  239. graph.AddEdge( e );
  240. hiddenEdges.Remove( e );
  241. GetHiddenEdgeListOf( e.Source ).Remove( e );
  242. GetHiddenEdgeListOf( e.Target ).Remove( e );
  243. OnEdgeUnhidden( e );
  244. return true;
  245. }
  246. public void UnhideEdgesIf( Func<TEdge, bool> predicate )
  247. {
  248. var edgesToUnhide = new List<TEdge>();
  249. foreach ( var e in hiddenEdges )
  250. {
  251. if ( predicate( e ) )
  252. edgesToUnhide.Add( e );
  253. }
  254. UnhideEdges( edgesToUnhide );
  255. }
  256. public void UnhideEdges( IEnumerable<TEdge> edges )
  257. {
  258. var edgesToUnhide = new List<TEdge>( edges );
  259. foreach ( var e in edgesToUnhide )
  260. {
  261. UnhideEdge( e );
  262. }
  263. }
  264. public bool Unhide( string tag )
  265. {
  266. HiddenCollection h = GetHiddenCollection( tag );
  267. foreach ( TVertex v in h.hiddenVertices )
  268. {
  269. UnhideVertex( v );
  270. }
  271. foreach ( TEdge e in h.hiddenEdges )
  272. {
  273. UnhideEdge( e );
  274. }
  275. return hiddenCollections.Remove( tag );
  276. }
  277. public bool UnhideAll()
  278. {
  279. while ( hiddenVertices.Count > 0 )
  280. {
  281. UnhideVertex( hiddenVertices[0] );
  282. }
  283. while ( hiddenEdges.Count > 0 )
  284. {
  285. UnhideEdge( hiddenEdges[0] );
  286. }
  287. return true;
  288. }
  289. public int HiddenVertexCount
  290. {
  291. get { return hiddenVertices.Count; }
  292. }
  293. public int HiddenEdgeCount
  294. {
  295. get { return hiddenEdges.Count; }
  296. }
  297. #endregion
  298. #region IBidirectionalGraph<TVertex,TEdge> Members
  299. public int Degree( TVertex v )
  300. {
  301. throw new NotImplementedException();
  302. }
  303. public int InDegree( TVertex v )
  304. {
  305. throw new NotImplementedException();
  306. }
  307. public TEdge InEdge( TVertex v, int index )
  308. {
  309. throw new NotImplementedException();
  310. }
  311. public IEnumerable<TEdge> InEdges( TVertex v )
  312. {
  313. throw new NotImplementedException();
  314. }
  315. public bool IsInEdgesEmpty( TVertex v )
  316. {
  317. throw new NotImplementedException();
  318. }
  319. #endregion
  320. #region IIncidenceGraph<TVertex,TEdge> Members
  321. public bool ContainsEdge( TVertex source, TVertex target )
  322. {
  323. throw new NotImplementedException();
  324. }
  325. public bool TryGetEdge( TVertex source, TVertex target, out TEdge edge )
  326. {
  327. throw new NotImplementedException();
  328. }
  329. public bool TryGetEdges( TVertex source, TVertex target, out IEnumerable<TEdge> edges )
  330. {
  331. throw new NotImplementedException();
  332. }
  333. #endregion
  334. #region IImplicitGraph<TVertex,TEdge> Members
  335. public bool IsOutEdgesEmpty( TVertex v )
  336. {
  337. throw new NotImplementedException();
  338. }
  339. public int OutDegree( TVertex v )
  340. {
  341. throw new NotImplementedException();
  342. }
  343. public TEdge OutEdge( TVertex v, int index )
  344. {
  345. throw new NotImplementedException();
  346. }
  347. public IEnumerable<TEdge> OutEdges( TVertex v )
  348. {
  349. throw new NotImplementedException();
  350. }
  351. #endregion
  352. #region IGraph<TVertex,TEdge> Members
  353. public bool AllowParallelEdges
  354. {
  355. get { throw new NotImplementedException(); }
  356. }
  357. public bool IsDirected
  358. {
  359. get { throw new NotImplementedException(); }
  360. }
  361. #endregion
  362. #region IVertexSet<TVertex,TEdge> Members
  363. public bool ContainsVertex( TVertex vertex )
  364. {
  365. throw new NotImplementedException();
  366. }
  367. public bool IsVerticesEmpty
  368. {
  369. get { throw new NotImplementedException(); }
  370. }
  371. public int VertexCount
  372. {
  373. get { throw new NotImplementedException(); }
  374. }
  375. public IEnumerable<TVertex> Vertices
  376. {
  377. get { throw new NotImplementedException(); }
  378. }
  379. #endregion
  380. #region IEdgeListGraph<TVertex,TEdge> Members
  381. public bool ContainsEdge( TEdge edge )
  382. {
  383. throw new NotImplementedException();
  384. }
  385. public int EdgeCount
  386. {
  387. get { throw new NotImplementedException(); }
  388. }
  389. public IEnumerable<TEdge> Edges
  390. {
  391. get { throw new NotImplementedException(); }
  392. }
  393. public bool IsEdgesEmpty
  394. {
  395. get { throw new NotImplementedException(); }
  396. }
  397. #endregion
  398. public bool TryGetInEdges( TVertex v, out IEnumerable<TEdge> edges )
  399. {
  400. throw new NotImplementedException();
  401. }
  402. public bool TryGetOutEdges( TVertex v, out IEnumerable<TEdge> edges )
  403. {
  404. throw new NotImplementedException();
  405. }
  406. }
  407. }