当前位置: 首页>>代码示例>>C#>>正文


C# ISet.SelectMany方法代码示例

本文整理汇总了C#中ISet.SelectMany方法的典型用法代码示例。如果您正苦于以下问题:C# ISet.SelectMany方法的具体用法?C# ISet.SelectMany怎么用?C# ISet.SelectMany使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ISet的用法示例。


在下文中一共展示了ISet.SelectMany方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SolveResourceDependencies

        /// <summary>
        /// Given a set of processes which are not interdependent, split up into multiple sets which do not use the same resource concurrently
        /// </summary>
        /// <param name="processes">The processes.</param>
        /// <returns></returns>
        private IEnumerable<ISet<OrderedProcess>> SolveResourceDependencies(ISet<OrderedProcess> processes)
        {
            // if there are no resources in this graph, or none of the processes in this set have any
            // resources, we can simply return the set of processes
            if (_resources.Count == 0 || !processes.SelectMany(p => p.ResourcesSet).Any())
                yield return processes;
            else
            {
                HashSet<HashSet<OrderedProcess>> result = new HashSet<HashSet<OrderedProcess>>();

                foreach (var process in processes)
                {
                    var process1 = process;

                    //all sets this process may be added to
                    IEnumerable<HashSet<OrderedProcess>> agreeableSets = result                     //from the set of result sets
                        .Where(set => set                                                           //select a candidate set to add to
                            .Where(p => p.ResourcesSet.Overlaps(process1.Resources))                //select processes whose resource usage overlaps this one
                            .IsEmpty());                                                            //if there are none which overlap, then this is a valid set

                    //the single best set to add to
                    HashSet<OrderedProcess> agreeableSet;

                    if (agreeableSets.IsEmpty())
                    {
                        //no sets can hold this process, create a new one
                        agreeableSet = new HashSet<OrderedProcess>();
                        result.Add(agreeableSet);
                    }
                    else
                        agreeableSet = agreeableSets.Aggregate((a, b) => a.Count < b.Count ? a : b);    //pick the smallest set

                    //finally, add this process to the selected set
                    agreeableSet.Add(process);
                }

                foreach (var set in result)
                    yield return set;
            }
        }
开发者ID:martindevans,项目名称:TopologicalSorting,代码行数:45,代码来源:DependencyGraph.cs


注:本文中的ISet.SelectMany方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。