Esempio n. 1
0
        public IEnumerable <Simular> FindSimular(string carId, string rulesSetId, string hashValue, double threshold, RulesSet set = null)
        {
            RulesSet.Rule[] workedRules = null;

            if (!_dictionary.ContainsKey(rulesSetId))
            {
                yield break;
            }

            foreach (var pair in _dictionary[rulesSetId])
            {
                if (pair.Key == carId)
                {
                    continue;
                }

                var value = set == null?RulesSet.CompareHashes(hashValue, pair.Value) : RulesSet.CompareHashes(hashValue, pair.Value, set, out workedRules);

                if (value > threshold)
                {
                    yield return(new Simular {
                        CarId = pair.Key, Value = value, WorkedRules = workedRules
                    });
                }
            }
        }
Esempio n. 2
0
        public IEnumerable <Simular> FindSimular([NotNull] string carId, [NotNull] string setId, [NotNull] byte[] hashValues, double threshold,
                                                 [NotNull] RulesSet set, bool keepWorkedRules)
        {
            var index = _setsKeys.IndexOf(setId);

            if (index < 0)
            {
                throw new Exception($"Set “{setId}” is not defined");
            }

            foreach (var pair in _dictionary)
            {
                if (pair.Key == carId)
                {
                    continue;
                }

                var pairHash = pair.Value[index];
                var value    = RulesSet.CompareHashes(hashValues, pairHash, set, keepWorkedRules, out var workedRules);
                if (value > threshold)
                {
                    yield return(new Simular {
                        CarId = pair.Key, Value = value, WorkedRules = workedRules
                    });
                }
            }
        }
Esempio n. 3
0
        public static double CompareHashes(string lhs, string rhs, RulesSet rules, out Rule[] workedRules)
        {
            var lhsLines = Encoding.UTF8.GetString(Convert.FromBase64String(lhs)).Split('\n');
            var rhsLines = Encoding.UTF8.GetString(Convert.FromBase64String(rhs)).Split('\n');

            var size = lhsLines.Length - 1;

            if (size != rhsLines.Length - 1 || size != rules._rules.Length)
            {
                workedRules = new Rule[] {};
                return(0.0);
            }

            var workedRulesList = new List <Rule>();

            var same = 0;

            for (var i = 0; i < size; i++)
            {
                if (lhsLines[i] == rhsLines[i])
                {
                    workedRulesList.Add(rules._rules[i]);
                    same++;
                }
            }

            workedRules = workedRulesList.ToArray();
            return((double)same / size);
        }
Esempio n. 4
0
        public IEnumerable<Simular> FindSimular(string carId, string rulesSetId, string hashValue, double threshold, RulesSet set = null) {
            RulesSet.Rule[] workedRules = null;

            if (!_dictionary.ContainsKey(rulesSetId)) yield break;

            foreach (var pair in _dictionary[rulesSetId]) {
                if (pair.Key == carId) continue;

                var value = set == null ? RulesSet.CompareHashes(hashValue, pair.Value) : RulesSet.CompareHashes(hashValue, pair.Value, set, out workedRules);
                if (value > threshold) {
                    yield return new Simular {CarId = pair.Key, Value = value, WorkedRules = workedRules};
                }
            }
        }
Esempio n. 5
0
 public static RulesEntry Create(KeyValuePair <string, string> x)
 {
     return(new RulesEntry(x.Key, x.Key.Split(':')[0], RulesSet.FromText(x.Value)));
 }
Esempio n. 6
0
 public RulesEntry(string id, string commonId, RulesSet rules)
 {
     Id       = id;
     CommonId = commonId;
     Rules    = rules;
 }
Esempio n. 7
0
        public static double CompareHashes(string lhs, string rhs, RulesSet rules, out Rule[] workedRules) {
            var lhsLines = Encoding.UTF8.GetString(Convert.FromBase64String(lhs)).Split('\n');
            var rhsLines = Encoding.UTF8.GetString(Convert.FromBase64String(rhs)).Split('\n');

            var size = lhsLines.Length - 1;
            if (size != rhsLines.Length - 1 || size != rules._rules.Length) {
                workedRules = new Rule[]{};
                return 0.0;
            }

            var workedRulesList = new List<Rule>();

            var same = 0;
            for (var i = 0; i < size; i++) {
                if (lhsLines[i] == rhsLines[i]) {
                    workedRulesList.Add(rules._rules[i]);
                    same++;
                }
            }

            workedRules = workedRulesList.ToArray();
            return (double)same / size;
        }
Esempio n. 8
0
        public static unsafe double CompareHashes([NotNull] byte[] lhs, [NotNull] byte[] rhs, [NotNull] RulesSet rules, bool keepWorkedRules,
                                                  [CanBeNull] out Rule[] workedRules)
        {
            if (lhs.Length != rhs.Length)
            {
                throw new Exception("Different amount of enties");
            }

            if (lhs.Length % sizeof(int) != 0)
            {
                throw new Exception("Array of integers required");
            }

            var workedRulesList = keepWorkedRules ? new List <Rule>() : null;

            var a     = lhs.Length / sizeof(int);
            var total = 0d;
            var same  = 0d;
            var zeros = true;

            fixed(byte *lhp = lhs)
            fixed(byte *rhp = rhs)
            {
                int *ld = (int *)lhp, rd = (int *)rhp;

                for (var i = 0; i < a; i++)
                {
                    var rule = rules._rules[i];

                    var l = ld[i];
                    var r = rd[i];

                    if (l == r)
                    {
                        if (l == -1)
                        {
                            continue;
                        }

                        workedRulesList?.Add(rule);
                        same += rule.Weight;

                        if (l != 0)
                        {
                            zeros = false;
                        }
                    }

                    total += rule.Weight;
                }
            }

            if (zeros)
            {
                workedRules = keepWorkedRules ? new Rule[0] : null;
                return(0d);
            }

            workedRules = workedRulesList?.ToArray();
            return(same / total);
        }