Creates an {@code Iterable} that starts at {@code node} and ends at its root parent.
187708
public static <T> Iterable<T> toParent(TreeDef.Parented<T> treeDef, T node) {
		return () -> new Iterator<T>() {
			T tip = node;

			@Override
			public boolean hasNext() {
				return tip != null;
			}

			@Override
			public T next() {
				if (tip == null) {
					throw new NoSuchElementException();
				}
				T next = tip;
				tip = treeDef.parentOf(tip);
				return next;
			}
		};
	}
21664
private Iterable<YamlNode> cast(Iterable<Object> nodes) {
        return () -> new UnmodifiableIterator<YamlNode>() {
            private final Iterator<Object> iter = nodes.iterator();

            @Override
            public boolean hasNext() {
                return iter.hasNext();
            }

            @Override
            public YamlNode next() {
                return (YamlNode) iter.next();
            }
        };
    }
504333
private Node createNode(final Iterable<Range<C>> ranges) {
        Range<C> span = Iterables.getFirst(ranges, null);
        if (span == null) {
            return null;
        }
        for (Range<C> range : ranges) {
            checkNotNull(range, "ranges must not contain null ranges");
            span = range.span(span);
        }
        if (span.isEmpty()) {
            return null;
        }
        C center = Ranges.center(span);
        List<Range<C>> left = Lists.newArrayList();
        List<Range<C>> right = Lists.newArrayList();
        List<Range<C>> overlap = Lists.newArrayList();
        for (Range<C> range : ranges) {
            if (Ranges.isLessThan(range, center)) {
                left.add(range);
            }
            else if (Ranges.isGreaterThan(range, center)) {
                right.add(range);
            }
            else {
                overlap.add(range);
            }
        }
        return new Node(center, createNode(left), createNode(right), overlap);
    }
1044767
public DTMIterator createDTMIterator(int node)
  {
    // DescendantIterator iter = new DescendantIterator();
    DTMIterator iter = new org.apache.xpath.axes.OneStepIteratorForward(Axis.SELF);
    iter.setRoot(node, this);
    return iter;
    // return m_dtmManager.createDTMIterator(node);
  }
70257
public static <T> Iterable<T> wrap(final Iterable<T> base) {
        return new Iterable<T>() {
            public Iterator<T> iterator() {
                final Iterator<T> itr = base.iterator();
                return new Iterator<T>() {
                    public boolean hasNext() {
                        return itr.hasNext();
                    }

                    public T next() {
                        return itr.next();
                    }

                    public void remove() {
                        itr.remove();
                    }
                };
            }
        };
    }
1089418
function (start, end) {
            var ret = null, l;
            l = end - start;
            if (l > 0) {
                ret = this.nodeFactory.createCDataNode(this.page, start, end);
            }
            return ret;
        }
902335
private static Map<String,List<Node>> getChildNodes(Node parent) {
		if (parent==null)
			return Collections.emptyMap();

		Map<String,List<Node>> children = new HashMap<String,List<Node>>();

		NodeList nodes = parent.getChildNodes();
		int nNodes = nodes.getLength();
		for (int i=0; i<nNodes; i++) {
			Node node = nodes.item(i);
			if (node.getNodeType()!=Node.ELEMENT_NODE)
				continue;

			String name = node.getNodeName();
			List<Node> namesakes = children.get(name);
			if (namesakes==null) {
				namesakes = new ArrayList<Node>();
				children.put(name, namesakes);
			}
			namesakes.add(node);
		}

		return children;
	}
7997
public Node nextNode() throws NoSuchElementException
   {
      initialize();
      if (next == null)
      {
         throw new NoSuchElementException();
      }
      NodeImpl n = next;
      fetchNext();
      return n;
   }
766034
public static <T> Iterable<T> simpleIterable(final Iterable<T> iterable) {
        requireNonNull(iterable);
        return () -> iterable.iterator();
    }
1202392
public static <E> FluentCloseableIterable<E> from(final CloseableIterable<E> iterable) {
        return (iterable instanceof FluentCloseableIterable) ? (FluentCloseableIterable<E>) iterable
                : new FluentCloseableIterable<E>() {

            @Override
            protected void doClose() {
                iterable.close();
            }

            @Override
            protected Iterator<E> retrieveIterator() {
                return iterable.iterator();
            }
        };
    }
