Exemplo n.º 1
0
 public StNode(SuffixTree tree, StNode suffixNode)
 {
     this.tree = tree;
     this.id = tree.NodeCount++;
     this.childEdges = new Dictionary<char, StEdge>();
     this.suffixNode = suffixNode;
 }
Exemplo n.º 2
0
 public StNode(SuffixTree tree, StNode suffixNode)
 {
     this.tree       = tree;
     this.id         = tree.NodeCount++;
     this.childEdges = new Dictionary <char, StEdge>();
     this.suffixNode = suffixNode;
 }
Exemplo n.º 3
0
 // Rule 1: Try to find matching edge for the parent node.
 private ExtensionResult extendSuffixByRuleOne(
     ref StSuffix active, ref StNode parentNode, int endIndex)
 {
     if (active.IsExplicit)
     {
         StEdge edge = active.OriginNode.GetChildEdge(text[endIndex]);
         if (edge != null && edge.IsSet())
         {
             return(ExtensionResult.Done);
         }
     }
     else    // active suffix is implicit
     {
         StEdge edge = active.OriginNode.GetChildEdge(text[active.BeginIndex]);
         int    span = active.EndIndex - active.BeginIndex;
         if (text[edge.BeginIndex + span + 1] == text[endIndex])
         {
             return(ExtensionResult.Done);
         }
         StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format(
                              "  Rule #1: About to split edge E{0:d} (\"{1:s}\") at suffix {2:s}",
                              edge.Id, edge.GetText(), active.ToString()));
         parentNode = edge.Split(active);
     }
     return(ExtensionResult.NotDone);
 }
Exemplo n.º 4
0
 private void setSuffixLink(StNode node, StNode suffixNode)
 {
     if ((node != null) && (node != root))
     {
         if (node.SuffixNode == null)
         {
             StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format(
                                  "  New suffix link from N{0:d} to N{1:d}",
                                  node.Id, suffixNode.Id));
         }
         else
         {
             if (node.SuffixNode.Id == suffixNode.Id)
             {
                 StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format(
                                      "  Suffix link (N{0:d} to N{1:d}) retaining same value",
                                      node.Id, node.SuffixNode.Id));
             }
             else
             {
                 StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format(
                                      "  Suffix link (N{0:d} to N{1:d}) set to new value (N{0:d} to N{2:d})",
                                      node.Id, node.SuffixNode.Id, suffixNode.Id));
             }
         }
         node.SuffixNode = suffixNode;
     }
 }
Exemplo n.º 5
0
 public StSuffix(
     SuffixTree tree,
     StNode originNode,
     int beginIndex = 0,
     int endIndex   = int.MaxValue)
 {
     this.tree       = tree;
     this.OriginNode = originNode;
     this.beginIndex = beginIndex;
     this.endIndex   = endIndex;
 }
Exemplo n.º 6
0
 public StEdge(
     SuffixTree tree,
     StNode parentNode,
     int indexOfFirstChar,
     int indexOfLastChar)
 {
     this.id = StEdge.nextId++;
     this.tree = tree;
     this.ParentNode = parentNode;
     this.ChildNode = new StNode(tree, null);
     this.BeginIndex = indexOfFirstChar;
     this.EndIndex = indexOfLastChar;
 }
Exemplo n.º 7
0
 public StEdge(
     SuffixTree tree,
     StNode parentNode,
     int indexOfFirstChar,
     int indexOfLastChar)
 {
     this.id         = StEdge.nextId++;
     this.tree       = tree;
     this.ParentNode = parentNode;
     this.ChildNode  = new StNode(tree, null);
     this.BeginIndex = indexOfFirstChar;
     this.EndIndex   = indexOfLastChar;
 }
Exemplo n.º 8
0
        public void PushDown()
        {
            if (L == R)
            {
                return;
            }

            if (Left == null)
            {
                var mid = (L + R) >> 1;
                Left  = new StNode(L, mid);
                Right = new StNode(mid + 1, R);
            }
        }
