~ubuntu-branches/ubuntu/saucy/clamav/saucy

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/include/llvm/ADT/DepthFirstIterator.h

  • Committer: Bazaar Package Importer
  • Author(s): Leonel Nunez
  • Date: 2008-02-11 22:52:13 UTC
  • mfrom: (1.1.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 38.
  • Revision ID: james.westby@ubuntu.com-20080211225213-p2uwj4czso1w2f8h
Tags: upstream-0.92~dfsg
ImportĀ upstreamĀ versionĀ 0.92~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//===- llvm/ADT/DepthFirstIterator.h - Depth First iterator -----*- C++ -*-===//
2
 
//
3
 
//                     The LLVM Compiler Infrastructure
4
 
//
5
 
// This file is distributed under the University of Illinois Open Source
6
 
// License. See LICENSE.TXT for details.
7
 
//
8
 
//===----------------------------------------------------------------------===//
9
 
//
10
 
// This file builds on the ADT/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 LLVM_ADT_DEPTHFIRSTITERATOR_H
34
 
#define LLVM_ADT_DEPTHFIRSTITERATOR_H
35
 
 
36
 
#include "llvm/ADT/GraphTraits.h"
37
 
#include "llvm/ADT/SmallPtrSet.h"
38
 
#include "llvm/ADT/PointerIntPair.h"
39
 
#include <set>
40
 
#include <vector>
41
 
 
42
 
namespace llvm {
43
 
 
44
 
// df_iterator_storage - A private class which is used to figure out where to
45
 
// store the visited set.
46
 
template<class SetType, bool External>   // Non-external set
47
 
class df_iterator_storage {
48
 
public:
49
 
  SetType Visited;
50
 
};
51
 
 
52
 
template<class SetType>
53
 
class df_iterator_storage<SetType, true> {
54
 
public:
55
 
  df_iterator_storage(SetType &VSet) : Visited(VSet) {}
56
 
  df_iterator_storage(const df_iterator_storage &S) : Visited(S.Visited) {}
57
 
  SetType &Visited;
58
 
};
59
 
 
60
 
 
61
 
// Generic Depth First Iterator
62
 
template<class GraphT,
63
 
class SetType = llvm::SmallPtrSet<typename GraphTraits<GraphT>::NodeType*, 8>,
64
 
         bool ExtStorage = false, class GT = GraphTraits<GraphT> >
65
 
class df_iterator : public std::iterator<std::forward_iterator_tag,
66
 
                                         typename GT::NodeType, ptrdiff_t>,
67
 
                    public df_iterator_storage<SetType, ExtStorage> {
68
 
  typedef std::iterator<std::forward_iterator_tag,
69
 
                        typename GT::NodeType, ptrdiff_t> super;
70
 
 
71
 
  typedef typename GT::NodeType          NodeType;
72
 
  typedef typename GT::ChildIteratorType ChildItTy;
73
 
  typedef PointerIntPair<NodeType*, 1>   PointerIntTy;
74
 
 
75
 
  // VisitStack - Used to maintain the ordering.  Top = current block
76
 
  // First element is node pointer, second is the 'next child' to visit
77
 
  // if the int in PointerIntTy is 0, the 'next child' to visit is invalid
78
 
  std::vector<std::pair<PointerIntTy, ChildItTy> > VisitStack;
79
 
private:
80
 
  inline df_iterator(NodeType *Node) {
81
 
    this->Visited.insert(Node);
82
 
    VisitStack.push_back(std::make_pair(PointerIntTy(Node, 0), 
83
 
                                        GT::child_begin(Node)));
84
 
  }
85
 
  inline df_iterator() { 
86
 
    // End is when stack is empty 
87
 
  }
88
 
  inline df_iterator(NodeType *Node, SetType &S)
89
 
    : df_iterator_storage<SetType, ExtStorage>(S) {
90
 
    if (!S.count(Node)) {
91
 
      VisitStack.push_back(std::make_pair(PointerIntTy(Node, 0), 
92
 
                                          GT::child_begin(Node)));
93
 
      this->Visited.insert(Node);
94
 
    }
95
 
  }
96
 
  inline df_iterator(SetType &S)
97
 
    : df_iterator_storage<SetType, ExtStorage>(S) {
98
 
    // End is when stack is empty
99
 
  }
100
 
 
101
 
  inline void toNext() {
102
 
    do {
103
 
      std::pair<PointerIntTy, ChildItTy> &Top = VisitStack.back();
104
 
      NodeType *Node = Top.first.getPointer();
105
 
      ChildItTy &It  = Top.second;
106
 
      if (!Top.first.getInt()) {
107
 
        // now retrieve the real begin of the children before we dive in
108
 
        It = GT::child_begin(Node);
109
 
        Top.first.setInt(1);
110
 
      }
111
 
 
112
 
      while (It != GT::child_end(Node)) {
113
 
        NodeType *Next = *It++;
114
 
        // Has our next sibling been visited?
115
 
        if (Next && !this->Visited.count(Next)) {  
116
 
          // No, do it now.
117
 
          this->Visited.insert(Next);
118
 
          VisitStack.push_back(std::make_pair(PointerIntTy(Next, 0), 
119
 
                                              GT::child_begin(Next)));
120
 
          return;
121
 
        }
122
 
      }
123
 
 
124
 
      // Oops, ran out of successors... go up a level on the stack.
125
 
      VisitStack.pop_back();
126
 
    } while (!VisitStack.empty());
127
 
  }
128
 
 
129
 
public:
130
 
  typedef typename super::pointer pointer;
131
 
  typedef df_iterator<GraphT, SetType, ExtStorage, GT> _Self;
132
 
 
133
 
  // Provide static begin and end methods as our public "constructors"
134
 
  static inline _Self begin(const GraphT& G) {
135
 
    return _Self(GT::getEntryNode(G));
136
 
  }
137
 
  static inline _Self end(const GraphT& G) { return _Self(); }
138
 
 
139
 
  // Static begin and end methods as our public ctors for external iterators
140
 
  static inline _Self begin(const GraphT& G, SetType &S) {
141
 
    return _Self(GT::getEntryNode(G), S);
142
 
  }
143
 
  static inline _Self end(const GraphT& G, SetType &S) { return _Self(S); }
144
 
 
145
 
  inline bool operator==(const _Self& x) const {
146
 
    return VisitStack.size() == x.VisitStack.size() &&
147
 
           VisitStack == x.VisitStack;
148
 
  }
149
 
  inline bool operator!=(const _Self& x) const { return !operator==(x); }
150
 
 
151
 
  inline pointer operator*() const {
152
 
    return VisitStack.back().first.getPointer();
153
 
  }
154
 
 
155
 
  // This is a nonstandard operator-> that dereferences the pointer an extra
156
 
  // time... so that you can actually call methods ON the Node, because
157
 
  // the contained type is a pointer.  This allows BBIt->getTerminator() f.e.
158
 
  //
159
 
  inline NodeType *operator->() const { return operator*(); }
160
 
 
161
 
  inline _Self& operator++() {   // Preincrement
162
 
    toNext();
163
 
    return *this;
164
 
  }
165
 
 
166
 
  // skips all children of the current node and traverses to next node
167
 
  //
168
 
  inline _Self& skipChildren() {  
169
 
    VisitStack.pop_back();
170
 
    if (!VisitStack.empty())
171
 
      toNext();
172
 
    return *this;
173
 
  }
174
 
 
175
 
  inline _Self operator++(int) { // Postincrement
176
 
    _Self tmp = *this; ++*this; return tmp;
177
 
  }
178
 
 
179
 
  // nodeVisited - return true if this iterator has already visited the
180
 
  // specified node.  This is public, and will probably be used to iterate over
181
 
  // nodes that a depth first iteration did not find: ie unreachable nodes.
182
 
  //
183
 
  inline bool nodeVisited(NodeType *Node) const {
184
 
    return this->Visited.count(Node) != 0;
185
 
  }
186
 
 
187
 
  /// getPathLength - Return the length of the path from the entry node to the
188
 
  /// current node, counting both nodes.
189
 
  unsigned getPathLength() const { return VisitStack.size(); }
190
 
 
191
 
  /// getPath - Return the n'th node in the path from the the entry node to the
192
 
  /// current node.
193
 
  NodeType *getPath(unsigned n) const {
194
 
    return VisitStack[n].first.getPointer();
195
 
  }
196
 
};
197
 
 
198
 
 
199
 
// Provide global constructors that automatically figure out correct types...
200
 
//
201
 
template <class T>
202
 
df_iterator<T> df_begin(const T& G) {
203
 
  return df_iterator<T>::begin(G);
204
 
}
205
 
 
206
 
template <class T>
207
 
df_iterator<T> df_end(const T& G) {
208
 
  return df_iterator<T>::end(G);
209
 
}
210
 
 
211
 
// Provide global definitions of external depth first iterators...
212
 
template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
213
 
struct df_ext_iterator : public df_iterator<T, SetTy, true> {
214
 
  df_ext_iterator(const df_iterator<T, SetTy, true> &V)
215
 
    : df_iterator<T, SetTy, true>(V) {}
216
 
};
217
 
 
218
 
template <class T, class SetTy>
219
 
df_ext_iterator<T, SetTy> df_ext_begin(const T& G, SetTy &S) {
220
 
  return df_ext_iterator<T, SetTy>::begin(G, S);
221
 
}
222
 
 
223
 
template <class T, class SetTy>
224
 
df_ext_iterator<T, SetTy> df_ext_end(const T& G, SetTy &S) {
225
 
  return df_ext_iterator<T, SetTy>::end(G, S);
226
 
}
227
 
 
228
 
 
229
 
// Provide global definitions of inverse depth first iterators...
230
 
template <class T,
231
 
  class SetTy = llvm::SmallPtrSet<typename GraphTraits<T>::NodeType*, 8>,
232
 
          bool External = false>
233
 
struct idf_iterator : public df_iterator<Inverse<T>, SetTy, External> {
234
 
  idf_iterator(const df_iterator<Inverse<T>, SetTy, External> &V)
235
 
    : df_iterator<Inverse<T>, SetTy, External>(V) {}
236
 
};
237
 
 
238
 
template <class T>
239
 
idf_iterator<T> idf_begin(const T& G) {
240
 
  return idf_iterator<T>::begin(Inverse<T>(G));
241
 
}
242
 
 
243
 
template <class T>
244
 
idf_iterator<T> idf_end(const T& G){
245
 
  return idf_iterator<T>::end(Inverse<T>(G));
246
 
}
247
 
 
248
 
// Provide global definitions of external inverse depth first iterators...
249
 
template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
250
 
struct idf_ext_iterator : public idf_iterator<T, SetTy, true> {
251
 
  idf_ext_iterator(const idf_iterator<T, SetTy, true> &V)
252
 
    : idf_iterator<T, SetTy, true>(V) {}
253
 
  idf_ext_iterator(const df_iterator<Inverse<T>, SetTy, true> &V)
254
 
    : idf_iterator<T, SetTy, true>(V) {}
255
 
};
256
 
 
257
 
template <class T, class SetTy>
258
 
idf_ext_iterator<T, SetTy> idf_ext_begin(const T& G, SetTy &S) {
259
 
  return idf_ext_iterator<T, SetTy>::begin(Inverse<T>(G), S);
260
 
}
261
 
 
262
 
template <class T, class SetTy>
263
 
idf_ext_iterator<T, SetTy> idf_ext_end(const T& G, SetTy &S) {
264
 
  return idf_ext_iterator<T, SetTy>::end(Inverse<T>(G), S);
265
 
}
266
 
 
267
 
} // End llvm namespace
268
 
 
269
 
#endif