Mercurial > hg > truffle
comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @ 20131:4b12d5355811
Truffle: do not use iterators for visitors.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Thu, 02 Apr 2015 01:27:27 +0200 |
parents | 8dc73c226c63 |
children | 286aef83a9a7 |
comparison
equal
deleted
inserted
replaced
20130:8dc73c226c63 | 20131:4b12d5355811 |
---|---|
329 } | 329 } |
330 | 330 |
331 return true; | 331 return true; |
332 } | 332 } |
333 | 333 |
334 static boolean forEachChildRecursive(Node parent, NodeVisitor visitor) { | |
335 NodeClass parentNodeClass = parent.getNodeClass(); | |
336 | |
337 for (NodeFieldAccessor field : parentNodeClass.getChildFields()) { | |
338 if (!visitChild((Node) field.getObject(parent), visitor)) { | |
339 return false; | |
340 } | |
341 } | |
342 | |
343 for (NodeFieldAccessor field : parentNodeClass.getChildrenFields()) { | |
344 Object arrayObject = field.getObject(parent); | |
345 if (arrayObject == null) { | |
346 continue; | |
347 } | |
348 Object[] array = (Object[]) arrayObject; | |
349 for (int i = 0; i < array.length; i++) { | |
350 if (!visitChild((Node) array[i], visitor)) { | |
351 return false; | |
352 } | |
353 } | |
354 } | |
355 | |
356 return true; | |
357 } | |
358 | |
359 private static boolean visitChild(Node child, NodeVisitor visitor) { | |
360 if (child == null) { | |
361 return true; | |
362 } | |
363 if (!visitor.visit(child)) { | |
364 return false; | |
365 } | |
366 if (!forEachChildRecursive(child, visitor)) { | |
367 return false; | |
368 } | |
369 return true; | |
370 } | |
371 | |
334 /** Returns all declared fields in the class hierarchy. */ | 372 /** Returns all declared fields in the class hierarchy. */ |
335 static Field[] getAllFields(Class<? extends Object> clazz) { | 373 static Field[] getAllFields(Class<? extends Object> clazz) { |
336 Field[] declaredFields = clazz.getDeclaredFields(); | 374 Field[] declaredFields = clazz.getDeclaredFields(); |
337 if (clazz.getSuperclass() != null) { | 375 if (clazz.getSuperclass() != null) { |
338 return concat(getAllFields(clazz.getSuperclass()), declaredFields); | 376 return concat(getAllFields(clazz.getSuperclass()), declaredFields); |
458 }); | 496 }); |
459 return nodeList; | 497 return nodeList; |
460 } | 498 } |
461 | 499 |
462 public static int countNodes(Node root) { | 500 public static int countNodes(Node root) { |
463 Iterator<Node> nodeIterator = makeRecursiveIterator(root); | 501 return countNodes(root, NodeCountFilter.NO_FILTER); |
464 int count = 0; | |
465 while (nodeIterator.hasNext()) { | |
466 nodeIterator.next(); | |
467 count++; | |
468 } | |
469 return count; | |
470 } | 502 } |
471 | 503 |
472 public static int countNodes(Node root, NodeCountFilter filter) { | 504 public static int countNodes(Node root, NodeCountFilter filter) { |
473 Iterator<Node> nodeIterator = makeRecursiveIterator(root); | 505 NodeCounter counter = new NodeCounter(filter); |
474 int count = 0; | 506 root.accept(counter); |
475 while (nodeIterator.hasNext()) { | 507 return counter.count; |
476 Node node = nodeIterator.next(); | |
477 if (node != null && filter.isCounted(node)) { | |
478 count++; | |
479 } | |
480 } | |
481 return count; | |
482 } | 508 } |
483 | 509 |
484 public interface NodeCountFilter { | 510 public interface NodeCountFilter { |
511 | |
512 NodeCountFilter NO_FILTER = new NodeCountFilter() { | |
513 | |
514 public boolean isCounted(Node node) { | |
515 return true; | |
516 } | |
517 }; | |
485 | 518 |
486 boolean isCounted(Node node); | 519 boolean isCounted(Node node); |
487 | 520 |
488 } | 521 } |
489 | 522 |
799 } | 832 } |
800 currentFrom = currentFrom.getSuperclass(); | 833 currentFrom = currentFrom.getSuperclass(); |
801 } | 834 } |
802 return true; | 835 return true; |
803 } | 836 } |
837 | |
838 private static final class NodeCounter implements NodeVisitor { | |
839 | |
840 public int count; | |
841 private final NodeCountFilter filter; | |
842 | |
843 public NodeCounter(NodeCountFilter filter) { | |
844 this.filter = filter; | |
845 } | |
846 | |
847 public boolean visit(Node node) { | |
848 if (filter.isCounted(node)) { | |
849 count++; | |
850 } | |
851 return true; | |
852 } | |
853 | |
854 } | |
804 } | 855 } |