Skip to content

Commit

Permalink
Add John.PriorityQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
john72831 committed Mar 6, 2023
1 parent 5acd316 commit 1d9941c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
10 changes: 10 additions & 0 deletions John.PriorityQueue/John.PriorityQueue.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>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
39 changes: 39 additions & 0 deletions John.PriorityQueue/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Diagnostics;

var source = new CancellationTokenSource();
var queue = new PriorityQueue<string, (Status, long)>(StatusCompare.Instance);

queue.Enqueue("Mike", (Status.Normal, Stopwatch.GetTimestamp()));
queue.Enqueue("Nike", (Status.Gold, Stopwatch.GetTimestamp()));
queue.Enqueue("John", (Status.Gold, Stopwatch.GetTimestamp()));
queue.Enqueue("Poke", (Status.Normal, Stopwatch.GetTimestamp()));


while (!source.IsCancellationRequested)
{
if (queue.Count > 0)
{
await Task.Delay(1000);
Console.WriteLine(queue.Dequeue());
}
}

enum Status
{
Normal,
Gold,
Platinum
}

class StatusCompare : IComparer<(Status, long)>
{
public static readonly StatusCompare Instance = new();

int IComparer<(Status, long)>.Compare((Status, long) x, (Status, long) y)
{
if (x.Item1 == y.Item1)
return x.CompareTo(y);

return y.CompareTo(x);
}
}
9 changes: 8 additions & 1 deletion John.sln
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "John.BenDemystifier", "John
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "John.ImplicitAndExplicitOperator", "John.ImplicitAndExplicitOperator\John.ImplicitAndExplicitOperator.csproj", "{8E8DCFE9-FDCB-4378-A06B-902DE9C4E68B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "John.Mapster", "John.Mapster\John.Mapster.csproj", "{1CD3B12A-9336-4708-A670-13F04C469367}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "John.Mapster", "John.Mapster\John.Mapster.csproj", "{1CD3B12A-9336-4708-A670-13F04C469367}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "John.PriorityQueue", "John.PriorityQueue\John.PriorityQueue.csproj", "{0F3DCA4A-02A7-47D9-A708-1C62BE620FDD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -103,6 +105,10 @@ Global
{1CD3B12A-9336-4708-A670-13F04C469367}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1CD3B12A-9336-4708-A670-13F04C469367}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1CD3B12A-9336-4708-A670-13F04C469367}.Release|Any CPU.Build.0 = Release|Any CPU
{0F3DCA4A-02A7-47D9-A708-1C62BE620FDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0F3DCA4A-02A7-47D9-A708-1C62BE620FDD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F3DCA4A-02A7-47D9-A708-1C62BE620FDD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F3DCA4A-02A7-47D9-A708-1C62BE620FDD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -124,6 +130,7 @@ Global
{DC42C11D-4856-4ECD-9791-58ECFBDA3762} = {0E4DC26C-0E24-419F-A8F8-73D7C7172263}
{8E8DCFE9-FDCB-4378-A06B-902DE9C4E68B} = {A9756AC7-5794-4122-8CD4-EA27917AC914}
{1CD3B12A-9336-4708-A670-13F04C469367} = {0E4DC26C-0E24-419F-A8F8-73D7C7172263}
{0F3DCA4A-02A7-47D9-A708-1C62BE620FDD} = {A9756AC7-5794-4122-8CD4-EA27917AC914}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4A169F98-AF3D-41C2-86AA-956352BF2D93}
Expand Down

0 comments on commit 1d9941c

Please sign in to comment.