Ejemplo n.º 1
0
 public Suffix(Suffix suffix)
 {
     this.originNode = suffix.originNode;
     this.indexOfFirstCharacter = suffix.indexOfFirstCharacter;
     this.indexOfLastCharacter = suffix.indexOfLastCharacter;
     //this.theString = suffix.theString;
     //this.edges = suffix.edges;
 }
Ejemplo n.º 2
0
 public void BuildTree()
 {
     Suffix active = new Suffix(SuffixTree.theString, Edges, 0, 0, -1);
     for (int i = 0; i <= theString.Length - 1; i++)
     {
         AddPrefix(active, i);
     }
 }
Ejemplo n.º 3
0
 public Suffix(Suffix suffix)
 {
     this.originNode            = suffix.originNode;
     this.indexOfFirstCharacter = suffix.indexOfFirstCharacter;
     this.indexOfLastCharacter  = suffix.indexOfLastCharacter;
     //this.theString = suffix.theString;
     //this.edges = suffix.edges;
 }
Ejemplo n.º 4
0
        public void BuildTree()
        {
            Suffix active = new Suffix(SuffixTree.theString, Edges, 0, 0, -1);

            for (int i = 0; i <= theString.Length - 1; i++)
            {
                AddPrefix(active, i);
            }
        }
Ejemplo n.º 5
0
        private void AddPrefix(Suffix active, int indexOfLastCharacter)
        {
            int parentNode;
            int lastParentNode = -1;

            for (; ;)
            {
                Edge edge = new Edge(-1);
                parentNode = active.originNode;

                if (active.IsExplicit)
                {
                    edge = Edge.Find(SuffixTree.theString, SuffixTree.Edges, active.originNode, theString[indexOfLastCharacter]);
                    if (edge.startNode != -1)
                    {
                        break;
                    }
                }
                else
                {
                    edge = Edge.Find(SuffixTree.theString, SuffixTree.Edges, active.originNode, theString[active.indexOfFirstCharacter]);
                    int span = active.indexOfLastCharacter - active.indexOfFirstCharacter;
                    if (theString[edge.indexOfFirstCharacter + span + 1] == theString[indexOfLastCharacter])
                    {
                        break;
                    }
                    parentNode = Edge.SplitEdge(active, theString, Edges, Nodes, ref edge);
                }

                Edge newEdge = new Edge(SuffixTree.theString, indexOfLastCharacter, SuffixTree.theString.Length - 1, parentNode);
                Edge.Insert(newEdge);
                if (lastParentNode > 0)
                {
                    Nodes[lastParentNode].suffixNode = parentNode;
                }
                lastParentNode = parentNode;

                if (active.originNode == 0)
                {
                    active.indexOfFirstCharacter++;
                }
                else
                {
                    active.originNode = Nodes[active.originNode].suffixNode;
                }
                active.Canonize();
            }
            if (lastParentNode > 0)
            {
                Nodes[lastParentNode].suffixNode = parentNode;
            }
            active.indexOfLastCharacter++;
            active.Canonize();
        }
Ejemplo n.º 6
0
        static public int SplitEdge(Suffix s, string theString, Dictionary <int, Edge> edges, Dictionary <int, Node> nodes, ref Edge edge)
        {
            Remove(edge);
            Edge newEdge = new Edge(theString, edge.indexOfFirstCharacter,
                                    edge.indexOfFirstCharacter + s.indexOfLastCharacter
                                    - s.indexOfFirstCharacter, s.originNode);

            Edge.Insert(newEdge);
            if (nodes.ContainsKey(newEdge.endNode))
            {
                nodes[newEdge.endNode].suffixNode = s.originNode;
            }
            else
            {
                Node newNode = new Node();
                newNode.suffixNode = s.originNode;
                nodes.Add(newEdge.endNode, newNode);
            }

            edge.indexOfFirstCharacter += s.indexOfLastCharacter - s.indexOfFirstCharacter + 1;
            edge.startNode              = newEdge.endNode;
            Edge.Insert(edge);
            return(newEdge.endNode);
        }
Ejemplo n.º 7
0
        private void AddPrefix(Suffix active, int indexOfLastCharacter)
        {
            int parentNode;
            int lastParentNode = -1;

            for (; ; )
            {
                Edge edge = new Edge(-1);
                parentNode = active.originNode;

                if (active.IsExplicit)
                {
                    edge = Edge.Find(SuffixTree.theString, SuffixTree.Edges, active.originNode, theString[indexOfLastCharacter]);
                    if (edge.startNode != -1)
                    {
                        break;
                    }
                }
                else
                {
                    edge = Edge.Find(SuffixTree.theString, SuffixTree.Edges, active.originNode, theString[active.indexOfFirstCharacter]);
                    int span = active.indexOfLastCharacter - active.indexOfFirstCharacter;
                    if (theString[edge.indexOfFirstCharacter + span + 1] == theString[indexOfLastCharacter])
                    {
                        break;
                    }
                    parentNode = Edge.SplitEdge(active, theString, Edges, Nodes,ref edge);
                }

                Edge newEdge = new Edge(SuffixTree.theString, indexOfLastCharacter, SuffixTree.theString.Length - 1, parentNode);
                Edge.Insert(newEdge);
                if (lastParentNode > 0)
                {
                    Nodes[lastParentNode].suffixNode = parentNode;
                }
                lastParentNode = parentNode;

                if (active.originNode == 0)
                {
                    active.indexOfFirstCharacter++;
                }
                else
                {
                    active.originNode = Nodes[active.originNode].suffixNode;
                }
                active.Canonize();
            }
            if (lastParentNode > 0)
            {
                Nodes[lastParentNode].suffixNode = parentNode;
            }
            active.indexOfLastCharacter++;
            active.Canonize();
        }
Ejemplo n.º 8
0
        public static int SplitEdge(Suffix s, string theString, Dictionary<int, Edge> edges, Dictionary<int, Node> nodes,ref Edge edge)
        {
            Remove(edge);
            Edge newEdge = new Edge(theString, edge.indexOfFirstCharacter,
                edge.indexOfFirstCharacter + s.indexOfLastCharacter
                - s.indexOfFirstCharacter, s.originNode);
            Edge.Insert(newEdge);
            if (nodes.ContainsKey(newEdge.endNode))
            {
                nodes[newEdge.endNode].suffixNode = s.originNode;
            }
            else
            {
                Node newNode = new Node();
                newNode.suffixNode = s.originNode;
                nodes.Add(newEdge.endNode, newNode);
            }

            edge.indexOfFirstCharacter += s.indexOfLastCharacter - s.indexOfFirstCharacter + 1;
            edge.startNode = newEdge.endNode;
            Edge.Insert(edge);
            return newEdge.endNode;
        }