1 //===- Support/DepthFirstIterator.h - Depth First iterator ------*- C++ -*-===//
3 // This file builds on the Support/GraphTraits.h file to build generic depth
4 // first graph iterator. This file exposes the following functions/types:
6 // df_begin/df_end/df_iterator
7 // * Normal depth-first iteration - visit a node and then all of its children.
9 // idf_begin/idf_end/idf_iterator
10 // * Depth-first iteration on the 'inverse' graph.
12 //===----------------------------------------------------------------------===//
14 #ifndef SUPPORT_DEPTHFIRSTITERATOR_H
15 #define SUPPORT_DEPTHFIRSTITERATOR_H
17 #include "Support/GraphTraits.h"
18 #include "Support/iterator"
22 // Generic Depth First Iterator
23 template<class GraphT, class GT = GraphTraits<GraphT> >
24 class df_iterator : public forward_iterator<typename GT::NodeType, ptrdiff_t> {
25 typedef forward_iterator<typename GT::NodeType, ptrdiff_t> super;
27 typedef typename GT::NodeType NodeType;
28 typedef typename GT::ChildIteratorType ChildItTy;
30 std::set<NodeType *> Visited; // All of the blocks visited so far...
31 // VisitStack - Used to maintain the ordering. Top = current block
32 // First element is node pointer, second is the 'next child' to visit
33 std::vector<std::pair<NodeType *, ChildItTy> > VisitStack;
35 inline df_iterator(NodeType *Node) {
37 VisitStack.push_back(std::make_pair(Node, GT::child_begin(Node)));
39 inline df_iterator() { /* End is when stack is empty */ }
42 typedef typename super::pointer pointer;
43 typedef df_iterator<GraphT, GT> _Self;
45 // Provide static begin and end methods as our public "constructors"
46 static inline _Self begin(GraphT G) {
47 return _Self(GT::getEntryNode(G));
49 static inline _Self end(GraphT G) { return _Self(); }
52 inline bool operator==(const _Self& x) const {
53 return VisitStack.size() == x.VisitStack.size() &&
54 VisitStack == x.VisitStack;
56 inline bool operator!=(const _Self& x) const { return !operator==(x); }
58 inline pointer operator*() const {
59 return VisitStack.back().first;
62 // This is a nonstandard operator-> that dereferences the pointer an extra
63 // time... so that you can actually call methods ON the Node, because
64 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
66 inline NodeType *operator->() const { return operator*(); }
68 inline _Self& operator++() { // Preincrement
70 std::pair<NodeType *, ChildItTy> &Top = VisitStack.back();
71 NodeType *Node = Top.first;
72 ChildItTy &It = Top.second;
74 while (It != GT::child_end(Node)) {
75 NodeType *Next = *It++;
76 if (!Visited.count(Next)) { // Has our next sibling been visited?
79 VisitStack.push_back(std::make_pair(Next, GT::child_begin(Next)));
84 // Oops, ran out of successors... go up a level on the stack.
85 VisitStack.pop_back();
86 } while (!VisitStack.empty());
90 inline _Self operator++(int) { // Postincrement
91 _Self tmp = *this; ++*this; return tmp;
94 // nodeVisited - return true if this iterator has already visited the
95 // specified node. This is public, and will probably be used to iterate over
96 // nodes that a depth first iteration did not find: ie unreachable nodes.
98 inline bool nodeVisited(NodeType *Node) const {
99 return Visited.count(Node) != 0;
104 // Provide global constructors that automatically figure out correct types...
107 df_iterator<T> df_begin(T G) {
108 return df_iterator<T>::begin(G);
112 df_iterator<T> df_end(T G) {
113 return df_iterator<T>::end(G);
116 // Provide global definitions of inverse depth first iterators...
118 struct idf_iterator : public df_iterator<Inverse<T> > {
119 idf_iterator(const df_iterator<Inverse<T> > &V) :df_iterator<Inverse<T> >(V){}
123 idf_iterator<T> idf_begin(T G) {
124 return idf_iterator<T>::begin(G);
128 idf_iterator<T> idf_end(T G){
129 return idf_iterator<T>::end(G);