BreadthFirstIterator.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/ADT/BreadthFirstIterator.h - Breadth First iterator -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file builds on the ADT/GraphTraits.h file to build a generic breadth
  15. // first graph iterator. This file exposes the following functions/types:
  16. //
  17. // bf_begin/bf_end/bf_iterator
  18. // * Normal breadth-first iteration - visit a graph level-by-level.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_ADT_BREADTHFIRSTITERATOR_H
  22. #define LLVM_ADT_BREADTHFIRSTITERATOR_H
  23. #include "llvm/ADT/GraphTraits.h"
  24. #include "llvm/ADT/None.h"
  25. #include "llvm/ADT/Optional.h"
  26. #include "llvm/ADT/SmallPtrSet.h"
  27. #include "llvm/ADT/iterator_range.h"
  28. #include <iterator>
  29. #include <queue>
  30. #include <utility>
  31. namespace llvm {
  32. // bf_iterator_storage - A private class which is used to figure out where to
  33. // store the visited set. We only provide a non-external variant for now.
  34. template <class SetType> class bf_iterator_storage {
  35. public:
  36. SetType Visited;
  37. };
  38. // The visited state for the iteration is a simple set.
  39. template <typename NodeRef, unsigned SmallSize = 8>
  40. using bf_iterator_default_set = SmallPtrSet<NodeRef, SmallSize>;
  41. // Generic Breadth first search iterator.
  42. template <class GraphT,
  43. class SetType =
  44. bf_iterator_default_set<typename GraphTraits<GraphT>::NodeRef>,
  45. class GT = GraphTraits<GraphT>>
  46. class bf_iterator
  47. : public std::iterator<std::forward_iterator_tag, typename GT::NodeRef>,
  48. public bf_iterator_storage<SetType> {
  49. using super = std::iterator<std::forward_iterator_tag, typename GT::NodeRef>;
  50. using NodeRef = typename GT::NodeRef;
  51. using ChildItTy = typename GT::ChildIteratorType;
  52. // First element is the node reference, second is the next child to visit.
  53. using QueueElement = std::pair<NodeRef, Optional<ChildItTy>>;
  54. // Visit queue - used to maintain BFS ordering.
  55. // Optional<> because we need markers for levels.
  56. std::queue<Optional<QueueElement>> VisitQueue;
  57. // Current level.
  58. unsigned Level;
  59. private:
  60. inline bf_iterator(NodeRef Node) {
  61. this->Visited.insert(Node);
  62. Level = 0;
  63. // Also, insert a dummy node as marker.
  64. VisitQueue.push(QueueElement(Node, None));
  65. VisitQueue.push(None);
  66. }
  67. inline bf_iterator() = default;
  68. inline void toNext() {
  69. Optional<QueueElement> Head = VisitQueue.front();
  70. QueueElement H = Head.getValue();
  71. NodeRef Node = H.first;
  72. Optional<ChildItTy> &ChildIt = H.second;
  73. if (!ChildIt)
  74. ChildIt.emplace(GT::child_begin(Node));
  75. while (*ChildIt != GT::child_end(Node)) {
  76. NodeRef Next = *(*ChildIt)++;
  77. // Already visited?
  78. if (this->Visited.insert(Next).second)
  79. VisitQueue.push(QueueElement(Next, None));
  80. }
  81. VisitQueue.pop();
  82. // Go to the next element skipping markers if needed.
  83. if (!VisitQueue.empty()) {
  84. Head = VisitQueue.front();
  85. if (Head != None)
  86. return;
  87. Level += 1;
  88. VisitQueue.pop();
  89. // Don't push another marker if this is the last
  90. // element.
  91. if (!VisitQueue.empty())
  92. VisitQueue.push(None);
  93. }
  94. }
  95. public:
  96. using pointer = typename super::pointer;
  97. // Provide static begin and end methods as our public "constructors"
  98. static bf_iterator begin(const GraphT &G) {
  99. return bf_iterator(GT::getEntryNode(G));
  100. }
  101. static bf_iterator end(const GraphT &G) { return bf_iterator(); }
  102. bool operator==(const bf_iterator &RHS) const {
  103. return VisitQueue == RHS.VisitQueue;
  104. }
  105. bool operator!=(const bf_iterator &RHS) const { return !(*this == RHS); }
  106. const NodeRef &operator*() const { return VisitQueue.front()->first; }
  107. // This is a nonstandard operator-> that dereferences the pointer an extra
  108. // time so that you can actually call methods on the node, because the
  109. // contained type is a pointer.
  110. NodeRef operator->() const { return **this; }
  111. bf_iterator &operator++() { // Pre-increment
  112. toNext();
  113. return *this;
  114. }
  115. bf_iterator operator++(int) { // Post-increment
  116. bf_iterator ItCopy = *this;
  117. ++*this;
  118. return ItCopy;
  119. }
  120. unsigned getLevel() const { return Level; }
  121. };
  122. // Provide global constructors that automatically figure out correct types.
  123. template <class T> bf_iterator<T> bf_begin(const T &G) {
  124. return bf_iterator<T>::begin(G);
  125. }
  126. template <class T> bf_iterator<T> bf_end(const T &G) {
  127. return bf_iterator<T>::end(G);
  128. }
  129. // Provide an accessor method to use them in range-based patterns.
  130. template <class T> iterator_range<bf_iterator<T>> breadth_first(const T &G) {
  131. return make_range(bf_begin(G), bf_end(G));
  132. }
  133. } // end namespace llvm
  134. #endif // LLVM_ADT_BREADTHFIRSTITERATOR_H
  135. #ifdef __GNUC__
  136. #pragma GCC diagnostic pop
  137. #endif