1 //===- DSGraphTraits.h - Provide generic graph interface --------*- C++ -*-===//
3 // This file provides GraphTraits specializations for the DataStructure graph
4 // nodes, allowing datastructure graphs to be processed by generic graph
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_ANALYSIS_DSGRAPHTRAITS_H
10 #define LLVM_ANALYSIS_DSGRAPHTRAITS_H
12 #include "llvm/Analysis/DSGraph.h"
13 #include "Support/GraphTraits.h"
14 #include "Support/iterator"
15 #include "Support/STLExtras.h"
17 template<typename NodeTy>
18 class DSNodeIterator : public forward_iterator<const DSNode, ptrdiff_t> {
23 typedef DSNodeIterator<NodeTy> _Self;
25 DSNodeIterator(NodeTy *N) : Node(N), Offset(0) {} // begin iterator
26 DSNodeIterator(NodeTy *N, bool) : Node(N) { // Create end iterator
27 Offset = N->getNumLinks() << DS::PointerShift;
28 if (Offset == 0 && Node->getForwardNode() &&
29 (Node->NodeType & DSNode::DEAD)) // Model Forward link
30 Offset += DS::PointerSize;
33 DSNodeIterator(const DSNodeHandle &NH)
34 : Node(NH.getNode()), Offset(NH.getOffset()) {}
36 bool operator==(const _Self& x) const {
37 return Offset == x.Offset;
39 bool operator!=(const _Self& x) const { return !operator==(x); }
41 const _Self &operator=(const _Self &I) {
42 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
47 pointer operator*() const {
48 if (Node->NodeType & DSNode::DEAD)
49 return Node->getForwardNode();
51 return Node->getLink(Offset).getNode();
53 pointer operator->() const { return operator*(); }
55 _Self& operator++() { // Preincrement
56 Offset += (1 << DS::PointerShift);
59 _Self operator++(int) { // Postincrement
60 _Self tmp = *this; ++*this; return tmp;
63 unsigned getOffset() const { return Offset; }
64 const DSNode *getNode() const { return Node; }
67 // Provide iterators for DSNode...
68 inline DSNode::iterator DSNode::begin() {
69 return DSNode::iterator(this);
71 inline DSNode::iterator DSNode::end() {
72 return DSNode::iterator(this, false);
74 inline DSNode::const_iterator DSNode::begin() const {
75 return DSNode::const_iterator(this);
77 inline DSNode::const_iterator DSNode::end() const {
78 return DSNode::const_iterator(this, false);
81 template <> struct GraphTraits<DSNode*> {
82 typedef DSNode NodeType;
83 typedef DSNode::iterator ChildIteratorType;
85 static NodeType *getEntryNode(NodeType *N) { return N; }
86 static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
87 static ChildIteratorType child_end(NodeType *N) { return N->end(); }
90 template <> struct GraphTraits<const DSNode*> {
91 typedef const DSNode NodeType;
92 typedef DSNode::const_iterator ChildIteratorType;
94 static NodeType *getEntryNode(NodeType *N) { return N; }
95 static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
96 static ChildIteratorType child_end(NodeType *N) { return N->end(); }
99 static DSNode &dereference ( DSNode *N) { return *N; }
100 static const DSNode &dereferenceC(const DSNode *N) { return *N; }
102 template <> struct GraphTraits<DSGraph*> {
103 typedef DSNode NodeType;
104 typedef DSNode::iterator ChildIteratorType;
106 typedef std::pointer_to_unary_function<DSNode *, DSNode&> DerefFun;
108 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
109 typedef mapped_iterator<std::vector<DSNode*>::iterator,
110 DerefFun> nodes_iterator;
111 static nodes_iterator nodes_begin(DSGraph *G) {
112 return map_iterator(G->getNodes().begin(), DerefFun(dereference));
114 static nodes_iterator nodes_end(DSGraph *G) {
115 return map_iterator(G->getNodes().end(), DerefFun(dereference));
118 static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
119 static ChildIteratorType child_end(NodeType *N) { return N->end(); }
122 template <> struct GraphTraits<const DSGraph*> {
123 typedef const DSNode NodeType;
124 typedef DSNode::const_iterator ChildIteratorType;
126 typedef std::pointer_to_unary_function<const DSNode *,const DSNode&> DerefFun;
128 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
129 typedef mapped_iterator<std::vector<DSNode*>::const_iterator,
130 DerefFun> nodes_iterator;
131 static nodes_iterator nodes_begin(const DSGraph *G) {
132 return map_iterator(G->getNodes().begin(), DerefFun(dereferenceC));
134 static nodes_iterator nodes_end(const DSGraph *G) {
135 return map_iterator(G->getNodes().end(), DerefFun(dereferenceC));
138 static ChildIteratorType child_begin(const NodeType *N) { return N->begin(); }
139 static ChildIteratorType child_end(const NodeType *N) { return N->end(); }