c# - 如何在字典中查找重复的对?

标签 c# .net algorithm dictionary

我想计算 TCC 指标:

The Tight Class Cohesion (TCC) measures the ratio of the number of method pairs of directly connected visible methods in a class NDC(C) and the number of maximal possible method pairs of connections between the visible methods of a class NP(C). Two visible methods are directly connected, if they are accessing the same instance variables of the class. n is the number of visible methods leading to:

NP(C) = (n(n-1))/2

TCC(C) = NDC(C) / NP(C)

所以我编写了一个方法来解析我想要检查的类中的所有方法。此方法存储该类中的所有方法以及它们在字典中使用的字段,如下所示:

Dictionary<MethodDefinition, IList<FieldReference>> references = new Dictionary<MethodDefinition, IList<FieldReference>>();

那么现在,我如何迭代这个字典来检查上面提到的条件?如果我理解正确的话,我必须找到这两对使用同一组字段的方法?那么我怎样才能最好地做到这一点呢?我想我必须迭代字典并查看 IList 是否包含相同的集合? (即使顺序不同)?

有什么想法`吗?

我的代码如下,但无法正常工作:

class TCC
    {

        public static int calculate(TypeDefinition type)
        {
            int count = 0;


            Dictionary<MethodDefinition, HashSet<FieldReference>> references = new Dictionary<MethodDefinition, HashSet<FieldReference>>();

            foreach (MethodDefinition method in type.Methods)
            {
                if (method.IsPublic)
                {
                    references.Add(method, calculateReferences(method));
                }
            }

            for (int i = 0; i < references.Keys.Count; i++)
            {
                HashSet<FieldReference> list = new HashSet<FieldReference>();
                references.TryGetValue(references.Keys.ElementAt(i), out list);

                if (isPair(references, list)) {
                    count++;
                }

            }

            if (count > 0)
            {
                count = count / 2;
            }

            return count;
        }

        private static bool isPair(Dictionary<MethodDefinition, HashSet<FieldReference>> references, HashSet<FieldReference> compare)
        {
             for (int j = 0; j < references.Keys.Count; j++)
                {
                    HashSet<FieldReference> compareList = new HashSet<FieldReference>();
                    references.TryGetValue(references.Keys.ElementAt(j), out compareList);

                    for (int i = 0; i < compare.Count; i++)
                    {
                        if (containsAllElements(compareList, compare)) {
                            return true;
                        }
                    }
                }

             return false;
        }

        private static bool containsAllElements(HashSet<FieldReference> compareList, HashSet<FieldReference> compare)
        {
            for (int i = 0; i < compare.Count; i++)
            {
                if (!compareList.Contains(compare.ElementAt(i)))
                {
                    return false;
                }
            }
            return true;
        }

        private static HashSet<FieldReference> calculateReferences(MethodDefinition method)
        {
            HashSet<FieldReference> references = new HashSet<FieldReference>();
            foreach (Instruction instruction in method.Body.Instructions)
            {
                if (instruction.OpCode == OpCodes.Ldfld)
                {
                    FieldReference field = instruction.Operand as FieldReference;
                    if (field != null)
                    {
                        references.Add(field);
                    }
                }
            }

            return references;
        }
    }

最佳答案

好吧,如果你不介意再保留一本字典,我们可以用大锤子敲这东西。
简而言之,如果我们想象一个字典,其中ordered_set(field-references)是键,并且我们保留每个键的值列表......不用说这不是最聪明的方法,但它是快速、简单,并使用您已经熟悉的数据结构。

EG:
hashset< hashset < FieldReference >, Ilist< methods >> Favorite_delicatessen

Build ReferenceSet for method<br/> Look up ReferenceSet in Favorite_Delicatessen<br/> If there:<br/> Add method to method list<br/> Else:<br/> Add Referenceset,method pair

因此,如果您允许我创造一个术语,您的方法列表就是共享相同状态签名的方法列表。

关于c# - 如何在字典中查找重复的对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4268989/

相关文章:

c# - 以谓词为参数的方法

string - 字符串 2 的字谜是字符串 1 的子字符串

javascript - 从墙点定义 "inside room point"

c# - C#SIP音频和视频通话

c# - 从 .Net 应用程序(控制台)访问受 kerberos 保护的 webhdfs

c# - 二进制对象图序列化

java - 使 Sim Hash(局部敏感哈希)算法更准确?

c# - 使用 C# 的欧拉项目 #4

c# - 如何在编码模式下将超链接放入标签中?

c# - Visual Studio 中字符串的快捷方式