简体   繁体   中英

Is there an Algorithm to index a collection on permutation of properties?

Given:

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;
         }
    }

    }

Note: Expecting IMatchCriteriaSet to be 0.1m records. Expecting GetMatchingLimits to be called 1m times. The requirement is to do all this for a real-time application.

Essentially what I need is a way to index list of IMatchCriteria. But can not use Dictionary because my key is not defined. Looking for some algorithm to tackle this problem efficiently .

Any suggested solution in scope of .net (not just c#) would be useful.

Thanks.

Use one dictionary for each indexable property, mapping to a set of matchers. Then you can do a dictionary lookup for every property that is set in your record (logarithmic complexity), and intersect the resulting sets. Start with the smallest result set and whittle it down to get the best run time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM