public void Pop(int count) { if (count == 0) { return; } for (int i = 0; i < count; i++) { head = head.Previous; } head.Next = null; HeadIndex -= count; if (HeadIndex == -1) { root = null; head = null; HeadIndex = 0; } else { ResetPattern(); } }
private void ResetPattern() { matchedStartIndex = 0; patternStartIndex = 0; patternStartNode = root; matchedStartNode = null; CurrentPattern.Clear(); for (var currentNode = root; currentNode != null; currentNode = currentNode.Next) { CurrentPattern.Add(currentNode.Value); } MinimizePattern(); }
public T Pop() { T value = head.Value; head = head.Previous; head.Next = null; HeadIndex--; if (HeadIndex == 0) { root = null; } ResetPattern(); return(value); }
public bool Push(T value) { PatternStackNode <T> node = new PatternStackNode <T>(null, head, value); if (head != null) { head.Next = node; } else { root = node; HeadIndex = -1; } head = node; HeadIndex++; return(AddToPattern(head)); }
/// <summary> /// Shrinks pattern until it finds a match or becomes empty /// </summary> /// <returns> /// Returns false if pattern becomes empty /// </returns> private bool MinimizePattern() { while (patternStartIndex != HeadIndex + 1) { if (FindMatch()) { return(true); } CurrentPattern.RemoveAt(0); patternStartIndex++; patternStartNode = patternStartNode.Next; } matchedStartIndex = 0; patternStartIndex = 0; matchedStartNode = null; patternStartNode = null; return(false); }
/// <summary> /// Adds a new node to the current pattern and revalidates /// </summary> /// <returns> /// Returns true if pattern is matched fully and repetition is hit /// </returns> private bool AddToPattern(PatternStackNode <T> node) { CurrentPattern.Add(node.Value); if (patternStartNode == null) { patternStartNode = node; patternStartIndex = HeadIndex; } if (matchedStartNode != null) { if (CheckIfMatches()) { if (PatternFull()) { return(true); } return(false); } } if (FindMatch()) { if (PatternFull()) { return(true); } return(false); } if (MinimizePattern()) { if (PatternFull()) { return(true); } } return(false); }
/// <summary> /// Tries to find a match for the current Pattern /// </summary> /// <returns> /// Returns true if pattern is matched by match nodes. /// Note: Does not require the match to be a full match (repetition might not be hit) /// </returns> private bool FindMatch() { if (patternStartIndex == 0) { return(false); } var currentNode = patternStartNode.Previous; for (int i = patternStartIndex - 1; i >= 0; i--) { matchedStartIndex = i; matchedStartNode = currentNode; if (CheckIfMatches()) { return(true); } currentNode = currentNode.Previous; } matchedStartNode = null; matchedStartIndex = 0; return(false); }
public PatternStackNode(PatternStackNode <T> Next, PatternStackNode <T> Previous, T Value) { this.Next = Next; this.Previous = Previous; this.Value = Value; }