public static IVirtualForest <T> Prune <T>(
            this IVirtualForest <T> forest,
            Func <T, bool> predicate,
            PruneOption pruneOption)
        {
            PruneTreeWalker <T> walker =
                new PruneTreeWalker <T>(
                    forest.TreeWalker,
                    predicate,
                    pruneOption);

            switch (pruneOption)
            {
            case PruneOption.PruneDescendants:
                return
                    (forest
                     .ShallowCopy(
                         walker,
                         roots => roots));

            case PruneOption.PruneTree:
                return
                    (forest
                     .ShallowCopy(
                         walker,
                         roots => roots.Where(x => !predicate(x))));

            default:
                throw new NotImplementedException("Whoops!  Somebody introduced a new pruning option and forgot to update the switch block.");
            }
        }
Exemple #2
0
 public static IVirtualForest <T> Siblings <T>(this IVirtualForest <T> forest)
 {
     return
         (forest
          .ShallowCopy(roots =>
                       roots
                       .SelectMany(forest.TreeWalker.GetSiblings)));
 }
        public static IVirtualForest <DepthNode <T> > ScanDepth <T>(this IVirtualForest <T> forest)
        {
            DepthTreeWalker <T> walker = new DepthTreeWalker <T>(forest.TreeWalker);

            return(new VirtualForest <DepthNode <T> >(
                       walker,
                       forest.Roots.Select(x => new DepthNode <T>(x, 0))));
        }
 public static IVirtualForest <DepthNode <T> > MaxDepth <T>(
     this IVirtualForest <T> forest,
     long maxDepth)
 {
     return
         (forest
          .ScanDepth()
          .Prune(x => x.Depth == maxDepth, PruneOption.PruneDescendants));
 }
Exemple #5
0
 public static IVirtualForest <T> PrecedingSiblings <T>(
     this IVirtualForest <T> forest,
     IEqualityComparer <T> comparer)
 {
     return(new VirtualForest <T>(
                forest.TreeWalker,
                comparer,
                forest.Roots.SelectMany(x => forest.TreeWalker.GetPrecedingSiblings(x, comparer))));
 }
Exemple #6
0
 public static IEnumerable <TResult> Select <T, TResult>(
     this IVirtualForest <T> forest,
     Func <ITreeWalker <T>, T, int, TResult> selector)
 {
     return
         (forest
          .Roots
          .Select((x, i) => selector(forest.TreeWalker, x, i)));
 }
Exemple #7
0
 public static IEnumerable <TResult> SelectMany <T, TResult>(
     this IVirtualForest <T> forest,
     Func <ITreeWalker <T>, T, IEnumerable <TResult> > selector)
 {
     return
         (forest
          .Roots
          .SelectMany(x => selector(forest.TreeWalker, x)));
 }
Exemple #8
0
 internal static IVirtualForest <T> ShallowCopy <T>(
     this IVirtualForest <T> source,
     Func <IEnumerable <T>, IEnumerable <T> > rootsSelector)
 {
     return(new VirtualForest <T>(
                source.TreeWalker,
                source.Comparer,
                rootsSelector(source.Roots)));
 }
 public static IOrderedVirtualForest <T> OrderChildrenByDescending <T, TKey>(
     this IVirtualForest <T> forest,
     Func <T, TKey> keySelector)
 {
     return
         (forest
          .OrderChildrenByDescending(
              keySelector,
              null));
 }
 public static IVirtualForest <T> Descendants <T>(
     this IVirtualForest <T> forest,
     T key,
     IEqualityComparer <T> comparer)
 {
     return(new VirtualForest <T>(
                forest.TreeWalker,
                comparer,
                forest.TreeWalker.GetDescendants(forest.Roots, key, comparer)));
 }
 public static IVirtualForest <T> Children <T>(
     this IVirtualForest <T> forest,
     T key,
     IEqualityComparer <T> comparer)
 {
     return(new VirtualForest <T>(
                forest.TreeWalker,
                comparer,
                forest.Roots.Where(x => comparer.Equals(key, x))));
 }
