【问题标题】:Is there an Algorithm to index a collection on permutation of properties?是否有一种算法可以对属性排列的集合进行索引?
【发布时间】:2013-03-03 00:35:34
【问题描述】:

给定:

IMatchCriteria {
    string PropA{get;}
    string PropB{get;}
    int? PropC {get;}
    int? PropD {get;}
}

IReportRecord : IMatchCriteria {...}

IMatchCriteriaSet : IMatchCriteria {
    int MatchId {get;}
    double Limit{get;}
}

public class Worker{

private List<IMatchCriteriaSet> _matchers = GetIt();
//Expecting this list to be huge, ***upto 0.1m***. Some of the sample matchers:
// MatchId=1, Limit=1000, PropA=A, PropC=101, PropD=201
// MatchId=2, Limit=10,   PropA=A
// MatchId=3, Limit=20,            PropC=101
// MatchId=4, Limit=500,                      PropD=201

   //Based on sample entries:
   //Input: reportRecord{ PropA=A, PropC=101 }, Ouput: 1000, 20
   //Input: reportRecord{ PropA=A1, PropC=102, PropD=201 }, Ouput: 500
    public IEnumerable<double> GetMatchingLimits(IReportRecord reportRecord) {
         //Bad, very bad option:
         foreach(var matcher in _matchers){
             var matchFound=true;
             if(reportRecord.PropA!=null && reportRecord.PropA!=matcher.PropA){                
                 continue;
             }
             if(reportRecord.PropB!=null && reportRecord.PropA!=matcher.PropB){
                 continue;
             }
             if(reportRecord.PropC!=null && reportRecord.PropC.Value!=matcher.PropC.Value){
                 continue;
             }
             if(reportRecord.PropD!=null && reportRecord.PropD.Value!=matcher.PropD.Value){
                 continue;
             }
             yield return matcher.Limit;
         }
    }

    }

注意:期望 IMatchCriteriaSet 为 0.1m 条记录。 期望 GetMatchingLimits 被调用 1m 次。 要求是为实时应用程序执行所有这些操作。

基本上我需要的是一种索引 IMatchCriteria 列表的方法。但不能使用字典,因为我的键没有定义。 正在寻找一些算法来有效地解决这个问题。

.net(不仅仅是 c#)范围内的任何建议解决方案都会很有用。

谢谢。

【问题讨论】:

    标签: c# .net algorithm indexing


    【解决方案1】:

    为每个可索引属性使用一个字典,映射到一组匹配器。然后,您可以对记录中设置的每个属性(对数复杂度)进行字典查找,并与结果集相交。从最小的结果集开始,然后对其进行缩减以获得最佳运行时间。

    【讨论】:

    • 我刚刚找到了相同的解决方案。缺点是限制了属性匹配器(新属性需要新的字典和代码更改)。
    • 又发现了一个差距。即使我为每个属性创建一个字典,我也无法使用任何映射来减少集合!因为,对于每个属性,我需要考虑两组:1)范围内的属性值和匹配器的映射,2)未指定该属性的位置。在建议的解决方案中,我们将错过 2) 如果我们从 propertyMatcherDict 开始并减少集合。
    • “未指定”可以很容易地被视为特殊值 (null)。至于代码更改,是的,这是一个问题,但添加属性也是代码更改。为了本地化影响,将需要枚举属性的方法分解出来,并将它们放在靠近接口声明的地方。如果您有许多这样的接口,请考虑代码生成(编译时或通过动态方法运行时)。
    猜你喜欢
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2018-04-20
    • 1970-01-01
    • 2018-04-08
    • 2010-12-30
    • 1970-01-01
    相关资源
    最近更新 更多