|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Diagnostics.Contracts; |
4 | 4 | using ReClassNET.Extensions; |
5 | | -using ReClassNET.Util; |
6 | 5 |
|
7 | 6 | namespace ReClassNET.Nodes |
8 | 7 | { |
@@ -41,6 +40,55 @@ public int FindNodeIndex(BaseNode node) |
41 | 40 | return Nodes.FindIndex(n => n == node); |
42 | 41 | } |
43 | 42 |
|
| 43 | + /// <summary> |
| 44 | + /// Tries to get the predecessor of the given node in the container. |
| 45 | + /// </summary> |
| 46 | + /// <param name="node">The root node.</param> |
| 47 | + /// <param name="predecessor">The predecessor of the given node.</param> |
| 48 | + /// <returns>True if a predecessor exists, otherwise false.</returns> |
| 49 | + public bool TryGetPredecessor(BaseNode node, out BaseNode predecessor) |
| 50 | + { |
| 51 | + Contract.Requires(node != null); |
| 52 | + |
| 53 | + return TryGetNeighbour(node, -1, out predecessor); |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Tries to get the successor of the given node in the container. |
| 58 | + /// </summary> |
| 59 | + /// <param name="node">The root node.</param> |
| 60 | + /// <param name="successor">The successor of the given node.</param> |
| 61 | + /// <returns>True if a successor exists, otherwise false.</returns> |
| 62 | + public bool TryGetSuccessor(BaseNode node, out BaseNode successor) |
| 63 | + { |
| 64 | + Contract.Requires(node != null); |
| 65 | + |
| 66 | + return TryGetNeighbour(node, 1, out successor); |
| 67 | + } |
| 68 | + |
| 69 | + private bool TryGetNeighbour(BaseNode node, int offset, out BaseNode neighbour) |
| 70 | + { |
| 71 | + Contract.Requires(node != null); |
| 72 | + |
| 73 | + neighbour = null; |
| 74 | + |
| 75 | + var index = FindNodeIndex(node); |
| 76 | + if (index == -1) |
| 77 | + { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + var neighbourIndex = index + offset; |
| 82 | + if (neighbourIndex < 0 || neighbourIndex >= nodes.Count) |
| 83 | + { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + neighbour = nodes[neighbourIndex]; |
| 88 | + |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
44 | 92 | public bool ReplaceChildNode(BaseNode child, Type nodeType) |
45 | 93 | { |
46 | 94 | Contract.Requires(nodeType != null); |
|
0 commit comments