Chemical Data Processing Library C++ API - Version 1.4.0
TopologicalEntityAlignment.hpp
Go to the documentation of this file.
1 /*
2  * TopologicalEntityAlignment.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_CHEM_TOPOLOGICALENTITYALIGNMENT_HPP
30 #define CDPL_CHEM_TOPOLOGICALENTITYALIGNMENT_HPP
31 
32 #include <cstddef>
33 #include <vector>
34 #include <functional>
35 
36 #include <boost/iterator/indirect_iterator.hpp>
37 
39 #include "CDPL/Util/Array.hpp"
40 #include "CDPL/Base/Exceptions.hpp"
41 
42 
43 namespace CDPL
44 {
45 
46  namespace Chem
47  {
48 
63  template <typename T>
65  {
66 
67  public:
71  typedef T EntityType;
72 
76  typedef std::vector<const EntityType*> EntitySet;
77 
81  typedef boost::indirect_iterator<typename EntitySet::const_iterator, const EntityType> ConstEntityIterator;
82 
86  typedef std::function<bool(const EntityType&, const EntityType&)> EntityMatchFunction;
87 
91  typedef std::function<bool(const EntityType&, const EntityType&, const EntityType&, const EntityType&)> EntityPairMatchFunction;
92 
97  changes(true) {}
98 
103 
109 
115 
122 
128 
135  void addEntity(const EntityType& entity, bool first_set);
136 
141  void clearEntities(bool first_set);
142 
147  std::size_t getNumEntities(bool first_set) const;
148 
154  ConstEntityIterator getEntitiesBegin(bool first_set) const;
155 
161  ConstEntityIterator getEntitiesEnd(bool first_set) const;
162 
170  const EntityType& getEntity(std::size_t idx, bool first_set) const;
171 
179 
184  void reset();
185 
186  private:
187  void init();
188 
189  typedef std::vector<Util::STPair> CompatGraphNodeArray;
190 
191  EntityMatchFunction entityMatchFunc;
192  EntityPairMatchFunction entityPairMatchFunc;
193  Util::BronKerboschAlgorithm bkAlgorithm;
194  CompatGraphNodeArray compatGraphNodes;
195  Util::BitSetArray adjMatrix;
196  Util::BitSet clique;
197  EntitySet firstEntities;
198  EntitySet secondEntities;
199  bool changes;
200  };
201  } // namespace Chem
202 } // namespace CDPL
203 
204 
205 // Implementation
206 
207 template <typename T>
209 {
210  entityMatchFunc = func;
211  changes = true;
212 }
213 
214 template <typename T>
217 {
218  return entityMatchFunc;
219 }
220 
221 template <typename T>
223 {
224  entityPairMatchFunc = func;
225  changes = true;
226 }
227 
228 template <typename T>
231 {
232  return entityPairMatchFunc;
233 }
234 
235 template <typename T>
237 {
238  return (first_set ? firstEntities : secondEntities).size();
239 }
240 
241 template <typename T>
243 {
244  (first_set ? firstEntities : secondEntities).push_back(&entity);
245  changes = true;
246 }
247 
248 template <typename T>
250 {
251  (first_set ? firstEntities : secondEntities).clear();
252  changes = true;
253 }
254 
255 template <typename T>
258 {
259  return (first_set ? firstEntities : secondEntities).begin();
260 }
261 
262 template <typename T>
265 {
266  return (first_set ? firstEntities : secondEntities).end();
267 }
268 
269 template <typename T>
271 CDPL::Chem::TopologicalEntityAlignment<T>::getEntity(std::size_t idx, bool first_set) const
272 {
273  const EntitySet& entity_set = (first_set ? firstEntities : secondEntities);
274 
275  if (idx >= entity_set.size())
276  throw Base::IndexError("TopologicalEntityAlignment: entity index out of bounds");
277 
278  return *entity_set[idx];
279 }
280 
281 template <typename T>
283 {
284  compatGraphNodes.clear();
285 
286  if (entityMatchFunc) {
287  std::size_t i = 0;
288 
289  for (typename EntitySet::const_iterator it1 = firstEntities.begin(), end1 = firstEntities.end(); it1 != end1; ++it1, i++) {
290  const EntityType* ent1 = *it1;
291  std::size_t j = 0;
292 
293  for (typename EntitySet::const_iterator it2 = secondEntities.begin(), end2 = secondEntities.end(); it2 != end2; ++it2, j++)
294  if (entityMatchFunc(*ent1, **it2))
295  compatGraphNodes.push_back(Util::STPair(i, j));
296  }
297 
298  } else {
299  for (std::size_t i = 0, num_ents1 = firstEntities.size(); i < num_ents1; i++)
300  for (std::size_t j = 0, num_ents2 = secondEntities.size(); j < num_ents2; j++)
301  compatGraphNodes.push_back(Util::STPair(i, j));
302  }
303 
304  std::size_t num_nodes = compatGraphNodes.size();
305 
306  adjMatrix.resize(num_nodes);
307 
308  for (std::size_t i = 0; i < num_nodes; i++) {
309  adjMatrix[i].resize(num_nodes);
310  adjMatrix[i].reset();
311  }
312 
313  for (std::size_t i = 0; i < num_nodes; i++) {
314  const Util::STPair& p1 = compatGraphNodes[i];
315 
316  for (std::size_t j = i + 1; j < num_nodes; j++) {
317  const Util::STPair& p2 = compatGraphNodes[j];
318 
319  if (p1.first == p2.first)
320  continue;
321 
322  if (p1.second == p2.second)
323  continue;
324 
325  if (!entityPairMatchFunc || entityPairMatchFunc(*firstEntities[p1.first], *firstEntities[p2.first],
326  *secondEntities[p1.second], *secondEntities[p2.second])) {
327  adjMatrix[i].set(j);
328  adjMatrix[j].set(i);
329  }
330  }
331  }
332 
333  bkAlgorithm.init(adjMatrix);
334 
335  changes = false;
336 }
337 
338 template <typename T>
340 {
341  if (changes)
342  init();
343 
344  if (!bkAlgorithm.nextClique(clique))
345  return false;
346 
347  mapping.clear();
348 
349  for (std::size_t i = clique.find_first(); i != Util::BitSet::npos; i = clique.find_next(i))
350  mapping.addElement(compatGraphNodes[i]);
351 
352  return true;
353 }
354 
355 template <typename T>
357 {
358  changes = true;
359 }
360 
361 #endif // CDPL_CHEM_TOPOLOGICALENTITYALIGNMENT_HPP
Definition of class CDPL::Util::Array.
Definition of exception classes.
Implementation of the Bron-Kerbosch algorithm.
Thrown to indicate that an index is out of range.
Definition: Base/Exceptions.hpp:152
Computes a topological alignment between two sets of arbitrary entities by performing a maximum commo...
Definition: TopologicalEntityAlignment.hpp:65
bool nextAlignment(Util::STPairArray &mapping)
Searches for the next alignment solution and stores the corresponding mapping of the entities in the ...
Definition: TopologicalEntityAlignment.hpp:339
std::function< bool(const EntityType &, const EntityType &, const EntityType &, const EntityType &)> EntityPairMatchFunction
Generic wrapper class used to store a user-defined entity pair match constraint function.
Definition: TopologicalEntityAlignment.hpp:91
const EntityPairMatchFunction & getEntityPairMatchFunction() const
Returns the function that was registered for checking the compatibility of entity pairs.
Definition: TopologicalEntityAlignment.hpp:230
ConstEntityIterator getEntitiesBegin(bool first_set) const
Returns a constant iterator pointing to the beginning of the entities stored in the specified set.
Definition: TopologicalEntityAlignment.hpp:257
TopologicalEntityAlignment()
Constructs the TopologicalEntityAlignment instance.
Definition: TopologicalEntityAlignment.hpp:96
void addEntity(const EntityType &entity, bool first_set)
Adds an entity to the specified alignment entity set.
Definition: TopologicalEntityAlignment.hpp:242
ConstEntityIterator getEntitiesEnd(bool first_set) const
Returns a constant iterator pointing to the end of the entities stored in the specified set.
Definition: TopologicalEntityAlignment.hpp:264
T EntityType
The actual entity type.
Definition: TopologicalEntityAlignment.hpp:71
virtual ~TopologicalEntityAlignment()
Virtual destructor.
Definition: TopologicalEntityAlignment.hpp:102
std::vector< const EntityType * > EntitySet
The container storing the entities to align.
Definition: TopologicalEntityAlignment.hpp:76
void setEntityMatchFunction(const EntityMatchFunction &func)
Specifies a function for restricting allowed entity mappings in the search for alignment solutions.
Definition: TopologicalEntityAlignment.hpp:208
void reset()
Discards the current alignment search state so that the next call to nextAlignment() restarts the com...
Definition: TopologicalEntityAlignment.hpp:356
boost::indirect_iterator< typename EntitySet::const_iterator, const EntityType > ConstEntityIterator
A constant iterator over the stored entities.
Definition: TopologicalEntityAlignment.hpp:81
void setEntityPairMatchFunction(const EntityPairMatchFunction &func)
Specifies a function for checking the compatibility of entity pairs in the search for alignment solut...
Definition: TopologicalEntityAlignment.hpp:222
std::function< bool(const EntityType &, const EntityType &)> EntityMatchFunction
Generic wrapper class used to store a user-defined entity match constraint function.
Definition: TopologicalEntityAlignment.hpp:86
const EntityType & getEntity(std::size_t idx, bool first_set) const
Returns a non-const reference to the stored entity at index idx in the specified set.
Definition: TopologicalEntityAlignment.hpp:271
const EntityMatchFunction & getEntityMatchFunction() const
Returns the function that was registered for restricting allowed entity mappings.
Definition: TopologicalEntityAlignment.hpp:216
void clearEntities(bool first_set)
Removes all entities in the specified alignment entity set.
Definition: TopologicalEntityAlignment.hpp:249
std::size_t getNumEntities(bool first_set) const
Returns the number of entities in the specified alignment entity set.
Definition: TopologicalEntityAlignment.hpp:236
Dynamic array class providing amortized constant time access to arbitrary elements.
Definition: Array.hpp:92
void clear()
Erases all elements.
Definition: Array.hpp:743
void addElement(const ValueType &value=ValueType())
Inserts a new element at the end of the array.
Definition: Array.hpp:768
Implementation of the Bron-Kerbosch clique-detection algorithm [BKA].
Definition: BronKerboschAlgorithm.hpp:51
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
boost::dynamic_bitset BitSet
Dynamic bitset class.
Definition: BitSet.hpp:46
Array< BitSet > BitSetArray
Array storing Util::BitSet objects.
Definition: Array.hpp:608
std::pair< std::size_t, std::size_t > STPair
Pair of unsigned integers of type std::size_t.
Definition: Array.hpp:588
The namespace of the Chemical Data Processing Library.