Example #1
0
        public void Split(int x, out Treap L, out Treap R)
        {
            Treap newTree = null;

            if (this.x <= x)
            {
                if (Right == null)
                {
                    R = null;
                }
                else
                {
                    Right.Split(x, out newTree, out R);
                }
                L = new Treap(this.x, y, Left, newTree);
            }
            else
            {
                if (Left == null)
                {
                    L = null;
                }
                else
                {
                    Left.Split(x, out L, out newTree);
                }
                R = new Treap(this.x, y, newTree, Right);
            }
        }
Example #2
0
 private TextOccurences()
 {
     _text       = string.Empty;
     _textLength = 0;
     _positions  = null;
     _count      = 0;
 }
Example #3
0
 private TextOccurences()
 {
     _text = string.Empty;
     _textLength = 0;
     _positions = null;
     _count = 0;
 }
Example #4
0
        public void Add(int value)
        {
            _count += 1;

            if (_first == null)
            {
                _first = new Treap(value);
                _last = _first;
                return;
            }

            int currentPriority = _priorityRandom.Next();

            if (_last.y > currentPriority)
            {
                _last.Right = new Treap() { x = value, y = currentPriority, Parent = _last };
                _last = _last.Right;
            }
            else
            {
                Treap cur = _last;
                while (cur.Parent != null && cur.y <= currentPriority)
                    cur = cur.Parent;
                if (cur.y <= currentPriority)
                    _last = new Treap() { x = value, y = currentPriority, Left = cur, Right = null };
                else
                {
                    _last = new Treap() { x = value, y = currentPriority, Left = cur.Right, Right = null, Parent = cur };
                    cur.Right = _last;
                }
            }
        }
Example #5
0
        public void AddMarks(TextOccurences occurences)
        {
            if (occurences == null) throw new ArgumentNullException("occurences");

            lock (_marksSyncRoot)
            {
                if (occurences != TextOccurences.Empty && occurences.Count > 0)
                {
                    _markLength = occurences.TextLength;

                    var n = occurences.Positions;
                    if (n != null)
                    {
                        if (_positions != null)
                        {
                            n.ForEachLessThan(_positions.GetMinX(), IncludeTextToScreenUpdate);
                            n.ForEachGreaterThan(_positions.GetMaxX(), IncludeTextToScreenUpdate);
                        }
                        else
                        {
                            n.ForEachInOrder(IncludeTextToScreenUpdate);
                        }
                    }

                    _positions = n;
                }
            }
        }
Example #6
0
 private Treap(int x, int y, Treap left, Treap right)
 {
     this.x = x;
     this.y = y;
     this.Left = left;
     this.Right = right;
 }
Example #7
0
        public void AddMarks(TextOccurences occurences)
        {
            if (occurences == null)
            {
                throw new ArgumentNullException("occurences");
            }

            lock (_marksSyncRoot)
            {
                if (occurences != TextOccurences.Empty && occurences.Count > 0)
                {
                    _markLength = occurences.TextLength;

                    var n = occurences.Positions;
                    if (n != null)
                    {
                        if (_positions != null)
                        {
                            n.ForEachLessThan(_positions.GetMinX(), IncludeTextToScreenUpdate);
                            n.ForEachGreaterThan(_positions.GetMaxX(), IncludeTextToScreenUpdate);
                        }
                        else
                        {
                            n.ForEachInOrder(IncludeTextToScreenUpdate);
                        }
                    }

                    _positions = n;
                }
            }
        }
Example #8
0
 private Treap(int x, int y, Treap left, Treap right)
 {
     this.x     = x;
     this.y     = y;
     this.Left  = left;
     this.Right = right;
 }
Example #9
0
        public Treap Add(int x)
        {
            Treap l, r;

            Split(x, out l, out r);
            Treap m = new Treap(x, _priorityRandom.Next());

            return(Merge(Merge(l, m), r));
        }
Example #10
0
        public TextOccurences(string text, TreapBuilder positions)
        {
            if (text == null) throw new ArgumentNullException("text");
            if (positions == null) throw new ArgumentNullException("positions");

            _text = text;
            _textLength = text.Length;
            _positions = positions.ToTreap();
            _count = positions.Count;
        }
Example #11
0
 public void Clear()
 {
     lock (_marksSyncRoot)
     {
         if (_positions != null)
         {
             _positions.ForEachInOrder(IncludeTextToScreenUpdate);
             _positions = null;
             _markLength = 0;
         }
     }
 }
