1 | /*
|
---|
2 | * BoostGraphCreator.hpp
|
---|
3 | *
|
---|
4 | * Created on: May 17, 2017
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 |
|
---|
9 | #ifndef GRAPH_BOOSTGRAPHCREATOR_HPP_
|
---|
10 | #define GRAPH_BOOSTGRAPHCREATOR_HPP_
|
---|
11 |
|
---|
12 | // include config.h
|
---|
13 | #ifdef HAVE_CONFIG_H
|
---|
14 | #include <config.h>
|
---|
15 | #endif
|
---|
16 |
|
---|
17 | #include <map>
|
---|
18 | #include <vector>
|
---|
19 |
|
---|
20 | #include <boost/function.hpp>
|
---|
21 | #include <boost/graph/adjacency_list.hpp>
|
---|
22 |
|
---|
23 | #include "types.hpp"
|
---|
24 |
|
---|
25 | class atom;
|
---|
26 | class bond;
|
---|
27 | class molecule;
|
---|
28 |
|
---|
29 | class BreadthFirstSearchGathererTest;
|
---|
30 |
|
---|
31 | /** This is a helper class that contains functions to create a boost::graph
|
---|
32 | * from the present bond graph of molecules.
|
---|
33 | */
|
---|
34 | struct BoostGraphCreator
|
---|
35 | {
|
---|
36 | //!> grant unit test access to private parts that use BoostGraphCreator's internal graph
|
---|
37 | friend class BreadthFirstSearchGathererTest;
|
---|
38 |
|
---|
39 | public:
|
---|
40 |
|
---|
41 | //!> typedef for an undirected graph using boost::graph
|
---|
42 | typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS,
|
---|
43 | boost::property<boost::vertex_name_t, atomId_t>,
|
---|
44 | boost::property<boost::vertex_color_t, boost::default_color_type> /* needed for limited-depth DFS,
|
---|
45 | otherwise the property_map gets full size of graph */
|
---|
46 | > UndirectedGraph;
|
---|
47 |
|
---|
48 | //!> typedef for a map of graph node indices
|
---|
49 | typedef boost::property_map < UndirectedGraph, boost::vertex_index_t >::type index_map_t;
|
---|
50 | typedef boost::property_map < UndirectedGraph, boost::vertex_index_t >::const_type const_index_map_t;
|
---|
51 | //!> typedef for a map of graph node indices
|
---|
52 | typedef boost::property_map < UndirectedGraph, boost::vertex_name_t >::type name_map_t;
|
---|
53 | typedef boost::property_map < UndirectedGraph, boost::vertex_name_t >::const_type const_name_map_t;
|
---|
54 | //!> typedef for the predicate to evaluate for adding the current edge or not
|
---|
55 | typedef boost::function<bool (const bond &)> predicate_t;
|
---|
56 | //!> typedef for a Vertex
|
---|
57 | typedef boost::graph_traits<UndirectedGraph>::vertex_descriptor Vertex;
|
---|
58 | //!> typedef for vertex iterator
|
---|
59 | typedef boost::graph_traits<UndirectedGraph>::vertex_iterator vertex_iter;
|
---|
60 |
|
---|
61 | //!> typedef for a node id
|
---|
62 | typedef size_t nodeId_t;
|
---|
63 | //!> typedef for map converting between node id in graph and the associated atomic id
|
---|
64 | typedef std::map<atomId_t, nodeId_t> atomids_nodeids_t;
|
---|
65 |
|
---|
66 | /** Creates the boost::graph using all atoms and bonds in the given \a _mol.
|
---|
67 | *
|
---|
68 | * \param _mol molecule whose bond graph to construct
|
---|
69 | * \param _pred predicate to evaluate on adding each edge/bond
|
---|
70 | */
|
---|
71 | void createFromMolecule(
|
---|
72 | const molecule &_mol,
|
---|
73 | const predicate_t &_pred);
|
---|
74 |
|
---|
75 | /** Creates the boost::graph using all atoms and bonds in the given vector
|
---|
76 | * of \a _atoms
|
---|
77 | *
|
---|
78 | * \param _atoms vector of _atoms whose bond graph to construct
|
---|
79 | * \param _pred predicate to evaluate on adding each edge/bond
|
---|
80 | */
|
---|
81 | void createFromAtoms(
|
---|
82 | const std::vector<atom *> &_atoms,
|
---|
83 | const predicate_t &_pred);
|
---|
84 |
|
---|
85 | /** Getter for the created graph.
|
---|
86 | *
|
---|
87 | * \return graph
|
---|
88 | */
|
---|
89 | UndirectedGraph get() const
|
---|
90 | { return graph; }
|
---|
91 |
|
---|
92 | /** Getter for the index map of the created graph.
|
---|
93 | *
|
---|
94 | * \return indexmap
|
---|
95 | */
|
---|
96 | index_map_t getIndexMap() const {
|
---|
97 | return boost::get(boost::vertex_index, graph);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Return the number of vertices contained in the created graph.
|
---|
101 | *
|
---|
102 | * \return number of vertices
|
---|
103 | */
|
---|
104 | size_t getNumVertices() const {
|
---|
105 | return boost::num_vertices(graph);
|
---|
106 | }
|
---|
107 |
|
---|
108 | /** Return the number of edges contained in the created graph.
|
---|
109 | *
|
---|
110 | * \return number of edges
|
---|
111 | */
|
---|
112 | size_t getNumEdges() const {
|
---|
113 | return boost::num_edges(graph);
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** Returns the node id to a given atom id \a _atomid.
|
---|
117 | *
|
---|
118 | * \param _atomid atom id
|
---|
119 | * \return node id
|
---|
120 | */
|
---|
121 | nodeId_t getNodeId(const atomId_t &_atomid) const;
|
---|
122 |
|
---|
123 | /** General purpose function that contains the internal logic of walking the
|
---|
124 | * bonds of a set of atoms given by \a _begin and \a _end iterators and
|
---|
125 | * adding its edges to a graph based on the evaluation of a given predicate
|
---|
126 | * \a _pred.
|
---|
127 | *
|
---|
128 | * \note We need \a _no_nodes because molecule::iterator does not work with
|
---|
129 | * std::distance.
|
---|
130 | *
|
---|
131 | * \param _begin begin iterator
|
---|
132 | * \param _end end iterator
|
---|
133 | * \param _no_nodes number of nodes
|
---|
134 | * \param _pred predicate
|
---|
135 | */
|
---|
136 | template <typename iterator>
|
---|
137 | void createFromRange(
|
---|
138 | const iterator &_begin,
|
---|
139 | const iterator &_end,
|
---|
140 | const size_t &_no_nodes,
|
---|
141 | const predicate_t &_pred
|
---|
142 | );
|
---|
143 |
|
---|
144 | private:
|
---|
145 | //!> internal graph that is created by creator functions
|
---|
146 | UndirectedGraph graph;
|
---|
147 | //!> external property map for all the atomic ids of each graph node
|
---|
148 | atomids_nodeids_t atomids_nodeids;
|
---|
149 | };
|
---|
150 |
|
---|
151 | #include "BoostGraphCreator_impl.hpp"
|
---|
152 |
|
---|
153 |
|
---|
154 | #endif /* GRAPH_BOOSTGRAPHCREATOR_HPP_ */
|
---|