public static Treap <Value> Rotate(ref Treap <Value> t, int dir) { var s = t.ch [1 - dir]; t.ch [1 - dir] = s.ch [dir]; s.ch [dir] = t; return(t = s); }
static List <int> Heapup <Value>(List <int> l, Treap <Value> t) { if (t == null) { return(l); } Heapup(l, t.ch [0]); l.Add((int)t.key); Heapup(l, t.ch [1]); return(l); }
public static IMemoryPool <Treap <Value> > Clear(Treap <Value> t, IMemoryPool <Treap <Value> > alloc) { if (t == null) { return(alloc); } for (var i = 0; i < 2; i++) { Clear(t.ch [i], alloc); } return(alloc.Free(t.Clear())); }
public static Treap <Value> Insert(ref Treap <Value> t, ulong key, IMemoryPool <Treap <Value> > alloc) { if (t == null) { return(t = alloc.New().Reset(key, Priority())); } var dir = (key < t.key ? 0 : 1); var n = Insert(ref t.ch [dir], key, alloc); if (t.pri < t.ch [dir].pri) { Rotate(ref t, 1 - dir); } return(n); }
public static bool TryGet(Treap <Value> t, ulong key, out Treap <Value> n) { n = t; if (t == null) { return(false); } if (t.key == key) { return(true); } var dir = (key < t.key ? 0 : 1); return(TryGet(t.ch [dir], key, out n)); }
public void EditorTest() { Treap <int> root = null; var alloc = new MemoryPool <Treap <int> > (() => new Treap <int>(), (v) => v.Clear(), (v) => { }); var n = 4; var numbers = new int[n]; var rands = new float[n]; for (var i = 0; i < n; i++) { numbers [i] = i; rands [i] = Random.value; } System.Array.Sort(rands, numbers); for (var i = 0; i < n; i++) { Treap <int> .Insert(ref root, (ulong)i, alloc); } var heap = Heapup <int>(new List <int> (), root); for (var i = 0; i < heap.Count; i++) { Assert.AreEqual(i, heap [i]); } for (var i = 0; i < n; i++) { Treap <int> t; Assert.True(Treap <int> .TryGet(root, (ulong)i, out t)); t.Values.AddLast(i); } for (var i = 0; i < n; i++) { Treap <int> t; Treap <int> .TryGet(root, (ulong)i, out t); Assert.AreEqual(1, t.Values.Count); Assert.AreEqual(i, t.Values.Last.Value); } }
public ITreapController <Value> Clear() { Treap <Value> .Clear(Root, _pool); return(this); }
public Treap <Value> Insert(ulong key) { return(Treap <Value> .Insert(ref _root, key, _pool)); }
public bool TryGet(ulong key, out Treap <Value> n) { return(Treap <Value> .TryGet(_root, key, out n)); }