C# 优先级队列在C语言中的实现#

C# 优先级队列在C语言中的实现#,c#,.net,data-structures,C#,.net,Data Structures,我正在尝试使用SortedDictionary实现一个优先级队列机制,我希望得到关于我当前实现的建议 我的执行情况如下: public class PriorityQueue { private Object lockObj; private SortedDictionary<PQMsgPriority, Queue<PQMessage>> messageDictionary; public PriorityQueue() {

我正在尝试使用SortedDictionary实现一个优先级队列机制,我希望得到关于我当前实现的建议

我的执行情况如下:

public class PriorityQueue
{
    private Object lockObj;
    private SortedDictionary<PQMsgPriority, Queue<PQMessage>> messageDictionary; 

    public PriorityQueue()
    {
        lockObj = new object();
        messageDictionary = new SortedDictionary<PQMsgPriority, Queue<PQMessage>>();
    }

    public void Enqueue(PQMessage item)
    {
        lock (lockObj)
        {
            if(item != null && item.MsgPriority == PQMsgPriority.None)
            {
                if (messageDictionary.ContainsKey(item.MsgPriority))
                {
                    Queue<PQMessage> dataList = messageDictionary[item.MsgPriority];
                    dataList.Enqueue(item);
                    messageDictionary[item.MsgPriority] = dataList;
                }
                else
                {
                    Queue<PQMessage> dataList = new Queue<PQMessage>();
                    dataList.Enqueue(item);
                    messageDictionary.Add(item.MsgPriority, dataList);
                }
            }
        }
    }

    public PQMessage Dequeue()
    {
        lock (lockObj)
        {
            PQMessage messageData = null;
            PQMsgPriority deleteKey = PQMsgPriority.None;

            //If no data available, throw an exception
            if (messageDictionary.Count == 0)
                throw new InvalidOperationException();

            foreach (KeyValuePair<PQMsgPriority, Queue<PQMessage>> item in messageDictionary)
            {
                Queue<PQMessage> dataList = item.Value;
                messageData = dataList.Dequeue();
                messageDictionary[item.Key] = dataList;

                //If there is no more elements remaining in the list, set a flag (deleteKey) for deleting the key
                if (dataList.Count == 0) 
                    deleteKey = item.Key;

                break;
            }

            //If the deleteKey flag is set, delete the key from the dictionary
            if (deleteKey != PQMsgPriority.None)
                messageDictionary.Remove(deleteKey);

            return messageData;
        }
    }

    public int Count()
    {
        lock (lockObj)
        {
            return messageDictionary.Count;
        }
    }

    public PQMessage Peek()
    {
        lock (lockObj)
        {
            PQMessage messageData = null;

            //If no data available, throw an exception
            if (messageDictionary.Count == 0)
                throw new InvalidOperationException();

            foreach (KeyValuePair<PQMsgPriority, Queue<PQMessage>> item in messageDictionary)
            {
                Queue<PQMessage> dataList = item.Value;
                messageData = dataList.Peek();
                break;
            }

            return messageData;
        }
    }
}

public enum PQMsgPriority
{
    High = 0,
    Medium = 1,
    Low = 2,
    None = 3
}

public class PQMessage
{
    private PQMsgPriority msgPriority;
    private Object message;

    #region Properties
    public PQMsgPriority MsgPriority
    {
        get { return msgPriority; }
        set { msgPriority = value; }
    }
    public Object Message
    {
        get { return message; }
        set { message = value; }
    }
    #endregion

    public PQMessage(PQMsgPriority msgPriority, Object message)
    {
        this.msgPriority = msgPriority;
        this.message = message;
    }
}
公共类优先队列
{
私有对象lockObj;
专用分类词典;
公共优先级队列()
{
lockObj=新对象();
messageDictionary=新的SortedDictionary();
}
公共无效排队(消息项)
{
锁(lockObj)
{
if(item!=null&&item.MsgPriority==PQMsgPriority.None)
{
if(messageDictionary.ContainsKey(item.MsgPriority))
{
Queue dataList=messageDictionary[item.MsgPriority];
dataList.Enqueue(项目);
messageDictionary[item.MsgPriority]=数据列表;
}
其他的
{
队列数据列表=新队列();
dataList.Enqueue(项目);
messageDictionary.Add(item.MsgPriority,dataList);
}
}
}
}
公共消息出列()
{
锁(lockObj)
{
PQMessage messageData=null;
PQMsgPriority deleteKey=PQMsgPriority.None;
//如果没有可用数据,则引发异常
if(messageDictionary.Count==0)
抛出新的InvalidOperationException();
foreach(messageDictionary中的KeyValuePair项)
{
队列数据列表=item.Value;
messageData=dataList.Dequeue();
messageDictionary[item.Key]=dataList;
//如果列表中没有其他元素,请设置删除键的标志(deleteKey)
如果(dataList.Count==0)
deleteKey=item.Key;
打破
}
//如果设置了deleteKey标志,则从字典中删除该键
if(deleteKey!=PQMsgPriority.None)
messageDictionary.Remove(deleteKey);
返回消息数据;
}
}
公共整数计数()
{
锁(lockObj)
{
返回messageDictionary.Count;
}
}
公共信息Peek()
{
锁(lockObj)
{
PQMessage messageData=null;
//如果没有可用数据,则引发异常
if(messageDictionary.Count==0)
抛出新的InvalidOperationException();
foreach(messageDictionary中的KeyValuePair项)
{
队列数据列表=item.Value;
messageData=dataList.Peek();
打破
}
返回消息数据;
}
}
}
公共枚举PQMSG优先级
{
高=0,
中等=1,
低=2,
无=3
}
公共类消息
{
私人PQMsgPriority msgPriority;
私有对象消息;
#区域属性
公共PQMSG优先级MSG优先级
{
获取{return msgPriority;}
设置{msgPriority=value;}
}
公共对象消息
{
获取{返回消息;}
设置{message=value;}
}
#端区
公共PQMessage(PQMsgPriority msgPriority,对象消息)
{
this.msgPriority=msgPriority;
this.message=消息;
}
}
如果有其他实现优先级队列的方法,请为我指出正确的方向

(我不想承认这一点,我假设您这样做是为了学习,而不是为了防弹的实现。如果您希望某个东西能够正常工作,那么最好重用现有的实现)

只是一些一般性的评论。在下面的示例中,第三行是不必要的

Queue<PQMessage> dataList = messageDictionary[item.MsgPriority];
dataList.Enqueue(item);
messageDictionary[item.MsgPriority] = dataList;
Queue dataList=messageDictionary[item.MsgPriority];
dataList.Enqueue(项目);
messageDictionary[item.MsgPriority]=数据列表;
messageDictionary
返回的
dataList
是映射中引用的副本。这意味着,当您
Enqueue
数据时,它与以前在相同的基础队列上工作(不是队列的副本),因此无需再次将其放回,您只需删除该行即可

在出列实现中,您有一个循环,每次在第一个元素上中断(例如,您只循环一次)。也许您可以研究使用LINQ获取
第一个
元素,然后直接返回它?(与
Peek
实现类似)


最后,给出<<代码> pqMeule本身有一个优先级,也许您可以考虑使用<代码> SoRealdList以实现?(请参阅)

如果需要提高并发性能,则只能锁定一个队列进行写入。(读取整个已分拣词典的锁,仍需整理)


别忘了按正确的顺序设置和释放锁

我不明白为什么要使用item.MsgPriority==PQMsgPriority.None在队列中?为了什么?问得好。如果设置了某些标志,则不需要将某些消息添加到队列中,而是为这些消息添加检查。是否有完整源代码的最终解决方案?