Beispiel #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;
        }
Beispiel #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);
        }
Beispiel #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]);
        }
Beispiel #4
0
 private bool IsDuplicatePalindrome(char letter, PalindromeNode longestPalindromeSuffixForNextPalindrome)
 {
     return(longestPalindromeSuffixForNextPalindrome.OutgoingNodes.ContainsKey(letter));
 }