Guest User

Untitled

a guest
Feb 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Nightingale.Entities;
  5.  
  6. namespace Nightingale.Sessions
  7. {
  8. /// <summary>
  9. /// Represents the session graph which contains the order of the entity hierarchy.
  10. /// </summary>
  11. public class SessionGraph : IEnumerable<Entity>
  12. {
  13. /// <summary>
  14. /// Gets the session graph nodes.
  15. /// </summary>
  16. public IEnumerable<SessionGraphNode> Nodes => _nodes;
  17.  
  18. private readonly List<SessionGraphNode> _nodes;
  19. private readonly HashSet<Entity> _hashSet;
  20.  
  21. /// <summary>
  22. /// Initializes a new SessionGraph class.
  23. /// </summary>
  24. public SessionGraph()
  25. {
  26. _nodes = new List<SessionGraphNode>();
  27. _hashSet = new HashSet<Entity>();
  28. }
  29.  
  30. /// <summary>
  31. /// Adds a new session graph node to this node.
  32. /// </summary>
  33. /// <param name="sessionGraphNode">The session graph node.</param>
  34. public void AddNode(SessionGraphNode sessionGraphNode)
  35. {
  36. if (!Contains(sessionGraphNode.Entity))
  37. {
  38. AddEntity(sessionGraphNode.Entity);
  39. _nodes.Add(sessionGraphNode);
  40. }
  41. }
  42.  
  43. /// <summary>
  44. /// Adds a new session graph node to this node.
  45. /// </summary>
  46. /// <param name="entity">The entity.</param>
  47. public void AddNode(Entity entity)
  48. {
  49. AddNode(new SessionGraphNode(this, entity));
  50. }
  51.  
  52. /// <summary>
  53. /// Removes a session graph node from this node.
  54. /// </summary>
  55. /// <param name="sessionGraphNode">The session graph node.</param>
  56. /// <returns>Returns true when the session graph node was removed.</returns>
  57. public bool RemoveNode(SessionGraphNode sessionGraphNode)
  58. {
  59. if (_nodes.Remove(sessionGraphNode))
  60. {
  61. RemoveEntity(sessionGraphNode.Entity);
  62. return true;
  63. }
  64.  
  65. return false;
  66. }
  67.  
  68. /// <summary>
  69. /// Removes a session graph node from this node.
  70. /// </summary>
  71. /// <param name="entity">The entity.</param>
  72. /// <returns>Returns true when the session graph node was removed.</returns>
  73. public bool RemoveNode(Entity entity)
  74. {
  75. var sessionGraphNode = _nodes.FirstOrDefault(x => x.Entity == entity);
  76. if (sessionGraphNode == null)
  77. return false;
  78.  
  79. return RemoveNode(sessionGraphNode);
  80. }
  81.  
  82. /// <summary>
  83. /// Gets a value indicating whether the <see cref="SessionGraph"/> contains a specific entity.
  84. /// </summary>
  85. /// <param name="entity">The entity.</param>
  86. /// <returns>Returns true when the <see cref="SessionGraph"/> contains the entity.</returns>
  87. public bool Contains(Entity entity)
  88. {
  89. return _hashSet.Contains(entity);
  90. }
  91.  
  92. /// <summary>
  93. /// Adds an entity to the internal hashset.
  94. /// </summary>
  95. /// <param name="entity">The entity.</param>
  96. internal void AddEntity(Entity entity)
  97. {
  98. _hashSet.Add(entity);
  99. }
  100.  
  101. /// <summary>
  102. /// Removes the entity from the internal hashset.
  103. /// </summary>
  104. /// <param name="entity">The entity.</param>
  105. internal void RemoveEntity(Entity entity)
  106. {
  107. _hashSet.Remove(entity);
  108. }
  109.  
  110. /// <summary>Returns an enumerator that iterates through the collection.</summary>
  111. /// <returns>An enumerator that can be used to iterate through the collection.</returns>
  112. public IEnumerator<Entity> GetEnumerator()
  113. {
  114. var entities = new List<Entity>();
  115. TraverseSessionGraphNodes(Nodes, entities);
  116.  
  117. return entities.GetEnumerator();
  118. }
  119.  
  120. /// <summary>Returns an enumerator that iterates through a collection.</summary>
  121. /// <returns>An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.</returns>
  122. IEnumerator IEnumerable.GetEnumerator()
  123. {
  124. return GetEnumerator();
  125. }
  126.  
  127. /// <summary>
  128. /// Traverses all session graph nodes recursive and collects the entities.
  129. /// </summary>
  130. /// <param name="nodes">The session graph nodes.</param>
  131. /// <param name="entities">The entities.</param>
  132. private void TraverseSessionGraphNodes(IEnumerable<SessionGraphNode> nodes, List<Entity> entities)
  133. {
  134. foreach (var sessionGraphNode in nodes)
  135. {
  136. TraverseSessionGraphNodes(sessionGraphNode.Nodes, entities);
  137. entities.Add(sessionGraphNode.Entity);
  138. }
  139. }
  140. }
  141. }
Add Comment
Please, Sign In to add comment