2002-10-10 20:36:37 +00:00
|
|
|
//===- DSGraphTraits.h - Provide generic graph interface --------*- C++ -*-===//
|
2002-03-30 09:13:23 +00:00
|
|
|
//
|
|
|
|
// This file provides GraphTraits specializations for the DataStructure graph
|
|
|
|
// nodes, allowing datastructure graphs to be processed by generic graph
|
|
|
|
// algorithms.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-10-10 20:36:37 +00:00
|
|
|
#ifndef LLVM_ANALYSIS_DSGRAPHTRAITS_H
|
|
|
|
#define LLVM_ANALYSIS_DSGRAPHTRAITS_H
|
2002-03-30 09:13:23 +00:00
|
|
|
|
2002-10-10 20:36:37 +00:00
|
|
|
#include "llvm/Analysis/DSGraph.h"
|
2002-07-25 15:00:45 +00:00
|
|
|
#include "Support/GraphTraits.h"
|
|
|
|
#include "Support/iterator"
|
2002-10-13 19:31:34 +00:00
|
|
|
#include "Support/STLExtras.h"
|
2002-10-01 22:34:45 +00:00
|
|
|
|
2002-10-17 01:01:06 +00:00
|
|
|
class DSNodeIterator : public forward_iterator<const DSNode, ptrdiff_t> {
|
2002-03-31 07:11:20 +00:00
|
|
|
friend class DSNode;
|
2002-10-17 01:01:06 +00:00
|
|
|
const DSNode * const Node;
|
2002-10-16 01:43:11 +00:00
|
|
|
unsigned Offset;
|
2002-03-30 09:13:23 +00:00
|
|
|
|
|
|
|
typedef DSNodeIterator _Self;
|
2002-03-31 07:11:20 +00:00
|
|
|
|
2002-10-17 01:01:06 +00:00
|
|
|
DSNodeIterator(const DSNode *N) : Node(N), Offset(0) {} // begin iterator
|
|
|
|
DSNodeIterator(const DSNode *N, bool) // Create end iterator
|
2002-10-16 01:43:11 +00:00
|
|
|
: Node(N), Offset(N->getSize()) {
|
2002-03-30 09:13:23 +00:00
|
|
|
}
|
2002-03-31 07:11:20 +00:00
|
|
|
public:
|
2002-10-16 01:43:11 +00:00
|
|
|
DSNodeIterator(const DSNodeHandle &NH)
|
|
|
|
: Node(NH.getNode()), Offset(NH.getOffset()) {}
|
2002-03-30 09:13:23 +00:00
|
|
|
|
|
|
|
bool operator==(const _Self& x) const {
|
2002-10-16 01:43:11 +00:00
|
|
|
return Offset == x.Offset;
|
2002-03-30 09:13:23 +00:00
|
|
|
}
|
|
|
|
bool operator!=(const _Self& x) const { return !operator==(x); }
|
2002-10-13 19:31:34 +00:00
|
|
|
|
|
|
|
const _Self &operator=(const _Self &I) {
|
|
|
|
assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
|
2002-10-16 01:43:11 +00:00
|
|
|
Offset = I.Offset;
|
2002-10-13 19:31:34 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2002-03-30 09:13:23 +00:00
|
|
|
|
|
|
|
pointer operator*() const {
|
2002-10-17 01:01:06 +00:00
|
|
|
const DSNodeHandle *NH = Node->getLink(Offset);
|
2002-10-13 19:31:34 +00:00
|
|
|
return NH ? NH->getNode() : 0;
|
2002-03-30 09:13:23 +00:00
|
|
|
}
|
|
|
|
pointer operator->() const { return operator*(); }
|
|
|
|
|
|
|
|
_Self& operator++() { // Preincrement
|
2002-10-16 01:43:11 +00:00
|
|
|
++Offset;
|
2002-03-30 09:13:23 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
_Self operator++(int) { // Postincrement
|
|
|
|
_Self tmp = *this; ++*this; return tmp;
|
|
|
|
}
|
2002-10-13 19:31:34 +00:00
|
|
|
|
2002-10-16 01:43:11 +00:00
|
|
|
unsigned getOffset() const { return Offset; }
|
2002-10-17 01:01:06 +00:00
|
|
|
const DSNode *getNode() const { return Node; }
|
2002-03-30 09:13:23 +00:00
|
|
|
};
|
|
|
|
|
2002-10-13 19:31:34 +00:00
|
|
|
// Provide iterators for DSNode...
|
2002-10-17 01:01:06 +00:00
|
|
|
inline DSNode::iterator DSNode::begin() const { return DSNodeIterator(this); }
|
|
|
|
inline DSNode::iterator DSNode::end() const { return DSNodeIterator(this, false); }
|
2002-03-30 09:13:23 +00:00
|
|
|
|
|
|
|
template <> struct GraphTraits<DSNode*> {
|
|
|
|
typedef DSNode NodeType;
|
2002-03-31 07:11:20 +00:00
|
|
|
typedef DSNode::iterator ChildIteratorType;
|
2002-03-30 09:13:23 +00:00
|
|
|
|
2002-10-13 19:31:34 +00:00
|
|
|
static NodeType *getEntryNode(NodeType *N) { return N; }
|
2002-03-31 07:11:20 +00:00
|
|
|
static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
|
|
|
|
static ChildIteratorType child_end(NodeType *N) { return N->end(); }
|
2002-03-30 09:13:23 +00:00
|
|
|
};
|
|
|
|
|
2002-10-17 01:01:06 +00:00
|
|
|
static DSNode &dereference ( DSNode *N) { return *N; }
|
|
|
|
static const DSNode &dereferenceC(const DSNode *N) { return *N; }
|
2002-03-30 09:13:23 +00:00
|
|
|
|
2002-10-13 19:31:34 +00:00
|
|
|
template <> struct GraphTraits<DSGraph*> {
|
|
|
|
typedef DSNode NodeType;
|
|
|
|
typedef DSNode::iterator ChildIteratorType;
|
|
|
|
|
|
|
|
typedef std::pointer_to_unary_function<DSNode *, DSNode&> DerefFun;
|
|
|
|
|
|
|
|
// nodes_iterator/begin/end - Allow iteration over all nodes in the graph
|
|
|
|
typedef mapped_iterator<std::vector<DSNode*>::iterator,
|
|
|
|
DerefFun> nodes_iterator;
|
2002-10-17 01:01:06 +00:00
|
|
|
static nodes_iterator nodes_begin(DSGraph *G) {
|
|
|
|
return map_iterator(G->getNodes().begin(), DerefFun(dereference));
|
|
|
|
}
|
|
|
|
static nodes_iterator nodes_end(DSGraph *G) {
|
|
|
|
return map_iterator(G->getNodes().end(), DerefFun(dereference));
|
|
|
|
}
|
2002-10-13 19:31:34 +00:00
|
|
|
|
|
|
|
static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
|
|
|
|
static ChildIteratorType child_end(NodeType *N) { return N->end(); }
|
|
|
|
};
|
2002-10-01 22:34:45 +00:00
|
|
|
|
2002-10-17 01:01:06 +00:00
|
|
|
template <> struct GraphTraits<const DSGraph*> {
|
|
|
|
typedef const DSNode NodeType;
|
|
|
|
typedef DSNode::iterator ChildIteratorType;
|
|
|
|
|
|
|
|
typedef std::pointer_to_unary_function<const DSNode *,const DSNode&> DerefFun;
|
|
|
|
|
|
|
|
// nodes_iterator/begin/end - Allow iteration over all nodes in the graph
|
|
|
|
typedef mapped_iterator<std::vector<DSNode*>::const_iterator,
|
|
|
|
DerefFun> nodes_iterator;
|
|
|
|
static nodes_iterator nodes_begin(const DSGraph *G) {
|
|
|
|
return map_iterator(G->getNodes().begin(), DerefFun(dereferenceC));
|
|
|
|
}
|
|
|
|
static nodes_iterator nodes_end(const DSGraph *G) {
|
|
|
|
return map_iterator(G->getNodes().end(), DerefFun(dereferenceC));
|
|
|
|
}
|
|
|
|
|
|
|
|
static ChildIteratorType child_begin(const NodeType *N) { return N->begin(); }
|
|
|
|
static ChildIteratorType child_end(const NodeType *N) { return N->end(); }
|
|
|
|
};
|
|
|
|
|
2002-10-01 22:34:45 +00:00
|
|
|
#endif
|