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 
62  template <typename T>
64  {
65 
66  public:
70  typedef T EntityType;
71 
75  typedef std::vector<const EntityType*> EntitySet;
76 
80  typedef boost::indirect_iterator<typename EntitySet::const_iterator, const EntityType> ConstEntityIterator;
81 
85  typedef std::function<bool(const EntityType&, const EntityType&)> EntityMatchFunction;
86 
90  typedef std::function<bool(const EntityType&, const EntityType&, const EntityType&, const EntityType&)> EntityPairMatchFunction;
91 
96  changes(true) {}
97 
102 
108 
114 
121 
127 
134  void addEntity(const EntityType& entity, bool first_set);
135 
140  void clearEntities(bool first_set);
141 
146  std::size_t getNumEntities(bool first_set) const;
147 
153  ConstEntityIterator getEntitiesBegin(bool first_set) const;
154 
160  ConstEntityIterator getEntitiesEnd(bool first_set) const;
161 
169  const EntityType& getEntity(std::size_t idx, bool first_set) const;
170 
178 
183  void reset();
184 
185  private:
186  void init();
187 
188  typedef std::vector<Util::STPair> CompatGraphNodeArray;
189 
190  EntityMatchFunction entityMatchFunc;
191  EntityPairMatchFunction entityPairMatchFunc;
192  Util::BronKerboschAlgorithm bkAlgorithm;
193  CompatGraphNodeArray compatGraphNodes;
194  Util::BitSetArray adjMatrix;
195  Util::BitSet clique;
196  EntitySet firstEntities;
197  EntitySet secondEntities;
198  bool changes;
199  };
200  } // namespace Chem
201 } // namespace CDPL
202 
203 
204 // Implementation
205 
206 template <typename T>
208 {
209  entityMatchFunc = func;
210  changes = true;
211 }
212 
213 template <typename T>
216 {
217  return entityMatchFunc;
218 }
219 
220 template <typename T>
222 {
223  entityPairMatchFunc = func;
224  changes = true;
225 }
226 
227 template <typename T>
230 {
231  return entityPairMatchFunc;
232 }
233 
234 template <typename T>
236 {
237  return (first_set ? firstEntities : secondEntities).size();
238 }
239 
240 template <typename T>
242 {
243  (first_set ? firstEntities : secondEntities).push_back(&entity);
244  changes = true;
245 }
246 
247 template <typename T>
249 {
250  (first_set ? firstEntities : secondEntities).clear();
251  changes = true;
252 }
253 
254 template <typename T>
257 {
258  return (first_set ? firstEntities : secondEntities).begin();
259 }
260 
261 template <typename T>
264 {
265  return (first_set ? firstEntities : secondEntities).end();
266 }
267 
268 template <typename T>
270 CDPL::Chem::TopologicalEntityAlignment<T>::getEntity(std::size_t idx, bool first_set) const
271 {
272  const EntitySet& entity_set = (first_set ? firstEntities : secondEntities);
273 
274  if (idx >= entity_set.size())
275  throw Base::IndexError("TopologicalEntityAlignment: entity index out of bounds");
276 
277  return *entity_set[idx];
278 }
279 
280 template <typename T>
282 {
283  compatGraphNodes.clear();
284 
285  if (entityMatchFunc) {
286  std::size_t i = 0;
287 
288  for (typename EntitySet::const_iterator it1 = firstEntities.begin(), end1 = firstEntities.end(); it1 != end1; ++it1, i++) {
289  const EntityType* ent1 = *it1;
290  std::size_t j = 0;
291 
292  for (typename EntitySet::const_iterator it2 = secondEntities.begin(), end2 = secondEntities.end(); it2 != end2; ++it2, j++)
293  if (entityMatchFunc(*ent1, **it2))
294  compatGraphNodes.push_back(Util::STPair(i, j));
295  }
296 
297  } else {
298  for (std::size_t i = 0, num_ents1 = firstEntities.size(); i < num_ents1; i++)
299  for (std::size_t j = 0, num_ents2 = secondEntities.size(); j < num_ents2; j++)
300  compatGraphNodes.push_back(Util::STPair(i, j));
301  }
302 
303  std::size_t num_nodes = compatGraphNodes.size();
304 
305  adjMatrix.resize(num_nodes);
306 
307  for (std::size_t i = 0; i < num_nodes; i++) {
308  adjMatrix[i].resize(num_nodes);
309  adjMatrix[i].reset();
310  }
311 
312  for (std::size_t i = 0; i < num_nodes; i++) {
313  const Util::STPair& p1 = compatGraphNodes[i];
314 
315  for (std::size_t j = i + 1; j < num_nodes; j++) {
316  const Util::STPair& p2 = compatGraphNodes[j];
317 
318  if (p1.first == p2.first)
319  continue;
320 
321  if (p1.second == p2.second)
322  continue;
323 
324  if (!entityPairMatchFunc || entityPairMatchFunc(*firstEntities[p1.first], *firstEntities[p2.first],
325  *secondEntities[p1.second], *secondEntities[p2.second])) {
326  adjMatrix[i].set(j);
327  adjMatrix[j].set(i);
328  }
329  }
330  }
331 
332  bkAlgorithm.init(adjMatrix);
333 
334  changes = false;
335 }
336 
337 template <typename T>
339 {
340  if (changes)
341  init();
342 
343  if (!bkAlgorithm.nextClique(clique))
344  return false;
345 
346  mapping.clear();
347 
348  for (std::size_t i = clique.find_first(); i != Util::BitSet::npos; i = clique.find_next(i))
349  mapping.addElement(compatGraphNodes[i]);
350 
351  return true;
352 }
353 
354 template <typename T>
356 {
357  changes = true;
358 }
359 
360 #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 entities by reducing the alignment problem to a ...
Definition: TopologicalEntityAlignment.hpp:64
bool nextAlignment(Util::STPairArray &mapping)
Searches for the next alignment solution and stores the corresponding mapping of the entities in the ...
Definition: TopologicalEntityAlignment.hpp:338
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:90
const EntityPairMatchFunction & getEntityPairMatchFunction() const
Returns the function that was registered for checking the compatibility of entity-pairs.
Definition: TopologicalEntityAlignment.hpp:229
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:256
TopologicalEntityAlignment()
Constructs the TopologicalEntityAlignment instance.
Definition: TopologicalEntityAlignment.hpp:95
void addEntity(const EntityType &entity, bool first_set)
Adds an entity to the specified alignment entity set.
Definition: TopologicalEntityAlignment.hpp:241
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:263
T EntityType
The actual entity type.
Definition: TopologicalEntityAlignment.hpp:70
virtual ~TopologicalEntityAlignment()
Virtual destructor.
Definition: TopologicalEntityAlignment.hpp:101
std::vector< const EntityType * > EntitySet
The container storing the entities to align.
Definition: TopologicalEntityAlignment.hpp:75
void setEntityMatchFunction(const EntityMatchFunction &func)
Specifies a function for restricting allowed entity mappings in the search for alignment solutions.
Definition: TopologicalEntityAlignment.hpp:207
void reset()
Discards the current alignment-search state so that the next call to nextAlignment() restarts the com...
Definition: TopologicalEntityAlignment.hpp:355
boost::indirect_iterator< typename EntitySet::const_iterator, const EntityType > ConstEntityIterator
A constant iterator over the stored entities.
Definition: TopologicalEntityAlignment.hpp:80
void setEntityPairMatchFunction(const EntityPairMatchFunction &func)
Specifies a function for checking the compatibility of entity-pairs in the search for alignment solut...
Definition: TopologicalEntityAlignment.hpp:221
std::function< bool(const EntityType &, const EntityType &)> EntityMatchFunction
Generic wrapper class used to store a user-defined entity match constraint function.
Definition: TopologicalEntityAlignment.hpp:85
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:270
const EntityMatchFunction & getEntityMatchFunction() const
Returns the function that was registered for restricting allowed entity mappings.
Definition: TopologicalEntityAlignment.hpp:215
void clearEntities(bool first_set)
Removes all entities in the specified alignment entity set.
Definition: TopologicalEntityAlignment.hpp:248
std::size_t getNumEntities(bool first_set) const
Returns the number of entities in the specified alignment entity set.
Definition: TopologicalEntityAlignment.hpp:235
Dynamic array class providing amortized constant time access to arbitrary elements.
Definition: Array.hpp:92
void clear()
Erases all elements.
Definition: Array.hpp:740
void addElement(const ValueType &value=ValueType())
Inserts a new element at the end of the array.
Definition: Array.hpp:765
Implementation of the Bron-Kerbosch clique-detection algorithm [BKA].
Definition: BronKerboschAlgorithm.hpp:49
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:605
std::pair< std::size_t, std::size_t > STPair
Pair of unsigned integers of type std::size_t.
Definition: Array.hpp:585
The namespace of the Chemical Data Processing Library.