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