17392
public static <E extends Comparable<?>> TreeSet<E> newTreeSet(Iterable<? extends E> elements) {
		TreeSet<E> set = newTreeSet();
		for (E element : elements) {
			set.add(element);
		}
		return set;
	}
738468
public static List<Element> getAllChildElements(Element parent) {
        List<Element> result;
        NodeList nodes;
        int i;
        int max;
        Node node;

        result = new ArrayList<>();
        nodes = parent.getChildNodes();
        max = nodes.getLength();
        for (i = 0; i < max; i++) {
            node = nodes.item(i);
            if (node instanceof Element) {
                result.add((Element) node);
            }
        }
        return result;
    }
484766
protected N buildTree(int[] msg, int cur, int parent, ArrayList<N> nodes) {
    // Count the number of children:
    int c = 0;
    for(int i = 1; i < msg.length; i += 2) {
      if((msg[i - 1] == cur && msg[i] != parent) || (msg[i] == cur && msg[i - 1] != parent)) {
        c++;
      }
    }
    // Build children:
    List<N> children = Collections.emptyList();
    if(c > 0) {
      children = new ArrayList<>(c);
      for(int i = 1; i < msg.length; i += 2) {
        if(msg[i - 1] == cur && msg[i] != parent) {
          children.add(buildTree(msg, msg[i], cur, nodes));
        }
        else if(msg[i] == cur && msg[i - 1] != parent) {
          children.add(buildTree(msg, msg[i - 1], cur, nodes));
        }
      }
    }
    N node = makeNode(cur, children);
    nodes.set(cur, node);
    return node;
  }
