1 //===- llvm/ADT/BreadthFirstIterator.h - Breadth First iterator -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file builds on the ADT/GraphTraits.h file to build a generic breadth
11 // first graph iterator. This file exposes the following functions/types:
13 // bf_begin/bf_end/bf_iterator
14 // * Normal breadth-first iteration - visit a graph level-by-level.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_ADT_BREADTHFIRSTITERATOR_H
19 #define LLVM_ADT_BREADTHFIRSTITERATOR_H
21 #include "llvm/ADT/GraphTraits.h"
22 #include "llvm/ADT/None.h"
23 #include "llvm/ADT/Optional.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/iterator_range.h"
33 // bf_iterator_storage - A private class which is used to figure out where to
34 // store the visited set. We only provide a non-external variant for now.
35 template <class SetType> class bf_iterator_storage {
40 // The visited state for the iteration is a simple set.
41 template <typename NodeRef, unsigned SmallSize = 8>
42 using bf_iterator_default_set = SmallPtrSet<NodeRef, SmallSize>;
44 // Generic Breadth first search iterator.
45 template <class GraphT,
47 bf_iterator_default_set<typename GraphTraits<GraphT>::NodeRef>,
48 class GT = GraphTraits<GraphT>>
50 : public std::iterator<std::forward_iterator_tag, typename GT::NodeRef>,
51 public bf_iterator_storage<SetType> {
52 typedef std::iterator<std::forward_iterator_tag, typename GT::NodeRef> super;
54 typedef typename GT::NodeRef NodeRef;
55 typedef typename GT::ChildIteratorType ChildItTy;
57 // First element is the node reference, second is the next child to visit.
58 typedef std::pair<NodeRef, Optional<ChildItTy>> QueueElement;
60 // Visit queue - used to maintain BFS ordering.
61 // Optional<> because we need markers for levels.
62 std::queue<Optional<QueueElement>> VisitQueue;
68 inline bf_iterator(NodeRef Node) {
69 this->Visited.insert(Node);
72 // Also, insert a dummy node as marker.
73 VisitQueue.push(QueueElement(Node, None));
74 VisitQueue.push(None);
77 inline bf_iterator() = default;
79 inline void toNext() {
80 Optional<QueueElement> Head = VisitQueue.front();
81 QueueElement H = Head.getValue();
82 NodeRef Node = H.first;
83 Optional<ChildItTy> &ChildIt = H.second;
86 ChildIt.emplace(GT::child_begin(Node));
87 while (*ChildIt != GT::child_end(Node)) {
88 NodeRef Next = *(*ChildIt)++;
91 if (this->Visited.insert(Next).second)
92 VisitQueue.push(QueueElement(Next, None));
96 // Go to the next element skipping markers if needed.
97 if (!VisitQueue.empty()) {
98 Head = VisitQueue.front();
104 // Don't push another marker if this is the last
106 if (!VisitQueue.empty())
107 VisitQueue.push(None);
112 typedef typename super::pointer pointer;
114 // Provide static begin and end methods as our public "constructors"
115 static bf_iterator begin(const GraphT &G) {
116 return bf_iterator(GT::getEntryNode(G));
119 static bf_iterator end(const GraphT &G) { return bf_iterator(); }
121 bool operator==(const bf_iterator &RHS) const {
122 return VisitQueue == RHS.VisitQueue;
125 bool operator!=(const bf_iterator &RHS) const { return !(*this == RHS); }
127 const NodeRef &operator*() const { return VisitQueue.front()->first; }
129 // This is a nonstandard operator-> that dereferenfces the pointer an extra
130 // time so that you can actually call methods on the node, because the
131 // contained type is a pointer.
132 NodeRef operator->() const { return **this; }
134 bf_iterator &operator++() { // Pre-increment
139 bf_iterator operator++(int) { // Post-increment
140 bf_iterator ItCopy = *this;
145 unsigned getLevel() const { return Level; }
148 // Provide global constructors that automatically figure out correct types.
149 template <class T> bf_iterator<T> bf_begin(const T &G) {
150 return bf_iterator<T>::begin(G);
153 template <class T> bf_iterator<T> bf_end(const T &G) {
154 return bf_iterator<T>::end(G);
157 // Provide an accessor method to use them in range-based patterns.
158 template <class T> iterator_range<bf_iterator<T>> breadth_first(const T &G) {
159 return make_range(bf_begin(G), bf_end(G));
162 } // end namespace llvm
164 #endif // LLVM_ADT_BREADTHFIRSTITERATOR_H