Example #1
0
        public virtual PrefixTreeNode addString(System.String newString)
        {
            if (finalized)
            {
                throw new System.SystemException("Can't manipulate a finalized Prefix Tree");
            }

            if (disablePrefixing)
            {
                PrefixTreeNode newNode = new PrefixTreeNode(newString.ToCharArray());
                newNode.setTerminal();
                root.addChild(newNode);
                return(newNode);
            }

            PrefixTreeNode current = root;

            char[] chars        = newString.ToCharArray();
            int    currentIndex = 0;

            while (currentIndex < chars.Length)
            {
                //The length of the string we've incorporated into the tree
                int len = 0;

                //The (potential) next node in the tree which prefixes the rest of the string
                PrefixTreeNode node = null;

                //TODO: This would be way faster if we sorted upon insertion....
                if (current.getChildren() != null)
                {
                    //UPGRADE_TODO: Method 'java.util.Enumeration.hasMoreElements' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationhasMoreElements'"
                    for (System.Collections.IEnumerator e = current.getChildren().elements(); e.MoveNext();)
                    {
                        //UPGRADE_TODO: Method 'java.util.Enumeration.nextElement' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationnextElement'"
                        node = (PrefixTreeNode)e.Current;

                        char[] prefix = node.Prefix;
                        //if(prefix.equals(s)) {
                        if (ArrayUtilities.arraysEqual(prefix, 0, chars, currentIndex))
                        {
                            return(node);
                        }

                        len = sharedPrefixLength(chars, currentIndex, prefix);
                        if (len > minimumPrefixLength)
                        {
                            //See if we have any breaks which might make more heuristic sense than simply grabbing the biggest
                            //difference

                            for (char c: delimiters)
                            {
                                int sepLen = -1;
                                for (int i = currentIndex + len - 1; i >= currentIndex; i--)
                                {
                                    if (chars[i] == c)
                                    {
                                        sepLen = i - currentIndex;
                                        break;
                                    }
                                }
                                if (sepLen != -1 && len - sepLen < delSacrifice && sepLen > minimumHeuristicLength)
                                {
                                    len = sepLen;
                                    break;
                                }
                            }

                            break;
                        }
                        node = null;
                    }
                }

                //If we didn't find anything that shared any common roots
                if (node == null)
                {
                    //Create a placeholder for the rest of the string
                    char[] newArray;
                    if (currentIndex == 0)
                    {
                        newArray = chars;
                    }
                    else
                    {
                        newArray = new char[chars.Length - currentIndex];
                        for (int i = 0; i < chars.Length - currentIndex; ++i)
                        {
                            newArray[i] = chars[i + currentIndex];
                        }
                    }
                    node = new PrefixTreeNode(newArray);

                    len = chars.Length - currentIndex;

                    //Add this to the highest level prefix we've found
                    current.addChild(node);
                }
                //Otherwise check to see if we are going to split the current prefix
                else if (len < node.Prefix.Length)
                {
                    char[] newPrefix = new char[len];
                    for (int i = 0; i < len; ++i)
                    {
                        newPrefix[i] = chars[currentIndex + i];
                    }

                    PrefixTreeNode interimNode = current.budChild(node, newPrefix, len);

                    node = interimNode;
                }

                current      = node;
                currentIndex = currentIndex + len;
            }

            current.setTerminal();
            return(current);
        }
Example #2
0
 public override bool Equals(System.Object o)
 {
     //uh... is this right?
     return(o is PrefixTreeNode?prefix == ((PrefixTreeNode)o).prefix || ArrayUtilities.arraysEqual(prefix, 0, ((PrefixTreeNode)o).prefix, 0):false);
 }