Created
September 29, 2018 00:14
-
-
Save jackmott/61b83f9ffd27e1c21adc0a4c2107ec93 to your computer and use it in GitHub Desktop.
pqueue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
namespace FB | |
{ | |
public class PQueue<T> | |
{ | |
private SortedDictionary<float, Stack<T>> sdict; | |
public PQueue() | |
{ | |
sdict = new SortedDictionary<float, Stack<T>>(); | |
} | |
public void Put(T thing, float priority) | |
{ | |
if (sdict.ContainsKey(priority)) | |
{ | |
sdict[priority].Push(thing); | |
} else | |
{ | |
var s = new Stack<T>(); | |
s.Push(thing); | |
sdict.Add(priority,s); | |
} | |
} | |
public T Get() | |
{ | |
var kvp = sdict.ElementAt(0); | |
var result = kvp.Value.Pop(); | |
if (kvp.Value.Count == 0) | |
{ | |
sdict.Remove(kvp.Key); | |
} | |
return result; | |
} | |
public bool Empty() | |
{ | |
return sdict.Count == 0; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment