diff --git a/Queue/Queue.Src/Program.cs b/Queue/Queue.Src/Program.cs new file mode 100644 index 0000000..3129e8e --- /dev/null +++ b/Queue/Queue.Src/Program.cs @@ -0,0 +1,24 @@ +using DataStructures; + +var queue = new DataStructures.Queue(); +int[] a = new int[10]; +Console.WriteLine(a.Length); +Console.WriteLine(queue.CurrentCapacity); +queue.Enqueue(1, 1); +Console.WriteLine(queue.CurrentCapacity); +queue.Enqueue(2, 2); +Console.WriteLine(queue.CurrentCapacity); +queue.Enqueue(6, 6); +Console.WriteLine(queue.CurrentCapacity); +queue.Enqueue(8, 8); +Console.WriteLine(queue.CurrentCapacity); +queue.Enqueue(3, 3); +Console.WriteLine(queue.CurrentCapacity); +Console.WriteLine(); + +Console.WriteLine(queue.Dequeue()); +Console.WriteLine(queue.Dequeue()); +Console.WriteLine(queue.Dequeue()); +Console.WriteLine(queue.Dequeue()); +Console.WriteLine(queue.Dequeue()); +Console.WriteLine(queue.Dequeue()); \ No newline at end of file diff --git a/Queue/Queue.Src/Queue.cs b/Queue/Queue.Src/Queue.cs new file mode 100644 index 0000000..b67d0b0 --- /dev/null +++ b/Queue/Queue.Src/Queue.cs @@ -0,0 +1,74 @@ +using System.Security; + +namespace DataStructures; + +public class Queue(int startCapacity = 1) +{ + private Tuple[] heap = new Tuple[startCapacity]; + public int CurrentCapacity = startCapacity; + public int Count = -1; + + public void Enqueue(T value, int priority) + { + if (Empty()) + { + throw new QueueIsEmtpyException("Queue is empty"); + } + if (Count == CurrentCapacity - 1) + { + IncreaseCapacity(); + } + + ++Count; + heap[Count] = new Tuple(value, priority); + + MoveUp(Count); + } + + public T Dequeue() + { + T result = heap[0].Item1; + heap[0] = heap[Count--]; + MoveDown(0); + return result; + } + + public bool Empty() => Count == 0; + + private void IncreaseCapacity() + { + CurrentCapacity *= 2; + Array.Resize(ref heap, CurrentCapacity); + } + + private void MoveUp(int i) + { + int parent = ParentIndex(i); + while (i > 0 && heap[parent].Item2 < heap[i].Item2) + { + Swap(parent, i); + i = parent; + parent = ParentIndex(i); + } + } + + private void MoveDown(int i) + { + int current = i; + int left = LeftChildIndex(current); + current = left <= Count && heap[left].Item2 > heap[current].Item2 ? left : current; + int right = RightChildIndex(current); + current = right <= Count && heap[right].Item2 > heap[current].Item2 ? right : current; + + if (i != current) + { + Swap(current, i); + MoveDown(current); + } + } + + private void Swap(int a, int b) => (heap[b], heap[a]) = (heap[a], heap[b]); + private static int ParentIndex(int index) => (index - 1) / 2; + private static int LeftChildIndex(int index) => (2 * index) + 1; + private static int RightChildIndex(int index) => (2 * index) + 2; +} diff --git a/Queue/Queue.Src/Queue.csproj b/Queue/Queue.Src/Queue.csproj new file mode 100644 index 0000000..206b89a --- /dev/null +++ b/Queue/Queue.Src/Queue.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/Queue/Queue.Src/QueueIsEmptyException.cs b/Queue/Queue.Src/QueueIsEmptyException.cs new file mode 100644 index 0000000..f55cae4 --- /dev/null +++ b/Queue/Queue.Src/QueueIsEmptyException.cs @@ -0,0 +1,13 @@ +namespace DataStructures; + +[System.Serializable] +public class QueueIsEmtpyException : System.Exception +{ + public QueueIsEmtpyException() { } + public QueueIsEmtpyException(string message) : base(message) { } + public QueueIsEmtpyException(string message, System.Exception inner) : base(message, inner) { } + protected QueueIsEmtpyException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } +} + diff --git a/Queue/Queue.Tests/Queue.Test.cs b/Queue/Queue.Tests/Queue.Test.cs new file mode 100644 index 0000000..2ceff43 --- /dev/null +++ b/Queue/Queue.Tests/Queue.Test.cs @@ -0,0 +1,24 @@ +namespace Queue.Tests; +using DataStructures; + +public class Tests +{ + private readonly int testSize = 6; + private readonly int[] priorities = [1, 2, 6, 3, 8, 4]; + private readonly string[] values = ["one", "two", "six", "three", "eight", "four"]; + private readonly string[] answers = ["eight", "six", "four", "three", "two", "one"]; + + [Test] + public void TestQueue() + { + var queue = new DataStructures.Queue(testSize + 1); + for (int i = 0; i < testSize; ++i) + { + queue.Enqueue(values[i], priorities[i]); + } + for (int i = 0; i < testSize; ++i) + { + Assert.That(queue.Dequeue, Is.EqualTo(answers[i])); + } + } +} \ No newline at end of file diff --git a/Queue/Queue.Tests/Queue.Tests.csproj b/Queue/Queue.Tests/Queue.Tests.csproj new file mode 100644 index 0000000..faca671 --- /dev/null +++ b/Queue/Queue.Tests/Queue.Tests.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + diff --git a/Queue/Queue.sln b/Queue/Queue.sln new file mode 100644 index 0000000..e8bdc91 --- /dev/null +++ b/Queue/Queue.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Queue", "Queue.Src\Queue.csproj", "{8F95C232-4F66-475D-9779-8733EFA231D3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Queue.Tests", "Queue.Tests\Queue.Tests.csproj", "{991979B8-471F-4930-AB00-061351951E1A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8F95C232-4F66-475D-9779-8733EFA231D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8F95C232-4F66-475D-9779-8733EFA231D3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8F95C232-4F66-475D-9779-8733EFA231D3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8F95C232-4F66-475D-9779-8733EFA231D3}.Release|Any CPU.Build.0 = Release|Any CPU + {991979B8-471F-4930-AB00-061351951E1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {991979B8-471F-4930-AB00-061351951E1A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {991979B8-471F-4930-AB00-061351951E1A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {991979B8-471F-4930-AB00-061351951E1A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal