Exemple #1
0
        public void return_correct_min_element_and_remove_it_from_heap_on_delete_min_operation()
        {
            _heap.Add(7);
            _heap.Add(5);
            _heap.Add(3);

            Assert.That(_heap.DeleteMin(), Is.EqualTo(3));
            Assert.That(_heap.Count, Is.EqualTo(2));
        }
 private void PurgeTimedOutRequests()
 {
     if (_doNotTimeout)
     {
         return;
     }
     try {
         while (_pending.Count > 0)
         {
             var req = _pending.FindMin();
             if (req.Item1 <= DateTime.UtcNow || req.Item2.IsProcessing)
             {
                 req = _pending.DeleteMin();
                 req.Item2.ReplyStatus(HttpStatusCode.RequestTimeout,
                                       "Server was unable to handle request in time",
                                       e => Log.Debug(
                                           "Error occurred while closing timed out connection (HTTP service core): {e}.",
                                           e.Message));
             }
             else
             {
                 break;
             }
         }
     } catch (Exception exc) {
         Log.ErrorException(exc, "Error purging timed out requests in HTTP request processor.");
     }
 }
    // Test program
    public static void Main(string[] args)
    {
        PairingHeap <int> h = new PairingHeap <int>( );
        int numItems        = 10000;
        int i = 37;
        int j;

        Console.WriteLine("Checking; no bad output is good");
        for (i = 37; i != 0; i = (i + 37) % numItems)
        {
            h.Insert(i);
        }
        for (i = 1; i < numItems; i++)
        {
            if (h.DeleteMin( ) != i)
            {
                Console.WriteLine("Oops! " + i);
            }
        }

        List <IPriorityQueuePosition <int> > p = new List <IPriorityQueuePosition <int> >( );

        for (i = 0; i < numItems; i++)
        {
            p.Add(null);
        }

        for (i = 0, j = numItems / 2; i < numItems; i++, j = (j + 71) % numItems)
        {
            p[j] = h.Insert(j + numItems);
        }
        for (i = 0, j = numItems / 2; i < numItems; i++, j = (j + 53) % numItems)
        {
            h.DecreaseKey(p[j], p[j].GetValue( ) - numItems);
        }
        i = -1;
        while (!h.IsEmpty( ))
        {
            if (h.DeleteMin( ) != ++i)
            {
                Console.WriteLine("Oops! " + i + " ");
            }
        }
        Console.WriteLine("Check completed");
    }
Exemple #4
0
        public IEnumerable <OutstandingMessage> GetMessagesExpiringBefore(DateTime time)
        {
            while (_byTime.Count > 0 && _byTime.FindMin().DueTime <= time)
            {
                var item = _byTime.DeleteMin();
                OutstandingMessage m;
                if (_outstandingRequests.TryGetValue(item.MessageId, out m))
                {
                    yield return(_outstandingRequests[item.MessageId]);

                    _outstandingRequests.Remove(item.MessageId);
                    _bySequences.Remove(m.ResolvedEvent.OriginalEventNumber);
                }
            }
        }
