コード例 #1
0
        public void ShouldReduceRedundantNodes()
        {
            var tree = new Dictionary <string, List <string> >
            {
                { "A", new List <string>() },
                { "B", new List <string> {
                      "A"
                  } },
                { "C", new List <string> {
                      "B"
                  } },
                { "D", new List <string> {
                      "A", "C"
                  } }
            };

            var result = DependencyReductionFilter.Filter(tree);

            Assert.That(result["A"], Is.EquivalentTo(new List <string>()));
            Assert.That(result["B"], Is.EquivalentTo(new List <string> {
                "A"
            }));
            Assert.That(result["C"], Is.EquivalentTo(new List <string> {
                "B"
            }));
            Assert.That(result["D"], Is.EquivalentTo(new List <string> {
                "C"
            }));
        }
コード例 #2
0
        /// <summary>
        /// Performs action on each element.
        /// </summary>
        /// <param name="source">elements on </param>
        /// <param name="action">action to be performed on elements</param>
        /// <param name="depends">all elements returned by this expresion will be finished before given element</param>
        /// <param name="threads">maximum threads used except main thread</param>
        public static void ForEach <T>(this IEnumerable <T> source, Action <T> action, Func <T, IEnumerable <T> > depends,
                                       int threads = 1)
        {
            var tree = source.ToDictionary(x => x, x => depends(x).ToList());

            CircularityRule.Test(tree);
            tree = DependencyReductionFilter.Filter(tree);

            var elements = new ConcurrentDictionary <T, bool>(tree.Keys.ToDictionary(x => x, x => false));

            elements.Keys.ForEach(x => { action(x); elements[x] = true; }, x => tree[x].All(y => elements[y]), threads);
        }
コード例 #3
0
        public void ShouldLeavePlainTree()
        {
            var tree = new Dictionary <string, List <string> >
            {
                { "A", new List <string> {
                      "B"
                  } },
                { "B", new List <string> {
                      "C"
                  } },
                { "C", new List <string>() }
            };

            var result = DependencyReductionFilter.Filter(tree);

            Assert.That(result["A"], Is.EquivalentTo(new List <string> {
                "B"
            }));
            Assert.That(result["B"], Is.EquivalentTo(new List <string> {
                "C"
            }));
            Assert.That(result["C"], Is.EquivalentTo(new List <string>()));
        }