IMP logo
IMP Reference Guide  develop.27926d84dc,2024/04/18
The Integrative Modeling Platform
graph_macros.h
Go to the documentation of this file.
1 /**
2  * \file IMP/graph_macros.h
3  * \brief Helper macros for handling graphs.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPKERNEL_GRAPH_MACROS_H
10 #define IMPKERNEL_GRAPH_MACROS_H
11 #include <IMP/kernel_config.h>
12 #include <boost/graph/adjacency_list.hpp>
13 #include "file.h"
14 #include "internal/base_graph_utility.h"
15 #include <boost/unordered_map.hpp>
16 #include <boost/version.hpp>
17 
18 #if defined(IMP_DOXYGEN)
19 //! Define a graph object in \imp
20 /** The docs for the graph should appear before the macro
21  invocation. Directionality should be one of
22  - \c bidirectional
23  - \c directed
24  - \c undirected
25 
26  ShowVertex should take the VertexData as a variable named `vertex` and write
27  to a stream `out`.
28  */
29 #define IMP_GRAPH(Name, directionality, VertexData, EdgeData, ShowVertex) \
30  /** See \ref graphs "Graphs in IMP" for more information.*/ \
31  typedef boost::graph Name; \
32  typedef Name::VertexNameMap Name##ConstVertexName; \
33  typedef Name::EdgeNameMap Name##ConstEdgeName; \
34  typedef boost::graph_traits<Name> Name##Traits; \
35  typedef Name::vertex_descriptor Name##Vertex; \
36  typedef Name::edge_descriptor Name##Edge; \
37  class Name##VertexIndex {}; \
38  inline void show_as_graphviz(const Name& name, TextOutput out); \
39  Name##VertexIndex get_vertex_index(const Name& g)
40 
41 #elif defined(SWIG)
42 #define IMP_GRAPH(Name, directionality, VertexData, EdgeData, ShowVertex) \
43  class Name; \
44  class Name##VertexIndex {}; \
45  inline void show_as_graphviz(const Name& name, TextOutput out); \
46  inline Name##VertexIndex get_vertex_index(const Name& g)
47 
48 #else // swig and doxygen
49 
50 #define IMP_GRAPH_MAP_TYPE boost::unordered_map
51 
52 #define IMP_GRAPH(Name, directionality, VertexData, EdgeData, ShowVertex) \
53  typedef boost::adjacency_list< \
54  boost::vecS, boost::vecS, boost::directionality##S, \
55  boost::property<boost::vertex_name_t, VertexData>, \
56  boost::property<boost::edge_name_t, EdgeData> > Name; \
57  typedef boost::property_map<Name, boost::vertex_name_t>::const_type \
58  Name##ConstVertexName; \
59  typedef boost::property_map<Name, boost::edge_name_t>::const_type \
60  Name##ConstEdgeName; \
61  typedef boost::graph_traits<Name> Name##Traits; \
62  typedef Name##Traits::vertex_descriptor Name##Vertex; \
63  typedef Name##Traits::edge_descriptor Name##Edge; \
64  typedef IMP_GRAPH_MAP_TYPE<VertexData, Name##Vertex> Name##VertexIndex; \
65  inline Name##VertexIndex get_vertex_index(const Name& g) { \
66  return IMP::internal::get_graph_vertex_index< \
67  Name, VertexData, Name##Vertex, Name##Traits>(g); \
68  } \
69  struct Show##Name##Vertex { \
70  void operator()(VertexData vertex, TextOutput out) const { \
71  ShowVertex; \
72  } \
73  }; \
74  inline void show_as_graphviz(const Name& graph, TextOutput out) { \
75  IMP::internal::show_as_graphviz(graph, Show##Name##Vertex(), out); \
76  } \
77  typedef boost::property_map<Name, boost::edge_name_t>::type Name##EdgeName; \
78  typedef boost::property_map<Name, boost::vertex_name_t>::type Name##VertexName
79 #endif // swig and doxygen
80 
81 #if defined(IMP_DOXYGEN) || defined(SWIG)
82 //! Define a graph object in \imp
83 /** See IMP_GRAPH() for more info. Edges have a floating point weight.
84  */
85 #define IMP_WEIGHTED_GRAPH(Name, directionality, VertexData, ShowVertex) \
86  /** See \ref graphs "Graphs" for more information.*/ \
87  IMP_GRAPH(Name, directionality, VertexData, double, ShowVertex)
88 
89 #elif defined(SWIG)
90 #define IMP_WEIGHTED_GRAPH(Name, directionality, VertexData, ShowVertex) \
91  class Name
92 #else
93 #define IMP_WEIGHTED_GRAPH(Name, directionality, VertexData, ShowVertex) \
94  typedef boost::adjacency_list< \
95  boost::vecS, boost::vecS, boost::directionality##S, \
96  boost::property<boost::vertex_name_t, VertexData>, \
97  boost::property<boost::edge_weight_t, double> > Name; \
98  typedef boost::property_map<Name, boost::vertex_name_t>::const_type \
99  Name##ConstVertexName; \
100  typedef boost::property_map<Name, boost::edge_weight_t>::const_type \
101  Name##ConstEdgeWeight; \
102  typedef boost::graph_traits<Name> Name##Traits; \
103  typedef Name##Traits::vertex_descriptor Name##Vertex; \
104  typedef Name##Traits::edge_descriptor Name##Edge; \
105  typedef boost::unordered_map<VertexData, Name##Vertex> Name##VertexIndex; \
106  inline Name##VertexIndex get_vertex_index(const Name& g) { \
107  return IMP::internal::get_graph_vertex_index< \
108  Name, VertexData, Name##Vertex, Name##Traits>(g); \
109  } \
110  struct Show##Name##Vertex { \
111  void operator()(VertexData vertex, TextOutput out) const { \
112  ShowVertex; \
113  } \
114  }; \
115  inline void show_as_graphviz(const Name& graph, TextOutput out) { \
116  IMP::internal::show_as_graphviz(graph, Show##Name##Vertex(), out); \
117  } \
118  typedef boost::property_map<Name, boost::edge_weight_t>::type \
119  Name##EdgeWeight; \
120  typedef boost::property_map<Name, boost::vertex_name_t>::type Name##VertexName
121 #endif
122 
123 #endif /* IMPKERNEL_GRAPH_MACROS_H */
Handling of file input/output.