| 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 BoostGraphCreatorTest;
|
|---|
| 30 | class BreadthFirstSearchGathererTest;
|
|---|
| 31 |
|
|---|
| 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 | {
|
|---|
| 37 | //!> grant unit test access to private parts
|
|---|
| 38 | friend class BoostGraphCreatorTest;
|
|---|
| 39 | //!> grant unit test access to private parts that use BoostGraphCreator's internal graph
|
|---|
| 40 | friend class BreadthFirstSearchGathererTest;
|
|---|
| 41 |
|
|---|
| 42 | public:
|
|---|
| 43 |
|
|---|
| 44 | //!> typedef for an undirected graph using boost::graph
|
|---|
| 45 | typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS,
|
|---|
| 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 |
|
|---|
| 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;
|
|---|
| 54 | //!> typedef for a map of graph node indices
|
|---|
| 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;
|
|---|
| 57 | //!> typedef for the predicate to evaluate for adding the current edge or not
|
|---|
| 58 | typedef boost::function<bool (const bond &)> predicate_t;
|
|---|
| 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;
|
|---|
| 63 |
|
|---|
| 64 | //!> typedef for a node id
|
|---|
| 65 | typedef size_t nodeId_t;
|
|---|
| 66 | //!> typedef for map converting between node id in graph and the associated atomic id
|
|---|
| 67 | typedef std::map<atomId_t, nodeId_t> atomids_nodeids_t;
|
|---|
| 68 |
|
|---|
| 69 | /** Creates the boost::graph using all atoms and bonds in the given \a _mol.
|
|---|
| 70 | *
|
|---|
| 71 | * \param _mol molecule whose bond graph to construct
|
|---|
| 72 | * \param _pred predicate to evaluate on adding each edge/bond
|
|---|
| 73 | */
|
|---|
| 74 | void createFromMolecule(
|
|---|
| 75 | const molecule &_mol,
|
|---|
| 76 | const predicate_t &_pred);
|
|---|
| 77 |
|
|---|
| 78 | /** Creates the boost::graph using all atoms and bonds in the given vector
|
|---|
| 79 | * of \a _atoms
|
|---|
| 80 | *
|
|---|
| 81 | * \param _atoms vector of _atoms whose bond graph to construct
|
|---|
| 82 | * \param _pred predicate to evaluate on adding each edge/bond
|
|---|
| 83 | */
|
|---|
| 84 | void createFromAtoms(
|
|---|
| 85 | const std::vector<atom *> &_atoms,
|
|---|
| 86 | const predicate_t &_pred);
|
|---|
| 87 |
|
|---|
| 88 | /** Getter for the created graph.
|
|---|
| 89 | *
|
|---|
| 90 | * \return graph
|
|---|
| 91 | */
|
|---|
| 92 | UndirectedGraph get() const
|
|---|
| 93 | { return graph; }
|
|---|
| 94 |
|
|---|
| 95 | /** Getter for the index map of the created graph.
|
|---|
| 96 | *
|
|---|
| 97 | * \return indexmap
|
|---|
| 98 | */
|
|---|
| 99 | index_map_t getIndexMap() const {
|
|---|
| 100 | return boost::get(boost::vertex_index, graph);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /** Return the number of vertices contained in the created graph.
|
|---|
| 104 | *
|
|---|
| 105 | * \return number of vertices
|
|---|
| 106 | */
|
|---|
| 107 | size_t getNumVertices() const {
|
|---|
| 108 | return boost::num_vertices(graph);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | /** Return the number of edges contained in the created graph.
|
|---|
| 112 | *
|
|---|
| 113 | * \return number of edges
|
|---|
| 114 | */
|
|---|
| 115 | size_t getNumEdges() const {
|
|---|
| 116 | return boost::num_edges(graph);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /** Returns the node id to a given atom id \a _atomid.
|
|---|
| 120 | *
|
|---|
| 121 | * \param _atomid atom id
|
|---|
| 122 | * \return node id
|
|---|
| 123 | */
|
|---|
| 124 | nodeId_t getNodeId(const atomId_t &_atomid) const;
|
|---|
| 125 |
|
|---|
| 126 | /** General purpose function that contains the internal logic of walking the
|
|---|
| 127 | * bonds of a set of atoms given by \a _begin and \a _end iterators and
|
|---|
| 128 | * adding its edges to a graph based on the evaluation of a given predicate
|
|---|
| 129 | * \a _pred.
|
|---|
| 130 | *
|
|---|
| 131 | * \note We need \a _no_nodes because molecule::iterator does not work with
|
|---|
| 132 | * std::distance.
|
|---|
| 133 | *
|
|---|
| 134 | * \param _begin begin iterator
|
|---|
| 135 | * \param _end end iterator
|
|---|
| 136 | * \param _no_nodes number of nodes
|
|---|
| 137 | * \param _pred predicate
|
|---|
| 138 | */
|
|---|
| 139 | template <typename iterator>
|
|---|
| 140 | void createFromRange(
|
|---|
| 141 | const iterator &_begin,
|
|---|
| 142 | const iterator &_end,
|
|---|
| 143 | const size_t &_no_nodes,
|
|---|
| 144 | const predicate_t &_pred
|
|---|
| 145 | );
|
|---|
| 146 |
|
|---|
| 147 | private:
|
|---|
| 148 | //!> internal graph that is created by creator functions
|
|---|
| 149 | UndirectedGraph graph;
|
|---|
| 150 | //!> external property map for all the atomic ids of each graph node
|
|---|
| 151 | atomids_nodeids_t atomids_nodeids;
|
|---|
| 152 | };
|
|---|
| 153 |
|
|---|
| 154 | #include "BoostGraphCreator_impl.hpp"
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 | #endif /* GRAPH_BOOSTGRAPHCREATOR_HPP_ */
|
|---|