Exemplo n.º 9
0
            // Rule 2: Create a new edge and add it to the tree at the parent's position.
            //     Part of this is inserting the new edge into the hash table,
            //     and creating a suffix link to the new node from the last one visited.
            private void extendSuffixByRuleTwo(
                ref StSuffix active, StNode parentNode, ref StNode prevParentNode, int endIndex)
            {
                StEdge newEdge = new StEdge(this, parentNode, endIndex, this.text.Length - 1);

                newEdge.Add();
                StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format(
                                     "  Rule #2: New edge E{0:d} (\"{1:s}\") connects N{2:d} (old parent) to N{3:d} (new child)",
                                     newEdge.Id,
                                     newEdge.GetText(),
                                     newEdge.ParentNode.Id,
                                     newEdge.ChildNode.Id
                                     ));
                setSuffixLink(prevParentNode, parentNode);
                prevParentNode = parentNode;
            }
Exemplo n.º 10
0
    public MajorityChecker(int[] arr)
    {
        _root = new StNode(0, arr.Length - 1);
        _idx  = new Dictionary <int, List <int> >();

        for (int i = 0; i < arr.Length; i++)
        {
            var num    = arr[i];
            var subArr = _idx.ContainsKey(num) ? _idx[num] : new List <int>();
            subArr.Add(i);

            _idx[num] = subArr;

            UpdateSingle(_root, i, arr[i]);
        }
    }
Exemplo n.º 11
0
    private void QueryHelper(StNode root, int left, int right, int[] majority, int[] count)
    {
        if (root.R < left || root.L > right)
        {
            return;
        }

        if (left <= root.L && root.R <= right)
        {
            MergeQuery(majority, count, root.Majority, root.Count);
            return;
        }

        QueryHelper(root.Left, left, right, majority, count);
        QueryHelper(root.Right, left, right, majority, count);
    }
Exemplo n.º 12
0
            private void extendSuffixes(ref StSuffix active, int endIndex)
            {
                StNode parentNode;
                StNode prevParentNode = null;

                for (   ; ; incrSuffix(ref active))
                {
                    parentNode = active.OriginNode;
                    if (extendSuffixByRuleOne(ref active, ref parentNode, endIndex) == ExtensionResult.Done)
                    {
                        break;
                    }
                    extendSuffixByRuleTwo(ref active, parentNode, ref prevParentNode, endIndex);
                }
                setSuffixLink(prevParentNode, parentNode);
                active.EndIndex++;
                active.Canonicalize();
            }
Exemplo n.º 13
0
            public StNode Split(StSuffix s)
            {
                Remove();
                StEdge newEdge = new StEdge(tree, s.OriginNode, BeginIndex, BeginIndex + s.Span);

                newEdge.Add();
                newEdge.ChildNode.SuffixNode = s.OriginNode;
                BeginIndex += s.Span + 1;
                ParentNode  = newEdge.ChildNode;
                Add();
                StUtil.WriteLine(StVerbosityLevel.Normal, String.Format(
                                     "  Split E{0:d} into E{1:d} + E{0:d} = \"{2:s}\" + \"{3:s}\"",
                                     Id, newEdge.Id,
                                     newEdge.GetText(),
                                     this.GetText()
                                     ));
                return(newEdge.ChildNode);
            }
Exemplo n.º 14
0
    private void UpdateSingle(StNode root, int pos, int val)
    {
        if (root.L == root.R)
        {
            root.UpdateByVal(val, 1);
            return;
        }

        var mid = (root.L + root.R) >> 1;

        root.PushDown();
        if (pos <= mid)
        {
            UpdateSingle(root.Left, pos, val);
        }
        else
        {
            UpdateSingle(root.Right, pos, val);
        }

        root.UpdateFromSon();
    }
Exemplo n.º 15
0
            public double xt; /* Scanbeam top x coordinate         */

            #endregion Fields

            #region Constructors

            public StNode( EdgeNode edge, StNode prev )
            {
                this.edge = edge ;
                this.xb = edge.xb ;
                this.xt = edge.xt ;
                this.dx = edge.dx ;
                this.prev = prev ;
            }
