public void AddRemove() { PriorityQueue<string, int> pq = new PriorityQueue<string, int>(10); Assert.AreEqual<int>(10, pq.Capacity); Assert.AreEqual<int>(0, pq.Count); pq.Enqueue("A", 1); Assert.AreEqual<int>(1, pq.Count); Assert.AreEqual<string>("A", pq.Dequeue(false)); pq.Enqueue("B", 2); Assert.AreEqual<int>(2, pq.Count); Assert.AreEqual<string>("B", pq.Dequeue(false)); pq.Enqueue("C", 1); Assert.AreEqual<int>(3, pq.Count); Assert.AreEqual<string>("B", pq.Dequeue(false)); Assert.AreEqual<string>("B", pq.Dequeue()); Assert.AreEqual<string>("A", pq.Dequeue(true)); Assert.AreEqual<string>("C", pq.Dequeue()); Assert.AreEqual<int>(0, pq.Count); Assert.AreEqual<int>(10, pq.Capacity); pq.Enqueue("A", 1); pq.Enqueue("B", 2); pq.Enqueue("C", 3); pq.Clear(); Assert.AreEqual<int>(0, pq.Count); Assert.AreEqual<int>(10, pq.Capacity); }
public void ClearTest() { PriorityQueue<int> actual = new PriorityQueue<int>(); actual.Clear(); Assert.AreEqual(0, actual.Count); }
public void Clear_non_empty_queue() { var queue = new List<PriorityQueue<string>.Element> {new PriorityQueue<string>.Element("a", 1)}; var sut = new PriorityQueue<string>(queue); sut.Clear(); Assert.AreEqual(0, queue.Count()); }
public void Clear() { var pq = new PriorityQueue<int>(new int[] { 1, 2, 3 }); int temp; Assert.AreEqual(3, pq.Count); Assert.IsTrue(pq.Peek(out temp)); pq.Clear(); Assert.AreEqual(0, pq.Count); Assert.IsFalse(pq.Peek(out temp)); }
public void PriorityQueueClearEmptiesTheQueue() { var q = new PriorityQueue<int>(); q.Enqueue(AnyInt); q.Enqueue(AnyInt); q.Clear(); Assert.Equal(0, q.Count); Assert.Throws<InvalidOperationException>(() => q.Peek()); }
public void WorkPriorityQueue() { Student firstStudent = new Student("Alina", "Kylish", 123456789, new DateTime(1988, 4, 3)); Student secondStudent = new Student("Elena", "Kylish", 987654321, new DateTime(1987, 8, 15)); Student thirdStudent = new Student("Oleg", "Ivanov", 975312468, new DateTime(1992, 11, 20)); Student fourthStudent = new Student("Andrey", "Pavlov", 1243576890, new DateTime(1977, 8, 10)); PriorityQueue<Student> students = new PriorityQueue<Student>(); students.Enqueue(firstStudent, 0); students.Enqueue(secondStudent, 1); students.Enqueue(thirdStudent, 2); students.Enqueue(fourthStudent, 0); Console.WriteLine(students.Count()); Console.WriteLine(students.First()); Console.WriteLine(students.Last()); students.Dequeue(); Console.WriteLine(students.Count()); Console.ReadKey(); students.Clear(); students.Dequeue(); // throw exeption with message "Queue is empty. There is no value in queue" }
static void WorkWithPriorityQueue() { PriorityQueue<int> numbers = new PriorityQueue<int>(); try { Console.WriteLine("Count = {0}", numbers.Count()); //Console.WriteLine(numbers.First()); //Console.WriteLine(numbers.Last()); //numbers.Add(10); // Print(numbers); numbers.Enqueue(1, 5); numbers.Enqueue(2, 11); numbers.Enqueue(1, 1); // Print(numbers); numbers.Enqueue(2, 1); numbers.Enqueue(3, 1); numbers.Enqueue(15, 2); numbers.Enqueue(25, 2); numbers.Enqueue(21, 2); // Print(numbers); numbers.Enqueue(1021, 3); numbers.Enqueue(375, 5); numbers.Enqueue(124243323, 8); // Print(numbers); Console.WriteLine("Count = {0}", numbers.Count()); Console.WriteLine("Count with priority 1= {0}", numbers.GetCount(1)); Console.WriteLine("Count with priority 2= {0}", numbers.GetCount(2)); Console.WriteLine("Count with priority 3= {0}", numbers.GetCount(3)); Console.WriteLine("Count with priority 4= {0}", numbers.GetCount(4)); Console.WriteLine("Count with priority 5= {0}", numbers.GetCount(5)); Console.WriteLine("Count with priority 8= {0}", numbers.GetCount(8)); Console.WriteLine("Count with priority 11= {0}", numbers.GetCount(11)); Console.WriteLine(numbers.First()); Console.WriteLine(numbers.Last()); Console.WriteLine(numbers.Dequeue()); // Print(numbers); Console.WriteLine(numbers.Dequeue()); Console.WriteLine(numbers.Dequeue()); Console.WriteLine(numbers.Dequeue()); // Print(numbers); Console.WriteLine(numbers.Dequeue()); Console.WriteLine(numbers.Dequeue()); Console.WriteLine(numbers.Dequeue()); // Print(numbers); Console.WriteLine("Count = {0}", numbers.Count()); Console.WriteLine("Count with priority 1= {0}", numbers.GetCount(1)); Console.WriteLine("Count with priority 2= {0}", numbers.GetCount(2)); Console.WriteLine("Count with priority 3= {0}", numbers.GetCount(3)); Console.WriteLine("Count with priority 4= {0}", numbers.GetCount(4)); Console.WriteLine("Count with priority 5= {0}", numbers.GetCount(5)); Console.WriteLine("Count with priority 8= {0}", numbers.GetCount(8)); Console.WriteLine("Count with priority 11= {0}", numbers.GetCount(11)); numbers.Clear(); Console.WriteLine("Count = {0}", numbers.Count<int>()); // Print(numbers); numbers.Add(10); numbers.Add(11); Console.WriteLine(numbers.First()); Console.WriteLine(numbers.Last()); Console.WriteLine(); numbers.Enqueue(1, 5); numbers.Add(5); Print(numbers); Console.WriteLine(); } catch { Console.WriteLine("Is Exeption"); } Console.ReadKey(); }
private void SendQueued (PriorityQueue<EntityUpdate, double> m_entsqueue) { PriorityQueueItem<EntityUpdate, double> up; //Enqueue them all while (m_entsqueue.TryDequeue (out up)) { QueueEntityUpdate (up.Value); } m_entsqueue.Clear (); m_lastUpdatePos = (m_presence.IsChildAgent) ? m_presence.AbsolutePosition : m_presence.CameraPosition; }
public void WorkPriorityQueue() { PriorityQueue<string> q1 = new PriorityQueue<string>(); PriorityQueue<int> q2 = new PriorityQueue<int>(); PriorityQueue<Group> q3 = new PriorityQueue<Group>(); q1.Enqueue(new List<string> { "A", "B", "C" }, 5); q1.Enqueue(new List<string> { "F", "G", "H" }, 1); q1.Enqueue(new List<string> { "X", "V", "B" }, 0); q1.Enqueue(new List<string> { "Q", "W", "E" }, 1); q1.Enqueue("Z", 1); q2.Enqueue(new List<int> { 4, 5, 6 }, 5); q2.Enqueue(new List<int> { 1, 2, 3 }, 1); q2.Enqueue(new List<int> { 7, 8, 9 }, 0); q2.Enqueue(new List<int> { 0, 3, 5 }, 1); q2.Enqueue(9, 2); q3.Enqueue(new Group("CS1", 7), 1); q3.Enqueue(new Group("CS2", 14), 3); q3.Enqueue(new Group("CS3", 24), 5); q3.Enqueue(new Group("CS4", 31), 1); Console.WriteLine("Dequeue in Q1: " + q1.Dequeue()); Console.WriteLine("First in Q1: " + q1.First()); Console.WriteLine("First with priority = 1 in Q1: " + q1.First(1)); Console.WriteLine("Last in Q1: " + q1.Last()); Console.WriteLine("Last with priority = 1 in Q1: " + q1.Last(1)); Console.WriteLine("Count in Q1: " + q1.Count); Console.WriteLine("Count with priority = 1 in Q1: " + q1.GetCount(1)); Console.WriteLine("__________________________________________________"); Console.WriteLine("Dequeue in Q2: " + q2.Dequeue()); Console.WriteLine("First in Q2: " + q2.First()); Console.WriteLine("First with priority = 1 in Q2: " + q2.First(1)); Console.WriteLine("Last in Q2: " + q2.Last()); Console.WriteLine("Last with priority = 1 in Q2: " + q2.Last(1)); Console.WriteLine("Count in Q2: " + q2.Count); Console.WriteLine("Count with priority = 1 in Q2: " + q2.GetCount(1)); Console.WriteLine("__________________________________________________"); Console.WriteLine("Dequeue in Q3: " + q3.Dequeue().Title); Console.WriteLine("First in Q3: " + q3.First().Title); Console.WriteLine("First with priority = 1 in Q3: " + q3.First(1).Title); Console.WriteLine("Last in Q3: " + q3.Last().Title); Console.WriteLine("Last with priority = 1 in Q3: " + q3.Last(1).Title); Console.WriteLine("Count in Q3: " + q3.Count); Console.WriteLine("Count with priority = 1 in Q3: " + q3.GetCount(1)); Console.WriteLine("__________________________________________________"); bool q = q1.Contains(new Tuple<string, int>("7", 1)); Console.WriteLine("Contains('7', 1) in Q1: " + q); q = q1.Contains(new Tuple<string, int>("Z", 1)); Console.WriteLine("Contains('Z', 1) in Q1: " + q); q2.Clear(); Console.WriteLine("Clear in Q2: " + q2.Count); Tuple<string, int>[] array = new Tuple<string, int>[15]; q1.CopyTo(array, 0); for (int i = 0; i < array.Length; i++) { Console.WriteLine("Array[{0}] = ( {1} {2})", i, array[i].Item2, array[i].Item1); } Console.ReadLine(); }
public void TestClear() { var queue = new PriorityQueue<string> { "string " }; queue.Clear(); Assert.AreEqual(0, queue.Count); }
/// <summary> /// A discrete event simulation of combat over time. /// </summary> private void SimulateCombat(PriorityQueue<Spell> queue) { int threadID = System.Threading.Thread.CurrentThread.ManagedThreadId; float timer = 0; float timelimit = Options.Duration; //in seconds float latency = (Options.Latency / 1000); //in milliseconds DateTime start = DateTime.Now; Debug.WriteLine("-------------------"); Debug.WriteLine(String.Format("thread:[{0}] | time: {1:0.00} - simulation starts [timelimit: {2:0.00}]", threadID, timer, timelimit)); //Trace.TraceInformation("Combat simulation started!"); //Trace.Flush(); while (queue.Count != 0) { //get the spell at the front of the queue Spell spell = queue.Dequeue(); //this will be the next event that must occur in the combat timeline //so align the simulation timer to match the scheduledtime of the event timer = (spell.ScheduledTime); //events that are scheduled to occur after the simulation has ended are irrelevant if (timer >= timelimit) { timer = timelimit; queue.Clear(); continue; } #region if this is a dot spell, check that its ready if (spell.IsTicking) { //perform the tick Debug.WriteLine(String.Format("thread:[{0}] | time: {1:0.00} - {2} ticks for {3:0} damage", threadID, timer, spell.Name, spell.TickHitDamage())); //schedule the next tick spell.ScheduledTime += spell.TickTime(); //add it back to the queue queue.Enqueue(spell); //and skip to the next iteration continue; } #endregion //get the cast time or the GCD for this ability float casttime = spell.ExecuteTime() > 0 ? spell.ExecuteTime() : spell.GlobalCooldown(); //check if there is enough time left to cast this spell if ((timer + casttime + latency) < timelimit) { //TODO: recalculate stats to account for all combat effects (e.g. +spell, +haste, +spi, +crit bonuses etc) //spell.Stats = updatedStats; //or //spell.Execute(updatedStats); //the spell lands on the target, so calculate damage and trigger any related effects spell.Execute(); //prioritise - i.e. the next 'event' in the timeline //(think of this as the time when the spell can be recast [for direct damage spells] or the next tick [for dots]). spell.ScheduledTime += spell.GetTimeDelay(); timer += latency; //append to the events history //Events.Add(spell); if (spell.ScheduledTime < timelimit) { //this spell can be re-cast before the simulation ends, so add it back to the queue queue.Enqueue(spell); } else { //the simulation will end before this spell can be re-cast, so we wont bother adding it to the queue Debug.WriteLine(String.Format("thread:[{0}] | time: {1:0.00} - removing {2} - the simulation ends before it can be re-casted", threadID, timer, spell.Name)); } } else { //There is still some simulation time left, but not enough to cast the current spell. //This means that the simulation is almost finished. //However, there might be enough time to cast the next spell in the queue ... if (queue.Count > 0) { Spell next = queue.Peek(); Debug.WriteLine(String.Format("thread:[{0}] | time: {1:0.00} - not enough time to cast {2} [{3:0.00}s cast, {4:0.00}s lat] - trying next spell: {5}", threadID, timer, spell.Name, spell.ExecuteTime(), latency, next.Name)); } else { Debug.WriteLine(String.Format("thread:[{0}] | time: {1:0.00} - not enough time to cast {2} - [this was the last spell in the queue]", threadID, timer, spell.Name)); } } } Debug.WriteLine(String.Format("thread:[{0}] | time: {1:0.00} - simulation ends [no spells left in the queue]", threadID, timer)); DateTime stop = DateTime.Now; Debug.WriteLine(String.Format("thread:[{0}] | simulation time: {1} seconds", threadID, (stop - start).Seconds)); //Trace.TraceInformation("Combat simulation finished!"); //Trace.Flush(); }