comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @ 10793:6e12e0ace0d5

Truffle: add NodeUtil.countNodes overload that counts nodes of a specific type
author Andreas Woess <andreas.woess@jku.at>
date Wed, 17 Jul 2013 13:53:11 +0200
parents 7f6580db1e88
children e9248ebb1d79
comparison
equal deleted inserted replaced
10792:7a8d6ba83a04 10793:6e12e0ace0d5
539 }); 539 });
540 return nodeList; 540 return nodeList;
541 } 541 }
542 542
543 public static int countNodes(Node root) { 543 public static int countNodes(Node root) {
544 NodeCountVisitor nodeCount = new NodeCountVisitor(); 544 return countNodes(root, null);
545 }
546
547 public static int countNodes(Node root, Class<?> clazz) {
548 NodeCountVisitor nodeCount = new NodeCountVisitor(clazz);
545 root.accept(nodeCount); 549 root.accept(nodeCount);
546 return nodeCount.nodeCount; 550 return nodeCount.nodeCount;
547 } 551 }
548 552
549 private static class NodeCountVisitor implements NodeVisitor { 553 private static final class NodeCountVisitor implements NodeVisitor {
550 554
555 private final Class<?> clazz;
551 int nodeCount; 556 int nodeCount;
557
558 private NodeCountVisitor(Class<?> clazz) {
559 this.clazz = clazz;
560 }
552 561
553 @Override 562 @Override
554 public boolean visit(Node node) { 563 public boolean visit(Node node) {
555 if (node instanceof RootNode && nodeCount > 0) { 564 if (node instanceof RootNode && nodeCount > 0) {
556 return false; 565 return false;
557 } 566 }
558 nodeCount++; 567 if (clazz == null || clazz.isInstance(node)) {
568 nodeCount++;
569 }
559 return true; 570 return true;
560 } 571 }
561 } 572 }
562 573
563 public static String printCompactTreeToString(Node node) { 574 public static String printCompactTreeToString(Node node) {