obiter.h

Go to the documentation of this file.
00001 /**********************************************************************
00002 obiter.h - STL-style iterators for Open Babel.
00003  
00004 Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
00005 Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
00006  
00007 This file is part of the Open Babel project.
00008 For more information, see <http://openbabel.sourceforge.net/>
00009  
00010 This program is free software; you can redistribute it and/or modify
00011 it under the terms of the GNU General Public License as published by
00012 the Free Software Foundation version 2 of the License.
00013  
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 ***********************************************************************/
00019 
00020 #ifndef OB_OBITER_H
00021 #define OB_OBITER_H
00022 
00023 #include <openbabel/babelconfig.h>
00024 #include <openbabel/base.h>
00025 #include <openbabel/bitvec.h>
00026 
00027 #include <vector>
00028 #include <stack>
00029 #include <queue>
00030 
00031 namespace OpenBabel
00032 {
00033 
00034   class OBMol;
00035   class OBAtom;
00036   class OBBond;
00037   class OBResidue;
00038 
00039   // more detailed descriptions and documentation in obiter.cpp
00040 
00042   class OBAPI OBMolAtomIter {
00043     std::vector<OBAtom*>::iterator _i;
00044     OBMol *_parent;
00045     OBAtom *_ptr;
00046   public:
00047 
00048     OBMolAtomIter() :_parent(NULL), _ptr(NULL) { }
00049     OBMolAtomIter(OBMol *mol);
00050     OBMolAtomIter(OBMol &mol);
00051     OBMolAtomIter(const OBMolAtomIter &ai);
00052     ~OBMolAtomIter() { }
00053 
00054     OBMolAtomIter& operator=(const OBMolAtomIter &ai);
00056     operator bool() const        { return _ptr != NULL; }
00058     OBMolAtomIter& operator++();
00060     OBMolAtomIter  operator++(int);
00062     OBAtom* operator->() const   { return _ptr;      }
00064     OBAtom& operator*() const    { return *_ptr;     }
00065   };
00066 
00068   class OBAPI OBMolAtomDFSIter {
00069     OBMol               *_parent;
00070     OBAtom              *_ptr;
00071     OBBitVec             _notVisited;
00072     std::stack<OBAtom *> _stack;
00073   public:
00074 
00075     OBMolAtomDFSIter() : _parent(NULL), _ptr(NULL) { }
00076     OBMolAtomDFSIter(OBMol *mol, int StartIndex=1);
00077     OBMolAtomDFSIter(OBMol &mol, int StartIndex=1);
00078     OBMolAtomDFSIter(const OBMolAtomDFSIter &ai);
00079     ~OBMolAtomDFSIter() { }
00080 
00081     OBMolAtomDFSIter& operator=(const OBMolAtomDFSIter &ai);
00083     operator bool() const        { return _ptr != NULL; }
00085     OBMolAtomDFSIter& operator++();
00087     OBMolAtomDFSIter  operator++(int);
00089     OBAtom* operator->() const   { return _ptr;      }
00091     OBAtom& operator*() const    { return *_ptr;     }
00093     OBAtom* next()
00094     { 
00095       if(_stack.empty())
00096         return NULL; //end of a disconnected fragment
00097       else
00098         return _stack.top(); //the next atom
00099     }
00100   };
00101 
00103   class OBAPI OBMolAtomBFSIter {
00104     OBMol               *_parent;
00105     OBAtom              *_ptr;
00106     OBBitVec             _notVisited;
00107     std::queue<OBAtom *> _queue;
00108   public:
00109 
00110     OBMolAtomBFSIter(): _parent(NULL), _ptr(NULL) { }
00111     OBMolAtomBFSIter(OBMol *mol);
00112     OBMolAtomBFSIter(OBMol &mol);
00113     OBMolAtomBFSIter(const OBMolAtomBFSIter &ai);
00114     ~OBMolAtomBFSIter() { }
00115 
00116     OBMolAtomBFSIter& operator=(const OBMolAtomBFSIter &ai);
00118     operator bool() const        { return _ptr != NULL; }
00120     OBMolAtomBFSIter& operator++();
00122     OBMolAtomBFSIter  operator++(int);
00124     OBAtom* operator->() const   { return _ptr;      }
00126     OBAtom& operator*() const    { return *_ptr;     }
00127   };
00128 
00130   class OBAPI OBMolBondIter {
00131     std::vector<OBBond*>::iterator _i;
00132     OBMol *_parent;
00133     OBBond *_ptr;
00134   public:
00135 
00136     OBMolBondIter() : _parent(NULL), _ptr(NULL) {}
00137     OBMolBondIter(OBMol *mol);
00138     OBMolBondIter(OBMol &mol);
00139     OBMolBondIter(const OBMolBondIter &bi);
00140     ~OBMolBondIter() { }
00141 
00142     OBMolBondIter& operator=(const OBMolBondIter &bi);
00144     operator bool() const        { return _ptr != NULL; }
00146     OBMolBondIter& operator++();
00148     OBMolBondIter  operator++(int);
00150     OBBond* operator->() const   { return _ptr;      }
00152     OBBond& operator*() const    { return *_ptr;     }
00153   };
00154 
00156   class OBAPI OBAtomAtomIter {
00157     std::vector<OBBond*>::iterator _i;
00158     OBAtom *_parent;
00159     OBAtom *_ptr;
00160   public:
00161 
00162     OBAtomAtomIter() : _parent(NULL), _ptr(NULL) { }
00163     OBAtomAtomIter(OBAtom *atm);
00164     OBAtomAtomIter(OBAtom &atm);
00165     OBAtomAtomIter(const OBAtomAtomIter &ai);
00166     ~OBAtomAtomIter() { }
00167 
00168     OBAtomAtomIter& operator=(const OBAtomAtomIter &ai);
00170     operator bool() const        { return _ptr != NULL; }
00172     OBAtomAtomIter& operator++();
00174     OBAtomAtomIter  operator++(int);
00176     OBAtom* operator->() const   { return _ptr;      }
00178     OBAtom& operator*() const    { return *_ptr;     }
00179   };
00180 
00182   class OBAPI OBAtomBondIter {
00183     std::vector<OBBond*>::iterator _i;
00184     OBAtom *_parent;
00185     OBBond *_ptr;
00186   public:
00187 
00188     OBAtomBondIter(): _parent(NULL), _ptr(NULL) { }
00189     OBAtomBondIter(OBAtom *atm);
00190     OBAtomBondIter(OBAtom &atm);
00191     OBAtomBondIter(const OBAtomBondIter &bi);
00192     ~OBAtomBondIter() { }
00193 
00194     OBAtomBondIter& operator=(const OBAtomBondIter &bi);
00196     operator bool() const        { return _ptr != NULL; }
00198     OBAtomBondIter& operator++();
00200     OBAtomBondIter  operator++(int);
00202     OBBond* operator->() const   { return _ptr; }
00204     OBBond& operator*() const    { return *_ptr;}
00205   };
00206 
00208   class OBAPI OBResidueIter {
00209     std::vector<OBResidue*>::iterator _i;
00210     OBResidue *_ptr;
00211     OBMol *_parent;
00212   public:
00213 
00214     OBResidueIter() : _ptr(NULL), _parent(NULL) { }
00215     OBResidueIter(OBMol *mol);
00216     OBResidueIter(OBMol &mol);
00217     OBResidueIter(const OBResidueIter &ri);
00218     ~OBResidueIter() { }
00219 
00220     OBResidueIter& operator=(const OBResidueIter &ri);
00222     operator bool() const        { return _ptr != NULL; }
00224     OBResidueIter& operator++();
00226     OBResidueIter  operator++(int);
00228     OBResidue* operator->() const{ return _ptr; }
00230     OBResidue& operator*() const { return *_ptr;}
00231   };
00232 
00234   class OBAPI OBResidueAtomIter {
00235     std::vector<OBAtom*>::iterator _i;
00236     OBResidue *_parent;
00237     OBAtom    *_ptr;
00238   public:
00239 
00240     OBResidueAtomIter() : _parent(NULL), _ptr(NULL) { }
00241     OBResidueAtomIter(OBResidue *res);
00242     OBResidueAtomIter(OBResidue &res);
00243     OBResidueAtomIter(const OBResidueAtomIter &ri);
00244     ~OBResidueAtomIter() { }
00245 
00246     OBResidueAtomIter &operator = (const OBResidueAtomIter &ri);
00248     operator bool() const        { return _ptr != NULL; }
00250     OBResidueAtomIter& operator++ ();
00252     OBResidueAtomIter  operator++ (int);
00254     OBAtom *operator->() const   { return _ptr; }
00256     OBAtom &operator*() const    { return *_ptr;}
00257   };
00258   
00260   class OBAPI OBMolAngleIter {
00261     OBMol     *_parent;
00262     std::vector<std::vector<unsigned int> > _vangle;
00263     std::vector<std::vector<unsigned int> >::iterator _i;
00264     std::vector<unsigned int> _angle;
00265   public:
00266 
00267     OBMolAngleIter() :_parent(NULL) { }
00268     OBMolAngleIter(OBMol *mol);
00269     OBMolAngleIter(OBMol &mol);
00270     OBMolAngleIter(const OBMolAngleIter &ai);
00271     ~OBMolAngleIter() { }
00272 
00273     OBMolAngleIter& operator=(const OBMolAngleIter &ai);
00275     operator bool() const        { return (_i != _vangle.end()); }
00277     OBMolAngleIter& operator++();
00280     std::vector<unsigned int> operator*() const    { return _angle;     }
00281   };
00282 
00284   class OBAPI OBMolTorsionIter {
00285     OBMol *_parent;
00286     std::vector<std::vector<unsigned int> > _vtorsion;
00287     std::vector<std::vector<unsigned int> >::iterator _i;
00288     std::vector<unsigned int> _torsion;
00289   public:
00290 
00291     OBMolTorsionIter() :_parent(NULL) { }
00292     OBMolTorsionIter(OBMol *mol);
00293     OBMolTorsionIter(OBMol &mol);
00294     OBMolTorsionIter(const OBMolTorsionIter &ai);
00295     ~OBMolTorsionIter() { }
00296 
00297     OBMolTorsionIter& operator=(const OBMolTorsionIter &ai);
00299     operator bool() const        { return (_i != _vtorsion.end()); }
00301     OBMolTorsionIter& operator++();
00304     std::vector<unsigned int> operator*() const    { return _torsion;     }
00305   };
00306   
00308   class OBAPI OBMolPairIter {
00309     OBMol *_parent;
00310     std::vector<std::vector<unsigned int> > _vpair;
00311     std::vector<std::vector<unsigned int> >::iterator _i;
00312     std::vector<unsigned int> _pair;
00313  
00314   public:
00315 
00316     OBMolPairIter() :_parent(NULL) { }
00317     OBMolPairIter(OBMol *mol);
00318     OBMolPairIter(OBMol &mol);
00319     OBMolPairIter(const OBMolPairIter &ai);
00320     ~OBMolPairIter() { }
00321 
00322     OBMolPairIter& operator=(const OBMolPairIter &ai);
00324     operator bool() const        { return (_i != _vpair.end()); }
00326     OBMolPairIter& operator++();
00329     std::vector<unsigned int> operator*() const    { return _pair;     }
00330   };
00331 
00332 
00334   class OBAPI OBMolRingIter {
00335     std::vector<OBRing*>::iterator _i;
00336     OBRing *_ptr;
00337     OBMol *_parent;
00338     OBRingData *_rings;
00339   public:
00340 
00341     OBMolRingIter() : _ptr(NULL), _parent(NULL), _rings(NULL) { }
00342     OBMolRingIter(OBMol *mol);
00343     OBMolRingIter(OBMol &mol);
00344     OBMolRingIter(const OBMolRingIter &ri);
00345     ~OBMolRingIter() { }
00346 
00347     OBMolRingIter& operator=(const OBMolRingIter &ri);
00349     operator bool()      const { return _ptr != NULL; }
00351     OBMolRingIter& operator++();
00353     OBMolRingIter  operator++(int);
00355     OBRing* operator->() const { return _ptr; }
00357     OBRing& operator*()  const { return *_ptr;}
00358   };
00359 
00360 #define FOR_ATOMS_OF_MOL(a,m)     for( OBMolAtomIter     a(m); a; ++a )
00361 #define FOR_BONDS_OF_MOL(b,m)     for( OBMolBondIter     b(m); b; ++b )
00362 #define FOR_NBORS_OF_ATOM(a,p)    for( OBAtomAtomIter    a(p); a; ++a )
00363 #define FOR_BONDS_OF_ATOM(b,p)    for( OBAtomBondIter    b(p); b; ++b )
00364 #define FOR_RESIDUES_OF_MOL(r,m)  for( OBResidueIter     r(m); r; ++r )
00365 #define FOR_ATOMS_OF_RESIDUE(a,r) for( OBResidueAtomIter a(r); a; ++a )
00366 #define FOR_DFS_OF_MOL(a,m)       for( OBMolAtomDFSIter  a(m); a; ++a )
00367 #define FOR_BFS_OF_MOL(a,m)       for( OBMolAtomBFSIter  a(m); a; ++a )
00368 #define FOR_RINGS_OF_MOL(a,m)     for( OBMolRingIter     r(m); r; ++r )
00369 #define FOR_ANGLES_OF_MOL(a,m)    for( OBMolAngleIter    a(m); a; ++a )
00370 #define FOR_TORSIONS_OF_MOL(t,m)  for( OBMolTorsionIter  t(m); t; ++t )
00371 #define FOR_PAIRS_OF_MOL(p,m)     for( OBMolPairIter     p(m); p; ++p )
00372 
00373 } // namespace OpenBabel
00374 #endif // OB_OBITER_H
00375