Example #12
0
 public void Clear()
 {
     lock (_marksSyncRoot)
     {
         if (_positions != null)
         {
             _positions.ForEachInOrder(IncludeTextToScreenUpdate);
             _positions  = null;
             _markLength = 0;
         }
     }
 }
Example #13
0
 public static Treap Find(Treap node, int searchX)
 {
     if (node != null)
     {
         if (node.x == searchX)
             return node;
         if (searchX < node.x)
             return Find(node.Left, searchX);
         if (searchX > node.x)
             return Find(node.Right, searchX);
     }
     return null;
 }
Example #14
0
        public void ReplaceMarks(TextOccurences occurences, int start, int end, int tailOffset)
        {
            if (occurences == null)
            {
                throw new ArgumentNullException("occurences");
            }

            lock (_marksSyncRoot)
            {
                if (_positions == null)
                {
                    ReplaceMarks(occurences);
                    return;
                }

                Treap markThatContainsStart = FindMarkThatContainsPosition(_positions, start);
                if (markThatContainsStart != null)
                {
                    start = markThatContainsStart.x;
                }

                Treap right   = null;
                Treap garbage = null;
                _positions.Split(start - 1, out _positions, out right);

                if (right != null)
                {
                    right.Split(end, out garbage, out right);
                }

                if (garbage != null)
                {
                    garbage.ForEachInOrder(IncludeTextToScreenUpdate);
                }

                if (occurences != TextOccurences.Empty && occurences.Count > 0)
                {
                    _markLength = occurences.TextLength;
                    occurences.Positions.ForEachInOrder(IncludeTextToScreenUpdate);
                    _positions = Treap.Merge(_positions, occurences.Positions);
                }

                if (right != null)
                {
                    TreapBuilder shiftedMarks = new TreapBuilder();
                    right.ForEachInOrder((x) => { shiftedMarks.Add(x + tailOffset); });
                    _positions = Treap.Merge(_positions, shiftedMarks.ToTreap());
                }
            }
        }
Example #15
0
        public TextOccurences(string text, TreapBuilder positions)
        {
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }
            if (positions == null)
            {
                throw new ArgumentNullException("positions");
            }

            _text       = text;
            _textLength = text.Length;
            _positions  = positions.ToTreap();
            _count      = positions.Count;
        }
Example #16
0
        public static Treap Merge(Treap L, Treap R)
        {
            if (L == null) return R;
            if (R == null) return L;

            if (L.y > R.y)
            {
                var newR = Merge(L.Right, R);
                return new Treap(L.x, L.y, L.Left, newR);
            }
            else
            {
                var newL = Merge(L, R.Left);
                return new Treap(R.x, R.y, newL, R.Right);
            }
        }
Example #17
0
        public void Add(int value)
        {
            _count += 1;

            if (_first == null)
            {
                _first = new Treap(value);
                _last  = _first;
                return;
            }

            int currentPriority = _priorityRandom.Next();

            if (_last.y > currentPriority)
            {
                _last.Right = new Treap()
                {
                    x = value, y = currentPriority, Parent = _last
                };
                _last = _last.Right;
            }
            else
            {
                Treap cur = _last;
                while (cur.Parent != null && cur.y <= currentPriority)
                {
                    cur = cur.Parent;
                }
                if (cur.y <= currentPriority)
                {
                    _last = new Treap()
                    {
                        x = value, y = currentPriority, Left = cur, Right = null
                    }
                }
                ;
                else
                {
                    _last = new Treap()
                    {
                        x = value, y = currentPriority, Left = cur.Right, Right = null, Parent = cur
                    };
                    cur.Right = _last;
                }
            }
        }
Example #18
0
 private Treap FindMarkThatContainsPosition(Treap node, int pos)
 {
     if (node != null)
     {
         if (node.x <= pos && pos <= node.x + _markLength)
         {
             return(node);
         }
         if (pos < node.x)
         {
             return(FindMarkThatContainsPosition(node.Left, pos));
         }
         if (pos > node.x)
         {
             return(FindMarkThatContainsPosition(node.Right, pos));
         }
     }
     return(null);
 }
