-
Notifications
You must be signed in to change notification settings - Fork 0
queue #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
queue #12
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| using DataStructures; | ||
|
|
||
| var queue = new DataStructures.Queue<int>(); | ||
| 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()); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| using System.Security; | ||
|
|
||
| namespace DataStructures; | ||
|
Comment on lines
+1
to
+3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| public class Queue<T>(int startCapacity = 1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Надо комментарии ещё |
||
| { | ||
| private Tuple<T, int>[] heap = new Tuple<T, int>[startCapacity]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tuple в современном C# не нужны, есть кортежи |
||
| public int CurrentCapacity = startCapacity; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. public-полей быть не должно вообще |
||
| public int Count = -1; | ||
|
|
||
| public void Enqueue(T value, int priority) | ||
| { | ||
| if (Empty()) | ||
| { | ||
| throw new QueueIsEmtpyException("Queue is empty"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Делать эту проверку в Enqueue, очевидно, неправильно :) Из-за неё тесты не проходят. |
||
| } | ||
| if (Count == CurrentCapacity - 1) | ||
| { | ||
| IncreaseCapacity(); | ||
| } | ||
|
|
||
| ++Count; | ||
| heap[Count] = new Tuple<T, int>(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; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,13 @@ | ||||||
| namespace DataStructures; | ||||||
|
|
||||||
| [System.Serializable] | ||||||
| public class QueueIsEmtpyException : System.Exception | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| { | ||||||
| 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) { } | ||||||
| } | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<string>(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])); | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тестов негусто. В частности, интересно поведение при одинаковых приоритетах |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| <IsTestProject>true</IsTestProject> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
| <PackageReference Include="NUnit" Version="3.14.0" /> | ||
| <PackageReference Include="NUnit.Analyzers" Version="3.9.0" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Using Include="NUnit.Framework" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Queue.Src\Queue.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это не нужно. Собирайте библиотеку и проверяйте работоспособность тестами