#ifndef LLVM_ADT_SCCITERATOR_H
#define LLVM_ADT_SCCITERATOR_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/GraphTraits.h"
#include "llvm/ADT/iterator.h"
#include <cassert>
#include <cstddef>
#include <iterator>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace llvm {
template <class GraphT, class GT = GraphTraits<GraphT>>
class scc_iterator : public iterator_facade_base<
scc_iterator<GraphT, GT>, std::forward_iterator_tag,
const std::vector<typename GT::NodeRef>, ptrdiff_t> {
using NodeRef = typename GT::NodeRef;
using ChildItTy = typename GT::ChildIteratorType;
using SccTy = std::vector<NodeRef>;
using reference = typename scc_iterator::reference;
struct StackElement {
NodeRef Node; ChildItTy NextChild; unsigned MinVisited;
StackElement(NodeRef Node, const ChildItTy &Child, unsigned Min)
: Node(Node), NextChild(Child), MinVisited(Min) {}
bool operator==(const StackElement &Other) const {
return Node == Other.Node &&
NextChild == Other.NextChild &&
MinVisited == Other.MinVisited;
}
};
unsigned visitNum;
DenseMap<NodeRef, unsigned> nodeVisitNumbers;
std::vector<NodeRef> SCCNodeStack;
SccTy CurrentSCC;
std::vector<StackElement> VisitStack;
void DFSVisitOne(NodeRef N);
void DFSVisitChildren();
void GetNextSCC();
scc_iterator(NodeRef entryN) : visitNum(0) {
DFSVisitOne(entryN);
GetNextSCC();
}
scc_iterator() = default;
public:
static scc_iterator begin(const GraphT &G) {
return scc_iterator(GT::getEntryNode(G));
}
static scc_iterator end(const GraphT &) { return scc_iterator(); }
bool isAtEnd() const {
assert(!CurrentSCC.empty() || VisitStack.empty());
return CurrentSCC.empty();
}
bool operator==(const scc_iterator &x) const {
return VisitStack == x.VisitStack && CurrentSCC == x.CurrentSCC;
}
scc_iterator &operator++() {
GetNextSCC();
return *this;
}
reference operator*() const {
assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
return CurrentSCC;
}
bool hasCycle() const;
void ReplaceNode(NodeRef Old, NodeRef New) {
assert(nodeVisitNumbers.count(Old) && "Old not in scc_iterator?");
auto tempVal = nodeVisitNumbers[Old];
nodeVisitNumbers[New] = tempVal;
nodeVisitNumbers.erase(Old);
}
};
template <class GraphT, class GT>
void scc_iterator<GraphT, GT>::DFSVisitOne(NodeRef N) {
++visitNum;
nodeVisitNumbers[N] = visitNum;
SCCNodeStack.push_back(N);
VisitStack.push_back(StackElement(N, GT::child_begin(N), visitNum));
#if 0#endif
}
template <class GraphT, class GT>
void scc_iterator<GraphT, GT>::DFSVisitChildren() {
assert(!VisitStack.empty());
while (VisitStack.back().NextChild != GT::child_end(VisitStack.back().Node)) {
NodeRef childN = *VisitStack.back().NextChild++;
typename DenseMap<NodeRef, unsigned>::iterator Visited =
nodeVisitNumbers.find(childN);
if (Visited == nodeVisitNumbers.end()) {
DFSVisitOne(childN);
continue;
}
unsigned childNum = Visited->second;
if (VisitStack.back().MinVisited > childNum)
VisitStack.back().MinVisited = childNum;
}
}
template <class GraphT, class GT> void scc_iterator<GraphT, GT>::GetNextSCC() {
CurrentSCC.clear(); while (!VisitStack.empty()) {
DFSVisitChildren();
NodeRef visitingN = VisitStack.back().Node;
unsigned minVisitNum = VisitStack.back().MinVisited;
assert(VisitStack.back().NextChild == GT::child_end(visitingN));
VisitStack.pop_back();
if (!VisitStack.empty() && VisitStack.back().MinVisited > minVisitNum)
VisitStack.back().MinVisited = minVisitNum;
#if 0#endif
if (minVisitNum != nodeVisitNumbers[visitingN])
continue;
do {
CurrentSCC.push_back(SCCNodeStack.back());
SCCNodeStack.pop_back();
nodeVisitNumbers[CurrentSCC.back()] = ~0U;
} while (CurrentSCC.back() != visitingN);
return;
}
}
template <class GraphT, class GT>
bool scc_iterator<GraphT, GT>::hasCycle() const {
assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
if (CurrentSCC.size() > 1)
return true;
NodeRef N = CurrentSCC.front();
for (ChildItTy CI = GT::child_begin(N), CE = GT::child_end(N); CI != CE;
++CI)
if (*CI == N)
return true;
return false;
}
template <class T> scc_iterator<T> scc_begin(const T &G) {
return scc_iterator<T>::begin(G);
}
template <class T> scc_iterator<T> scc_end(const T &G) {
return scc_iterator<T>::end(G);
}
template <class GraphT, class GT = GraphTraits<GraphT>>
class scc_member_iterator {
using NodeType = typename GT::NodeType;
using EdgeType = typename GT::EdgeType;
using NodesType = std::vector<NodeType *>;
struct NodeInfo {
NodeInfo *Group = this;
uint32_t Rank = 0;
bool Visited = true;
};
NodeInfo *find(NodeInfo *Node) {
if (Node->Group != Node)
Node->Group = find(Node->Group);
return Node->Group;
}
bool unionGroups(const EdgeType *Edge) {
NodeInfo *G1 = find(&NodeInfoMap[Edge->Source]);
NodeInfo *G2 = find(&NodeInfoMap[Edge->Target]);
if (G1 == G2)
return false;
if (G1->Rank < G1->Rank)
G1->Group = G2;
else {
G2->Group = G1;
if (G1->Rank == G2->Rank)
G2->Rank++;
}
return true;
}
std::unordered_map<NodeType *, NodeInfo> NodeInfoMap;
NodesType Nodes;
public:
scc_member_iterator(const NodesType &InputNodes);
NodesType &operator*() { return Nodes; }
};
template <class GraphT, class GT>
scc_member_iterator<GraphT, GT>::scc_member_iterator(
const NodesType &InputNodes) {
if (InputNodes.size() <= 1) {
Nodes = InputNodes;
return;
}
NodeInfoMap.clear();
for (auto *Node : InputNodes) {
(void)NodeInfoMap[Node].Group;
}
struct EdgeComparer {
bool operator()(const EdgeType *L, const EdgeType *R) const {
return L->Weight > R->Weight;
}
};
std::multiset<const EdgeType *, EdgeComparer> SortedEdges;
for (auto *Node : InputNodes) {
for (auto &Edge : Node->Edges) {
if (NodeInfoMap.count(Edge.Target))
SortedEdges.insert(&Edge);
}
}
std::unordered_set<const EdgeType *> MSTEdges;
for (auto *Edge : SortedEdges) {
if (unionGroups(Edge))
MSTEdges.insert(Edge);
}
for (const auto *Edge : MSTEdges)
NodeInfoMap[Edge->Target].Visited = false;
std::queue<NodeType *> Queue;
for (auto *Edge : SortedEdges) {
if (NodeInfoMap[Edge->Source].Visited) {
Queue.push(Edge->Source);
NodeInfoMap[Edge->Source].Visited = false;
}
}
while (!Queue.empty()) {
auto *Node = Queue.front();
Queue.pop();
Nodes.push_back(Node);
for (auto &Edge : Node->Edges) {
if (MSTEdges.count(&Edge) && !NodeInfoMap[Edge.Target].Visited) {
NodeInfoMap[Edge.Target].Visited = true;
Queue.push(Edge.Target);
}
}
}
assert(InputNodes.size() == Nodes.size() && "missing nodes in MST");
std::reverse(Nodes.begin(), Nodes.end());
}
}
#endif