xref: /aosp_15_r20/external/llvm/include/llvm/ADT/GraphTraits.h (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- llvm/ADT/GraphTraits.h - Graph traits template ----------*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file defines the little GraphTraits<X> template class that should be
11*9880d681SAndroid Build Coastguard Worker // specialized by classes that want to be iteratable by generic graph iterators.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker // This file also defines the marker class Inverse that is used to iterate over
14*9880d681SAndroid Build Coastguard Worker // graphs in a graph defined, inverse ordering...
15*9880d681SAndroid Build Coastguard Worker //
16*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
17*9880d681SAndroid Build Coastguard Worker 
18*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_ADT_GRAPHTRAITS_H
19*9880d681SAndroid Build Coastguard Worker #define LLVM_ADT_GRAPHTRAITS_H
20*9880d681SAndroid Build Coastguard Worker 
21*9880d681SAndroid Build Coastguard Worker namespace llvm {
22*9880d681SAndroid Build Coastguard Worker 
23*9880d681SAndroid Build Coastguard Worker // GraphTraits - This class should be specialized by different graph types...
24*9880d681SAndroid Build Coastguard Worker // which is why the default version is empty.
25*9880d681SAndroid Build Coastguard Worker //
26*9880d681SAndroid Build Coastguard Worker template<class GraphType>
27*9880d681SAndroid Build Coastguard Worker struct GraphTraits {
28*9880d681SAndroid Build Coastguard Worker   // Elements to provide:
29*9880d681SAndroid Build Coastguard Worker 
30*9880d681SAndroid Build Coastguard Worker   // typedef NodeType          - Type of Node in the graph
31*9880d681SAndroid Build Coastguard Worker   // typedef ChildIteratorType - Type used to iterate over children in graph
32*9880d681SAndroid Build Coastguard Worker 
33*9880d681SAndroid Build Coastguard Worker   // static NodeType *getEntryNode(const GraphType &)
34*9880d681SAndroid Build Coastguard Worker   //    Return the entry node of the graph
35*9880d681SAndroid Build Coastguard Worker 
36*9880d681SAndroid Build Coastguard Worker   // static ChildIteratorType child_begin(NodeType *)
37*9880d681SAndroid Build Coastguard Worker   // static ChildIteratorType child_end  (NodeType *)
38*9880d681SAndroid Build Coastguard Worker   //    Return iterators that point to the beginning and ending of the child
39*9880d681SAndroid Build Coastguard Worker   //    node list for the specified node.
40*9880d681SAndroid Build Coastguard Worker   //
41*9880d681SAndroid Build Coastguard Worker 
42*9880d681SAndroid Build Coastguard Worker 
43*9880d681SAndroid Build Coastguard Worker   // typedef  ...iterator nodes_iterator;
44*9880d681SAndroid Build Coastguard Worker   // static nodes_iterator nodes_begin(GraphType *G)
45*9880d681SAndroid Build Coastguard Worker   // static nodes_iterator nodes_end  (GraphType *G)
46*9880d681SAndroid Build Coastguard Worker   //    nodes_iterator/begin/end - Allow iteration over all nodes in the graph
47*9880d681SAndroid Build Coastguard Worker 
48*9880d681SAndroid Build Coastguard Worker   // static unsigned       size       (GraphType *G)
49*9880d681SAndroid Build Coastguard Worker   //    Return total number of nodes in the graph
50*9880d681SAndroid Build Coastguard Worker   //
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker 
53*9880d681SAndroid Build Coastguard Worker   // If anyone tries to use this class without having an appropriate
54*9880d681SAndroid Build Coastguard Worker   // specialization, make an error.  If you get this error, it's because you
55*9880d681SAndroid Build Coastguard Worker   // need to include the appropriate specialization of GraphTraits<> for your
56*9880d681SAndroid Build Coastguard Worker   // graph, or you need to define it for a new graph type. Either that or
57*9880d681SAndroid Build Coastguard Worker   // your argument to XXX_begin(...) is unknown or needs to have the proper .h
58*9880d681SAndroid Build Coastguard Worker   // file #include'd.
59*9880d681SAndroid Build Coastguard Worker   //
60*9880d681SAndroid Build Coastguard Worker   typedef typename GraphType::UnknownGraphTypeError NodeType;
61*9880d681SAndroid Build Coastguard Worker };
62*9880d681SAndroid Build Coastguard Worker 
63*9880d681SAndroid Build Coastguard Worker 
64*9880d681SAndroid Build Coastguard Worker // Inverse - This class is used as a little marker class to tell the graph
65*9880d681SAndroid Build Coastguard Worker // iterator to iterate over the graph in a graph defined "Inverse" ordering.
66*9880d681SAndroid Build Coastguard Worker // Not all graphs define an inverse ordering, and if they do, it depends on
67*9880d681SAndroid Build Coastguard Worker // the graph exactly what that is.  Here's an example of usage with the
68*9880d681SAndroid Build Coastguard Worker // df_iterator:
69*9880d681SAndroid Build Coastguard Worker //
70*9880d681SAndroid Build Coastguard Worker // idf_iterator<Method*> I = idf_begin(M), E = idf_end(M);
71*9880d681SAndroid Build Coastguard Worker // for (; I != E; ++I) { ... }
72*9880d681SAndroid Build Coastguard Worker //
73*9880d681SAndroid Build Coastguard Worker // Which is equivalent to:
74*9880d681SAndroid Build Coastguard Worker // df_iterator<Inverse<Method*> > I = idf_begin(M), E = idf_end(M);
75*9880d681SAndroid Build Coastguard Worker // for (; I != E; ++I) { ... }
76*9880d681SAndroid Build Coastguard Worker //
77*9880d681SAndroid Build Coastguard Worker template <class GraphType>
78*9880d681SAndroid Build Coastguard Worker struct Inverse {
79*9880d681SAndroid Build Coastguard Worker   const GraphType &Graph;
80*9880d681SAndroid Build Coastguard Worker 
InverseInverse81*9880d681SAndroid Build Coastguard Worker   inline Inverse(const GraphType &G) : Graph(G) {}
82*9880d681SAndroid Build Coastguard Worker };
83*9880d681SAndroid Build Coastguard Worker 
84*9880d681SAndroid Build Coastguard Worker // Provide a partial specialization of GraphTraits so that the inverse of an
85*9880d681SAndroid Build Coastguard Worker // inverse falls back to the original graph.
86*9880d681SAndroid Build Coastguard Worker template<class T>
87*9880d681SAndroid Build Coastguard Worker struct GraphTraits<Inverse<Inverse<T> > > {
88*9880d681SAndroid Build Coastguard Worker   typedef typename GraphTraits<T>::NodeType NodeType;
89*9880d681SAndroid Build Coastguard Worker   typedef typename GraphTraits<T>::ChildIteratorType ChildIteratorType;
90*9880d681SAndroid Build Coastguard Worker 
91*9880d681SAndroid Build Coastguard Worker   static NodeType *getEntryNode(Inverse<Inverse<T> > *G) {
92*9880d681SAndroid Build Coastguard Worker     return GraphTraits<T>::getEntryNode(G->Graph.Graph);
93*9880d681SAndroid Build Coastguard Worker   }
94*9880d681SAndroid Build Coastguard Worker 
95*9880d681SAndroid Build Coastguard Worker   static ChildIteratorType child_begin(NodeType* N) {
96*9880d681SAndroid Build Coastguard Worker     return GraphTraits<T>::child_begin(N);
97*9880d681SAndroid Build Coastguard Worker   }
98*9880d681SAndroid Build Coastguard Worker 
99*9880d681SAndroid Build Coastguard Worker   static ChildIteratorType child_end(NodeType* N) {
100*9880d681SAndroid Build Coastguard Worker     return GraphTraits<T>::child_end(N);
101*9880d681SAndroid Build Coastguard Worker   }
102*9880d681SAndroid Build Coastguard Worker };
103*9880d681SAndroid Build Coastguard Worker 
104*9880d681SAndroid Build Coastguard Worker } // End llvm namespace
105*9880d681SAndroid Build Coastguard Worker 
106*9880d681SAndroid Build Coastguard Worker #endif
107