Added LLVM notice.
[oota-llvm.git] / include / llvm / ADT / DepthFirstIterator.h
1 //===- Support/DepthFirstIterator.h - Depth First iterator ------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
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:
12 //
13 // df_begin/df_end/df_iterator
14 //   * Normal depth-first iteration - visit a node and then all of its children.
15 //
16 // idf_begin/idf_end/idf_iterator
17 //   * Depth-first iteration on the 'inverse' graph.
18 //
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
23 //     other purposes.
24 //
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
29 //     other purposes.
30 //
31 //===----------------------------------------------------------------------===//
32
33 #ifndef SUPPORT_DEPTHFIRSTITERATOR_H
34 #define SUPPORT_DEPTHFIRSTITERATOR_H
35
36 #include "Support/GraphTraits.h"
37 #include "Support/iterator"
38 #include <vector>
39 #include <set>
40
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 {
45 public:
46   SetType Visited;
47 };
48
49 template<class SetType>
50 class df_iterator_storage<SetType, true> {
51 public:
52   df_iterator_storage(SetType &VSet) : Visited(VSet) {}
53   df_iterator_storage(const df_iterator_storage &S) : Visited(S.Visited) {}
54   SetType &Visited;
55 };
56
57
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;
65
66   typedef typename GT::NodeType          NodeType;
67   typedef typename GT::ChildIteratorType ChildItTy;
68
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;
72 private:
73   inline df_iterator(NodeType *Node) {
74     this->Visited.insert(Node);
75     VisitStack.push_back(std::make_pair(Node, GT::child_begin(Node)));
76   }
77   inline df_iterator() { /* End is when stack is empty */ }
78
79   inline df_iterator(NodeType *Node, SetType &S)
80     : df_iterator_storage<SetType, ExtStorage>(S) {
81     if (!S.count(Node)) {
82       this->Visited.insert(Node);
83       VisitStack.push_back(std::make_pair(Node, GT::child_begin(Node)));
84     }
85   }
86   inline df_iterator(SetType &S) 
87     : df_iterator_storage<SetType, ExtStorage>(S) {
88     // End is when stack is empty
89   }
90
91 public:
92   typedef typename super::pointer pointer;
93   typedef df_iterator<GraphT, SetType, ExtStorage, GT> _Self;
94
95   // Provide static begin and end methods as our public "constructors"
96   static inline _Self begin(GraphT G) {
97     return _Self(GT::getEntryNode(G));
98   }
99   static inline _Self end(GraphT G) { return _Self(); }
100
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);
104   }
105   static inline _Self end(GraphT G, SetType &S) { return _Self(S); }
106
107   inline bool operator==(const _Self& x) const { 
108     return VisitStack.size() == x.VisitStack.size() &&
109            VisitStack == x.VisitStack;
110   }
111   inline bool operator!=(const _Self& x) const { return !operator==(x); }
112
113   inline pointer operator*() const { 
114     return VisitStack.back().first;
115   }
116
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.
120   //
121   inline NodeType *operator->() const { return operator*(); }
122
123   inline _Self& operator++() {   // Preincrement
124     do {
125       std::pair<NodeType *, ChildItTy> &Top = VisitStack.back();
126       NodeType *Node = Top.first;
127       ChildItTy &It  = Top.second;
128       
129       while (It != GT::child_end(Node)) {
130         NodeType *Next = *It++;
131         if (!this->Visited.count(Next)) {  // Has our next sibling been visited?
132           // No, do it now.
133           this->Visited.insert(Next);
134           VisitStack.push_back(std::make_pair(Next, GT::child_begin(Next)));
135           return *this;
136         }
137       }
138       
139       // Oops, ran out of successors... go up a level on the stack.
140       VisitStack.pop_back();
141     } while (!VisitStack.empty());
142     return *this; 
143   }
144
145   inline _Self operator++(int) { // Postincrement
146     _Self tmp = *this; ++*this; return tmp; 
147   }
148
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.
152   //
153   inline bool nodeVisited(NodeType *Node) const { 
154     return this->Visited.count(Node) != 0;
155   }
156 };
157
158
159 // Provide global constructors that automatically figure out correct types...
160 //
161 template <class T>
162 df_iterator<T> df_begin(T G) {
163   return df_iterator<T>::begin(G);
164 }
165
166 template <class T>
167 df_iterator<T> df_end(T G) {
168   return df_iterator<T>::end(G);
169 }
170
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) {}
176 };
177
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);
181 }
182
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);
186 }
187
188
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) {}
195 };
196
197 template <class T>
198 idf_iterator<T> idf_begin(T G) {
199   return idf_iterator<T>::begin(G);
200 }
201
202 template <class T>
203 idf_iterator<T> idf_end(T G){
204   return idf_iterator<T>::end(G);
205 }
206
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) {}
214 };
215
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);
219 }
220
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);
224 }
225
226
227 #endif