Chris Lattner | 555eaf5 | 2003-09-30 18:37:50 +0000 | [diff] [blame^] | 1 | //===- Support/DepthFirstIterator.h - Depth First iterator ------*- C++ -*-===// |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 2 | // |
| 3 | // This file builds on the Support/GraphTraits.h file to build generic depth |
| 4 | // first graph iterator. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
Brian Gaeke | a7a5013 | 2003-06-17 00:35:55 +0000 | [diff] [blame] | 8 | #ifndef SUPPORT_DEPTHFIRSTITERATOR_H |
| 9 | #define SUPPORT_DEPTHFIRSTITERATOR_H |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 10 | |
Chris Lattner | 5de2204 | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 11 | #include "Support/GraphTraits.h" |
Chris Lattner | 94a4f22 | 2002-10-28 02:11:53 +0000 | [diff] [blame] | 12 | #include "Support/iterator" |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 13 | #include <stack> |
| 14 | #include <set> |
| 15 | |
| 16 | // Generic Depth First Iterator |
| 17 | template<class GraphT, class GT = GraphTraits<GraphT> > |
Chris Lattner | 889cddf | 2002-07-24 22:08:36 +0000 | [diff] [blame] | 18 | class df_iterator : public forward_iterator<typename GT::NodeType, ptrdiff_t> { |
| 19 | typedef forward_iterator<typename GT::NodeType, ptrdiff_t> super; |
Chris Lattner | 889cddf | 2002-07-24 22:08:36 +0000 | [diff] [blame] | 20 | |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 21 | typedef typename GT::NodeType NodeType; |
| 22 | typedef typename GT::ChildIteratorType ChildItTy; |
| 23 | |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 24 | std::set<NodeType *> Visited; // All of the blocks visited so far... |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 25 | // VisitStack - Used to maintain the ordering. Top = current block |
| 26 | // First element is node pointer, second is the 'next child' to visit |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 27 | std::stack<std::pair<NodeType *, ChildItTy> > VisitStack; |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 28 | const bool Reverse; // Iterate over children before self? |
| 29 | private: |
| 30 | void reverseEnterNode() { |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 31 | std::pair<NodeType *, ChildItTy> &Top = VisitStack.top(); |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 32 | NodeType *Node = Top.first; |
| 33 | ChildItTy &It = Top.second; |
| 34 | for (; It != GT::child_end(Node); ++It) { |
| 35 | NodeType *Child = *It; |
| 36 | if (!Visited.count(Child)) { |
| 37 | Visited.insert(Child); |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 38 | VisitStack.push(std::make_pair(Child, GT::child_begin(Child))); |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 39 | reverseEnterNode(); |
| 40 | return; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | inline df_iterator(NodeType *Node, bool reverse) : Reverse(reverse) { |
| 46 | Visited.insert(Node); |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 47 | VisitStack.push(std::make_pair(Node, GT::child_begin(Node))); |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 48 | if (Reverse) reverseEnterNode(); |
| 49 | } |
| 50 | inline df_iterator() { /* End is when stack is empty */ } |
| 51 | |
| 52 | public: |
Chris Lattner | 33d5102 | 2003-07-25 17:49:28 +0000 | [diff] [blame] | 53 | typedef typename super::pointer pointer; |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 54 | typedef df_iterator<GraphT, GT> _Self; |
| 55 | |
| 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); |
| 59 | } |
| 60 | static inline _Self end(GraphT G) { return _Self(); } |
| 61 | |
| 62 | |
| 63 | inline bool operator==(const _Self& x) const { |
| 64 | return VisitStack == x.VisitStack; |
| 65 | } |
| 66 | inline bool operator!=(const _Self& x) const { return !operator==(x); } |
| 67 | |
| 68 | inline pointer operator*() const { |
| 69 | return VisitStack.top().first; |
| 70 | } |
| 71 | |
| 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. |
| 75 | // |
| 76 | inline NodeType *operator->() const { return operator*(); } |
| 77 | |
| 78 | inline _Self& operator++() { // Preincrement |
| 79 | if (Reverse) { // Reverse Depth First Iterator |
| 80 | if (VisitStack.top().second == GT::child_end(VisitStack.top().first)) |
| 81 | VisitStack.pop(); |
| 82 | if (!VisitStack.empty()) |
| 83 | reverseEnterNode(); |
| 84 | } else { // Normal Depth First Iterator |
| 85 | do { |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 86 | std::pair<NodeType *, ChildItTy> &Top = VisitStack.top(); |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 87 | NodeType *Node = Top.first; |
| 88 | ChildItTy &It = Top.second; |
| 89 | |
| 90 | while (It != GT::child_end(Node)) { |
| 91 | NodeType *Next = *It++; |
| 92 | if (!Visited.count(Next)) { // Has our next sibling been visited? |
| 93 | // No, do it now. |
| 94 | Visited.insert(Next); |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 95 | VisitStack.push(std::make_pair(Next, GT::child_begin(Next))); |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 96 | return *this; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Oops, ran out of successors... go up a level on the stack. |
| 101 | VisitStack.pop(); |
| 102 | } while (!VisitStack.empty()); |
| 103 | } |
| 104 | return *this; |
| 105 | } |
| 106 | |
| 107 | inline _Self operator++(int) { // Postincrement |
| 108 | _Self tmp = *this; ++*this; return tmp; |
| 109 | } |
| 110 | |
| 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. |
| 114 | // |
| 115 | inline bool nodeVisited(NodeType *Node) const { |
| 116 | return Visited.count(Node) != 0; |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | |
| 121 | // Provide global constructors that automatically figure out correct types... |
| 122 | // |
| 123 | template <class T> |
| 124 | df_iterator<T> df_begin(T G, bool Reverse = false) { |
| 125 | return df_iterator<T>::begin(G, Reverse); |
| 126 | } |
| 127 | |
| 128 | template <class T> |
| 129 | df_iterator<T> df_end(T G) { |
| 130 | return df_iterator<T>::end(G); |
| 131 | } |
| 132 | |
| 133 | // Provide global definitions of inverse depth first iterators... |
| 134 | template <class T> |
| 135 | struct idf_iterator : public df_iterator<Inverse<T> > { |
| 136 | idf_iterator(const df_iterator<Inverse<T> > &V) :df_iterator<Inverse<T> >(V){} |
| 137 | }; |
| 138 | |
| 139 | template <class T> |
| 140 | idf_iterator<T> idf_begin(T G, bool Reverse = false) { |
| 141 | return idf_iterator<T>::begin(G, Reverse); |
| 142 | } |
| 143 | |
| 144 | template <class T> |
| 145 | idf_iterator<T> idf_end(T G){ |
| 146 | return idf_iterator<T>::end(G); |
| 147 | } |
| 148 | |
| 149 | #endif |