public void RandomWithDeleteHandles() { Random ran = new Random(6754); int length = 1000; int[] a = new int[length]; ArrayList <int> shuffle = new ArrayList <int>(length); IPriorityQueueHandle <int>[] h = new IPriorityQueueHandle <int> [length]; for (int i = 0; i < length; i++) { shuffle.Add(i); queue.Add(ref h[i], a[i] = ran.Next()); Assert.IsTrue(queue.Check()); } Assert.IsTrue(queue.Check()); shuffle.Shuffle(ran); for (int i = 0; i < length; i++) { int j = shuffle[i]; Assert.AreEqual(a[j], queue.Delete(h[j])); Assert.IsTrue(queue.Check()); } Assert.IsTrue(queue.IsEmpty); }
public void Enqueue(HexCell cell) { IPriorityQueueHandle <HexCell> handle = null; m_heap.Add(ref handle, cell); m_handleMap.Add(cell, handle); }
/// <summary> /// Adds an Event to the Event queue /// </summary> internal void ScheduleEvent(Event _event) { IPriorityQueueHandle <Event> _h = null; FutureEventList.Add(ref _h, _event); this.eventHandles.Add(_event.UUID, _h); }
/// <summary> /// Check safely if a handle is valid for this queue and if so, report the corresponding queue item. /// </summary> /// <param name="handle">The handle to check</param> /// <param name="item">If the handle is valid this will contain the corresponding item on output.</param> /// <returns>True if the handle is valid.</returns> public bool Find(IPriorityQueueHandle <T> handle, out T item) { Handle myhandle = handle as Handle; if (myhandle == null) { item = default(T); return(false); } int toremove = myhandle.index; int cell = toremove / 2; bool isfirst = toremove % 2 == 0; { if (toremove == -1 || toremove >= size) { item = default(T); return(false); } Handle actualhandle = isfirst ? heap[cell].firsthandle : heap[cell].lasthandle; if (actualhandle != myhandle) { item = default(T); return(false); } } item = isfirst ? heap[cell].first : heap[cell].last; return(true); }
public static void AddEvent(GameEvent e) { IPriorityQueueHandle <GameEvent> handle = null; _eventQueue.Add(ref handle, e); e.Handle = handle; }
private static void AddHexToSearchList(Hexagon <T> hex) { IPriorityQueueHandle <Hexagon <T> > startRef = null; HexagonsToSearch.Add(ref startRef, hex); HexRefList.Add(startRef, hex); }
private void HandleSiteEvent(SiteEvent siteEvent) { EventTree.TreeNode node = _eventTree.GetClosest(this, _eventTree.GetRoot(), siteEvent); if (node == null || node is EventTree.BreakpointNode) { return; } EventTree.LeafNode closest = (EventTree.LeafNode)node; if (closest.GetDisappearEvent() != null) { _eventQueue.Delete(closest.GetDisappearEvent().GetHandle()); closest.SetDisappearEvent(null); } List <CircleEvent> circleEvents2 = _eventTree.InsertNewSiteEvent(closest, new TreeItem(siteEvent.V())); foreach (var ce in circleEvents2) { IPriorityQueueHandle <IEvent> h = null; _eventQueue.Add(ref h, ce); ce.SetHandle(h); } }
public void RandomIndexing() { Random ran = new Random(6754); int length = 1000; int[] a = new int[length]; int[] b = new int[length]; ArrayList <int> shuffle = new ArrayList <int>(length); IPriorityQueueHandle <int>[] h = new IPriorityQueueHandle <int> [length]; for (int i = 0; i < length; i++) { shuffle.Add(i); queue.Add(ref h[i], a[i] = ran.Next()); b[i] = ran.Next(); Assert.IsTrue(queue.Check()); } Assert.IsTrue(queue.Check()); shuffle.Shuffle(ran); for (int i = 0; i < length; i++) { int j = shuffle[i]; Assert.AreEqual(a[j], queue[h[j]]); queue[h[j]] = b[j]; Assert.AreEqual(b[j], queue[h[j]]); Assert.IsTrue(queue.Check()); } }
public void Bug20130208() { IPriorityQueue <double> q = new IntervalHeap <double>(); IPriorityQueueHandle <double> h0 = null, h2 = null, h4 = null, h7 = null, h5 = null; // Add(43, 0); q.Add(ref h0, 43); // Remove(); q.DeleteMin(); // XAddMaxReplace(9, 2); q.Add(ref h2, Double.MaxValue); q[h2] = 9; // XAddMaxReplace(32, 4); q.Add(ref h4, Double.MaxValue); q[h4] = 32; // XAddMaxReplace(44, 7); q.Add(ref h7, Double.MaxValue); q[h7] = 44; // Remove(); q.DeleteMin(); // XAddMaxReplace(0, 5); q.Add(ref h5, Double.MaxValue); q[h5] = 0; // Internally inconsistent data structure already now Assert.IsTrue(q.Check()); }
public void Add(T vertex, double weight) { IPriorityQueueHandle <QueueItem> h = null; _priorityQueue.Add(ref h, new QueueItem(vertex, weight)); _handles.Add(vertex, h); }
public T RemoveMax(out IPriorityQueueHandle <T> handle) { // Collection must be non-empty Requires(!IsEmpty, CollectionMustBeNonEmpty); // Collection must be non-fixed-sized Requires(!IsFixedSize, CollectionMustBeNonFixedSize); // Result is non-null Ensures(AllowsNull || Result <T>() != null); // All items in the collection are less than or equal to the result Ensures(ForAll(this, item => Comparer.Compare(item, Result <T>()) <= 0)); // Result is same as FindMax Ensures(Result <T>().IsSameAs(OldValue(FindMax()))); // Removing an item decreases the count by one Ensures(Count == OldValue(Count) - 1); // The handle is no longer associated with an item in the priority queue Ensures(!Contains(ValueAtReturn(out handle))); // Return value is from the collection Ensures(OldValue(ToArray()).ContainsSame(Result <T>())); handle = null; return(default(T)); }
public void Replace5b() { for (int size = 0; size < 130; size++) { IPriorityQueue <double> q = new IntervalHeap <double>(); IPriorityQueueHandle <double> handle1 = null; q.Add(ref handle1, -3.0); Assert.AreEqual(-3.0, q.FindMax()); for (int i = 1; i < size; i++) { q.Add(-i - 3.0); } Assert.AreEqual(-3.0, q.FindMax()); for (int max = -2; max <= 10; max++) { Assert.AreEqual(max - 1.0, q.Replace(handle1, max)); Assert.AreEqual(max, q.FindMax()); } Assert.AreEqual(10.0, q.DeleteMax()); for (int i = 1; i < size; i++) { Assert.AreEqual(-i - 3.0, q.DeleteMax()); } Assert.IsTrue(q.IsEmpty); } }
public void RandomWithHandles() { int length = 1000; int[] a = new int[length]; Random ran = new Random(6754); for (int i = 0; i < length; i++) { IPriorityQueueHandle <int> h = null; queue.Add(ref h, a[i] = ran.Next()); Assert.IsTrue(queue.Check()); } Assert.IsTrue(queue.Check()); Array.Sort(a); for (int i = 0; i < length / 2; i++) { Assert.AreEqual(a[length - i - 1], queue.DeleteMax()); Assert.IsTrue(queue.Check()); Assert.AreEqual(a[i], queue.DeleteMin()); Assert.IsTrue(queue.Check()); } Assert.IsTrue(queue.IsEmpty); }
public void Replace5a() { for (int size = 0; size < 130; size++) { IPriorityQueue <double> q = new IntervalHeap <double>(); IPriorityQueueHandle <double> handle1 = null; q.Add(ref handle1, 3.0); Assert.AreEqual(3.0, q.FindMin()); for (int i = 1; i < size; i++) { q.Add(i + 3.0); } Assert.AreEqual(3.0, q.FindMin()); for (int min = 2; min >= -10; min--) { Assert.AreEqual(min + 1.0, q.Replace(handle1, min)); Assert.AreEqual(min, q.FindMin()); } Assert.AreEqual(-10.0, q.DeleteMin()); for (int i = 1; i < size; i++) { Assert.AreEqual(i + 3.0, q.DeleteMin()); } Assert.IsTrue(q.IsEmpty); } }
public void ErrorAddValidHandle() { IPriorityQueueHandle <int> handle = null; queue.Add(ref handle, 7); queue.Add(ref handle, 8); }
private void AddSafely(LinkedListNode <Seg> seg) { IPriorityQueueHandle <LinkedListNode <Seg> > handle = null; Heap.Add(ref handle, seg); Handles[seg.Value] = handle; }
public T FindMax(out IPriorityQueueHandle <T> handle) { // Collection must be non-empty Requires(!IsEmpty, CollectionMustBeNonEmpty); // Result is non-null Ensures(AllowsNull || Result <T>() != null); // All items in the collection are less than or equal to the result Ensures(ForAll(this, item => Comparer.Compare(item, Result <T>()) <= 0)); // Result is same as FindMax Ensures(Result <T>().IsSameAs(FindMax())); // The handle is associated with the result Ensures(this[ValueAtReturn(out handle)].IsSameAs(Result <T>())); // The count remains the same Ensures(Count == OldValue(Count)); // Return value is from the collection Ensures(this.ContainsSame(Result <T>())); handle = null; return(default(T)); }
/// <summary> /// Updates the key for a key/value pair that is currently in the priority queue. /// If the given handle is not active, an <see cref="InvalidOperationException"/> is thrown. /// </summary> /// <param name="handle">The handle for the key/value pair.</param> /// <param name="key">The current key is replaced with this value.</param> /// <exception cref="System.ArgumentNullException">Thrown if the handle is null.</exception> /// <exception cref="System.InvalidOperationException">Thrown if the handle is not active or if the handle belongs to a different priority queue.</exception> public void UpdateKey(IPriorityQueueHandle <TKey, TValue> handle, TKey key) { if (handle == null) { throw new ArgumentNullException("handle"); } var h = handle as PairingHeapHandle <TKey, TValue>; if (h == null || h.pairingHeapId != id) { throw new InvalidOperationException("Tried to update the key of a handle of a different priority queue than that which created it."); } if (!h.IsActive) { throw new InvalidOperationException("Tried to update the key of an inactive handle."); } if (comparer.Compare(key, h.Key) <= 0) { h.key = key; if (h != root) { SpliceOut(h); root = Pair(root, h); } } else { Remove(h); h.key = key; Add(h); } }
/// <summary> /// Removes a key/value pair from the priority queue. /// If the given handle is not active, an <see cref="InvalidOperationException"/> is thrown. /// </summary> /// <param name="handle">The handle for the key/value pair to remove.</param> /// <exception cref="System.ArgumentNullException">Thrown if the handle is null.</exception> /// <exception cref="System.InvalidOperationException">Thrown if the handle is not active or if the handle belongs to a different priority queue.</exception> public void Remove(IPriorityQueueHandle <TKey, TValue> handle) { if (handle == null) { throw new ArgumentNullException("handle"); } var h = handle as PairingHeapHandle <TKey, TValue>; if (h == null || h.pairingHeapId != id) { throw new InvalidOperationException("Tried to remove a handle from a different priority queue than that which created it."); } if (!h.IsActive) { throw new InvalidOperationException("Tried to remove an inactive handle."); } count--; if (h == root) { root = DeleteRoot(root); } else { SpliceOut(h); root = Pair(root, DeleteRoot(h)); } h.isActive = false; h.left = null; h.right = null; h.firstChild = null; }
private void UpdatePriority([NotNull] IPriorityQueue <int> priorityQueue, int nodeIndex) { // This is necessary to ensure correct internal ordering after updating a distance IPriorityQueueHandle <int> handle = _queueItemHandles[nodeIndex]; priorityQueue.Replace(handle, nodeIndex); }
public bool add(T e) { IPriorityQueueHandle <T> h = null; bool r = queue.Add(ref h, e); dict.Add(e, h); return(r); }
public void ErrorReplaceInvalidHandle() { IPriorityQueueHandle <int> handle = null; queue.Add(ref handle, 7); queue.Delete(handle); queue.Replace(handle, 13); }
public void ReuseHandle() { IPriorityQueueHandle <int> handle = null; queue.Add(ref handle, 7); queue.Delete(handle); queue.Add(ref handle, 8); }
public void Change(T vertex, double weight) { _priorityQueue.Delete(_handles[vertex]); IPriorityQueueHandle <QueueItem> h = null; _priorityQueue.Add(ref h, new QueueItem(vertex, weight)); _handles[vertex] = h; }
public void ErrorAddValidHandle() { IPriorityQueueHandle <int> handle = null; queue.Add(ref handle, 7); Assert.Throws <InvalidPriorityQueueHandleException>(() => queue.Add(ref handle, 8)); }
public void Replace3() { IPriorityQueueHandle <int> handle = null; queue.Add(ref handle, 10); Assert.AreEqual(10, queue.Replace(handle, 12)); Assert.IsTrue(queue.Check()); }
public void ErrorReplaceInvalidHandle() { IPriorityQueueHandle <int> handle = null; queue.Add(ref handle, 7); queue.Delete(handle); Assert.Throws <InvalidPriorityQueueHandleException>(() => queue.Replace(handle, 13)); }
/// <summary> /// Get or set the item corresponding to a handle. /// </summary> /// <exception cref="InvalidPriorityQueueHandleException">if the handle is invalid for this queue</exception> /// <param name="handle">The reference into the heap</param> /// <returns></returns> public T this[IPriorityQueueHandle <T> handle] { get { CheckHandle(handle, out int cell, out bool isfirst); return(isfirst ? heap[cell].first : heap[cell].last); } set => Replace(handle, value);
public void WithHandles() { listen(); IPriorityQueueHandle <int> handle = null, handle2; collection.Add(34); seen.Check(new CollectionEvent <int>[] { new CollectionEvent <int>(EventTypeEnum.Added, new ItemCountEventArgs <int>(34, 1), collection), new CollectionEvent <int>(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.Add(56); seen.Check(new CollectionEvent <int>[] { new CollectionEvent <int>(EventTypeEnum.Added, new ItemCountEventArgs <int>(56, 1), collection), new CollectionEvent <int>(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.Add(ref handle, 34); seen.Check(new CollectionEvent <int>[] { new CollectionEvent <int>(EventTypeEnum.Added, new ItemCountEventArgs <int>(34, 1), collection), new CollectionEvent <int>(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.Add(12); seen.Check(new CollectionEvent <int>[] { new CollectionEvent <int>(EventTypeEnum.Added, new ItemCountEventArgs <int>(12, 1), collection), new CollectionEvent <int>(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.DeleteMax(out handle2); seen.Check(new CollectionEvent <int>[] { new CollectionEvent <int>(EventTypeEnum.Removed, new ItemCountEventArgs <int>(56, 1), collection), new CollectionEvent <int>(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.DeleteMin(out handle2); seen.Check(new CollectionEvent <int>[] { new CollectionEvent <int>(EventTypeEnum.Removed, new ItemCountEventArgs <int>(12, 1), collection), new CollectionEvent <int>(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.Replace(handle, 117); seen.Check(new CollectionEvent <int>[] { new CollectionEvent <int>(EventTypeEnum.Removed, new ItemCountEventArgs <int>(34, 1), collection), new CollectionEvent <int>(EventTypeEnum.Added, new ItemCountEventArgs <int>(117, 1), collection), new CollectionEvent <int>(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.Delete(handle); seen.Check(new CollectionEvent <int>[] { new CollectionEvent <int>(EventTypeEnum.Removed, new ItemCountEventArgs <int>(117, 1), collection), new CollectionEvent <int>(EventTypeEnum.Changed, new EventArgs(), collection), }); }
private void HandleCircleEvent(CircleEvent circleEvent) { HalfEdge CL = circleEvent.L().GetHalfEdge(); if (CL.GetFace() == circleEvent.L().GetNode()) { CL = CL.Twin(); } HalfEdge CR = circleEvent.C().GetHalfEdge(); if (CR.GetFace() == circleEvent.R().GetNode()) { CR = CR.Twin(); } HalfEdge RC = CR.Twin(); RC.SetTarget(circleEvent); CL.SetTarget(circleEvent); circleEvent.halfEdge = CR; EventTree.LeafNode prev = (EventTree.LeafNode)_eventTree.GetPreviousLeaf(circleEvent.GetCenterLeafNode()); EventTree.LeafNode next = (EventTree.LeafNode)_eventTree.GetNextLeaf(circleEvent.GetCenterLeafNode()); if (prev != null) { if (prev.GetDisappearEvent() != null) { _eventQueue.Delete(prev.GetDisappearEvent().GetHandle()); prev.SetDisappearEvent(null); } } if (next != null) { if (next.GetDisappearEvent() != null) { _eventQueue.Delete(next.GetDisappearEvent().GetHandle()); next.SetDisappearEvent(null); } } List <CircleEvent> newCircles = _eventTree.RemoveNode(circleEvent, prev, circleEvent.GetCenterLeafNode(), next); if (newCircles != null) { foreach (CircleEvent ce in newCircles) { IPriorityQueueHandle <IEvent> h = null; _eventQueue.Add(ref h, ce); ce.SetHandle(h); } } }
public void RandomWithDeleteHandles() { Random ran = new Random(6754); int length = 1000; int[] a = new int[length]; ArrayList<int> shuffle = new ArrayList<int>(length); IPriorityQueueHandle<int>[] h = new IPriorityQueueHandle<int>[length]; for (int i = 0; i < length; i++) { shuffle.Add(i); queue.Add(ref h[i], a[i] = ran.Next()); Assert.IsTrue(queue.Check()); } Assert.IsTrue(queue.Check()); shuffle.Shuffle(ran); for (int i = 0; i < length; i++) { int j = shuffle[i]; Assert.AreEqual(a[j], queue.Delete(h[j])); Assert.IsTrue(queue.Check()); } Assert.IsTrue(queue.IsEmpty); }
public void RandomIndexing() { Random ran = new Random(6754); int length = 1000; int[] a = new int[length]; int[] b = new int[length]; ArrayList<int> shuffle = new ArrayList<int>(length); IPriorityQueueHandle<int>[] h = new IPriorityQueueHandle<int>[length]; for (int i = 0; i < length; i++) { shuffle.Add(i); queue.Add(ref h[i], a[i] = ran.Next()); b[i] = ran.Next(); Assert.IsTrue(queue.Check()); } Assert.IsTrue(queue.Check()); shuffle.Shuffle(ran); for (int i = 0; i < length; i++) { int j = shuffle[i]; Assert.AreEqual(a[j], queue[h[j]]); queue[h[j]] = b[j]; Assert.AreEqual(b[j], queue[h[j]]); Assert.IsTrue(queue.Check()); } }
public void Handles() { IPriorityQueueHandle<int>[] handles = new IPriorityQueueHandle<int>[10]; queue.Add(ref handles[0], 7); Assert.IsTrue(queue.Check()); queue.Add(ref handles[1], 72); Assert.IsTrue(queue.Check()); queue.Add(ref handles[2], 27); Assert.IsTrue(queue.Check()); queue.Add(ref handles[3], 17); Assert.IsTrue(queue.Check()); queue.Add(ref handles[4], 70); Assert.IsTrue(queue.Check()); queue.Add(ref handles[5], 1); Assert.IsTrue(queue.Check()); queue.Add(ref handles[6], 2); Assert.IsTrue(queue.Check()); queue.Add(ref handles[7], 7); Assert.IsTrue(queue.Check()); queue.Add(ref handles[8], 8); Assert.IsTrue(queue.Check()); queue.Add(ref handles[9], 9); Assert.IsTrue(queue.Check()); queue.Delete(handles[2]); Assert.IsTrue(queue.Check()); queue.Delete(handles[0]); Assert.IsTrue(queue.Check()); queue.Delete(handles[8]); Assert.IsTrue(queue.Check()); queue.Delete(handles[4]); Assert.IsTrue(queue.Check()); queue.Delete(handles[6]); Assert.IsTrue(queue.Check()); Assert.AreEqual(5, queue.Count); }
/// <summary> /// Create a new instance of this class. /// </summary> public PriorityQueueNode() : base() { Handle = null; }
public void Dispose() { m_asset = null; PriorityQueueHandle = null; }