Exemplo n.º 1
0
        internal static IEnumerable <AnalysisProxy> UnionIter(this IEnumerable <AnalysisProxy> items, UnionComparer comparer, out bool wasChanged)
        {
#endif
            wasChanged = false;

            var asSet = items as IAnalysisSet;
            if (asSet != null && asSet.Comparer == comparer)
            {
                return(items);
            }

            var newItems  = new List <AnalysisProxy>();
            var anyMerged = true;

            while (anyMerged)
            {
                anyMerged = false;
                var matches = new Dictionary <AnalysisProxy, List <AnalysisProxy> >(comparer);

                foreach (var ns in items)
                {
                    List <AnalysisProxy> list;
                    if (matches.TryGetValue(ns, out list))
                    {
                        if (list == null)
                        {
                            matches[ns] = list = new List <AnalysisProxy>();
                        }
                        list.Add(ns);
                    }
                    else
                    {
                        matches[ns] = null;
                    }
                }

                newItems.Clear();

                foreach (var keyValue in matches)
                {
                    var item = keyValue.Key;
                    if (keyValue.Value != null)
                    {
                        foreach (var other in keyValue.Value)
                        {
                            bool merged;
#if FULL_VALIDATION
                            Validation.Assert(comparer.Equals(item, other));
#endif
                            item = comparer.MergeTypes(item, other, out merged);
                            if (merged)
                            {
                                anyMerged  = true;
                                wasChanged = true;
                            }
                        }
                    }
                    newItems.Add(item);
                }
                items = newItems;
            }

            return(items);
        }