Exemple #12
0
 public static IVirtualForest <T> PrecedingSiblings <T>(this IVirtualForest <T> forest)
 {
     return
         (forest
          .ShallowCopy(roots =>
                       roots
                       .SelectMany(y =>
                                   forest
                                   .TreeWalker
                                   .GetPrecedingSiblings(y, forest.Comparer))));
 }
Exemple #13
0
 public static IVirtualForest <T> Siblings <T>(this IVirtualForest <T> forest, IEqualityComparer <T> comparer)
 {
     return
         (forest
          .ShallowCopy(roots =>
                       roots
                       .SelectMany(x =>
                                   forest
                                   .TreeWalker
                                   .GetSiblings(x, comparer))));
 }
Exemple #14
0
 public static IVirtualForest <T> FollowingSiblingsAndSelf <T>(this IVirtualForest <T> forest)
 {
     return
         (forest
          .ShallowCopy(roots =>
                       roots
                       .SelectMany(y =>
                                   forest
                                   .TreeWalker
                                   .GetFollowingSiblingsAndSelf(y, forest.Comparer))));
 }
Exemple #15
0
 // TODO: what happens when two roots are siblings (have the same parent).
 // Should I deduplicate the results?  If so how?  Using an IEqualityComparer might cause
 // issues.
 public static IVirtualForest <T> Parents <T>(this IVirtualForest <T> forest)
 {
     return
         (forest
          .ShallowCopy(roots =>
                       roots
                       .SelectMany(x =>
                                   forest
                                   .TreeWalker
                                   .GetAncestors(x)
                                   .Take(1))));
 }
Exemple #16
0
 public static IVirtualForest <T> FollowingSiblingsAndSelf <T>(
     this IVirtualForest <T> forest,
     IEqualityComparer <T> comparer)
 {
     return(new VirtualForest <T>(
                forest.TreeWalker,
                comparer,
                forest.Roots.SelectMany(x =>
                                        forest
                                        .TreeWalker
                                        .GetFollowingSiblingsAndSelf(x, comparer))));
 }
 public static IVirtualForest <T> Level <T>(
     this IVirtualForest <T> forest,
     int depth)
 {
     return
         (forest
          .ShallowCopy(roots =>
                       roots
                       .SelectMany(x =>
                                   forest
                                   .TreeWalker
                                   .GetLevel(x, depth))));
 }
Exemple #18
0
        public static IVirtualForest <T> Internals <T>(this IVirtualForest <T> forest)
        {
            InternalsTreeWalker <T> walker = new InternalsTreeWalker <T>(forest.TreeWalker);

            return
                (forest
                 .ShallowCopy(
                     walker,
                     roots =>
                     roots
                     .Where(x =>
                            forest
                            .TreeWalker
                            .HasChildren(x))));
        }
