[d24ef58] | 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 |
|
---|
[bccbe9] | 17 | #include <map>
|
---|
[d24ef58] | 18 | #include <vector>
|
---|
| 19 |
|
---|
| 20 | #include <boost/function.hpp>
|
---|
| 21 | #include <boost/graph/adjacency_list.hpp>
|
---|
| 22 |
|
---|
[bccbe9] | 23 | #include "types.hpp"
|
---|
| 24 |
|
---|
[d24ef58] | 25 | class atom;
|
---|
| 26 | class bond;
|
---|
| 27 | class molecule;
|
---|
| 28 |
|
---|
[0dc8bf2] | 29 | class BoostGraphCreatorTest;
|
---|
[6e5b8d] | 30 | class BreadthFirstSearchGathererTest;
|
---|
| 31 |
|
---|
[d24ef58] | 32 | /** This is a helper class that contains functions to create a boost::graph
|
---|
| 33 | * from the present bond graph of molecules.
|
---|
| 34 | */
|
---|
| 35 | struct BoostGraphCreator
|
---|
| 36 | {
|
---|
[0dc8bf2] | 37 | //!> grant unit test access to private parts
|
---|
| 38 | friend class BoostGraphCreatorTest;
|
---|
[6e5b8d] | 39 | //!> grant unit test access to private parts that use BoostGraphCreator's internal graph
|
---|
| 40 | friend class BreadthFirstSearchGathererTest;
|
---|
| 41 |
|
---|
[d24ef58] | 42 | public:
|
---|
[e3ec8a8] | 43 |
|
---|
[d24ef58] | 44 | //!> typedef for an undirected graph using boost::graph
|
---|
| 45 | typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS,
|
---|
[e3ec8a8] | 46 | boost::property<boost::vertex_name_t, atomId_t>,
|
---|
| 47 | boost::property<boost::vertex_color_t, boost::default_color_type> /* needed for limited-depth DFS,
|
---|
| 48 | otherwise the property_map gets full size of graph */
|
---|
| 49 | > UndirectedGraph;
|
---|
| 50 |
|
---|
[bccbe9] | 51 | //!> typedef for a map of graph node indices
|
---|
| 52 | typedef boost::property_map < UndirectedGraph, boost::vertex_index_t >::type index_map_t;
|
---|
| 53 | typedef boost::property_map < UndirectedGraph, boost::vertex_index_t >::const_type const_index_map_t;
|
---|
[d24ef58] | 54 | //!> typedef for a map of graph node indices
|
---|
[bccbe9] | 55 | typedef boost::property_map < UndirectedGraph, boost::vertex_name_t >::type name_map_t;
|
---|
| 56 | typedef boost::property_map < UndirectedGraph, boost::vertex_name_t >::const_type const_name_map_t;
|
---|
[d24ef58] | 57 | //!> typedef for the predicate to evaluate for adding the current edge or not
|
---|
| 58 | typedef boost::function<bool (const bond &)> predicate_t;
|
---|
[bccbe9] | 59 | //!> typedef for a Vertex
|
---|
| 60 | typedef boost::graph_traits<UndirectedGraph>::vertex_descriptor Vertex;
|
---|
| 61 | //!> typedef for vertex iterator
|
---|
| 62 | typedef boost::graph_traits<UndirectedGraph>::vertex_iterator vertex_iter;
|
---|
[e0b960] | 63 | //!> typedef for a Edge
|
---|
| 64 | typedef boost::graph_traits<UndirectedGraph>::edge_descriptor Edge;
|
---|
| 65 | //!> typedef for edge iterator
|
---|
| 66 | typedef boost::graph_traits<UndirectedGraph>::edge_iterator edge_iter;
|
---|
[d24ef58] | 67 |
|
---|
[bccbe9] | 68 | //!> typedef for a node id
|
---|
| 69 | typedef size_t nodeId_t;
|
---|
| 70 | //!> typedef for map converting between node id in graph and the associated atomic id
|
---|
| 71 | typedef std::map<atomId_t, nodeId_t> atomids_nodeids_t;
|
---|
[d24ef58] | 72 |
|
---|
| 73 | /** Creates the boost::graph using all atoms and bonds in the given \a _mol.
|
---|
| 74 | *
|
---|
| 75 | * \param _mol molecule whose bond graph to construct
|
---|
| 76 | * \param _pred predicate to evaluate on adding each edge/bond
|
---|
| 77 | */
|
---|
| 78 | void createFromMolecule(
|
---|
| 79 | const molecule &_mol,
|
---|
| 80 | const predicate_t &_pred);
|
---|
| 81 |
|
---|
| 82 | /** Creates the boost::graph using all atoms and bonds in the given vector
|
---|
| 83 | * of \a _atoms
|
---|
| 84 | *
|
---|
| 85 | * \param _atoms vector of _atoms whose bond graph to construct
|
---|
| 86 | * \param _pred predicate to evaluate on adding each edge/bond
|
---|
| 87 | */
|
---|
| 88 | void createFromAtoms(
|
---|
| 89 | const std::vector<atom *> &_atoms,
|
---|
| 90 | const predicate_t &_pred);
|
---|
| 91 |
|
---|
| 92 | /** Getter for the created graph.
|
---|
| 93 | *
|
---|
| 94 | * \return graph
|
---|
| 95 | */
|
---|
| 96 | UndirectedGraph get() const
|
---|
| 97 | { return graph; }
|
---|
| 98 |
|
---|
| 99 | /** Getter for the index map of the created graph.
|
---|
| 100 | *
|
---|
| 101 | * \return indexmap
|
---|
| 102 | */
|
---|
| 103 | index_map_t getIndexMap() const {
|
---|
| 104 | return boost::get(boost::vertex_index, graph);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /** Return the number of vertices contained in the created graph.
|
---|
| 108 | *
|
---|
| 109 | * \return number of vertices
|
---|
| 110 | */
|
---|
| 111 | size_t getNumVertices() const {
|
---|
| 112 | return boost::num_vertices(graph);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /** Return the number of edges contained in the created graph.
|
---|
| 116 | *
|
---|
| 117 | * \return number of edges
|
---|
| 118 | */
|
---|
| 119 | size_t getNumEdges() const {
|
---|
| 120 | return boost::num_edges(graph);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[bccbe9] | 123 | /** Returns the node id to a given atom id \a _atomid.
|
---|
| 124 | *
|
---|
| 125 | * \param _atomid atom id
|
---|
| 126 | * \return node id
|
---|
| 127 | */
|
---|
| 128 | nodeId_t getNodeId(const atomId_t &_atomid) const;
|
---|
| 129 |
|
---|
[d24ef58] | 130 | /** General purpose function that contains the internal logic of walking the
|
---|
| 131 | * bonds of a set of atoms given by \a _begin and \a _end iterators and
|
---|
| 132 | * adding its edges to a graph based on the evaluation of a given predicate
|
---|
| 133 | * \a _pred.
|
---|
| 134 | *
|
---|
| 135 | * \note We need \a _no_nodes because molecule::iterator does not work with
|
---|
| 136 | * std::distance.
|
---|
| 137 | *
|
---|
| 138 | * \param _begin begin iterator
|
---|
| 139 | * \param _end end iterator
|
---|
| 140 | * \param _no_nodes number of nodes
|
---|
| 141 | * \param _pred predicate
|
---|
| 142 | */
|
---|
| 143 | template <typename iterator>
|
---|
| 144 | void createFromRange(
|
---|
| 145 | const iterator &_begin,
|
---|
| 146 | const iterator &_end,
|
---|
| 147 | const size_t &_no_nodes,
|
---|
| 148 | const predicate_t &_pred
|
---|
| 149 | );
|
---|
| 150 |
|
---|
[e0b960] | 151 | /** Finds a given edge by its two atomic indices.
|
---|
| 152 | *
|
---|
| 153 | * \param _firstid first atomic id of edge
|
---|
| 154 | * \param _secondid second atomic id of edge
|
---|
| 155 | * \return edge descriptor in graph or empty descriptor
|
---|
| 156 | */
|
---|
| 157 | Edge findEdge(const atomId_t &_firstid, const atomId_t &_secondid);
|
---|
| 158 |
|
---|
| 159 | /** Allows to remove a present edge in the graph.
|
---|
| 160 | *
|
---|
| 161 | * \param _firstid first atomic id of edge
|
---|
| 162 | * \param _secondid second atomic id of edge
|
---|
| 163 | * \return true - edge found and removed, false - else
|
---|
| 164 | */
|
---|
| 165 | bool removeEdge(const atomId_t &_firstid, const atomId_t &_secondid);
|
---|
| 166 |
|
---|
| 167 | /** Adds an edge to the graph if not already present.
|
---|
| 168 | *
|
---|
| 169 | * \param _firstid first atomic id of edge
|
---|
| 170 | * \param _secondid second atomic id of edge
|
---|
| 171 | * \return true - edge not found thus added, false - else
|
---|
| 172 | */
|
---|
| 173 | bool addEdge(const atomId_t &_firstid, const atomId_t &_secondid);
|
---|
| 174 |
|
---|
[d24ef58] | 175 | private:
|
---|
| 176 | //!> internal graph that is created by creator functions
|
---|
| 177 | UndirectedGraph graph;
|
---|
[bccbe9] | 178 | //!> external property map for all the atomic ids of each graph node
|
---|
| 179 | atomids_nodeids_t atomids_nodeids;
|
---|
[d24ef58] | 180 | };
|
---|
| 181 |
|
---|
[1356af] | 182 | #include "BoostGraphCreator_impl.hpp"
|
---|
| 183 |
|
---|
[d24ef58] | 184 |
|
---|
| 185 | #endif /* GRAPH_BOOSTGRAPHCREATOR_HPP_ */
|
---|