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