Esempio n. 1
0
        /// <summary>
        ///     Transform a <see cref="GraphSet{T}" /> to <see cref="GraphSet{TX}" />.
        /// </summary>
        /// <typeparam name="T">the type of element</typeparam>
        /// <typeparam name="TX">The type of element of the new <see cref="GraphSet{T}" />.</typeparam>
        /// <param name="enumerable">The enumerable.</param>
        /// <param name="func">The function.</param>
        /// <returns>new <see cref="GraphSet{T}" /></returns>
        public static GraphSet <TX> ToGraphSet <T, TX>(this IEnumerable <GraphNode <T> > enumerable, Func <T, TX> func)
        {
            var nodes      = enumerable.ToArray();
            var dictionary = new Dictionary <GraphNode <T>, GraphNode <TX> >();
            var graphSet   = new GraphSet <TX>();

            foreach (var item in nodes)
            {
                var node = graphSet.Add(func(item.Value));
                dictionary[item] = node;
            }
            foreach (var item in nodes)
            {
                var currentNode = dictionary[item];
                foreach (var child in item.Children)
                {
                    var childNode = dictionary[child];
                    currentNode.AddChild(childNode);
                }
                foreach (var parent in item.Parents)
                {
                    var parentNode = dictionary[parent];
                    currentNode.AddParent(parentNode);
                }
            }
            return(graphSet);
        }
Esempio n. 2
0
 internal GraphNode(GraphSet <T> set, T value)
 {
     _set      = set;
     _parents  = new HashSet <GraphNode <T> >();
     _children = new HashSet <GraphNode <T> >();
     Value     = value;
     set.Add(this);
 }