示例#1
0
        private void SortTopologically(ICollection <int> result, out bool loop)
        {
            var graph = ListPool <List <int> > .Rent();

            for (var i = 0; i < _dependencies.Count; i++)
            {
                graph.Add(ListPool <int> .Rent());
            }

            for (var i = 0; i < _dependencies.Count; i++)
            {
                for (var j = 0; j < _dependencies.Count; j++)
                {
                    var dependency1 = _dependencies[i];
                    var dependency2 = _dependencies[j];

                    var type1 = dependency1.GetTypeOrObjectType();
                    var type2 = dependency2.GetTypeOrObjectType();
                    if (!Dependency.DependsOn(type2, type1))
                    {
                        continue;
                    }

                    graph[i].Add(j);
                }
            }

            TopologicalSorting.Sort(graph, _dependencies.Count, result, out loop);

            foreach (var graphList in graph)
            {
                ListPool <int> .Return(graphList);
            }

            ListPool <List <int> > .Return(graph);
        }
示例#2
0
        public bool TrySortTopologically(bool dryRun = false)
        {
            var graph = ListPool <List <int> > .Rent();

            for (var i = 0; i < _dependencies.Count; i++)
            {
                graph.Add(new List <int>());
            }

            for (var i = 0; i < _dependencies.Count; i++)
            {
                for (var j = 0; j < _dependencies.Count; j++)
                {
                    var dependency1 = _dependencies[i];
                    var dependency2 = _dependencies[j];

                    var type1 = dependency1.GetTypeOrObjectType();
                    var type2 = dependency2.GetTypeOrObjectType();
                    if (!Dependency.DependsOn(type2, type1))
                    {
                        continue;
                    }

                    graph[i].Add(j);
                }
            }

            var result = ListPool <int> .Rent();

            SortTopologically(result, out var loop);

            if (loop)
            {
                ListPool <int> .Return(result);

                return(false);
            }

            var sortedDependencies = ListPool <Dependency> .Rent();

            for (var resultIndex = result.Count - 1; resultIndex >= 0; resultIndex--)
            {
                var index = result[resultIndex];
                sortedDependencies.Add(_dependencies[index]);
            }

            if (!dryRun)
            {
                _dependencies.Clear();

                foreach (var dependency in sortedDependencies)
                {
                    _dependencies.Add(dependency);
                }
            }

            ListPool <Dependency> .Return(sortedDependencies);

            ListPool <int> .Return(result);

            return(true);
        }