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) // Create end iterator
28 Offset = (N->getSize()+((1 << DS::PointerShift)-1)) &
29 ~((1 << DS::PointerShift)-1);
32 DSNodeIterator(const DSNodeHandle &NH)
33 : Node(NH.getNode()), Offset(NH.getOffset()) {}
35 bool operator==(const _Self& x) const {
36 return Offset == x.Offset;
38 bool operator!=(const _Self& x) const { return !operator==(x); }
40 const _Self &operator=(const _Self &I) {
41 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
46 pointer operator*() const {
47 return Node->getLink(Offset).getNode();
49 pointer operator->() const { return operator*(); }
51 _Self& operator++() { // Preincrement
52 Offset += (1 << DS::PointerShift);
55 _Self operator++(int) { // Postincrement
56 _Self tmp = *this; ++*this; return tmp;
59 unsigned getOffset() const { return Offset; }
60 const DSNode *getNode() const { return Node; }
63 // Provide iterators for DSNode...
64 inline DSNode::iterator DSNode::begin() {
65 return DSNode::iterator(this);
67 inline DSNode::iterator DSNode::end() {
68 return DSNode::iterator(this, false);
70 inline DSNode::const_iterator DSNode::begin() const {
71 return DSNode::const_iterator(this);
73 inline DSNode::const_iterator DSNode::end() const {
74 return DSNode::const_iterator(this, false);
77 template <> struct GraphTraits<DSNode*> {
78 typedef DSNode NodeType;
79 typedef DSNode::iterator ChildIteratorType;
81 static NodeType *getEntryNode(NodeType *N) { return N; }
82 static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
83 static ChildIteratorType child_end(NodeType *N) { return N->end(); }
86 template <> struct GraphTraits<const DSNode*> {
87 typedef const DSNode NodeType;
88 typedef DSNode::const_iterator ChildIteratorType;
90 static NodeType *getEntryNode(NodeType *N) { return N; }
91 static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
92 static ChildIteratorType child_end(NodeType *N) { return N->end(); }
95 static DSNode &dereference ( DSNode *N) { return *N; }
96 static const DSNode &dereferenceC(const DSNode *N) { return *N; }
98 template <> struct GraphTraits<DSGraph*> {
99 typedef DSNode NodeType;
100 typedef DSNode::iterator ChildIteratorType;
102 typedef std::pointer_to_unary_function<DSNode *, DSNode&> DerefFun;
104 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
105 typedef mapped_iterator<std::vector<DSNode*>::iterator,
106 DerefFun> nodes_iterator;
107 static nodes_iterator nodes_begin(DSGraph *G) {
108 return map_iterator(G->getNodes().begin(), DerefFun(dereference));
110 static nodes_iterator nodes_end(DSGraph *G) {
111 return map_iterator(G->getNodes().end(), DerefFun(dereference));
114 static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
115 static ChildIteratorType child_end(NodeType *N) { return N->end(); }
118 template <> struct GraphTraits<const DSGraph*> {
119 typedef const DSNode NodeType;
120 typedef DSNode::const_iterator ChildIteratorType;
122 typedef std::pointer_to_unary_function<const DSNode *,const DSNode&> DerefFun;
124 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
125 typedef mapped_iterator<std::vector<DSNode*>::const_iterator,
126 DerefFun> nodes_iterator;
127 static nodes_iterator nodes_begin(const DSGraph *G) {
128 return map_iterator(G->getNodes().begin(), DerefFun(dereferenceC));
130 static nodes_iterator nodes_end(const DSGraph *G) {
131 return map_iterator(G->getNodes().end(), DerefFun(dereferenceC));
134 static ChildIteratorType child_begin(const NodeType *N) { return N->begin(); }
135 static ChildIteratorType child_end(const NodeType *N) { return N->end(); }