本文整理汇总了C#中PriorityQueue.Add方法的典型用法代码示例。如果您正苦于以下问题:C# PriorityQueue.Add方法的具体用法?C# PriorityQueue.Add怎么用?C# PriorityQueue.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Simple
public void Simple()
{
var priorityQueue = new PriorityQueue<string, int>(PriorityQueueType.Minimum) { "5" };
Assert.AreEqual(priorityQueue.Count, 1);
Assert.IsTrue(priorityQueue.Contains("5"));
Assert.IsTrue(priorityQueue.Remove("5"));
Assert.AreEqual(priorityQueue.Count, 0);
priorityQueue.Add("6");
priorityQueue.Add("7");
priorityQueue.Add("2", 4);
Assert.AreEqual(priorityQueue.Count, 3);
Assert.IsTrue(priorityQueue.Contains("6"));
Assert.IsTrue(priorityQueue.Contains("7"));
Assert.IsTrue(priorityQueue.Contains("2"));
Assert.IsFalse(priorityQueue.Remove("8"));
Assert.IsTrue(priorityQueue.Remove("7"));
Assert.AreEqual(priorityQueue.Count, 2);
Assert.IsTrue(priorityQueue.Remove("2"));
Assert.AreEqual(priorityQueue.Count, 1);
Assert.IsTrue(priorityQueue.Remove("6"));
Assert.AreEqual(priorityQueue.Count, 0);
Assert.IsFalse(priorityQueue.Remove("7"));
}
示例2: RandomTest
public void RandomTest()
{
PriorityQueue<int> TestObject = new PriorityQueue<int>();
Utilities.Random.Random Rand = new Utilities.Random.Random();
int Value=0;
for (int x = 0; x < 10; ++x)
{
Value=Rand.Next();
TestObject.Add(x, Value);
Assert.Equal(Value, TestObject.Peek());
}
int HighestValue = TestObject.Peek();
for (int x = 9; x >= 0; --x)
{
Value = Rand.Next();
TestObject.Add(x, Value);
Assert.Equal(HighestValue, TestObject.Peek());
}
int Count=0;
foreach(int Priority in TestObject.Keys)
{
foreach(int Item in TestObject[Priority])
{
++Count;
}
}
Assert.Equal(20, Count);
}
示例3: AddExample
public void AddExample()
{
var priorityQueue = new PriorityQueue<string, int>(PriorityQueueType.Minimum);
priorityQueue.Add("cat");
priorityQueue.Add("dog");
priorityQueue.Add("canary");
// There will be 3 items in the priorityQueue.
Assert.AreEqual(3, priorityQueue.Count);
}
示例4: AddPriorityExample
public void AddPriorityExample()
{
var priorityQueue = new PriorityQueue<string, int>(PriorityQueueType.Minimum);
priorityQueue.Add("cat", 2);
priorityQueue.Add("canary", 1);
priorityQueue.Add("dog", 3);
// There will be 3 items in the priorityQueue.
Assert.AreEqual(3, priorityQueue.Count);
// "canary" will be at the top as it has the highest priority
Assert.AreEqual("canary", priorityQueue.Peek());
}
示例5: Simple
public void Simple()
{
var priorityQueue = new PriorityQueue<int, int>(PriorityQueueType.Maximum);
Assert.IsFalse(priorityQueue.IsReadOnly);
priorityQueue.Add(4);
Assert.IsFalse(priorityQueue.IsReadOnly);
priorityQueue.Add(99);
Assert.IsFalse(priorityQueue.IsReadOnly);
priorityQueue.Clear();
Assert.IsFalse(priorityQueue.IsReadOnly);
}
示例6: Simple
public void Simple()
{
var priorityQueue = new PriorityQueue<int, int>(PriorityQueueType.Minimum);
Assert.AreEqual(priorityQueue.Count, 0);
priorityQueue.Add(4);
Assert.AreEqual(priorityQueue.Count, 1);
priorityQueue.Add(99);
Assert.AreEqual(priorityQueue.Count, 2);
priorityQueue.Clear();
Assert.AreEqual(priorityQueue.Count, 0);
}
示例7: Main
public static void Main(string[] args)
{
var priorityQueue = new PriorityQueue<int>();
priorityQueue.Add(1);
priorityQueue.Add(2);
priorityQueue.Add(513);
priorityQueue.Add(51);
priorityQueue.Add(1551);
while (priorityQueue.Count > 0)
{
Console.WriteLine(priorityQueue.Dequeue());
}
}
示例8: Main
/* Task 1:
* Implement a class PriorityQueue<T> based on the data structure "binary heap".
*/
static void Main(string[] args)
{
var prioQueue = new PriorityQueue<int>();
prioQueue.Add(25);
prioQueue.Add(15);
prioQueue.Add(105);
prioQueue.Add(5);
prioQueue.Add(35);
Console.WriteLine(prioQueue.Count());
Console.WriteLine(prioQueue.Dequeue());
Console.WriteLine(prioQueue.Dequeue());
Console.WriteLine(prioQueue.Dequeue());
Console.WriteLine(prioQueue.Dequeue());
}
示例9: PriorityQueueGeneralTest
public void PriorityQueueGeneralTest()
{
var queue = new PriorityQueue<int>();
Assert.IsTrue(queue.Empty, "New queue should start out empty.");
Assert.Throws<InvalidOperationException>(() => queue.Peek(), "Peeking at an empty queue should throw.");
Assert.Throws<InvalidOperationException>(() => queue.Pop(), "Popping an empty queue should throw.");
foreach (var value in new[] { 4, 3, 5, 1, 2 })
{
queue.Add(value);
Assert.IsFalse(queue.Empty, "Queue should not be empty - items have been added.");
}
foreach (var value in new[] { 1, 2, 3, 4, 5 })
{
Assert.AreEqual(value, queue.Peek(), "Peek returned the wrong item - should be in order.");
Assert.IsFalse(queue.Empty, "Queue should not be empty yet.");
Assert.AreEqual(value, queue.Pop(), "Pop returned the wrong item - should be in order.");
}
Assert.IsTrue(queue.Empty, "Queue should now be empty.");
Assert.Throws<InvalidOperationException>(() => queue.Peek(), "Peeking at an empty queue should throw.");
Assert.Throws<InvalidOperationException>(() => queue.Pop(), "Popping an empty queue should throw.");
}
示例10: Simple
public void Simple()
{
var priorityQueue1 = new PriorityQueue<int, int>(PriorityQueueType.Minimum) { 4 };
Assert.AreEqual(priorityQueue1.Count, 1);
Assert.AreEqual(priorityQueue1.Dequeue(), 4);
priorityQueue1.Add(5);
priorityQueue1.Add(6, 2);
Assert.AreEqual(priorityQueue1.Dequeue(), 5);
Assert.AreEqual(priorityQueue1.Dequeue(), 6);
priorityQueue1.Add(6, 2);
priorityQueue1.Add(5);
Assert.AreEqual(priorityQueue1.Dequeue(), 5);
Assert.AreEqual(priorityQueue1.Dequeue(), 6);
var priorityQueue2 = new PriorityQueue<string, int>(PriorityQueueType.Minimum)
{
{"a", 1},
{"b", 2},
{"c", 3},
{"d", 4},
{"e", 5},
{"f", 6},
{"z", 6},
{"y", 5},
{"x", 4},
{"w", 3},
{"v", 2},
{"u", 1},
{"z", 1},
{"y", 2},
{"x", 3},
{"w", 4},
{"v", 5},
{"u", 6}
};
Assert.AreEqual(priorityQueue2.Count, 18);
priorityQueue2.Clear();
Assert.AreEqual(priorityQueue2.Count, 0);
}
示例11: Main
private static void Main()
{
var priorityQueue = new PriorityQueue<int>();
priorityQueue.Add(1);
priorityQueue.Add(1);
priorityQueue.Add(3);
priorityQueue.Add(1);
priorityQueue.Add(3);
priorityQueue.Add(10);
priorityQueue.Add(2);
priorityQueue.Add(2);
priorityQueue.Add(3);
priorityQueue.Add(2);
while (priorityQueue.Count != 0)
{
Console.WriteLine(priorityQueue.Dequeue());
}
}
示例12: WithPriority
public void WithPriority()
{
var priorityQueue = new PriorityQueue<string, int>(PriorityQueueType.Minimum);
int priority;
priorityQueue.Add("5", 3);
Assert.AreEqual(priorityQueue.Count, 1);
Assert.IsTrue(priorityQueue.Contains("5"));
Assert.IsTrue(priorityQueue.Remove("5", out priority));
Assert.AreEqual(priority, 3);
Assert.AreEqual(priorityQueue.Count, 0);
priorityQueue.Add("6");
priorityQueue.Add("7");
priorityQueue.Add("2", 4);
Assert.AreEqual(priorityQueue.Count, 3);
Assert.IsTrue(priorityQueue.Contains("6"));
Assert.IsTrue(priorityQueue.Contains("7"));
Assert.IsTrue(priorityQueue.Contains("2"));
Assert.IsFalse(priorityQueue.Remove("8", out priority));
Assert.IsTrue(priorityQueue.Remove("7", out priority));
Assert.AreEqual(priority, 0);
Assert.AreEqual(priorityQueue.Count, 2);
Assert.IsTrue(priorityQueue.Remove("2", out priority));
Assert.AreEqual(priority, 4);
Assert.AreEqual(priorityQueue.Count, 1);
Assert.IsTrue(priorityQueue.Remove("6", out priority));
Assert.AreEqual(priority, 0);
Assert.AreEqual(priorityQueue.Count, 0);
Assert.IsFalse(priorityQueue.Remove("7", out priority));
}
示例13: TestPriorityQueueCount
public void TestPriorityQueueCount()
{
PriorityQueue<int> pq = new PriorityQueue<int>();
pq.Add(5);
pq.Add(6);
pq.Add(3);
pq.Add(7);
pq.Add(11);
pq.Add(-5);
pq.GetTop();
pq.GetTop();
Assert.AreEqual(4, pq.Count, "Incorrectly counting the queue elements");
}
示例14: TestPriorityQueueOrder_2
public void TestPriorityQueueOrder_2()
{
PriorityQueue<int> pq = new PriorityQueue<int>();
pq.Add(5);
pq.Add(6);
pq.Add(3);
pq.Add(7);
Assert.AreEqual(3, pq.GetTop(), "Incorrect element returned!");
pq.Add(11);
pq.Add(-5);
Assert.AreEqual(-5, pq.GetTop(), "Incorrect element returned!");
pq.Add(6);
Assert.AreEqual(5, pq.GetTop(), "Incorrect element returned!");
Assert.AreEqual(6, pq.GetTop(), "Incorrect element returned!");
Assert.AreEqual(6, pq.GetTop(), "Incorrect element returned!");
Assert.AreEqual(7, pq.GetTop(), "Incorrect element returned!");
Assert.AreEqual(11, pq.GetTop(), "Incorrect element returned!");
}
示例15: TestWithPrimitiveType
public void TestWithPrimitiveType()
{
var priorityQueueInts = new PriorityQueue<int>();
priorityQueueInts.Add(5);
priorityQueueInts.Add(10);
priorityQueueInts.Add(-5);
priorityQueueInts.Add(1);
priorityQueueInts.Add(13);
priorityQueueInts.Add(13);
priorityQueueInts.Add(0);
priorityQueueInts.Add(25);
var numbersActualOrder = new List<int>();
while (priorityQueueInts.Count > 0)
{
numbersActualOrder.Add(priorityQueueInts.RemoveFirst());
}
var numbersExpectedOrder = new List<int>() { -5, 0, 1, 5, 10, 13, 13, 25 };
CollectionAssert.AreEqual(numbersExpectedOrder, numbersActualOrder);
}