Exemplo n.º 16
0
        private static StNode add_st_edge( StNode st, ItNodeTable it, EdgeNode edge, double dy)
        {
            if (st == null)
            {
                /* Append edge onto the tail end of the ST */
                st = new StNode( edge, null );
            }
            else
            {
                double den= (st.xt - st.xb) - (edge.xt - edge.xb);

                /* If new edge and ST edge don't cross */
                if( (edge.xt >= st.xt) || (edge.dx == st.dx) || (Math.Abs(den) <= GPC_EPSILON))
                {
                    /* No intersection - insert edge here (before the ST edge) */
                    StNode existing_node = st;
                    st = new StNode( edge, existing_node );
                }
                else
                {
                    /* Compute intersection between new edge and ST edge */
                    double r= (edge.xb - st.xb) / den;
                    double x= st.xb + r * (st.xt - st.xb);
                    double y= r * dy;

                    /* Insert the edge pointers and the intersection point in the IT */
                    it.top_node = add_intersection(it.top_node, st.edge, edge, x, y);

                    /* Head further into the ST */
                    st.prev = add_st_edge(st.prev, it, edge, dy);
                }
            }
            return st ;
        }
Exemplo n.º 17
0
 public StNode Split(StSuffix s)
 {
     Remove();
     StEdge newEdge = new StEdge(tree, s.OriginNode, BeginIndex, BeginIndex + s.Span);
     newEdge.Add();
     newEdge.ChildNode.SuffixNode = s.OriginNode;
     BeginIndex += s.Span + 1;
     ParentNode = newEdge.ChildNode;
     Add();
     StUtil.WriteLine(StVerbosityLevel.Normal, String.Format(
         "  Split E{0:d} into E{1:d} + E{0:d} = \"{2:s}\" + \"{3:s}\"",
         Id, newEdge.Id,
         newEdge.GetText(),
         this.GetText()
         ));
     return newEdge.ChildNode;
 }
Exemplo n.º 18
0
 private void setSuffixLink(StNode node, StNode suffixNode)
 {
     if ((node != null) && (node != root))
     {
         if (node.SuffixNode == null)
         {
             StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format(
                 "  New suffix link from N{0:d} to N{1:d}",
                 node.Id, suffixNode.Id));
         } else {
             if (node.SuffixNode.Id == suffixNode.Id) {
                 StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format(
                     "  Suffix link (N{0:d} to N{1:d}) retaining same value",
                     node.Id, node.SuffixNode.Id));
             } else {
                 StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format(
                     "  Suffix link (N{0:d} to N{1:d}) set to new value (N{0:d} to N{2:d})",
                     node.Id, node.SuffixNode.Id, suffixNode.Id));
             }
         }
         node.SuffixNode = suffixNode;
     }
 }
Exemplo n.º 19
0
 // Rule 2: Create a new edge and add it to the tree at the parent's position.
 //     Part of this is inserting the new edge into the hash table,
 //     and creating a suffix link to the new node from the last one visited.
 private void extendSuffixByRuleTwo(
     ref StSuffix active, StNode parentNode, ref StNode prevParentNode,  int endIndex)
 {
     StEdge newEdge = new StEdge(this, parentNode, endIndex, this.text.Length - 1);
     newEdge.Add();
     StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format(
         "  Rule #2: New edge E{0:d} (\"{1:s}\") connects N{2:d} (old parent) to N{3:d} (new child)",
         newEdge.Id,
         newEdge.GetText(),
         newEdge.ParentNode.Id,
         newEdge.ChildNode.Id
         ));
     setSuffixLink(prevParentNode, parentNode);
     prevParentNode = parentNode;
 }
Exemplo n.º 20
0
 // Rule 1: Try to find matching edge for the parent node.
 private ExtensionResult extendSuffixByRuleOne(
     ref StSuffix active, ref StNode parentNode, int endIndex)
 {
     if (active.IsExplicit)
     {
         StEdge edge = active.OriginNode.GetChildEdge(text[endIndex]);
         if (edge != null && edge.IsSet())
         {
             return ExtensionResult.Done;
         }
     }
     else    // active suffix is implicit
     {
         StEdge edge = active.OriginNode.GetChildEdge(text[active.BeginIndex]);
         int span = active.EndIndex - active.BeginIndex;
         if (text[edge.BeginIndex + span + 1] == text[endIndex])
         {
             return ExtensionResult.Done;
         }
         StUtil.WriteLine(StVerbosityLevel.Verbose, String.Format(
             "  Rule #1: About to split edge E{0:d} (\"{1:s}\") at suffix {2:s}",
             edge.Id, edge.GetText(), active.ToString()));
             parentNode = edge.Split(active);
     }
     return ExtensionResult.NotDone;
 }