Exemplo n.º 1
0
        private void AddNewPalindromeNode(PalindromeNode longestPalindromePrefix, Insertion insertion)
        {
            var nextNodeIndex = _tree.Count; //todo: change null-suffix to smth

            var newNode = new PalindromeNode(nextNodeIndex);

            if (longestPalindromePrefix.IsImaginaryStringPalindromeNode)
            {
                newNode.Label = insertion.Letter.ToString();
            }
            else
            {
                newNode.Label = insertion.Letter + longestPalindromePrefix.Label + insertion.Letter;
            }
            _tree.Add(newNode);

            longestPalindromePrefix.OutgoingNodes.Add(insertion.Letter, newNode);

            if (longestPalindromePrefix.IsImaginaryStringPalindromeNode)
            {
                newNode = _tree[EmptyStringPalindromeNode.INDEX_EMPTY_STRING];
            }
            else
            {
                var suffixForNewNode =
                    GetLongestPalindromeSuffixForNewNode(insertion, longestPalindromePrefix);
                newNode.LongestPalindromeSuffix = suffixForNewNode;
            }

            _currentLongestPalindromeSuffixNodeIndex = newNode.Index;
        }
Exemplo n.º 2
0
        private PalindromeNode GetLongestPalindromeSuffixForNewNode(Insertion insertion,
                                                                    PalindromeNode longestPalindromePrefix)
        {
            var suffixForNewNode = longestPalindromePrefix.LongestPalindromeSuffix;

            while (isNecessaryToKeepTraversingTheSuffixChain(insertion, suffixForNewNode))
            {
                suffixForNewNode = suffixForNewNode.LongestPalindromeSuffix;
            }
            suffixForNewNode = suffixForNewNode.OutgoingNodes[insertion.Letter];
            return(suffixForNewNode);
        }
Exemplo n.º 3
0
        private bool isNecessaryToKeepTraversingTheSuffixChain(Insertion insertion, PalindromeNode currentSuffix)
        {
            if (currentSuffix.IsImaginaryStringPalindromeNode)
            {
                return(false);
            }

            var indexOfLetterPrecedingTheCurrentSuffix =
                insertion.LetterIndex - currentSuffix.GetPalindromeLength - 1;

            return(indexOfLetterPrecedingTheCurrentSuffix < 0 ||
                   insertion.Letter != _storedString[indexOfLetterPrecedingTheCurrentSuffix]);
        }
Exemplo n.º 4
0
 private bool IsDuplicatePalindrome(char letter, PalindromeNode longestPalindromeSuffixForNextPalindrome)
 {
     return(longestPalindromeSuffixForNextPalindrome.OutgoingNodes.ContainsKey(letter));
 }