Пример #1
0
        /// <summary>
        /// Executes the cloning actions specified by the context.
        /// </summary>
        public void Execute()
        {
            Contract.Requires(this.RootSource != null);
            Contract.Requires(this.RootTarget != null);

            var targetsByRoot = new Dictionary <object, IReadOnlyCollection <object> >(1)
            {
                { this.RootSource, new object[] { this.RootTarget } }
            };

            var clonersGetter = new ClonersGetDispatcher(this, targetsByRoot, this.ClonersBySource);

            var verticesSortedForCreation = TopologicalSorter.TopologicalSort(
                this.CilGraph.Vertices,
                ((IEnumerable <ICilEdge>) this.CilGraph.ParentChildEdges).Union(this.CilGraph.SiblingEdges));

            foreach (var source in verticesSortedForCreation)
            {
                this.ClonersBySource.Add(source, clonersGetter.InvokeFor(source));
            }

            var verticesSortedForCloning = TopologicalSorter.TopologicalSort(
                this.CilGraph.Vertices,
                this.CilGraph.DependencyEdges);

            foreach (var source in verticesSortedForCloning)
            {
                this.ClonersBySource[source].CloneAll();
            }
        }
Пример #2
0
        private IEnumerable <JsType> TopologicalSortTypesByInheritance(IEnumerable <JsType> types)
        {
            var backref = types.ToDictionary(c => c.CSharpTypeDefinition);
            var edges   = from s in backref.Keys from t in s.DirectBaseTypes.Select(x => x.GetDefinition()).Intersect(backref.Keys) select Tuple.Create(s, t);

            return(TopologicalSorter.TopologicalSort(backref.Keys, edges).Select(t => backref[t]));
        }
Пример #3
0
        public void TestGlobalSort <T>(Func <Dex, List <T> > provider, IComparer <T> comparer)
        {
            foreach (var file in GetTestFiles())
            {
                TestContext.WriteLine("Testing {0}", file);

                var dex   = Dex.Read(file);
                var list  = provider(dex);
                var items = new List <T>(list);
                items.Shuffle();
                items.Sort(comparer);

                if (comparer is IPartialComparer <T> partialComparer)
                {
                    var tsorter = new TopologicalSorter();
                    items = new List <T>(tsorter.TopologicalSort(items, partialComparer));
                }

                if (Extralog)
                {
                    DumpList(string.Concat(file, comparer.ToString(), ".expected.txt"), items);
                    DumpList(string.Concat(file, comparer.ToString(), ".actual.txt"), list);
                }

                for (var i = 0; i < items.Count; i++)
                {
                    var expected = items[i];
                    var actual   = list[i];

                    Assert.AreEqual(expected, actual);
                }
            }
        }
Пример #4
0
        internal static List <ClassDefinition> SortAndFlattenize(List <ClassDefinition> container)
        {
            var tsorter = new TopologicalSorter();

            container.Sort(new ClassDefinitionComparer());
            container = new List <ClassDefinition>(tsorter.TopologicalSort(container, new ClassDefinitionComparer()));

            return(Flattenize(container));
        }
Пример #5
0
        private static IEnumerable <DBTableSchema> SortTopologically(IEnumerable <DBTableSchema> tables, bool treatNullableForeignKeysAsDepedency)
        {
            var tableLookup = tables.ToLookup(table => table.Name);

            return(TopologicalSorter.TopologicalSort(
                       tables,
                       table => (
                           from foreignKey in table.ForeignKeys
                           where treatNullableForeignKeysAsDepedency || !foreignKey.IsNullable
                           select tableLookup[foreignKey.ReferenceTable]
                           ).Unpartition()
                       ));
        }
Пример #6
0
        /// <summary>
        /// Write the entire structure to the given writer.
        /// </summary>
        internal void WriteTo(BinaryWriter writer)
        {
            new ModelSorter().Collect(dex);
            //new ModelShuffler().Collect(Dex);
            map.Clear();

            stringLookup    = CollectStrings();
            typeLookup      = CollectTypeReferences();
            methodLookup    = CollectMethodReferences();
            fieldLookup     = CollectFieldReferences();
            prototypeLookup = CollectPrototypes();
            flatClasses     = ClassDefinition.Flattenize(dex.Classes);

            // Standard sort then topological sort
            var tsorter = new TopologicalSorter();
            var classDefinitionComparer = new ClassDefinitionComparer();

            flatClasses.Sort(classDefinitionComparer);
            flatClasses = new List <ClassDefinition>(tsorter.TopologicalSort(flatClasses, classDefinitionComparer));

            WriteHeader(writer);
            WriteStringId(writer);
            WriteTypeId(writer);
            WritePrototypeId(writer);
            WriteFieldId(writer);
            WriteMethodId(writer);
            WriteClassDef(writer);

            WriteAnnotationSetRefList(writer);
            WriteAnnotationSet(writer);
            WriteCodeItems(writer);
            WriteAnnotationsDirectoryItems(writer);
            WriteTypeLists(writer);
            WriteStringData(writer);
            WriteDebugInfo(writer);
            WriteAnnotations(writer);
            WriteEncodedArray(writer);
            WriteClassData(writer);

            WriteMapList(writer);

            ComputeSHA1Signature(writer);
            ComputeAdlerCheckSum(writer);
        }
Пример #7
0
        public void TestGlobalSort <T>(Func <Dex, List <T> > provider, IComparer <T> comparer)
        {
            foreach (var file in Directory.GetFiles(FilesDirectory))
            {
                TestContext.WriteLine("Testing {0}", file);

                var dex   = Dex.Read(file);
                var items = new List <T>(provider(dex));
                items.Shuffle();
                items.Sort(comparer);

                if (comparer is IPartialComparer <T> )
                {
                    var tsorter = new TopologicalSorter();
                    items = new List <T>(tsorter.TopologicalSort(items, comparer as IPartialComparer <T>));
                }

                for (var i = 0; i < items.Count; i++)
                {
                    Assert.AreEqual(items[i], provider(dex)[i]);
                }
            }
        }
Пример #8
0
        public void ShouldGiveEmptyResultForEmptyGraph()
        {
            var vertices = sorter.TopologicalSort(new Graph <int>());

            Assert.IsEmpty(vertices);
        }
Пример #9
0
 private static IEnumerable <Assembly> TopologicalSortPlugins(IList <Tuple <IUnresolvedAssembly, IList <string>, Assembly> > references)
 {
     return(TopologicalSorter.TopologicalSort(references, r => r.Item1.AssemblyName, references.SelectMany(a => a.Item2, (a, r) => Tuple.Create(a.Item1.AssemblyName, r)))
            .Select(r => r.Item3)
            .Where(a => a != null));
 }