Пример #1
0
        public MultiValueDict <TKey, TValue> IntersectBy <TInspect>(Func <TKey, TValue, TInspect> inspector)
        {
            var items = MapValues((i, o) => (Output: o, Inspect: inspector(i, o))).SortedByLength().ToList();

            if (items.Count == 1)
            {
                return(this);
            }

            var head    = items.First();
            var rest    = items.Rest();
            var commons = head.Value.Where(hp => rest.All(rp => rp.Value.Any(v => v.Inspect.Equals(hp.Inspect))))
                          .Select(p => p.Inspect);

            var dict = new MultiValueDict <TKey, TValue>();

            foreach (var p in items)
            {
                dict[p.Key] = new HashSet <TValue>();
                foreach (var common in commons)
                {
                    p.Value.Where(v => v.Inspect.Equals(common)).ToList().ForEach(x => dict[p.Key].Add(x.Output));
                }
            }
            return(dict);
        }
Пример #2
0
        public MultiValueDict <TKey, TValueNew> FlatMapValues <TValueNew>(Func <TKey, TValue, List <TValueNew> > mapper)
        {
            var dict = new MultiValueDict <TKey, TValueNew>();

            foreach (var key in Keys)
            {
                dict[key] = new HashSet <TValueNew>();
                foreach (var value in this[key])
                {
                    mapper(key, value).ForEach(e => dict[key].Add(e));
                }
            }
            return(dict);
        }
Пример #3
0
        /// <summary>
        /// Merges the specified multivaluedictionary into this instance.
        /// </summary>
        /// <param name="toMergeWith">To merge with.</param>
        public void Merge(MultiValueDict <TKey, TValue> toMergeWith)
        {
            if (toMergeWith == null)
            {
                return;
            }

            foreach (KeyValuePair <TKey, HashSet <TValue> > pair in toMergeWith)
            {
                foreach (TValue value in pair.Value)
                {
                    this.Add(pair.Key, value);
                }
            }
        }
Пример #4
0
        public IEnumerable <int> LocateErrFeatures(Label label, string token)
        {
            if (_errFeatureDict == null)
            {
                _errFeatureDict = new MultiValueDict <Record <Label, string>, int>();
                foreach (var p in err.FeatureChildren())
                {
                    foreach (var grp in p.child.Leaves().GroupBy(l => l.label))
                    {
                        // In this child, only leaf nodes with unique labels could be regarded as a feature.
                        if (grp.Count() == 1)
                        {
                            var grpLabel = grp.Key;
                            var grpToken = grp.AsEnumerable().First().code;
                            _errFeatureDict.Add(Record.Create(grpLabel, grpToken), p.index);
                        }
                    }
                }
            }

            return(_errFeatureDict.GetValues(Record.Create(label, token)));
        }
Пример #5
0
        private static Matching ComputeMatching(SyntaxNode target, SyntaxNode source)
        {
            var groups      = source.GetSubtrees().GroupBy(t => t.treeHash);
            var sourceTable = new MultiValueDict <int, SyntaxNode>(groups);

            var matching = new Matching();

            foreach (var tree in target.GetSubtrees())
            {
                foreach (var t in sourceTable.GetValues(tree.treeHash)) // `tree.hash = t.hash`
                {
                    if (matching.ContainsValue(tree, t))                // Tree `t` has been visited before.
                    {
                        continue;
                    }

                    // Let's check if `tree` and `t` are really identical.
                    if (tree.IdenticalTo(t)) // We find a match!
                    {
                        // Since `tree` is identical to `t`, so are their subtrees.
                        tree.GetSubtrees()
                        .Zip(t.GetSubtrees(), (t1, t2) => (tree: t1, t: t2))
                        .ToList()
                        .ForEach(pair => matching.Add(pair.tree, pair.t));
                    }
                    else
                    {
                        Log.Debug("Same hash {0} but different trees:", tree.treeHash);
                        var printer = new IndentPrinter();
                        tree.PrintTo(printer);
                        printer.PrintLine("<->");
                        t.PrintTo(printer);
                    }
                }
            }

            return(matching);
        }