Chemical Data Processing Library C++ API - Version 1.4.0
BasicMolecule.hpp
Go to the documentation of this file.
1 /*
2  * BasicMolecule.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_BASICMOLECULE_HPP
30 #define CDPL_CHEM_BASICMOLECULE_HPP
31 
32 #include <vector>
33 #include <memory>
34 
35 #include <boost/iterator/indirect_iterator.hpp>
36 
37 #include "CDPL/Chem/APIPrefix.hpp"
38 #include "CDPL/Chem/Molecule.hpp"
39 #include "CDPL/Chem/BasicAtom.hpp"
40 #include "CDPL/Chem/BasicBond.hpp"
41 #include "CDPL/Util/ObjectPool.hpp"
42 
43 
44 namespace CDPL
45 {
46 
47  namespace Chem
48  {
49 
60  {
61 
64  typedef AtomCache::SharedObjectPointer AtomPtr;
65  typedef BondCache::SharedObjectPointer BondPtr;
66  typedef std::vector<AtomPtr> AtomList;
67  typedef std::vector<BondPtr> BondList;
68 
69  public:
73  typedef std::shared_ptr<BasicMolecule> SharedPointer;
74 
76  typedef boost::indirect_iterator<AtomList::iterator, BasicAtom> AtomIterator;
78  typedef boost::indirect_iterator<AtomList::const_iterator, const BasicAtom> ConstAtomIterator;
80  typedef boost::indirect_iterator<BondList::iterator, BasicBond> BondIterator;
82  typedef boost::indirect_iterator<BondList::const_iterator, const BasicBond> ConstBondIterator;
83 
88 
94 
99  BasicMolecule(const Molecule& mol);
100 
106  explicit BasicMolecule(const MolecularGraph& molgraph);
107 
114 
118  void clear();
119 
124  std::size_t getNumAtoms() const;
125 
130  std::size_t getNumBonds() const;
131 
137 
143 
149 
155 
161 
167 
173 
179 
186  const BasicAtom& getAtom(std::size_t idx) const;
187 
194  BasicAtom& getAtom(std::size_t idx);
195 
201 
207  void removeAtom(std::size_t idx);
208 
221 
228  const BasicBond& getBond(std::size_t idx) const;
229 
236  BasicBond& getBond(std::size_t idx);
237 
245  BasicBond& addBond(std::size_t atom1_idx, std::size_t atom2_idx);
246 
252  void removeBond(std::size_t idx);
253 
262 
268  bool containsAtom(const Atom& atom) const;
269 
275  bool containsBond(const Bond& bond) const;
276 
283  std::size_t getAtomIndex(const Atom& atom) const;
284 
291  std::size_t getBondIndex(const Bond& bond) const;
292 
297  void orderAtoms(const AtomCompareFunction& func);
298 
303  void orderBonds(const BondCompareFunction& func);
304 
315 
316  using Molecule::operator=;
317 
328 
329  using Molecule::operator+=;
330 
332 
338  void copy(const BasicMolecule& mol);
339 
344  void copy(const Molecule& mol);
345 
350  void copy(const MolecularGraph& molgraph);
351 
358  void append(const BasicMolecule& mol);
359 
365  void append(const Molecule& mol);
366 
372  void append(const MolecularGraph& molgraph);
373 
378  void remove(const MolecularGraph& molgraph);
379 
384  void reserveMemoryForAtoms(std::size_t num_atoms);
385 
390  void reserveMemoryForBonds(std::size_t num_bonds);
391 
392  private:
393  template <typename T>
394  void doCopy(const T& mol);
395 
396  template <typename T>
397  void doAppend(const T& mol);
398 
399  void clearAtomsAndBonds();
400 
401  void renumberAtoms(std::size_t idx);
402  void renumberBonds(std::size_t idx);
403 
404  BasicAtom* createAtom();
405  BasicBond* createBond();
406 
407  static void destroyAtom(BasicAtom* atom);
408  static void destroyBond(BasicBond* bond);
409 
410  static void clearAtom(BasicAtom& atom);
411  static void clearBond(BasicBond& bond);
412 
413  AtomCache atomCache;
414  BondCache bondCache;
415  AtomList atoms;
416  BondList bonds;
417  };
418  } // namespace Chem
419 } // namespace CDPL
420 
421 #endif // CDPL_CHEM_BASICMOLECULE_HPP
Definition of class CDPL::Chem::BasicAtom.
Definition of class CDPL::Chem::BasicBond.
Definition of the preprocessor macro CDPL_CHEM_API.
#define CDPL_CHEM_API
Tells the compiler/linker which classes, functions and variables are part of the library API.
Definition of class CDPL::Chem::Molecule.
Definition of class CDPL::Util::ObjectPool.
Abstract base class representing a chemical atom and its bonded neighborhood.
Definition: Atom.hpp:57
Concrete Chem::Atom implementation used as the atom type of Chem::BasicMolecule.
Definition: BasicAtom.hpp:58
Default implementation of the Chem::Bond interface.
Definition: BasicBond.hpp:52
Concrete Chem::Molecule implementation that owns Chem::BasicAtom and Chem::BasicBond instances.
Definition: BasicMolecule.hpp:60
void removeAtom(std::size_t idx)
Removes the atom at index idx (along with any incident bonds).
void orderAtoms(const AtomCompareFunction &func)
Reorders the atom list using the binary comparator func.
BasicBond & addBond(std::size_t atom1_idx, std::size_t atom2_idx)
Creates a new bond between the atoms at indices atom1_idx and atom2_idx and returns a reference to it...
std::size_t getBondIndex(const Bond &bond) const
Returns the index of bond bond in this molecule.
const BasicBond & getBond(std::size_t idx) const
Returns a const reference to the bond at index idx.
std::shared_ptr< BasicMolecule > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated BasicMolecule instances.
Definition: BasicMolecule.hpp:73
boost::indirect_iterator< AtomList::const_iterator, const BasicAtom > ConstAtomIterator
Constant random-access iterator over the atoms of the molecule.
Definition: BasicMolecule.hpp:78
void append(const MolecularGraph &molgraph)
Extends the current set of atoms and bonds by a copy of those in molgraph.
boost::indirect_iterator< BondList::iterator, BasicBond > BondIterator
Mutable random-access iterator over the bonds of the molecule.
Definition: BasicMolecule.hpp:80
bool containsBond(const Bond &bond) const
Tells whether bond is part of this molecule.
BondIterator removeBond(const BondIterator &it)
Removes the bond specified by the iterator it.
void clear()
Removes all atoms, bonds, and properties of the molecule.
void append(const Molecule &mol)
Extends the current set of atoms and bonds by a copy of those in mol.
BasicAtom & addAtom()
Creates and appends a new atom and returns a reference to it.
void removeBond(std::size_t idx)
Removes the bond at index idx.
ConstAtomIterator getAtomsEnd() const
Returns a constant iterator pointing to the end of the atoms.
const BasicAtom & getAtom(std::size_t idx) const
Returns a const reference to the atom at index idx.
void copy(const Molecule &mol)
Replaces the current set of atoms, bonds and properties by a copy of mol.
void orderBonds(const BondCompareFunction &func)
Reorders the bond list using the binary comparator func.
BasicMolecule(const Molecule &mol)
Constructs a copy of the Chem::Molecule instance mol.
ConstBondIterator getBondsEnd() const
Returns a constant iterator pointing to the end of the bonds.
std::size_t getNumAtoms() const
Returns the number of atoms in the molecule.
void copy(const MolecularGraph &molgraph)
Replaces the current set of atoms, bonds and properties by a copy of molgraph.
std::size_t getAtomIndex(const Atom &atom) const
Returns the index of atom atom in this molecule.
AtomIterator removeAtom(const AtomIterator &it)
Removes the atom specified by the iterator it.
BasicMolecule & operator=(const BasicMolecule &mol)
Replaces the current set of atoms, bonds and properties by a copy of the atoms, bonds and properties ...
std::size_t getNumBonds() const
Returns the number of bonds in the molecule.
void copy(const BasicMolecule &mol)
Replaces the current set of atoms, bonds and properties by a copy of the atoms, bonds and properties ...
BasicBond & getBond(std::size_t idx)
Returns a mutable reference to the bond at index idx.
boost::indirect_iterator< BondList::const_iterator, const BasicBond > ConstBondIterator
Constant random-access iterator over the bonds of the molecule.
Definition: BasicMolecule.hpp:82
void reserveMemoryForAtoms(std::size_t num_atoms)
Reserves storage for at least num_atoms atoms to avoid reallocations on subsequent additions.
BasicAtom & getAtom(std::size_t idx)
Returns a mutable reference to the atom at index idx.
boost::indirect_iterator< AtomList::iterator, BasicAtom > AtomIterator
Mutable random-access iterator over the atoms of the molecule.
Definition: BasicMolecule.hpp:76
BondIterator getBondsEnd()
Returns a mutable iterator pointing to the end of the bonds.
MolecularGraph::SharedPointer clone() const
Creates a copy of the molecular graph.
BasicMolecule()
Constructs an empty BasicMolecule instance.
BasicMolecule & operator+=(const BasicMolecule &mol)
Extends the current set of atoms and bonds by a copy of the atoms and bonds in the molecule mol.
ConstAtomIterator getAtomsBegin() const
Returns a constant iterator pointing to the beginning of the atoms.
BasicMolecule(const MolecularGraph &molgraph)
Constructs a BasicMolecule instance with copies of the atoms and bonds of the Chem::MolecularGraph in...
ConstBondIterator getBondsBegin() const
Returns a constant iterator pointing to the beginning of the bonds.
bool containsAtom(const Atom &atom) const
Tells whether atom is part of this molecule.
void reserveMemoryForBonds(std::size_t num_bonds)
Reserves storage for at least num_bonds bonds to avoid reallocations on subsequent additions.
AtomIterator getAtomsBegin()
Returns a mutable iterator pointing to the beginning of the atoms.
AtomIterator getAtomsEnd()
Returns a mutable iterator pointing to the end of the atoms.
BondIterator getBondsBegin()
Returns a mutable iterator pointing to the beginning of the bonds.
void remove(const MolecularGraph &molgraph)
Removes the atoms and bonds of molgraph from this molecule.
void append(const BasicMolecule &mol)
Extends the current set of atoms and bonds by a copy of the atoms and bonds in the molecule mol.
BasicMolecule(const BasicMolecule &mol)
Constructs a copy of the BasicMolecule instance mol.
Abstract base class representing a chemical bond between two Chem::Atom instances.
Definition: Bond.hpp:54
Abstract base class for representations of a chemical structure as a graph of bonded atoms.
Definition: MolecularGraph.hpp:57
std::shared_ptr< MolecularGraph > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated MolecularGraph instances.
Definition: MolecularGraph.hpp:63
Abstract base class representing a mutable molecular graph that owns its atoms and bonds.
Definition: Molecule.hpp:53
STL compatible random access iterator for container elements accessible by index.
Definition: IndexedElementIterator.hpp:125
std::shared_ptr< ObjectType > SharedObjectPointer
A smart pointer to a borrowed object that returns the object to the pool on destruction.
Definition: ObjectPool.hpp:71
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
std::function< bool(const Atom &, const Atom &)> AtomCompareFunction
Generic wrapper class used to store a user-defined atom compare function.
Definition: AtomCompareFunction.hpp:41
std::function< bool(const Chem::Bond &, const Chem::Bond &)> BondCompareFunction
Generic wrapper class used to store a user-defined bond compare function.
Definition: BondCompareFunction.hpp:41
The namespace of the Chemical Data Processing Library.