Skip to content

Commit

Permalink
Automated g4 rollback of changelist 131198531.
Browse files Browse the repository at this point in the history
*** Reason for rollback ***

The ArrayDeque implementation in upstream GWT has a bug that is being fixed in https://gwt-review.googlesource.com/#/c/16680/

*** Original change description ***

Use ArrayDeque and PriorityQueue under GWT (where they're newly available), and provide synchronizedQueue, synchronizedDeque, and MinMaxPriorityQueue there.
This also lets us delete our manual implementation of TreeTraverser and avoid LinkedList internally.

Fixes google#2545

***

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=131218608
  • Loading branch information
Dominator008 authored and cpovirk committed Aug 25, 2016
1 parent 760da03 commit 5ebbc98
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import jsinterop.annotations.JsType;

import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;

/**
* Minimal GWT emulation of {@code com.google.common.collect.Platform}.
Expand Down Expand Up @@ -52,5 +54,9 @@ static MapMaker tryWeakKeys(MapMaker mapMaker) {
return mapMaker;
}

static <E> Deque<E> newFastestDeque(int ignored) {
return new LinkedList<E>();
}

private Platform() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/*
* Copyright (C) 2012 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.collect;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

/**
* Views elements of a type {@code T} as nodes in a tree, and provides methods to traverse the trees
* induced by this traverser.
*
* <p>For example, the tree <pre> {@code
*
* h
* / | \
* / e \
* d g
* /|\ |
* / | \ f
* a b c
* }</pre>
*
* can be iterated over in preorder (hdabcegf), postorder (abcdefgh), or breadth-first order
* (hdegabc).
*
* <p>Null nodes are strictly forbidden.
*
* @author Louis Wasserman
*/
public abstract class TreeTraverser<T> {

/**
* Returns the children of the specified node. Must not contain null.
*/
public abstract Iterable<T> children(T root);

/**
* Returns an unmodifiable iterable over the nodes in a tree structure, using pre-order
* traversal. That is, each node's subtrees are traversed after the node itself is returned.
*
* <p>No guarantees are made about the behavior of the traversal when nodes change while
* iteration is in progress or when the iterators generated by {@link #children} are advanced.
*/
public final FluentIterable<T> preOrderTraversal(final T root) {
checkNotNull(root);
return new FluentIterable<T>() {
@Override
public UnmodifiableIterator<T> iterator() {
return preOrderIterator(root);
}
};
}

// overridden in BinaryTreeTraverser
UnmodifiableIterator<T> preOrderIterator(T root) {
return new PreOrderIterator(root);
}

private final class PreOrderIterator extends UnmodifiableIterator<T> {
private final LinkedList<Iterator<T>> stack;

PreOrderIterator(T root) {
this.stack = Lists.newLinkedList();
stack.addLast(Iterators.singletonIterator(checkNotNull(root)));
}

@Override
public boolean hasNext() {
return !stack.isEmpty();
}

@Override
public T next() {
Iterator<T> itr = stack.getLast(); // throws NSEE if empty
T result = checkNotNull(itr.next());
if (!itr.hasNext()) {
stack.removeLast();
}
Iterator<T> childItr = children(result).iterator();
if (childItr.hasNext()) {
stack.addLast(childItr);
}
return result;
}
}

/**
* Returns an unmodifiable iterable over the nodes in a tree structure, using post-order
* traversal. That is, each node's subtrees are traversed before the node itself is returned.
*
* <p>No guarantees are made about the behavior of the traversal when nodes change while
* iteration is in progress or when the iterators generated by {@link #children} are advanced.
*/
public final FluentIterable<T> postOrderTraversal(final T root) {
checkNotNull(root);
return new FluentIterable<T>() {
@Override
public UnmodifiableIterator<T> iterator() {
return postOrderIterator(root);
}
};
}

// overridden in BinaryTreeTraverser
UnmodifiableIterator<T> postOrderIterator(T root) {
return new PostOrderIterator(root);
}

private static final class PostOrderNode<T> {
final T root;
final Iterator<T> childIterator;

PostOrderNode(T root, Iterator<T> childIterator) {
this.root = checkNotNull(root);
this.childIterator = checkNotNull(childIterator);
}
}

private final class PostOrderIterator extends AbstractIterator<T> {
private final LinkedList<PostOrderNode<T>> stack;

PostOrderIterator(T root) {
this.stack = Lists.newLinkedList();
stack.addLast(expand(root));
}

@Override
protected T computeNext() {
while (!stack.isEmpty()) {
PostOrderNode<T> top = stack.getLast();
if (top.childIterator.hasNext()) {
T child = top.childIterator.next();
stack.addLast(expand(child));
} else {
stack.removeLast();
return top.root;
}
}
return endOfData();
}

private PostOrderNode<T> expand(T t) {
return new PostOrderNode<T>(t, children(t).iterator());
}
}

/**
* Returns an unmodifiable iterable over the nodes in a tree structure, using breadth-first
* traversal. That is, all the nodes of depth 0 are returned, then depth 1, then 2, and so on.
*
* <p>No guarantees are made about the behavior of the traversal when nodes change while
* iteration is in progress or when the iterators generated by {@link #children} are advanced.
*/
public final FluentIterable<T> breadthFirstTraversal(final T root) {
checkNotNull(root);
return new FluentIterable<T>() {
@Override
public UnmodifiableIterator<T> iterator() {
return new BreadthFirstIterator(root);
}
};
}

private final class BreadthFirstIterator extends UnmodifiableIterator<T>
implements PeekingIterator<T> {
private final Queue<T> queue;

BreadthFirstIterator(T root) {
this.queue = Lists.newLinkedList();
queue.add(checkNotNull(root));
}

@Override
public boolean hasNext() {
return !queue.isEmpty();
}

@Override
public T peek() {
return queue.element();
}

@Override
public T next() {
T result = queue.remove();
for (T child : children(result)) {
queue.add(checkNotNull(child));
}
return result;
}
}
}
Loading

0 comments on commit 5ebbc98

Please sign in to comment.