Esempio n. 1
0
        /// <summary>
        ///     Determines whether the <see cref="GraphSet{T}" /> is cyclic.
        /// </summary>
        /// <typeparam name="T">type of element</typeparam>
        /// <param name="set">The set.</param>
        /// <returns>
        ///     <c>true</c> if the specified set is cyclic; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsCyclic <T>(this GraphSet <T> set)
        {
            var heads = set.GetNodes().Where(x => x.Parents.Count == 0).ToList();

            if (heads.Count == 0)
            {
                return(true);
            }
            var markedHeads = heads.Select(x => new MarkedNode <T>(x, new HashSet <GraphNode <T> >().ToIHashSet()));
            var stack       = new Stack <MarkedNode <T> >(markedHeads);

            while (stack.Any())
            {
                var node = stack.Pop();
                if (node.MarkingSet.Contains(node.Node))
                {
                    return(true);
                }
                node.MarkingSet.Add(node.Node);
                foreach (var child in node.Node.Children)
                {
                    stack.Push(new MarkedNode <T>(child, node.MarkingSet.Clone()));
                }
            }
            return(false);
        }
Esempio n. 2
0
        /// <summary>
        ///     Gets the when all task on a GraphSet of TaskPair
        /// </summary>
        /// <typeparam name="T">type of elements</typeparam>
        /// <param name="set">The set.</param>
        /// <returns>task</returns>
        public static Task GetWhenAllTask <T>(this GraphSet <TaskPair <T> > set)
        {
            var list = new List <Task>();

            foreach (var x in set.GetNodes())
            {
                list.Add(x.Value.Task);
            }
            var array = list.ToArray();

            return(Task.Factory.ContinueWhenAll(array, tasks => { }));
        }
Esempio n. 3
0
 /// <summary>
 ///     Get a <see cref="GraphSet{T}" /> with only elements
 ///     satisfying the <see cref="Predicate{T}" />
 /// </summary>
 /// <typeparam name="T">the type of elements</typeparam>
 /// <param name="set">The set.</param>
 /// <param name="predicate">The predicate.</param>
 /// <returns>new filtered <see cref="GraphSet{T}" /></returns>
 public static GraphSet <T> Filter <T>(this GraphSet <T> set, Predicate <T> predicate)
 {
     return(new GraphSet <T>(set.GetNodes().Where(x => predicate(x.Value)), predicate));
 }