1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.graph;
  24 
  25 import java.util.Iterator;
  26 import java.util.NoSuchElementException;
  27 
  28 class NodeUsageIterator implements Iterator<Node> {
  29 
  30     final Node node;
  31     int index = -1;
  32     Node current;
  33 
  34     void advance() {
  35         current = null;
  36         index++;
  37         if (index == 0) {
  38             current = node.usage0;
  39         } else if (index == 1) {
  40             current = node.usage1;
  41         } else {
  42             int relativeIndex = index - Node.INLINE_USAGE_COUNT;
  43             if (relativeIndex < node.extraUsagesCount) {
  44                 current = node.extraUsages[relativeIndex];
  45             }
  46         }
  47     }
  48 
  49     NodeUsageIterator(Node node) {
  50         this.node = node;
  51         advance();
  52     }
  53 
  54     @Override
  55     public boolean hasNext() {
  56         return current != null;
  57     }
  58 
  59     @Override
  60     public Node next() {
  61         Node result = current;
  62         if (result == null) {
  63             throw new NoSuchElementException();
  64         }
  65         advance();
  66         return result;
  67     }
  68 
  69     @Override
  70     public void remove() {
  71         throw new UnsupportedOperationException();
  72     }
  73 }