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