Remove a ton of extraneous #includes
[oota-llvm.git] / include / llvm / Analysis / DataStructure / DSGraphTraits.h
index d6ea1edac8c756ac6697f2782f067f9b038d80e8..7ea30c02c0db412b6b727ab9c7d459c2a7fc8d3e 100644 (file)
 #include "Support/iterator"
 #include "Support/STLExtras.h"
 
-class DSNodeIterator : public forward_iterator<DSNode, ptrdiff_t> {
+template<typename NodeTy>
+class DSNodeIterator : public forward_iterator<const DSNode, ptrdiff_t> {
   friend class DSNode;
-  DSNode * const Node;
+  NodeTy * const Node;
   unsigned Offset;
   
-  typedef DSNodeIterator _Self;
-
-  DSNodeIterator(DSNode *N) : Node(N), Offset(0) {}   // begin iterator
-  DSNodeIterator(DSNode *N, bool)       // Create end iterator
-    : Node(N), Offset(N->getSize()) {
+  typedef DSNodeIterator<NodeTy> _Self;
+
+  DSNodeIterator(NodeTy *N) : Node(N), Offset(0) {}   // begin iterator
+  DSNodeIterator(NodeTy *N, bool) : Node(N) {         // Create end iterator
+    Offset = N->getNumLinks() << DS::PointerShift;
+    if (Offset == 0 && Node->getForwardNode() &&
+        Node->isDeadNode())        // Model Forward link
+      Offset += DS::PointerSize;
   }
 public:
   DSNodeIterator(const DSNodeHandle &NH)
@@ -41,13 +45,15 @@ public:
   }
   
   pointer operator*() const {
-    DSNodeHandle *NH = Node->getLink(Offset);
-    return NH ? NH->getNode() : 0;
+    if (Node->isDeadNode())
+      return Node->getForwardNode();
+    else
+      return Node->getLink(Offset).getNode();
   }
   pointer operator->() const { return operator*(); }
   
   _Self& operator++() {                // Preincrement
-    ++Offset;
+    Offset += (1 << DS::PointerShift);
     return *this;
   }
   _Self operator++(int) { // Postincrement
@@ -55,12 +61,22 @@ public:
   }
 
   unsigned getOffset() const { return Offset; }
-  DSNode *getNode() const { return Node; }
+  const DSNode *getNode() const { return Node; }
 };
 
 // Provide iterators for DSNode...
-inline DSNode::iterator DSNode::begin() { return DSNodeIterator(this); }
-inline DSNode::iterator DSNode::end()   { return DSNodeIterator(this, false); }
+inline DSNode::iterator DSNode::begin() {
+  return DSNode::iterator(this);
+}
+inline DSNode::iterator DSNode::end() {
+  return DSNode::iterator(this, false);
+}
+inline DSNode::const_iterator DSNode::begin() const {
+  return DSNode::const_iterator(this);
+}
+inline DSNode::const_iterator DSNode::end() const {
+  return DSNode::const_iterator(this, false);
+}
 
 template <> struct GraphTraits<DSNode*> {
   typedef DSNode NodeType;
@@ -71,7 +87,17 @@ template <> struct GraphTraits<DSNode*> {
   static ChildIteratorType child_end(NodeType *N) { return N->end(); }
 };
 
-static DSNode &dereference(DSNode *N) { return *N; }
+template <> struct GraphTraits<const DSNode*> {
+  typedef const DSNode NodeType;
+  typedef DSNode::const_iterator ChildIteratorType;
+
+  static NodeType *getEntryNode(NodeType *N) { return N; }
+  static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
+  static ChildIteratorType child_end(NodeType *N) { return N->end(); }
+};
+
+static       DSNode &dereference (      DSNode *N) { return *N; }
+static const DSNode &dereferenceC(const DSNode *N) { return *N; }
 
 template <> struct GraphTraits<DSGraph*> {
   typedef DSNode NodeType;
@@ -82,11 +108,35 @@ template <> struct GraphTraits<DSGraph*> {
   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
   typedef mapped_iterator<std::vector<DSNode*>::iterator,
                           DerefFun> nodes_iterator;
-  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)); }
+  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));
+  }
 
   static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
   static ChildIteratorType child_end(NodeType *N) { return N->end(); }
 };
 
+template <> struct GraphTraits<const DSGraph*> {
+  typedef const DSNode NodeType;
+  typedef DSNode::const_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(); }
+};
+
 #endif