AtomIterators.h

Go to the documentation of this file.
00001 //
00002 //  Copyright (C) 2002-2006 Greg Landrum and Rational Discovery LLC
00003 //
00004 //   @@ All Rights Reserved @@
00005 //  This file is part of the RDKit.
00006 //  The contents are covered by the terms of the BSD license
00007 //  which is included in the file license.txt, found at the root
00008 //  of the RDKit source tree.
00009 //
00010 /*! \file AtomIterators.h
00011 
00012   \brief various tools for iterating over a molecule's Atoms.
00013 
00014    <b>WARNING:</b> If you go changing the molecule underneath one of
00015    these iterators you will be sad...
00016 */  
00017 #ifndef __RD_ATOM_ITERATORS_H__
00018 #define __RD_ATOM_ITERATORS_H__
00019 
00020 #ifdef _MSC_VER
00021 #pragma warning (disable: 4661)  // no suitable definition provided for explicit template instantiation request
00022 #endif
00023 
00024 namespace RDKit{
00025   class QueryAtom;
00026   
00027   //! A general random access iterator
00028   template <class Atom_, class Mol_>
00029   class AtomIterator_ {
00030   public:
00031     typedef AtomIterator_<Atom_,Mol_> ThisType;
00032     AtomIterator_() : _pos(0),_max(-1),_mol(0) {}; 
00033     AtomIterator_(Mol_ * mol);
00034     AtomIterator_(Mol_ * mol,int pos);
00035     AtomIterator_(const ThisType &other);
00036     AtomIterator_ &operator=(const ThisType &other);
00037     AtomIterator_ &operator+=(int val);
00038     AtomIterator_ &operator-=(int val);
00039     AtomIterator_ operator+(int val);
00040     AtomIterator_ operator-(int val);
00041 
00042     // iterator subtraction
00043     int operator-(ThisType &other);
00044 
00045     // dereference 
00046     Atom_ * operator*();
00047     // random access
00048     Atom_ * operator[](const int which);
00049     bool operator==(const ThisType &other);
00050     bool operator!=(const ThisType &other);
00051     bool operator<(const ThisType &other);
00052     bool operator<=(const ThisType &other);
00053     bool operator>(const ThisType &other);
00054     bool operator>=(const ThisType &other);
00055 
00056     // pre-increment
00057     ThisType &operator++();
00058     ThisType operator++(int);
00059 
00060     // pre-decrement
00061     ThisType &operator--();
00062     ThisType operator--(int);
00063   
00064   private:
00065     int _pos,_max;
00066     Mol_ * _mol;
00067   };
00068 
00069 
00070   //! Iterate over heteroatoms, this is bidirectional
00071   template <class Atom_, class Mol_>
00072   class HeteroatomIterator_ {
00073   public:
00074     typedef HeteroatomIterator_<Atom_,Mol_> ThisType;
00075     HeteroatomIterator_() : _mol(0) {}; 
00076     HeteroatomIterator_(Mol_ * mol);
00077     HeteroatomIterator_(Mol_ * mol,int pos);
00078     ~HeteroatomIterator_();
00079     HeteroatomIterator_(const ThisType &other);
00080     HeteroatomIterator_ &operator=(const ThisType &other);
00081     bool operator==(const ThisType &other);
00082     bool operator!=(const ThisType &other);
00083 
00084     Atom_ * operator*();
00085 
00086     // pre-increment
00087     ThisType &operator++();
00088     ThisType operator++(int);
00089 
00090     // pre-decrement
00091     ThisType &operator--();
00092     ThisType operator--(int);
00093   private:
00094     int _end,_pos;
00095     Mol_ * _mol;
00096     //FIX: somehow changing the following to a pointer make the regression test pass
00097     // QueryAtom _qA;
00098     QueryAtom *_qA;
00099 
00100     int _findNext(int from);  
00101     int _findPrev(int from);
00102   };
00103 
00104 
00105 
00106   //! Iterate over aromatic atoms, this is bidirectional
00107   template <class Atom_, class Mol_>
00108   class AromaticAtomIterator_ {
00109   public:
00110     typedef AromaticAtomIterator_<Atom_,Mol_> ThisType;
00111     AromaticAtomIterator_() : _mol(0) {}; 
00112     AromaticAtomIterator_(Mol_ * mol);
00113     AromaticAtomIterator_(Mol_ * mol,int pos);
00114     ~AromaticAtomIterator_();
00115     AromaticAtomIterator_(const ThisType &other);
00116     AromaticAtomIterator_ &operator=(const ThisType &other);
00117     bool operator==(const ThisType &other);
00118     bool operator!=(const ThisType &other);
00119 
00120     Atom_ * operator*();
00121 
00122     // pre-increment
00123     ThisType &operator++();
00124     ThisType operator++(int);
00125 
00126     // pre-decrement
00127     ThisType &operator--();
00128     ThisType operator--(int);
00129   private:
00130     int _end,_pos;
00131     Mol_ * _mol;
00132 
00133     int _findNext(int from);  
00134     int _findPrev(int from);
00135   };
00136 
00137 
00138   
00139   //! Iterate over atoms matching a query. This is bidirectional.
00140   template <class Atom_, class Mol_>
00141   class QueryAtomIterator_ {
00142   public:
00143     typedef QueryAtomIterator_<Atom_,Mol_> ThisType;
00144     QueryAtomIterator_() : _mol(0),_qA(0) {}; 
00145     QueryAtomIterator_(Mol_ * mol,QueryAtom const *what);
00146     QueryAtomIterator_(Mol_ * mol,int pos);
00147     ~QueryAtomIterator_();
00148     QueryAtomIterator_(const ThisType &other);
00149     QueryAtomIterator_ &operator=(const ThisType &other);
00150     bool operator==(const ThisType &other);
00151     bool operator!=(const ThisType &other);
00152 
00153     Atom_ * operator*();
00154 
00155     // pre-increment
00156     ThisType &operator++();
00157     ThisType operator++(int);
00158 
00159     // pre-decrement
00160     ThisType &operator--();
00161     ThisType operator--(int);
00162   private:
00163     int _end,_pos;
00164     Mol_ * _mol;
00165     QueryAtom *_qA;
00166 
00167     int _findNext(int from);  
00168     int _findPrev(int from);
00169   };
00170 
00171 
00172 }  /* end o namespace */
00173 
00174 #endif