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.
6 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_SUPPORT_DEPTH_FIRST_ITERATOR_H
9 #define LLVM_SUPPORT_DEPTH_FIRST_ITERATOR_H
11 #include "Support/GraphTraits.h"
12 #include <Support/iterator>
16 // Generic Depth First Iterator
17 template<class GraphT, class GT = GraphTraits<GraphT> >
18 class df_iterator : public forward_iterator<typename GT::NodeType, ptrdiff_t> {
19 typedef forward_iterator<typename GT::NodeType, ptrdiff_t> super;
20 typedef typename super::pointer pointer;
22 typedef typename GT::NodeType NodeType;
23 typedef typename GT::ChildIteratorType ChildItTy;
25 std::set<NodeType *> Visited; // All of the blocks visited so far...
26 // VisitStack - Used to maintain the ordering. Top = current block
27 // First element is node pointer, second is the 'next child' to visit
28 std::stack<std::pair<NodeType *, ChildItTy> > VisitStack;
29 const bool Reverse; // Iterate over children before self?
31 void reverseEnterNode() {
32 std::pair<NodeType *, ChildItTy> &Top = VisitStack.top();
33 NodeType *Node = Top.first;
34 ChildItTy &It = Top.second;
35 for (; It != GT::child_end(Node); ++It) {
36 NodeType *Child = *It;
37 if (!Visited.count(Child)) {
38 Visited.insert(Child);
39 VisitStack.push(std::make_pair(Child, GT::child_begin(Child)));
46 inline df_iterator(NodeType *Node, bool reverse) : Reverse(reverse) {
48 VisitStack.push(std::make_pair(Node, GT::child_begin(Node)));
49 if (Reverse) reverseEnterNode();
51 inline df_iterator() { /* End is when stack is empty */ }
54 typedef df_iterator<GraphT, GT> _Self;
56 // Provide static begin and end methods as our public "constructors"
57 static inline _Self begin(GraphT G, bool Reverse = false) {
58 return _Self(GT::getEntryNode(G), Reverse);
60 static inline _Self end(GraphT G) { return _Self(); }
63 inline bool operator==(const _Self& x) const {
64 return VisitStack == x.VisitStack;
66 inline bool operator!=(const _Self& x) const { return !operator==(x); }
68 inline pointer operator*() const {
69 return VisitStack.top().first;
72 // This is a nonstandard operator-> that dereferences the pointer an extra
73 // time... so that you can actually call methods ON the Node, because
74 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
76 inline NodeType *operator->() const { return operator*(); }
78 inline _Self& operator++() { // Preincrement
79 if (Reverse) { // Reverse Depth First Iterator
80 if (VisitStack.top().second == GT::child_end(VisitStack.top().first))
82 if (!VisitStack.empty())
84 } else { // Normal Depth First Iterator
86 std::pair<NodeType *, ChildItTy> &Top = VisitStack.top();
87 NodeType *Node = Top.first;
88 ChildItTy &It = Top.second;
90 while (It != GT::child_end(Node)) {
91 NodeType *Next = *It++;
92 if (!Visited.count(Next)) { // Has our next sibling been visited?
95 VisitStack.push(std::make_pair(Next, GT::child_begin(Next)));
100 // Oops, ran out of successors... go up a level on the stack.
102 } while (!VisitStack.empty());
107 inline _Self operator++(int) { // Postincrement
108 _Self tmp = *this; ++*this; return tmp;
111 // nodeVisited - return true if this iterator has already visited the
112 // specified node. This is public, and will probably be used to iterate over
113 // nodes that a depth first iteration did not find: ie unreachable nodes.
115 inline bool nodeVisited(NodeType *Node) const {
116 return Visited.count(Node) != 0;
121 // Provide global constructors that automatically figure out correct types...
124 df_iterator<T> df_begin(T G, bool Reverse = false) {
125 return df_iterator<T>::begin(G, Reverse);
129 df_iterator<T> df_end(T G) {
130 return df_iterator<T>::end(G);
133 // Provide global definitions of inverse depth first iterators...
135 struct idf_iterator : public df_iterator<Inverse<T> > {
136 idf_iterator(const df_iterator<Inverse<T> > &V) :df_iterator<Inverse<T> >(V){}
140 idf_iterator<T> idf_begin(T G, bool Reverse = false) {
141 return idf_iterator<T>::begin(G, Reverse);
145 idf_iterator<T> idf_end(T G){
146 return idf_iterator<T>::end(G);