1 //===- DSGraphTraits.h - Provide generic graph interface --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides GraphTraits specializations for the DataStructure graph
11 // nodes, allowing datastructure graphs to be processed by generic graph
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ANALYSIS_DSGRAPHTRAITS_H
17 #define LLVM_ANALYSIS_DSGRAPHTRAITS_H
19 #include "llvm/Analysis/DSGraph.h"
20 #include "Support/GraphTraits.h"
21 #include "Support/iterator"
22 #include "Support/STLExtras.h"
26 template<typename NodeTy>
27 class DSNodeIterator : public forward_iterator<const DSNode, ptrdiff_t> {
32 typedef DSNodeIterator<NodeTy> _Self;
34 DSNodeIterator(NodeTy *N) : Node(N), Offset(0) {} // begin iterator
35 DSNodeIterator(NodeTy *N, bool) : Node(N) { // Create end iterator
36 Offset = N->getNumLinks() << DS::PointerShift;
37 if (Offset == 0 && Node->getForwardNode() &&
38 Node->isDeadNode()) // Model Forward link
39 Offset += DS::PointerSize;
42 DSNodeIterator(const DSNodeHandle &NH)
43 : Node(NH.getNode()), Offset(NH.getOffset()) {}
45 bool operator==(const _Self& x) const {
46 return Offset == x.Offset;
48 bool operator!=(const _Self& x) const { return !operator==(x); }
50 const _Self &operator=(const _Self &I) {
51 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
56 pointer operator*() const {
57 if (Node->isDeadNode())
58 return Node->getForwardNode();
60 return Node->getLink(Offset).getNode();
62 pointer operator->() const { return operator*(); }
64 _Self& operator++() { // Preincrement
65 Offset += (1 << DS::PointerShift);
68 _Self operator++(int) { // Postincrement
69 _Self tmp = *this; ++*this; return tmp;
72 unsigned getOffset() const { return Offset; }
73 const DSNode *getNode() const { return Node; }
76 // Provide iterators for DSNode...
77 inline DSNode::iterator DSNode::begin() {
78 return DSNode::iterator(this);
80 inline DSNode::iterator DSNode::end() {
81 return DSNode::iterator(this, false);
83 inline DSNode::const_iterator DSNode::begin() const {
84 return DSNode::const_iterator(this);
86 inline DSNode::const_iterator DSNode::end() const {
87 return DSNode::const_iterator(this, false);
90 template <> struct GraphTraits<DSNode*> {
91 typedef DSNode NodeType;
92 typedef DSNode::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 template <> struct GraphTraits<const DSNode*> {
100 typedef const DSNode NodeType;
101 typedef DSNode::const_iterator ChildIteratorType;
103 static NodeType *getEntryNode(NodeType *N) { return N; }
104 static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
105 static ChildIteratorType child_end(NodeType *N) { return N->end(); }
108 static DSNode &dereference ( DSNode *N) { return *N; }
109 static const DSNode &dereferenceC(const DSNode *N) { return *N; }
111 template <> struct GraphTraits<DSGraph*> {
112 typedef DSNode NodeType;
113 typedef DSNode::iterator ChildIteratorType;
115 typedef std::pointer_to_unary_function<DSNode *, DSNode&> DerefFun;
117 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
118 typedef mapped_iterator<DSGraph::node_iterator, DerefFun> nodes_iterator;
119 static nodes_iterator nodes_begin(DSGraph *G) {
120 return map_iterator(G->node_begin(), DerefFun(dereference));
122 static nodes_iterator nodes_end(DSGraph *G) {
123 return map_iterator(G->node_end(), DerefFun(dereference));
126 static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
127 static ChildIteratorType child_end(NodeType *N) { return N->end(); }
130 template <> struct GraphTraits<const DSGraph*> {
131 typedef const DSNode NodeType;
132 typedef DSNode::const_iterator ChildIteratorType;
134 typedef std::pointer_to_unary_function<const DSNode *,const DSNode&> DerefFun;
136 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
137 typedef mapped_iterator<DSGraph::node_iterator, DerefFun> nodes_iterator;
138 static nodes_iterator nodes_begin(const DSGraph *G) {
139 return map_iterator(G->node_begin(), DerefFun(dereferenceC));
141 static nodes_iterator nodes_end(const DSGraph *G) {
142 return map_iterator(G->node_end(), DerefFun(dereferenceC));
145 static ChildIteratorType child_begin(const NodeType *N) { return N->begin(); }
146 static ChildIteratorType child_end(const NodeType *N) { return N->end(); }
149 } // End llvm namespace