首先
{
interface IKeyValue<T
{
K GetKey(T x);
V GetValue(T x);
void SetValue(T x
}
}
接著
using System;
using System
namespace Skyiv
{
class KeyedPriorityQueue<T
{
IComparer<T> comparer;
IKeyValue<T
Dictionary<K
bool hasKey;
T[] heap;
public int Count { get; private set; }
public KeyedPriorityQueue(IKeyValue<T
public KeyedPriorityQueue(int capacity
public KeyedPriorityQueue(IComparer<T> comparer
public KeyedPriorityQueue(int capacity
{
this
this
this
this
this
}
public bool ContainsKey(K key)
{
return keys
}
public void Update(T v)
{
if (!hasKey) throw new NotSupportedException();
if (typeof(T)
if (!ContainsKey(kver
var id = keys[kver
var cmp = comparer
kver
if (cmp <
else if (cmp >
}
public void Push(T v)
{
if (Count >= heap
if (hasKey) keys[kver
heap[Count] = v;
SiftUp(Count++);
}
public T Pop()
{
var v = Top();
if (hasKey) keys
heap[
if (Count >
return v;
}
public T Top()
{
if (Count >
throw new InvalidOperationException(
}
void SiftUp(int n)
{
var v = heap[n];
for (var n
heap[hasKey ? (keys[kver
heap[hasKey ? (keys[kver
}
void SiftDown(int n)
{
var v = heap[n];
for (var n
{
if (n
if (comparer
heap[hasKey ? (keys[kver
}
heap[hasKey ? (keys[kver
}
}
}
[
From:http://tw.wingwit.com/Article/program/net/201311/15449.html