maidh91 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Support for trees and traversals #174

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

As a suggestion; I find it quite useful!

/**
 * returns a depth-first iterator for a tree of T
 * @param f a function that returns the children of node of the tree
 * @param root The root element of the tree
 * @return
 */
public static <T> Iterable<T> walk(final T root,
    final Function<? super T, ? extends Iterable<? extends T>> getChildren) 
{
  final Function<T, Iterable<? extends T>> walkFunc =
      new Function<T, Iterable<? extends T>>() {
    public Iterable<? extends T> apply(final T from) {
      return walk(from, getChildren, this);
    }
  };
  return walk(root, getChildren, walkFunc);
}

private static <T> Iterable<T> walk(final T root,
  final Function<? super T, ? extends Iterable<? extends T>> getChildren,
    final Function<T, Iterable<? extends T>> walkFunction) {
  return Iterables.concat(
    Collections.singleton(root),
    Iterables.concat(
      Iterables.transform(
        getChildren.apply(root),
        walkFunction)));
}

And a shortcut in case the nodes implement Iterable:

@SuppressWarnings("unchecked")
public static <T extends Iterable<T>> Iterable<T> walk(final T root) {
  return walk(root, Functions.identity(), WalkIterableFunction.INSTANCE);
}

@SuppressWarnings("unchecked")
private static enum WalkIterableFunction implements Function {
  INSTANCE;

  public Object apply(final Object from) {
    return walk(from, Functions.identity(), this);
  }
};

Original issue reported on code.google.com by jvdne...@gmail.com on 25 May 2009 at 2:34

GoogleCodeExporter commented 9 years ago
I think it could also be something along the lines: 

https://github.com/JohannesLichtenberger/sirix/blob/master/bundles/sirix-core/sr
c/main/java/org/sirix/axis/visitor/VisitorDescendantAxis.java

and the package:

https://github.com/JohannesLichtenberger/sirix/tree/master/bundles/sirix-core/sr
c/main/java/org/sirix/api/visitor

which is modeled after the File walker Java7 API and might be really useful 
(however I don't know why the github sourcecode formatting, namely the tab-size 
is so ugly).

Probably the whole package (plus sub-packages)
https://github.com/JohannesLichtenberger/sirix/tree/master/bundles/sirix-core/sr
c/main/java/org/sirix/axis

is also interesting. The filters implement Predicate since some time ago and 
I've used some code from AbstractIterator of Guava to implement "AbstractAxis". 
Besides other things I'm currently thinking about how to integrate custom 
user-specified objects (currently it's used to store/query XML documents (and 
soon JSON) based on different versioning strategies). However, I think it's 
very simple to add custom types, probably implementing a simple Write- and 
Read-transaction which modifies/reads custom node-types. The user has to 
provide it's own serialization/deserialization if the in-memory store isn't 
selected. I'll likely implement insertion/update/delete/move-methods for a 
generic type parameter (I think the workaround with JAXB annotations is rather 
ugly).

Original comment by Lichtenb...@gmail.com on 20 Mar 2013 at 11:26

GoogleCodeExporter commented 9 years ago
Since TreeTraverser is in for 15.0, I'm gonna mark this as fixed. I think any 
specific enhancements or additions should be requested in separate issues.

Original comment by cgdecker@google.com on 3 Jul 2013 at 5:55

GoogleCodeExporter commented 9 years ago
I just wanted to say I finally migrated my graph library to use TreeTraverser 
with Louis's suggestion of using TreeTraverser<Walk<T>> for maintaining paths 
during the traversal. It works without any unexpected problems, and was a bit 
cleaner than what I had been doing previously; so thanks. The only dissonance 
remaining is that is that I'm using something named TreeTraverser for general 
graph traversals, but I can live with that.

Original comment by ray.a.co...@gmail.com on 5 Mar 2014 at 3:28

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:16

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:10