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
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PriorityQueueInCSharp;

namespace PriorityQueueInCSharp
public class HospitalQueueComparer : IComparer<Patient>
{
public class HospitalQueueComparer : IComparer<Patient>
public int Compare(Patient? x, Patient? y)
{
public int Compare(Patient x, Patient y)
{
Console.WriteLine($"Comparing {x.Name} and {y.Name}\n\r");
ArgumentNullException.ThrowIfNull(x);
ArgumentNullException.ThrowIfNull(y);

if (x.Age == y.Age)
return 0;
else if(x.Age > y.Age)
return -1;
else
return 1;
}
Console.WriteLine($"Comparing {x.Name} and {y.Name}");
Console.WriteLine();

if (x.Age == y.Age)
return 0;
else if (x.Age > y.Age)
return -1;
else
return 1;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PriorityQueueInCSharp;

namespace PriorityQueueInCSharp
public class Patient
{
public class Patient
public string Name { get; set; } = string.Empty;
public int Age { get; set; }

public Patient(string name, int age)
{
public string Name { get; set; } = string.Empty;
public int Age { get; set; }
public Patient(string name, int age)
{
Name = name;
Age = age;
}
Name = name;
Age = age;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using PriorityQueueInCSharp;
using PriorityQueueInCSharp;

ExploreEqualPriorities();
ExploreSortingMechanism();

static void ExploreEqualPriorities()
{
var hospitalQueue = new PriorityQueue<Patient, int>(5);
hospitalQueue.Enqueue(new("Sarah", 23), 1);
hospitalQueue.Enqueue(new("Joe", 50), 1);
hospitalQueue.Enqueue(new("Elizabeth", 60), 1);
hospitalQueue.Enqueue(new("Natalie", 16), 1);
hospitalQueue.Enqueue(new("Angie", 25), 1);
while (hospitalQueue.Count > 0)
var hospitalQueue = new PriorityQueue<Patient, int>(5);
hospitalQueue.Enqueue(new("Sarah", 23), 1);
hospitalQueue.Enqueue(new("Joe", 50), 1);
hospitalQueue.Enqueue(new("Elizabeth", 60), 1);
hospitalQueue.Enqueue(new("Natalie", 16), 1);
hospitalQueue.Enqueue(new("Angie", 25), 1);

while (hospitalQueue.Count > 0)
Console.WriteLine(hospitalQueue.Dequeue().Name);
}

Expand All @@ -23,7 +27,8 @@ static void ExploreSortingMechanism()
new("Angie", 25),
};
var hospitalQueue = new PriorityQueue<Patient, Patient>(new HospitalQueueComparer());
patients.ForEach(p => {
patients.ForEach(p =>
{
Console.WriteLine($"Enqueueing {p.Name}");
hospitalQueue.Enqueue(p, p);
});
Expand All @@ -32,4 +37,4 @@ static void ExploreSortingMechanism()

while (hospitalQueue.Count > 0)
Console.WriteLine($"Dequeuing {hospitalQueue.Dequeue().Name}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace PriorityQueueInCSharp;

public class SynchronizedHospitalQueue
{
private readonly PriorityQueue<Patient, int> _queue = new();
private readonly Lock _lock = new();

public void Enqueue(Patient patient, int priority)
{
lock (_lock)
{
_queue.Enqueue(patient, priority);
}
}

public bool TryDequeue(out Patient? patient)
{
lock (_lock)
{
return _queue.TryDequeue(out patient, out _);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.1" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.1" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading
Loading