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); } }
private TextOccurences() { _text = string.Empty; _textLength = 0; _positions = null; _count = 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; } } }
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; } } }
private Treap(int x, int y, Treap left, Treap right) { this.x = x; this.y = y; this.Left = left; this.Right = right; }
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; } } }
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)); }
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; }
public void Clear() { lock (_marksSyncRoot) { if (_positions != null) { _positions.ForEachInOrder(IncludeTextToScreenUpdate); _positions = null; _markLength = 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; }
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()); } } }
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; }
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); } }
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; } } }
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); }
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); }
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)); } }
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; } } }
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; }
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); } }
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()); } } }
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); }
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; } } }