Chris Lattner | 555eaf5 | 2003-09-30 18:37:50 +0000 | [diff] [blame] | 1 | //===-- Support/GraphTraits.h - Graph traits template -----------*- C++ -*-===// |
John Criswell | dd04329 | 2003-10-20 19:46:57 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines the little GraphTraits<X> template class that should be |
| 11 | // specialized by classes that want to be iteratable by generic graph iterators. |
| 12 | // |
| 13 | // This file also defines the marker class Inverse that is used to iterate over |
| 14 | // graphs in a graph defined, inverse ordering... |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Brian Gaeke | a7a5013 | 2003-06-17 00:35:55 +0000 | [diff] [blame] | 18 | #ifndef SUPPORT_GRAPHTRAITS_H |
| 19 | #define SUPPORT_GRAPHTRAITS_H |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 20 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 21 | namespace llvm { |
| 22 | |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 23 | // GraphTraits - This class should be specialized by different graph types... |
| 24 | // which is why the default version is empty. |
| 25 | // |
| 26 | template<class GraphType> |
| 27 | struct GraphTraits { |
| 28 | // Elements to provide: |
| 29 | |
| 30 | // typedef NodeType - Type of Node in the graph |
| 31 | // typedef ChildIteratorType - Type used to iterate over children in graph |
| 32 | |
| 33 | // static NodeType *getEntryNode(GraphType *) |
| 34 | // Return the entry node of the graph |
| 35 | |
| 36 | // static ChildIteratorType child_begin(NodeType *) |
| 37 | // static ChildIteratorType child_end (NodeType *) |
| 38 | // Return iterators that point to the beginning and ending of the child |
| 39 | // node list for the specified node. |
| 40 | // |
| 41 | |
| 42 | |
Chris Lattner | 0e0e32d | 2002-10-13 17:12:05 +0000 | [diff] [blame] | 43 | // typedef ...iterator nodes_iterator; |
| 44 | // static nodes_iterator nodes_begin(GraphType *G) |
| 45 | // static nodes_iterator nodes_end (GraphType *G) |
| 46 | // |
| 47 | // nodes_iterator/begin/end - Allow iteration over all nodes in the graph |
| 48 | |
| 49 | |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 50 | // If anyone tries to use this class without having an appropriate |
Chris Lattner | 274aeb7 | 2001-10-01 13:34:22 +0000 | [diff] [blame] | 51 | // specialization, make an error. If you get this error, it's because you |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 52 | // need to include the appropriate specialization of GraphTraits<> for your |
Chris Lattner | 274aeb7 | 2001-10-01 13:34:22 +0000 | [diff] [blame] | 53 | // graph, or you need to define it for a new graph type. Either that or |
| 54 | // your argument to XXX_begin(...) is unknown or needs to have the proper .h |
| 55 | // file #include'd. |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 56 | // |
| 57 | typedef typename GraphType::UnknownGraphTypeError NodeType; |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | // Inverse - This class is used as a little marker class to tell the graph |
| 62 | // iterator to iterate over the graph in a graph defined "Inverse" ordering. |
| 63 | // Not all graphs define an inverse ordering, and if they do, it depends on |
| 64 | // the graph exactly what that is. Here's an example of usage with the |
| 65 | // df_iterator: |
| 66 | // |
Chris Lattner | 274aeb7 | 2001-10-01 13:34:22 +0000 | [diff] [blame] | 67 | // idf_iterator<Method*> I = idf_begin(M), E = idf_end(M); |
| 68 | // for (; I != E; ++I) { ... } |
| 69 | // |
| 70 | // Which is equivalent to: |
| 71 | // df_iterator<Inverse<Method*> > I = idf_begin(M), E = idf_end(M); |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 72 | // for (; I != E; ++I) { ... } |
| 73 | // |
| 74 | template <class GraphType> |
| 75 | struct Inverse { |
| 76 | GraphType &Graph; |
| 77 | |
| 78 | inline Inverse(GraphType &G) : Graph(G) {} |
| 79 | }; |
| 80 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 81 | } // End llvm namespace |
| 82 | |
Chris Lattner | 409bbec | 2001-09-28 22:59:14 +0000 | [diff] [blame] | 83 | #endif |