Exemple #19
0
        public static IVirtualForest <WrappedNode <TBaseNode, TValue> > Scan <TBaseNode, TValue>(
            this IVirtualForest <TBaseNode> forest,
            Func <TBaseNode, TValue> seedFactory,
            Func <TBaseNode, TValue, TValue> accumulator,
            Func <TBaseNode, TValue, TValue> deccumulator)
        {
            ScanTreeWalker <TBaseNode, TValue> walker =
                new ScanTreeWalker <TBaseNode, TValue>(
                    forest.TreeWalker,
                    accumulator,
                    deccumulator);

            return(new VirtualForest <WrappedNode <TBaseNode, TValue> >(
                       walker,
                       forest.Roots.Select(x => new WrappedNode <TBaseNode, TValue>(x, seedFactory(x)))));
        }
 public static IOrderedVirtualForest <T> OrderChildrenBy <T, TKey>(
     this IVirtualForest <T> forest,
     Func <T, TKey> keySelector,
     IComparer <TKey> comparer)
 {
     return
         (new OrderedVirtualForest <T>(
              forest.TreeWalker,
              forest.Comparer,
              new CompoundComparer <T>(
                  new OrderByComparer <T, TKey>(
                      keySelector,
                      comparer ?? Comparer <TKey> .Default,
                      false)),
              forest.Roots));
 }
        public static IVirtualForest <T> Where <T>(
            this IVirtualForest <T> forest,
            Func <T, bool> predicate)
        {
            WhereTreeWalker <T> walker = new WhereTreeWalker <T>(forest.TreeWalker, predicate);

            return
                (forest
                 .ShallowCopy(
                     walker,
                     roots =>
                     roots
                     .SelectMany(x =>
                                 predicate(x) ?
                                 new T[] { x } :
                                 walker.GetDescendants(x, predicate))));
        }
        public void Descendants_ByKey_ArgumentComparerIsInvoked()
        {
            // Get a valid tree.
            var tree = TestTreeFactory.GetSimpleTree();

            // Create a mock IComparer.
            Mock <IEqualityComparer <Node <int> > > mockComparer = new Mock <IEqualityComparer <Node <int> > >();

            mockComparer
            .Setup(mock => mock.Equals(It.IsAny <Node <int> >(), It.IsAny <Node <int> >()))
            .Returns(true);

            // Create a VirtualForest.
            IVirtualForest <Node <int> > vt = VirtualForest.New(new NodeWalker <int>(), tree);

            // Create a key to use for comparison.  Any key is fine.
            Node <int> key = new Node <int>(0);

            // Execute Descendants.
            vt.Descendants(key, mockComparer.Object).Roots.ToArray();

            // Verify that the VirtualForest's comparer was used.
            //mockComparer.Verify(x => x.Equals(It.IsAny<Node<int>>(), It.IsAny<Node<int>>()), Times.Exactly(vt.GetDegree()));
        }
        public void ChildrenByKey_ArgumentComparerIsInvoked()
        {
            // Get a valid tree.
            var tree = TestTreeFactory.GetSimpleTree();

            // Create a mock IComparer.
            Mock <IEqualityComparer <Node <int> > > mockComparer = new Mock <IEqualityComparer <Node <int> > >();

            mockComparer
            .Setup(mock => mock.Equals(It.IsAny <Node <int> >(), It.IsAny <Node <int> >()))
            .Returns(true);

            // Create a VirtualForest.
            IVirtualForest <Node <int> > vt = VirtualForest.New(new NodeWalker <int>(), tree);

            // Create a key to use for comparison.  Any key is fine.
            Node <int> key = new Node <int>(0);

            // Execute the Children.
            vt.Children(key, mockComparer.Object).Roots.ToArray();

            // Verify that the comparer argument was used.
            mockComparer.Verify(x => x.Equals(key, It.IsAny <Node <int> >()), Times.AtLeastOnce);
        }
 public static IVirtualForest <T> Descendants <T>(
     this IVirtualForest <T> forest,
     Func <T, bool> predicate)
 {
     return(forest.ShallowCopy(roots => forest.TreeWalker.GetDescendants(roots, predicate)));
 }
 public static IVirtualForest <T> Children <T>(
     this IVirtualForest <T> forest,
     Func <T, bool> predicate)
 {
     return(forest.ShallowCopy(roots => forest.TreeWalker.GetChildren(roots, predicate)));
 }
Exemple #26
0
 public static IVirtualForest <T> ChildAt <T>(
     this IVirtualForest <T> forest,
     int index)
 {
     return(forest.ShallowCopy(roots => forest.TreeWalker.GetChildAt(roots, index)));
 }
 public static IVirtualForest <T> Descendants <T>(
     this IVirtualForest <T> forest,
     T key)
 {
     return(forest.ShallowCopy(roots => forest.TreeWalker.GetDescendants(roots, key, forest.Comparer)));
 }
 public static IVirtualForest <T> Children <T>(
     this IVirtualForest <T> forest,
     T key)
 {
     return(forest.ShallowCopy(roots => roots.Where(y => forest.Comparer.Equals(key, y))));
 }
 public static IVirtualForest <T> Children <T>(this IVirtualForest <T> forest)
 {
     return(forest.ShallowCopy(roots => roots.SelectMany(forest.TreeWalker.GetChildren)));
 }