Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Queue/Queue.Src/Program.cs
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());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это не нужно. Собирайте библиотеку и проверяйте работоспособность тестами

74 changes: 74 additions & 0 deletions Queue/Queue.Src/Queue.cs
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Не хватает заголовка с информацией о лицензии
  • namespace обычно пишут первой содержательной строкой в файле, сразу после лицензии, а using-и уже внутри


public class Queue<T>(int startCapacity = 1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо комментарии ещё

{
private Tuple<T, int>[] heap = new Tuple<T, int>[startCapacity];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tuple в современном C# не нужны, есть кортежи

public int CurrentCapacity = startCapacity;

Choose a reason for hiding this comment

The 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");

Choose a reason for hiding this comment

The 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;
}
10 changes: 10 additions & 0 deletions Queue/Queue.Src/Queue.csproj
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>
13 changes: 13 additions & 0 deletions Queue/Queue.Src/QueueIsEmptyException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace DataStructures;

[System.Serializable]
public class QueueIsEmtpyException : System.Exception

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class QueueIsEmtpyException : System.Exception
public class QueueIsEmptyException : 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) { }
}

24 changes: 24 additions & 0 deletions Queue/Queue.Tests/Queue.Test.cs
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]));
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тестов негусто. В частности, интересно поведение при одинаковых приоритетах

}
28 changes: 28 additions & 0 deletions Queue/Queue.Tests/Queue.Tests.csproj
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>
28 changes: 28 additions & 0 deletions Queue/Queue.sln
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