1 //===- Support/DepthFirstIterator.h - Depth First iterator ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file builds on the Support/GraphTraits.h file to build generic depth
11 // first graph iterator. This file exposes the following functions/types:
13 // df_begin/df_end/df_iterator
14 // * Normal depth-first iteration - visit a node and then all of its children.
16 // idf_begin/idf_end/idf_iterator
17 // * Depth-first iteration on the 'inverse' graph.
19 // df_ext_begin/df_ext_end/df_ext_iterator
20 // * Normal depth-first iteration - visit a node and then all of its children.
21 // This iterator stores the 'visited' set in an external set, which allows
22 // it to be more efficient, and allows external clients to use the set for
25 // idf_ext_begin/idf_ext_end/idf_ext_iterator
26 // * Depth-first iteration on the 'inverse' graph.
27 // This iterator stores the 'visited' set in an external set, which allows
28 // it to be more efficient, and allows external clients to use the set for
31 //===----------------------------------------------------------------------===//
33 #ifndef SUPPORT_DEPTHFIRSTITERATOR_H
34 #define SUPPORT_DEPTHFIRSTITERATOR_H
36 #include "Support/GraphTraits.h"
37 #include "Support/iterator"
41 // df_iterator_storage - A private class which is used to figure out where to
42 // store the visited set.
43 template<class SetType, bool External> // Non-external set
44 class df_iterator_storage {
49 template<class SetType>
50 class df_iterator_storage<SetType, true> {
52 df_iterator_storage(SetType &VSet) : Visited(VSet) {}
53 df_iterator_storage(const df_iterator_storage &S) : Visited(S.Visited) {}
58 // Generic Depth First Iterator
59 template<class GraphT, class SetType =
60 std::set<typename GraphTraits<GraphT>::NodeType*>,
61 bool ExtStorage = false, class GT = GraphTraits<GraphT> >
62 class df_iterator : public forward_iterator<typename GT::NodeType, ptrdiff_t>,
63 public df_iterator_storage<SetType, ExtStorage> {
64 typedef forward_iterator<typename GT::NodeType, ptrdiff_t> super;
66 typedef typename GT::NodeType NodeType;
67 typedef typename GT::ChildIteratorType ChildItTy;
69 // VisitStack - Used to maintain the ordering. Top = current block
70 // First element is node pointer, second is the 'next child' to visit
71 std::vector<std::pair<NodeType *, ChildItTy> > VisitStack;
73 inline df_iterator(NodeType *Node) {
74 this->Visited.insert(Node);
75 VisitStack.push_back(std::make_pair(Node, GT::child_begin(Node)));
77 inline df_iterator() { /* End is when stack is empty */ }
79 inline df_iterator(NodeType *Node, SetType &S)
80 : df_iterator_storage<SetType, ExtStorage>(S) {
82 this->Visited.insert(Node);
83 VisitStack.push_back(std::make_pair(Node, GT::child_begin(Node)));
86 inline df_iterator(SetType &S)
87 : df_iterator_storage<SetType, ExtStorage>(S) {
88 // End is when stack is empty
92 typedef typename super::pointer pointer;
93 typedef df_iterator<GraphT, SetType, ExtStorage, GT> _Self;
95 // Provide static begin and end methods as our public "constructors"
96 static inline _Self begin(GraphT G) {
97 return _Self(GT::getEntryNode(G));
99 static inline _Self end(GraphT G) { return _Self(); }
101 // Static begin and end methods as our public ctors for external iterators
102 static inline _Self begin(GraphT G, SetType &S) {
103 return _Self(GT::getEntryNode(G), S);
105 static inline _Self end(GraphT G, SetType &S) { return _Self(S); }
107 inline bool operator==(const _Self& x) const {
108 return VisitStack.size() == x.VisitStack.size() &&
109 VisitStack == x.VisitStack;
111 inline bool operator!=(const _Self& x) const { return !operator==(x); }
113 inline pointer operator*() const {
114 return VisitStack.back().first;
117 // This is a nonstandard operator-> that dereferences the pointer an extra
118 // time... so that you can actually call methods ON the Node, because
119 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
121 inline NodeType *operator->() const { return operator*(); }
123 inline _Self& operator++() { // Preincrement
125 std::pair<NodeType *, ChildItTy> &Top = VisitStack.back();
126 NodeType *Node = Top.first;
127 ChildItTy &It = Top.second;
129 while (It != GT::child_end(Node)) {
130 NodeType *Next = *It++;
131 if (!this->Visited.count(Next)) { // Has our next sibling been visited?
133 this->Visited.insert(Next);
134 VisitStack.push_back(std::make_pair(Next, GT::child_begin(Next)));
139 // Oops, ran out of successors... go up a level on the stack.
140 VisitStack.pop_back();
141 } while (!VisitStack.empty());
145 inline _Self operator++(int) { // Postincrement
146 _Self tmp = *this; ++*this; return tmp;
149 // nodeVisited - return true if this iterator has already visited the
150 // specified node. This is public, and will probably be used to iterate over
151 // nodes that a depth first iteration did not find: ie unreachable nodes.
153 inline bool nodeVisited(NodeType *Node) const {
154 return this->Visited.count(Node) != 0;
159 // Provide global constructors that automatically figure out correct types...
162 df_iterator<T> df_begin(T G) {
163 return df_iterator<T>::begin(G);
167 df_iterator<T> df_end(T G) {
168 return df_iterator<T>::end(G);
171 // Provide global definitions of external depth first iterators...
172 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
173 struct df_ext_iterator : public df_iterator<T, SetTy, true> {
174 df_ext_iterator(const df_iterator<T, SetTy, true> &V)
175 : df_iterator<T, SetTy, true>(V) {}
178 template <class T, class SetTy>
179 df_ext_iterator<T, SetTy> df_ext_begin(T G, SetTy &S) {
180 return df_ext_iterator<T, SetTy>::begin(G, S);
183 template <class T, class SetTy>
184 df_ext_iterator<T, SetTy> df_ext_end(T G, SetTy &S) {
185 return df_ext_iterator<T, SetTy>::end(G, S);
189 // Provide global definitions of inverse depth first iterators...
190 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*>,
191 bool External = false>
192 struct idf_iterator : public df_iterator<Inverse<T>, SetTy, External> {
193 idf_iterator(const df_iterator<Inverse<T>, SetTy, External> &V)
194 : df_iterator<Inverse<T>, SetTy, External>(V) {}
198 idf_iterator<T> idf_begin(T G) {
199 return idf_iterator<T>::begin(G);
203 idf_iterator<T> idf_end(T G){
204 return idf_iterator<T>::end(G);
207 // Provide global definitions of external inverse depth first iterators...
208 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
209 struct idf_ext_iterator : public idf_iterator<T, SetTy, true> {
210 idf_ext_iterator(const idf_iterator<T, SetTy, true> &V)
211 : idf_iterator<T, SetTy, true>(V) {}
212 idf_ext_iterator(const df_iterator<Inverse<T>, SetTy, true> &V)
213 : idf_iterator<T, SetTy, true>(V) {}
216 template <class T, class SetTy>
217 idf_ext_iterator<T, SetTy> idf_ext_begin(T G, SetTy &S) {
218 return idf_ext_iterator<T, SetTy>::begin(G, S);
221 template <class T, class SetTy>
222 idf_ext_iterator<T, SetTy> idf_ext_end(T G, SetTy &S) {
223 return idf_ext_iterator<T, SetTy>::end(G, S);