Chemical Data Processing Library C++ API - Version 1.4.0
Fragment.hpp
Go to the documentation of this file.
1 /*
2  * Fragment.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_FRAGMENT_HPP
30 #define CDPL_CHEM_FRAGMENT_HPP
31 
32 #include <vector>
33 #include <unordered_map>
34 #include <memory>
35 
36 #include <boost/iterator/indirect_iterator.hpp>
37 
38 #include "CDPL/Chem/APIPrefix.hpp"
40 
41 
42 namespace CDPL
43 {
44 
45  namespace Chem
46  {
47 
57  {
58 
59  typedef std::vector<Atom*> AtomList;
60  typedef std::vector<Bond*> BondList;
61 
62  public:
66  typedef std::shared_ptr<Fragment> SharedPointer;
67 
71  typedef boost::indirect_iterator<AtomList::const_iterator, const Atom> ConstAtomIterator;
72 
76  typedef boost::indirect_iterator<AtomList::iterator, Atom> AtomIterator;
77 
81  typedef boost::indirect_iterator<BondList::const_iterator, const Bond> ConstBondIterator;
82 
86  typedef boost::indirect_iterator<BondList::iterator, Bond> BondIterator;
87 
92 
97  Fragment(const Fragment& frag);
98 
103  explicit Fragment(const MolecularGraph& molgraph);
104 
111 
116  std::size_t getNumAtoms() const;
117 
122  std::size_t getNumBonds() const;
123 
129  bool containsAtom(const Atom& atom) const;
130 
136  bool containsBond(const Bond& bond) const;
137 
144  std::size_t getAtomIndex(const Atom& atom) const;
145 
152  std::size_t getBondIndex(const Bond& bond) const;
153 
159 
165 
171 
177 
183 
189 
195 
201 
208  const Atom& getAtom(std::size_t idx) const;
209 
216  Atom& getAtom(std::size_t idx);
217 
224  const Bond& getBond(std::size_t idx) const;
225 
232  Bond& getBond(std::size_t idx);
233 
239  bool addAtom(const Atom& atom);
240 
250  bool addBond(const Bond& bond);
251 
261  void removeAtom(std::size_t idx);
262 
275 
285  bool removeAtom(const Atom& atom);
286 
292  void removeBond(std::size_t idx);
293 
302 
308  bool removeBond(const Bond& bond);
309 
313  void clear();
314 
316 
322  void swap(Fragment& frag);
323 
328  void orderAtoms(const AtomCompareFunction& func);
329 
334  void orderBonds(const BondCompareFunction& func);
335 
342  Fragment& operator=(const Fragment& frag);
343 
350  Fragment& operator=(const MolecularGraph& molgraph);
351 
359  Fragment& operator+=(const MolecularGraph& molgraph);
360 
367  Fragment& operator-=(const MolecularGraph& molgraph);
368 
374 
379  void reserveMemoryForAtoms(std::size_t num_atoms);
380 
385  void reserveMemoryForBonds(std::size_t num_bonds);
386 
387  private:
388  typedef std::unordered_map<const Atom*, std::size_t> AtomIndexMap;
389  typedef std::unordered_map<const Bond*, std::size_t> BondIndexMap;
390 
391  AtomList atoms;
392  BondList bonds;
393  AtomIndexMap atomIndices;
394  BondIndexMap bondIndices;
395  };
396  } // namespace Chem
397 } // namespace CDPL
398 
399 #endif // CDPL_CHEM_FRAGMENT_HPP
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::MolecularGraph.
void swap(PropertyContainer &cntnr)
Exchanges the properties of this container with the properties of the container cntnr.
Abstract base class representing a chemical atom and its bonded neighborhood.
Definition: Atom.hpp:57
Abstract base class representing a chemical bond between two Chem::Atom instances.
Definition: Bond.hpp:54
Concrete Chem::MolecularGraph implementation that stores references to a selectable subset of atoms a...
Definition: Fragment.hpp:57
Atom & getAtom(std::size_t idx)
Returns a non-const reference to the atom at index idx.
ConstBondIterator getBondsEnd() const
Returns a constant iterator pointing to the end of the stored const Chem::Bond objects.
void orderAtoms(const AtomCompareFunction &func)
Orders the stored atoms according to criteria implemented by the provided atom comparison function.
AtomIterator getAtomsBegin()
Returns a mutable iterator pointing to the beginning of the stored const Chem::Atom objects.
BondIterator removeBond(const BondIterator &it)
Removes the bond specified by the iterator it.
std::size_t getBondIndex(const Bond &bond) const
Returns the index of the specified bond.
const Bond & getBond(std::size_t idx) const
Returns a const reference to the bond at index idx.
bool addBond(const Bond &bond)
Extends the fragment by the specified bond.
boost::indirect_iterator< AtomList::const_iterator, const Atom > ConstAtomIterator
A constant random access iterator used to iterate over the stored const Chem::Atom objects.
Definition: Fragment.hpp:71
Bond & getBond(std::size_t idx)
Returns a non-const reference to the bond at index idx.
Fragment & operator=(const MolecularGraph &molgraph)
Replaces the current set of atoms, bonds and properties by the atoms, bonds and properties of the mol...
Fragment(const Fragment &frag)
Constructs a copy of the Fragment instance frag.
std::size_t getNumBonds() const
Returns the number of bonds.
ConstBondIterator getBondsBegin() const
Returns a constant iterator pointing to the beginning of the stored const Chem::Bond objects.
Fragment & operator-=(const MolecularGraph &molgraph)
Removes the atoms and bonds referenced by the molecular graph molgraph from this Fragment instance.
ConstAtomIterator getAtomsBegin() const
Returns a constant iterator pointing to the beginning of the stored const Chem::Atom objects.
void reserveMemoryForAtoms(std::size_t num_atoms)
Reserves storage for at least num_atoms atom references to avoid reallocations on subsequent addition...
std::size_t getAtomIndex(const Atom &atom) const
Returns the index of the specified atom.
bool removeAtom(const Atom &atom)
Removes the specified atom.
void removeBond(std::size_t idx)
Removes the bond at the specified index.
Fragment & operator+=(const MolecularGraph &molgraph)
Extends the current set of atoms and bonds by the atoms and bonds in the molecular graph molgraph.
boost::indirect_iterator< AtomList::iterator, Atom > AtomIterator
A mutable random access iterator used to iterate over the stored const Chem::Atom objects.
Definition: Fragment.hpp:76
~Fragment()
Destructor.
bool addAtom(const Atom &atom)
Extends the fragment by the specified atom.
AtomIterator removeAtom(const AtomIterator &it)
Removes the atom specified by the iterator it.
BondIterator getBondsEnd()
Returns a mutable iterator pointing to the end of the stored const Chem::Bond objects.
bool containsAtom(const Atom &atom) const
Tells whether the specified atom is part of this fragment.
std::size_t getNumAtoms() const
Returns the number of atoms.
bool removeBond(const Bond &bond)
Removes the specified bond.
void clear()
Removes all atoms and bonds.
ConstAtomIterator getAtomsEnd() const
Returns a constant iterator pointing to the end of the stored const Chem::Atom objects.
void swap(Fragment &frag)
Exchanges the atoms, bonds and properties of this fragment with the atoms, bonds and properties of th...
boost::indirect_iterator< BondList::const_iterator, const Bond > ConstBondIterator
A constant random access iterator used to iterate over the stored const Chem::Bond objects.
Definition: Fragment.hpp:81
void orderBonds(const BondCompareFunction &func)
Orders the stored bonds according to criteria implemented by the provided bond comparison function.
void reserveMemoryForBonds(std::size_t num_bonds)
Reserves storage for at least num_bonds bond references to avoid reallocations on subsequent addition...
AtomIterator getAtomsEnd()
Returns a mutable iterator pointing to the end of the stored const Chem::Atom objects.
Fragment & operator=(const Fragment &frag)
Replaces the current set of atoms, bonds and properties by the atoms, bonds and properties of the fra...
bool containsBond(const Bond &bond) const
Tells whether the specified bond is part of this fragment.
const Atom & getAtom(std::size_t idx) const
Returns a const reference to the atom at index idx.
void removeAtom(std::size_t idx)
Removes the atom at the specified index.
BondIterator getBondsBegin()
Returns a mutable iterator pointing to the beginning of the stored const Chem::Bond objects.
std::shared_ptr< Fragment > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated Fragment instances.
Definition: Fragment.hpp:66
Fragment()
Constructs an empty Fragment instance.
boost::indirect_iterator< BondList::iterator, Bond > BondIterator
A mutable random access iterator used to iterate over the stored const Chem::Bond objects.
Definition: Fragment.hpp:86
MolecularGraph::SharedPointer clone() const
Creates a deep copy of this fragment (including the copied properties).
Fragment(const MolecularGraph &molgraph)
Constructs a Fragment instance storing the atoms, bonds and properties of the molecular graph molgrap...
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
STL compatible random access iterator for container elements accessible by index.
Definition: IndexedElementIterator.hpp:125
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.