1002012
private void visitForOf(Node node, Node parent) {
    Node variable = node.removeFirstChild();
    Node iterable = node.removeFirstChild();
    Node body = node.removeFirstChild();

    JSType typeParam = unknownType;
    if (addTypes) {
      // TODO(sdh): This is going to be null if the iterable is nullable or unknown. We might want
      // to consider some way of unifying rather than simply looking at the nominal type.
      ObjectType iterableType = iterable.getJSType().autobox().toMaybeObjectType();
      if (iterableType != null) {
        // This will be the unknown type if iterableType is not actually a subtype of Iterable
        typeParam =
            iterableType.getInstantiatedTypeArgument(
                registry.getNativeType(JSTypeNative.ITERABLE_TYPE));
      }
    }
    JSType iteratorType = createGenericType(JSTypeNative.ITERATOR_TYPE, typeParam);
    FunctionType iteratorNextType =
        addTypes
            ? iteratorType.toMaybeObjectType().getPropertyType("next").toMaybeFunctionType()
            : null;
    JSType iIterableResultType = addTypes ? iteratorNextType.getReturnType() : null;

    JSDocInfo varJSDocInfo = variable.getJSDocInfo();
    Node iterName =
        withType(IR.name(ITER_BASE + compiler.getUniqueNameIdSupplier().get()), iteratorType);
    iterName.makeNonIndexable();
    Node getNext =
        withType(
            IR.call(
                withType(
                    IR.getprop(iterName.cloneTree(), withStringType(IR.string("next"))),
                    iteratorNextType)),
            iIterableResultType);
    String iteratorResultName = ITER_RESULT;
    if (NodeUtil.isNameDeclaration(variable)) {
      iteratorResultName += variable.getFirstChild().getString();
    } else if (variable.isName()) {
      iteratorResultName += variable.getString();
    } else {
      // give arbitrary lhs expressions an arbitrary name
      iteratorResultName += namer.generateNextName();
    }
    Node iterResult = withType(IR.name(iteratorResultName), iIterableResultType);
    iterResult.makeNonIndexable();

    Node call = Es6ToEs3Util.makeIterator(compiler, iterable);
    if (addTypes) {
      // Create the function type for $jscomp.makeIterator.
      // Build "@param {string|!Iterable<T>|!Iterator<T>|!Arguments<T>}"
      UnionTypeBuilder paramBuilder =
          UnionTypeBuilder.create(registry)
              .addAlternate(registry.getNativeType(JSTypeNative.STRING_TYPE))
              .addAlternate(registry.getNativeType(JSTypeNative.ITERATOR_TYPE))
              .addAlternate(registry.getNativeType(JSTypeNative.ITERABLE_TYPE));
      JSType argumentsType = registry.getGlobalType("Arguments");
      // If the user didn't provide externs for Arguments, let TypeCheck take care of issuing a
      // warning.
      if (argumentsType != null) {
        paramBuilder.addAlternate(argumentsType);
      }
      FunctionType makeIteratorType =
          registry.createFunctionType(iteratorType, paramBuilder.build());

      // Put types on the $jscomp.makeIterator getprop
      Node getProp = call.getFirstChild();
      getProp.setJSType(makeIteratorType);
      // typing $jscomp as unknown since the $jscomp polyfill may not be injected before
      // typechecking. (See https://github.com/google/closure-compiler/issues/2908)
      getProp.getFirstChild().setJSType(registry.getNativeType(JSTypeNative.UNKNOWN_TYPE));
      getProp.getSecondChild().setJSType(registry.getNativeType(JSTypeNative.STRING_TYPE));

      call.setJSType(iteratorType);
    }
    Node init = IR.var(withType(iterName.cloneTree(), iterName.getJSType()), call);
    Node initIterResult = iterResult.cloneTree();
    initIterResult.addChildToFront(getNext.cloneTree());
    init.addChildToBack(initIterResult);

    Node cond =
        withBooleanType(
            IR.not(
                withBooleanType(
                    IR.getprop(iterResult.cloneTree(), withStringType(IR.string("done"))))));
    Node incr =
        withType(IR.assign(iterResult.cloneTree(), getNext.cloneTree()), iIterableResultType);

    Node declarationOrAssign;
    if (!NodeUtil.isNameDeclaration(variable)) {
      declarationOrAssign =
          withType(
              IR.assign(
                  withType(variable.cloneTree().setJSDocInfo(null), typeParam),
                  withType(
                      IR.getprop(iterResult.cloneTree(), withStringType(IR.string("value"))),
                      typeParam)),
              typeParam);
      declarationOrAssign.setJSDocInfo(varJSDocInfo);
      declarationOrAssign = IR.exprResult(declarationOrAssign);
    } else {
      Token declarationType = variable.getToken(); // i.e. VAR, CONST, or LET.
      declarationOrAssign =
          new Node(
                  declarationType,
                  IR.name(variable.getFirstChild().getString())
                      .useSourceInfoFrom(variable.getFirstChild()))
              .setJSType(typeParam);
      declarationOrAssign.getFirstChild().addChildToBack(
              withType(
                  IR.getprop(iterResult.cloneTree(), withStringType(IR.string("value"))),
                  typeParam));
      declarationOrAssign.setJSDocInfo(varJSDocInfo);
    }
    Node newBody = IR.block(declarationOrAssign, body).useSourceInfoFrom(body);
    Node newFor = IR.forNode(init, cond, incr, newBody);
    newFor.useSourceInfoIfMissingFromForTree(node);
    parent.replaceChild(node, newFor);
    compiler.reportChangeToEnclosingScope(newFor);
  }
900370
public static <T, K> ArrayList<T> buildTree(Collection<T> list,
			Function<T, K> keyMapper, Function<T, K> parentKeyMapper,
			Function<T, Collection<T>> childMapper) {
		HashMap<K, T> map = toHashMap(list, keyMapper);

		ArrayList<T> root = new ArrayList<T>();
		for (T t : list) {
			K key = keyMapper.apply(t);
			K parentKey = parentKeyMapper.apply(t);
			if (parentKey == null || key.equals(parentKey))
				root.add(t);
			else
				childMapper.apply(map.get(parentKey)).add(t);
		}

		return root;
	}
88105
public static Iterable<Element> elementIterable(final Element element, final String name) {

        return new Iterable<Element>() {

            public Iterator<Element> iterator() {

                return elementIterator(element, name);
            }
        };
    }
62643
function (node) {
            return this.setStart(node, node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length);
        }