2

我必须为这样的表达式实现调用图Id = Id(Param);,这不是问题。

现在我必须实现一个枚举器,它一次列出一个满足依赖顺序的调用中的所有拓扑顺序。

这就是麻烦所在。

这是调用图的一个简单节点:

class CallGraphNode
{
    private string name;
    public List<CallGraphNode> dependents = new List<CallGraphNode>();
    public int dependencies;
    private bool executed = false;
    public bool Executable { get { return dependencies == 0; } }
    public bool Executed { get { return executed; } set { executed = value; } }

    public CallGraphNode(string name)
    {
        this.name = name;
        dependencies = 0;    
    }

    public override string ToString()
    {
        return name;
    }

    public void AddDependent(CallGraphNode n)
    {
        dependents.Add(n);
    }          
}

这是调用图类本身:

class CallGraph : IEnumerable<List<CallGraphNode>>
{
    public List<CallGraphNode> nodes = new List<CallGraphNode>();

    public void AddNode(CallGraphNode n)
    {
        nodes.Add(n);
    }

    public static void Show(IEnumerable<CallGraphNode> n)
    {
        foreach (CallGraphNode node in n)
        {
            Console.Write("{0} ", node);
        }
        Console.WriteLine();
    }

    static IEnumerable<List<CallGraphNode>> EnumerateFunctions(List<CallGraphNode> executable, List<CallGraphNode> res)
    {
        if (executable.Count == 0)
            yield return res;
        else foreach (CallGraphNode n in executable)
            {
                if (!n.Executed)
                    res.Add(n);
                List<CallGraphNode> next_executable = new List<CallGraphNode>(executable);
                executable.Remove(n);
                foreach (CallGraphNode m in n.dependents)
                    if (--m.dependencies == 0)
                        next_executable.Add(m);
                foreach (List<CallGraphNode> others in EnumerateFunctions(next_executable, res))
                    yield return others;
                foreach (CallGraphNode m in n.dependents)
                    m.dependencies++;
                if (!n.Executed)
                    res.Remove(n);
            }
    }

    IEnumerator<List<CallGraphNode>> IEnumerable<List<CallGraphNode>>.GetEnumerator()
    {
        List<CallGraphNode> executable = new List<CallGraphNode>();
        foreach (CallGraphNode n in nodes)
            if (n.Executable || n.Executed)
                executable.Add(n);
        List<CallGraphNode> output = new List<CallGraphNode>();
        foreach (List<CallGraphNode> list in EnumerateFunctions(executable, output))
            yield return list;
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    { throw new NotImplementedException(); }
}

现在,问题是它不起作用。当我尝试创建一个IEnumerator并为其分配GetEnumerator()返回值时,我得到一个转换错误,老实说,这正是我尝试这样做时所期望的:

IEnumerator<List<CallGraphNode>> lt = cg.GetEnumerator();

然后我试过了:

System.Collections.Generic.List<CallGraphNode>.Enumerator en = cg.nodes.GetEnumerator();

这可行,但该方法EnumerateFunctions永远不会被调用,并且枚举器只包含图形节点的原始列表。

有任何想法吗?

4

1 回答 1

5

问题是您同时 IEnumerable<T>实现并IEnumerable使用显式接口实现

您可能想要更改此声明:

IEnumerator<List<CallGraphNode>> IEnumerable<List<CallGraphNode>>.GetEnumerator()

成为“正常”的接口实现:

public IEnumerator<List<CallGraphNode>> GetEnumerator()

或者,您可以坚持使用显式接口实现,但使用:

IEnumerable<List<CallGraphNode>> sequence = cg;
IEnumerator<List<CallGraphNode>> lt = sequence.GetEnumerator();
于 2012-07-09T19:45:20.563 回答