Example #19
0
 public static Treap Find(Treap node, int searchX)
 {
     if (node != null)
     {
         if (node.x == searchX)
         {
             return(node);
         }
         if (searchX < node.x)
         {
             return(Find(node.Left, searchX));
         }
         if (searchX > node.x)
         {
             return(Find(node.Right, searchX));
         }
     }
     return(null);
 }
Example #20
0
        public static Treap Merge(Treap L, Treap R)
        {
            if (L == null)
            {
                return(R);
            }
            if (R == null)
            {
                return(L);
            }

            if (L.y > R.y)
            {
                var newR = Merge(L.Right, R);
                return(new Treap(L.x, L.y, L.Left, newR));
            }
            else
            {
                var newL = Merge(L, R.Left);
                return(new Treap(R.x, R.y, newL, R.Right));
            }
        }
Example #21
0
        public void ReplaceMarks(TextOccurences occurences)
        {
            if (occurences == null)
            {
                throw new ArgumentNullException("occurences");
            }

            lock (_marksSyncRoot)
            {
                if (_positions != null)
                {
                    _positions.ForEachInOrder(IncludeTextToScreenUpdate);
                    _positions = null;
                }

                if (occurences != TextOccurences.Empty && occurences.Count > 0)
                {
                    _markLength = occurences.TextLength;
                    occurences.Positions.ForEachInOrder(IncludeTextToScreenUpdate);
                    _positions = occurences.Positions;
                }
            }
        }
Example #22
0
 private Treap FindMarkThatContainsPosition(Treap node, int pos)
 {
     if (node != null)
     {
         if (node.x <= pos && pos <= node.x + _markLength)
             return node;
         if (pos < node.x)
             return FindMarkThatContainsPosition(node.Left, pos);
         if (pos > node.x)
             return FindMarkThatContainsPosition(node.Right, pos);
     }
     return null;
 }
Example #23
0
 public void Split(int x, out Treap L, out Treap R)
 {
     Treap newTree = null;
     if (this.x <= x)
     {
         if (Right == null)
             R = null;
         else
             Right.Split(x, out newTree, out R);
         L = new Treap(this.x, y, Left, newTree);
     }
     else
     {
         if (Left == null)
             L = null;
         else
             Left.Split(x, out L, out newTree);
         R = new Treap(this.x, y, newTree, Right);
     }
 }
Example #24
0
        public void ReplaceMarks(TextOccurences occurences, int start, int end, int tailOffset)
        {
            if (occurences == null) throw new ArgumentNullException("occurences");

            lock (_marksSyncRoot)
            {
                if (_positions == null)
                {
                    ReplaceMarks(occurences);
                    return;
                }

                Treap markThatContainsStart = FindMarkThatContainsPosition(_positions, start);
                if (markThatContainsStart != null)
                {
                    start = markThatContainsStart.x;
                }

                Treap right = null;
                Treap garbage = null;
                _positions.Split(start - 1, out _positions, out right);

                if (right != null)
                {
                    right.Split(end, out garbage, out right);
                }

                if (garbage != null)
                {
                    garbage.ForEachInOrder(IncludeTextToScreenUpdate);
                }

                if (occurences != TextOccurences.Empty && occurences.Count > 0)
                {
                    _markLength = occurences.TextLength;
                    occurences.Positions.ForEachInOrder(IncludeTextToScreenUpdate);
                    _positions = Treap.Merge(_positions, occurences.Positions);
                }

                if (right != null)
                {
                    TreapBuilder shiftedMarks = new TreapBuilder();
                    right.ForEachInOrder((x) => { shiftedMarks.Add(x + tailOffset); });
                    _positions = Treap.Merge(_positions, shiftedMarks.ToTreap());
                }
            }
        }
Example #25
0
 public Treap Add(int x)
 {
     Treap l, r;
     Split(x, out l, out r);
     Treap m = new Treap(x, _priorityRandom.Next());
     return Merge(Merge(l, m), r);
 }
Example #26
0
        public void ReplaceMarks(TextOccurences occurences)
        {
            if (occurences == null) throw new ArgumentNullException("occurences");

            lock (_marksSyncRoot)
            {
                if (_positions != null)
                {
                    _positions.ForEachInOrder(IncludeTextToScreenUpdate);
                    _positions = null;
                }

                if (occurences != TextOccurences.Empty && occurences.Count > 0)
                {
                    _markLength = occurences.TextLength;
                    occurences.Positions.ForEachInOrder(IncludeTextToScreenUpdate);
                    _positions = occurences.Positions;
                }
            }
        }