C# 我想在C中遍历我的优先级队列#

C# 我想在C中遍历我的优先级队列#,c#,data-structures,C#,Data Structures,我在C#中实现了一个通用优先级队列,我使用SortedDictionary作为优先级键,队列作为值。 现在我想用一个foreach循环来迭代它,条件是跳过优先级为0的队列项目 这是我的优先级队列类 public class PQ<Tpriority, Titem> { readonly SortedDictionary<Tpriority, Queue<Titem>> value; public PQ(IComparer<Tp

我在C#中实现了一个通用优先级队列,我使用SortedDictionary作为优先级键,队列作为值。 现在我想用一个foreach循环来迭代它,条件是跳过优先级为0的队列项目

这是我的优先级队列类

      public class PQ<Tpriority, Titem>
{
    readonly SortedDictionary<Tpriority, Queue<Titem>> value;
    public PQ(IComparer<Tpriority> priorityComparer)
    {

        value = new SortedDictionary<Tpriority, Queue<Titem>>(priorityComparer);


    }
    public PQ() : this(Comparer<Tpriority>.Default) { }

    public bool Check{ get { return value.Any(); }}

    public int Count
    {
        get { return value.Sum(q => q.Value.Count); }
    }

    public void Add(Tpriority priority, Titem item)
    {
        if (!value.ContainsKey(priority))
        {
            AddQueueOfPriority(priority);
        }
        value[priority].Enqueue(item);
    }


    private void AddQueueOfPriority(Tpriority priority)
    {
        value.Add(priority, new Queue<Titem>());
    }

    public Titem Next()
    {
        if (value.Any())
            return Next_FHP();
        else
            throw new InvalidOperationException("The queue is empty");
    }

    private Titem Next_FHP()
    {
        KeyValuePair<Tpriority, Queue<Titem>> first = value.First();
        Titem nextItem = first.Value.Dequeue();
        if (!first.Value.Any())
        {
            value.Remove(first.Key);
        }
        return nextItem;
    }

    public SortedDictionary<Tpriority, Queue<Titem>>.Enumerator GetEnumerator()
    {
        return value.GetEnumerator();
    }
}

public interface IEnumerator<Tpriority, Titem>{}
公共级PQ
{
只读排序字典值;
公共PQ(I比较者优先比较者)
{
值=新分类词典(优先比较);
}
public PQ():此(Comparer.Default){}
公共布尔检查{get{返回值.Any();}}
公共整数计数
{
获取{return value.Sum(q=>q.value.Count);}
}
公共无效添加(优先级,滴度项)
{
如果(!value.ContainsKey(优先级))
{
优先权(优先权);
}
值[优先级]。排队(项目);
}
优先级的私有无效添加队列(优先级)
{
添加(优先级,新队列());
}
下一个公共时间()
{
if(value.Any())
返回下一个_FHP();
其他的
抛出新的InvalidOperationException(“队列为空”);
}
私人滴度下一个(FHP)
{
KeyValuePair first=value.first();
Titem nextItem=first.Value.Dequeue();
如果(!first.Value.Any())
{
value.Remove(第一个.Key);
}
返回nextItem;
}
public SortedDictionary.Enumerator GetEnumerator()
{
返回值。GetEnumerator();
}
}
公共接口IEnumerator{}
这是我的主要节目 就在这一天,我能够采取所有必要的行动,使工作顺利进行

    static
    void Main(string[] args)
    {
        var first = new PQ<int, string>();
        first.Add(1, "random1");
        first.Add(0, "random2");
        first.Add(2, "random3");

        while (first.Check)
        {
            Console.WriteLine(first.Next());
        }
        Console.WriteLine(first.Check);


        /*it gives the error "Can not convert
        type 'System.Collections.Generic.KeyValuePair<int,System.Collections.Generic.Queue<string>>'
        to exam.PQ<int, string>"*/

        foreach (PQ<int, string> h in first)
        {

        }
    }
静态
void Main(字符串[]args)
{
var first=新的PQ();
第一.加入(1,“第1条”);
第一,加上(0,“2”);
第一.加入(2,“3”);
while(首先检查)
{
Console.WriteLine(first.Next());
}
控制台。写入线(第一。检查);
/*它给出了错误“无法转换”
键入“System.Collections.Generic.KeyValuePair”
去考试。PQ“*/
foreach(第一个PQ h)
{
}
}

谢谢。

一种方法是在PQ类上添加您自己的方法,该方法返回通过(任何)验证检查的队列项集合

在你的PQ课上有类似的内容吗

 public IEnumerable<Queue<Titem>> GetItems(Tpriority priority)
 {
     var validKeys = value.Keys.Where(x => !x.Equals(priority) );
     return value.Where(x => validKeys.Contains(x.Key)).Select(x => x.Value);
 }
不过,您可能希望该方法有一个更好的名称,它清楚地说明了它将要做什么

更新

要打印出这些值,您可以这样做

public IEnumerable<Titem> GetItems(Tpriority priority)
{
    var validKeys = value.Keys.Where(x => !x.Equals(priority) );
    return value
        .Where(x => validKeys.Contains(x.Key))
        .SelectMany(x => x.Value);
}
public IEnumerable GetItems(优先级)
{
var validKeys=value.Keys.Where(x=>!x.Equals(priority));
返回值
.其中(x=>validKeys.Contains(x.Key))
.SelectMany(x=>x.Value);
}
在这里,它被分解成几个片段,并附上一些评论

public IEnumerable<Titem> GetItems(Tpriority priority)
    {
        // Find all the keys in the dictionary which do not match the supplied value
        var validKeys = value.Keys.Where(x => !x.Equals(priority) );

        // Find all the values in the dictionary where the key is in the valid keys list
        var validItems = value
            .Where(x => validKeys.Contains(x.Key));

        // Because an individual queue item is actually a collection of items,
        // SelectMany flattens them all into one collection
        var result = validItems.SelectMany(x => x.Value);

        return result;
    }
public IEnumerable GetItems(优先级)
{
//查找字典中与提供的值不匹配的所有键
var validKeys=value.Keys.Where(x=>!x.Equals(priority));
//查找字典中键位于有效键列表中的所有值
var validItems=值
其中(x=>validKeys.Contains(x.Key));
//因为单个队列项目实际上是项目的集合,
//SelectMany将它们全部展平到一个集合中
var result=validItems.SelectMany(x=>x.Value);
返回结果;
}

错误很明显。每个字典项都是一个
KeyValuePair
,因此不能迭代字典项(除非使逻辑复杂化)来处理每个队列。您试图解决什么问题?我曾考虑使用IEnumerator,但现在我知道我无法迭代,我不确定它是否有效,所以我可能会尝试另一种方法来实现优先级队列。谢谢谢谢方法和迭代可以工作,但它会打印出以下System.Collections.Generic.Queue1[System.String]System.Collections.Generic.Queue1[System.String.而不是值。队列是项目的集合。我将更新我的答案,以便只为您提供值,如果这是您想要的,请并感谢您。如果您觉得有帮助,请随时更新答案谢谢您做了很多完美的工作,现在我只需要理解它。
public IEnumerable<Titem> GetItems(Tpriority priority)
    {
        // Find all the keys in the dictionary which do not match the supplied value
        var validKeys = value.Keys.Where(x => !x.Equals(priority) );

        // Find all the values in the dictionary where the key is in the valid keys list
        var validItems = value
            .Where(x => validKeys.Contains(x.Key));

        // Because an individual queue item is actually a collection of items,
        // SelectMany flattens them all into one collection
        var result = validItems.SelectMany(x => x.Value);

        return result;
    }