Esempio n. 1
0
        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();
            }
        }
Esempio n. 2
0
        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();
        }
Esempio n. 3
0
        public T Pop()
        {
            T value = head.Value;

            head      = head.Previous;
            head.Next = null;

            HeadIndex--;
            if (HeadIndex == 0)
            {
                root = null;
            }


            ResetPattern();
            return(value);
        }
Esempio n. 4
0
        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));
        }
Esempio n. 5
0
        /// <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);
        }
Esempio n. 6
0
        /// <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);
        }
Esempio n. 7
0
        /// <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);
        }
Esempio n. 8
0
 public PatternStackNode(PatternStackNode <T> Next, PatternStackNode <T> Previous, T Value)
 {
     this.Next     = Next;
     this.Previous = Previous;
     this.Value    = Value;
 }