Exemple #5
0
        // Single-source weighted shortest-path algorithm using pairing heaps.
        public void Dijkstra2(string startName)
        {
            IPriorityQueue <Path> pq = new PairingHeap <Path>();

            Vertex start = vertexMap[startName]; // throws an exception if not found

            ClearAll();
            start.pos  = pq.Insert(new Path(start, 0));
            start.dist = 0;

            while (!pq.IsEmpty())
            {
                Path   vrec = pq.DeleteMin();
                Vertex v    = vrec.dest;

                foreach (Edge e in v.adj)
                {
                    Vertex w   = e.dest;
                    double cvw = e.cost;

                    if (cvw < 0)
                    {
                        throw new GraphException("Graph has negative edges");
                    }

                    if (w.dist > v.dist + cvw)
                    {
                        w.dist = v.dist + cvw;
                        w.prev = v;

                        Path newVal = new Path(w, w.dist);
                        if (w.pos == null)
                        {
                            w.pos = pq.Insert(newVal);
                        }
                        else
                        {
                            pq.DecreaseKey(w.pos, newVal);
                        }
                    }
                }
            }
        }
 public void throw_invalidoperationexception_when_trying_to_delete_min_element_on_empty_queue()
 {
     Assert.Throws <InvalidOperationException>(() => _heap.DeleteMin());
 }
        public void TestAllOperationsOnRandomNumbers()
        {
            int                      count = 10000; //MIN IS 100
            int                      idCounter = 1;
            List <Plane>             planes = new List <Plane>(count);
            List <Plane>             removed = new List <Plane>(count);
            PairingHeap <Plane, int> queue = new PairingHeap <Plane, int>();
            Random                   random = new Random();
            int                      addCount = 0, succAddCount = 0,
                                     changePriorityCount = 0, succChangePriorityCount = 0,
                                     deleteCount = 0, succDeleteCount = 0,
                                     compareCheck = 0;
            String history                        = "";
            int    deleteFileNumber = 1;


            for (int i = 0; i < count; i++)
            {
                int number = random.Next(0, 100 * count);
                if (number % 5 == 0 || number % 5 == 3 || number % 5 == 4)
                {
                    addCount++;
                    Plane newPlane = new Plane();
                    newPlane.SetInternationalID("id" + idCounter);
                    idCounter++;
                    newPlane.SetPriority(random.Next(11));
                    if (queue.Add(newPlane))
                    {
                        succAddCount++;
                        history += "ADDED: " + newPlane.GetInternationalID() + " with priority " + newPlane.GetPriority() + "\r\n";
                        history += "ROOT: " + queue.Root.Data.GetInternationalID() + " with priority "
                                   + queue.Root.Data.GetPriority() + "\r\n";
                        planes.Add(newPlane);
                    }
                }

                else
                if (number % 5 == 1 || number % 5 == 2)
                {
                    deleteCount++;
                    if (planes.Count > 0)
                    {
                        Plane toDelete = queue.DeleteMin();
                        if (toDelete != null)
                        {
                            history += "" + toDelete.GetInternationalID() + "\r\n";
                            succDeleteCount++;
                            planes.Remove(toDelete);
                            removed.Add(toDelete);
                            history += "DELETED: " + toDelete.GetInternationalID() + " with priority " + toDelete.GetPriority() + "\r\n";
                            if (queue.Root != null)
                            {
                                history += "ROOT: " + queue.Root.Data.GetInternationalID() + " with priority "
                                           + queue.Root.Data.GetPriority() + "\r\n";
                            }
                            else
                            {
                                history += "EMPTY HEAP\r\n";
                            }
                        }
                    }
                    if (removed.Count > 0)
                    {
                        Plane errorneousPlane = queue.Delete(removed[random.Next(0, removed.Count)]);  // SHOULD DO NOTHING
                        if (errorneousPlane != null)
                        {
                            history += "???DELETED: " + errorneousPlane.GetInternationalID()
                                       + " with priority " + errorneousPlane.GetPriority() + "\r\n";
                            if (queue.Root != null)
                            {
                                history += "ROOT: " + queue.Root.Data.GetInternationalID() + " with priority "
                                           + queue.Root.Data.GetPriority() + "\r\n";
                            }
                            else
                            {
                                history += "EMPTY HEAP\r\n";
                            }
                        }
                    }
                }
                if (count >= 100 && number % 100 == 0)
                {
                    changePriorityCount++;

                    if (planes.Count > 0)
                    {
                        int newPriority = random.Next(11);
                        int randomIndex = random.Next(0, planes.Count);
                        if (newPriority != planes[randomIndex].GetPriority())
                        {
                            Assert.IsTrue(queue.ChangePriority(planes[randomIndex],
                                                               newPriority));
                            planes[randomIndex].SetPriority(newPriority);
                            succChangePriorityCount++;
                        }
                    }

                    if (removed.Count > 0)
                    {
                        int newPriority = random.Next(11);
                        int randomIndex = random.Next(0, removed.Count);
                        if (newPriority != removed[randomIndex].GetPriority())
                        {
                            Assert.IsFalse(queue.ChangePriority(removed[randomIndex],
                                                                newPriority));
                        }
                    }

                    if (planes.Count > 0)
                    {
                        int randomIndex = random.Next(0, planes.Count);


                        Plane randomDelete = queue.Delete(planes[randomIndex]);

                        if (randomDelete != null)
                        {
                            planes.RemoveAt(randomIndex);
                            removed.Add(randomDelete);
                        }
                        deleteFileNumber++;
                    }



                    compareCheck++;
                    File.WriteAllText("D:\\.NET Projects\\PlaneDepartureTracking\\PlaneDepartureTrackingMSTest\\pairingheapTestHistory.txt",
                                      history);
                    foreach (Plane plane in planes)
                    {
                        Assert.IsTrue(queue.Contains(plane));
                    }
                    foreach (Plane plane in removed)
                    {
                        Assert.IsFalse(queue.Contains(plane));
                    }
                }
            }


            String treeTraversalLevelOrder = queue.TraverseLevelOrder();

            File.WriteAllText("D:\\.NET Projects\\PlaneDepartureTracking\\PlaneDepartureTrackingMSTest\\pairingheapTestLevelOrder.txt",
                              treeTraversalLevelOrder);
            String treeTraversalInOrder = queue.TraverseInOrder();

            File.WriteAllText("D:\\.NET Projects\\PlaneDepartureTracking\\PlaneDepartureTrackingMSTest\\pairingheapTestInOrder.txt",
                              treeTraversalInOrder);
            String checkInfo = "ADD checks: " + addCount + "\r\nSuccessfully added elements: " + succAddCount +
                               "\r\nCHANGE PRIORITY checks: " + changePriorityCount + "\r\nSuccessfully changed priorities: " + succChangePriorityCount +
                               "\r\nDELETE checks: " + deleteCount + "\r\nSuccessfully deleted elements: " + succDeleteCount +
                               "\r\nQueue-List comparisons: " + compareCheck;

            File.WriteAllText("D:\\.NET Projects\\PlaneDepartureTracking\\PlaneDepartureTrackingMSTest\\pairingheapTestStats.txt",
                              checkInfo);
            File.WriteAllText("D:\\.NET Projects\\PlaneDepartureTracking\\PlaneDepartureTrackingMSTest\\pairingheapTestHistory.txt",
                              history);
        }