LLVM API Documentation

GraphTraits.h
Go to the documentation of this file.
00001 //===-- llvm/ADT/GraphTraits.h - Graph traits template ----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the little GraphTraits<X> template class that should be
00011 // specialized by classes that want to be iteratable by generic graph iterators.
00012 //
00013 // This file also defines the marker class Inverse that is used to iterate over
00014 // graphs in a graph defined, inverse ordering...
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #ifndef LLVM_ADT_GRAPHTRAITS_H
00019 #define LLVM_ADT_GRAPHTRAITS_H
00020 
00021 namespace llvm {
00022 
00023 // GraphTraits - This class should be specialized by different graph types...
00024 // which is why the default version is empty.
00025 //
00026 template<class GraphType>
00027 struct GraphTraits {
00028   // Elements to provide:
00029 
00030   // typedef NodeType          - Type of Node in the graph
00031   // typedef ChildIteratorType - Type used to iterate over children in graph
00032 
00033   // static NodeType *getEntryNode(const GraphType &)
00034   //    Return the entry node of the graph
00035 
00036   // static ChildIteratorType child_begin(NodeType *)
00037   // static ChildIteratorType child_end  (NodeType *)
00038   //    Return iterators that point to the beginning and ending of the child
00039   //    node list for the specified node.
00040   //
00041 
00042 
00043   // typedef  ...iterator nodes_iterator;
00044   // static nodes_iterator nodes_begin(GraphType *G)
00045   // static nodes_iterator nodes_end  (GraphType *G)
00046   //    nodes_iterator/begin/end - Allow iteration over all nodes in the graph
00047 
00048   // static unsigned       size       (GraphType *G)
00049   //    Return total number of nodes in the graph
00050   //
00051 
00052 
00053   // If anyone tries to use this class without having an appropriate
00054   // specialization, make an error.  If you get this error, it's because you
00055   // need to include the appropriate specialization of GraphTraits<> for your
00056   // graph, or you need to define it for a new graph type. Either that or
00057   // your argument to XXX_begin(...) is unknown or needs to have the proper .h
00058   // file #include'd.
00059   //
00060   typedef typename GraphType::UnknownGraphTypeError NodeType;
00061 };
00062 
00063 
00064 // Inverse - This class is used as a little marker class to tell the graph
00065 // iterator to iterate over the graph in a graph defined "Inverse" ordering.
00066 // Not all graphs define an inverse ordering, and if they do, it depends on
00067 // the graph exactly what that is.  Here's an example of usage with the
00068 // df_iterator:
00069 //
00070 // idf_iterator<Method*> I = idf_begin(M), E = idf_end(M);
00071 // for (; I != E; ++I) { ... }
00072 //
00073 // Which is equivalent to:
00074 // df_iterator<Inverse<Method*> > I = idf_begin(M), E = idf_end(M);
00075 // for (; I != E; ++I) { ... }
00076 //
00077 template <class GraphType>
00078 struct Inverse {
00079   const GraphType &Graph;
00080 
00081   inline Inverse(const GraphType &G) : Graph(G) {}
00082 };
00083 
00084 // Provide a partial specialization of GraphTraits so that the inverse of an
00085 // inverse falls back to the original graph.
00086 template<class T>
00087 struct GraphTraits<Inverse<Inverse<T> > > {
00088   typedef typename GraphTraits<T>::NodeType NodeType;
00089   typedef typename GraphTraits<T>::ChildIteratorType ChildIteratorType;
00090 
00091   static NodeType *getEntryNode(Inverse<Inverse<T> > *G) {
00092     return GraphTraits<T>::getEntryNode(G->Graph.Graph);
00093   }
00094 
00095   static ChildIteratorType child_begin(NodeType* N) {
00096     return GraphTraits<T>::child_begin(N);
00097   }
00098 
00099   static ChildIteratorType child_end(NodeType* N) {
00100     return GraphTraits<T>::child_end(N);
00101   }
00102 };
00103 
00104 } // End llvm namespace
00105 
00106 #endif