Open Babel  3.0
obiter.h
Go to the documentation of this file.
1 /**********************************************************************
2 obiter.h - STL-style iterators for Open Babel.
3 
4 Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
5 Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
6 
7 This file is part of the Open Babel project.
8 For more information, see <http://openbabel.org/>
9 
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation version 2 of the License.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 ***********************************************************************/
19 
20 #ifndef OB_OBITER_H
21 #define OB_OBITER_H
22 
23 #include <openbabel/babelconfig.h>
24 #include <openbabel/bitvec.h>
25 
26 #include <vector>
27 #include <stack>
28 #include <queue>
29 
30 
31 namespace OpenBabel
32 {
33  class OBMol;
34  class OBAtom;
35  class OBBond;
36  class OBResidue;
37 
38  // more detailed descriptions and documentation in obiter.cpp
39 
41  class OBAPI OBMolAtomIter {
42  std::vector<OBAtom*>::iterator _i;
43  OBMol *_parent;
44  OBAtom *_ptr;
45  public:
46 
47  OBMolAtomIter() :_parent(NULL), _ptr(NULL) { }
48  OBMolAtomIter(OBMol *mol);
49  OBMolAtomIter(OBMol &mol);
50  OBMolAtomIter(const OBMolAtomIter &ai);
52 
53  OBMolAtomIter& operator=(const OBMolAtomIter &ai);
55  operator bool() const { return _ptr != NULL; }
57  OBMolAtomIter& operator++();
59  OBMolAtomIter operator++(int);
61  OBAtom* operator->() const { return _ptr; }
63  OBAtom& operator*() const { return *_ptr; }
64  };
65 
67  class OBAPI OBMolAtomDFSIter {
68  OBMol *_parent;
69  OBAtom *_ptr;
70  OBBitVec _notVisited;
71  std::stack<OBAtom *> _stack;
72  public:
73 
74  OBMolAtomDFSIter() : _parent(NULL), _ptr(NULL) { }
75  OBMolAtomDFSIter(OBMol *mol, int StartIndex=1);
76  OBMolAtomDFSIter(OBMol &mol, int StartIndex=1);
79 
80  OBMolAtomDFSIter& operator=(const OBMolAtomDFSIter &ai);
82  operator bool() const { return _ptr != NULL; }
84  OBMolAtomDFSIter& operator++();
86  OBMolAtomDFSIter operator++(int);
88  OBAtom* operator->() const { return _ptr; }
90  OBAtom& operator*() const { return *_ptr; }
93  {
94  if(_stack.empty())
95  return NULL; //end of a disconnected fragment
96  else
97  return _stack.top(); //the next atom
98  }
99  };
100 
102  class OBAPI OBMolAtomBFSIter {
103  OBMol *_parent;
104  OBAtom *_ptr;
105  OBBitVec _notVisited;
106  std::queue<OBAtom *> _queue;
107  std::vector<int> _depth;
108  public:
109 
110  OBMolAtomBFSIter(): _parent(NULL), _ptr(NULL) { }
111  OBMolAtomBFSIter(OBMol *mol, int StartIndex = 1);
112  OBMolAtomBFSIter(OBMol &mol, int StartIndex = 1);
115 
116  OBMolAtomBFSIter& operator=(const OBMolAtomBFSIter &ai);
118  operator bool() const { return _ptr != NULL; }
120  OBMolAtomBFSIter& operator++();
122  OBMolAtomBFSIter operator++(int);
124  OBAtom* operator->() const { return _ptr; }
126  OBAtom& operator*() const { return *_ptr; }
129  int CurrentDepth() const;
130  };
131 
133  class OBAPI OBMolBondBFSIter {
134  OBMol *_parent;
135  OBBond *_ptr;
136  OBBitVec _notVisited;
137  std::queue<OBBond *> _queue;
138  std::vector<int> _depth;
139  public:
140 
141  OBMolBondBFSIter(): _parent(NULL), _ptr(NULL) { }
142  OBMolBondBFSIter(OBMol *mol, int StartIndex = 0);
143  OBMolBondBFSIter(OBMol &mol, int StartIndex = 0);
146 
147  OBMolBondBFSIter& operator=(const OBMolBondBFSIter &ai);
149  operator bool() const { return _ptr != NULL; }
151  OBMolBondBFSIter& operator++();
153  OBMolBondBFSIter operator++(int);
155  OBBond* operator->() const { return _ptr; }
157  OBBond& operator*() const { return *_ptr; }
160  int CurrentDepth() const;
161  };
162 
164  class OBAPI OBMolBondIter {
165  std::vector<OBBond*>::iterator _i;
166  OBMol *_parent;
167  OBBond *_ptr;
168  public:
169 
170  OBMolBondIter() : _parent(NULL), _ptr(NULL) {}
171  OBMolBondIter(OBMol *mol);
172  OBMolBondIter(OBMol &mol);
173  OBMolBondIter(const OBMolBondIter &bi);
175 
176  OBMolBondIter& operator=(const OBMolBondIter &bi);
178  operator bool() const { return _ptr != NULL; }
180  OBMolBondIter& operator++();
182  OBMolBondIter operator++(int);
184  OBBond* operator->() const { return _ptr; }
186  OBBond& operator*() const { return *_ptr; }
187  };
188 
190  class OBAPI OBAtomAtomIter {
191  std::vector<OBBond*>::iterator _i;
192  OBAtom *_parent;
193  OBAtom *_ptr;
194  public:
195 
196  OBAtomAtomIter() : _parent(NULL), _ptr(NULL) { }
197  OBAtomAtomIter(OBAtom *atm);
198  OBAtomAtomIter(OBAtom &atm);
199  OBAtomAtomIter(const OBAtomAtomIter &ai);
201 
202  OBAtomAtomIter& operator=(const OBAtomAtomIter &ai);
204  operator bool() const { return _ptr != NULL; }
206  OBAtomAtomIter& operator++();
208  OBAtomAtomIter operator++(int);
210  OBAtom* operator->() const { return _ptr; }
212  OBAtom& operator*() const { return *_ptr; }
213  };
214 
216  class OBAPI OBAtomBondIter {
217  std::vector<OBBond*>::iterator _i;
218  OBAtom *_parent;
219  OBBond *_ptr;
220  public:
221 
222  OBAtomBondIter(): _parent(NULL), _ptr(NULL) { }
223  OBAtomBondIter(OBAtom *atm);
224  OBAtomBondIter(OBAtom &atm);
225  OBAtomBondIter(const OBAtomBondIter &bi);
227 
228  OBAtomBondIter& operator=(const OBAtomBondIter &bi);
230  operator bool() const { return _ptr != NULL; }
232  OBAtomBondIter& operator++();
234  OBAtomBondIter operator++(int);
236  OBBond* operator->() const { return _ptr; }
238  OBBond& operator*() const { return *_ptr;}
239  };
240 
242  class OBAPI OBResidueIter {
243  std::vector<OBResidue*>::iterator _i;
244  OBResidue *_ptr;
245  OBMol *_parent;
246  public:
247 
248  OBResidueIter() : _ptr(NULL), _parent(NULL) { }
249  OBResidueIter(OBMol *mol);
250  OBResidueIter(OBMol &mol);
251  OBResidueIter(const OBResidueIter &ri);
253 
254  OBResidueIter& operator=(const OBResidueIter &ri);
256  operator bool() const { return _ptr != NULL; }
258  OBResidueIter& operator++();
260  OBResidueIter operator++(int);
262  OBResidue* operator->() const{ return _ptr; }
264  OBResidue& operator*() const { return *_ptr;}
265  };
266 
268  class OBAPI OBResidueAtomIter {
269  std::vector<OBAtom*>::iterator _i;
270  OBResidue *_parent;
271  OBAtom *_ptr;
272  public:
273 
274  OBResidueAtomIter() : _parent(NULL), _ptr(NULL) { }
279 
280  OBResidueAtomIter &operator = (const OBResidueAtomIter &ri);
282  operator bool() const { return _ptr != NULL; }
284  OBResidueAtomIter& operator++ ();
286  OBResidueAtomIter operator++ (int);
288  OBAtom *operator->() const { return _ptr; }
290  OBAtom &operator*() const { return *_ptr;}
291  };
292 
294  class OBAPI OBMolAngleIter {
295  OBMol *_parent;
296  std::vector<std::vector<unsigned int> > _vangle;
297  std::vector<std::vector<unsigned int> >::iterator _i;
298  std::vector<unsigned int> _angle;
299  public:
300 
301  OBMolAngleIter() :_parent(NULL) { }
302  OBMolAngleIter(OBMol *mol);
303  OBMolAngleIter(OBMol &mol);
304  OBMolAngleIter(const OBMolAngleIter &ai);
306 
307  OBMolAngleIter& operator=(const OBMolAngleIter &ai);
309  operator bool() const { return (_i != _vangle.end()); }
311  OBMolAngleIter& operator++();
314  std::vector<unsigned int> operator*() const { return _angle; }
315  };
316 
318  class OBAPI OBMolTorsionIter {
319  OBMol *_parent;
320  std::vector<std::vector<unsigned int> > _vtorsion;
321  std::vector<std::vector<unsigned int> >::iterator _i;
322  std::vector<unsigned int> _torsion;
323  public:
324 
325  OBMolTorsionIter() :_parent(NULL) { }
326  OBMolTorsionIter(OBMol *mol);
327  OBMolTorsionIter(OBMol &mol);
330 
331  OBMolTorsionIter& operator=(const OBMolTorsionIter &ai);
333  operator bool() const { return (_i != _vtorsion.end()); }
335  OBMolTorsionIter& operator++();
338  std::vector<unsigned int> operator*() const { return _torsion; }
339  };
340 
342  class OBAPI OBMolPairIter {
343  std::vector<OBAtom*>::iterator _i;
344  std::vector<OBAtom*>::iterator _j;
345  OBMol *_parent;
346  //std::vector<std::vector<unsigned int> > _vpair;
347  //std::vector<std::vector<unsigned int> >::iterator _i;
348  std::vector<unsigned int> _pair;
349 
350  public:
351 
352  OBMolPairIter() :_parent(NULL) { }
353  OBMolPairIter(OBMol *mol);
354  OBMolPairIter(OBMol &mol);
355  OBMolPairIter(const OBMolPairIter &ai);
357 
358  OBMolPairIter& operator=(const OBMolPairIter &ai);
360  operator bool() const { return _pair.size()>0; }
362  OBMolPairIter& operator++();
365  std::vector<unsigned int> operator*() const { return _pair; }
366  };
367 
368  class OBRing;
369  class OBRingData;
370 
372  class OBAPI OBMolRingIter {
373  std::vector<OBRing*>::iterator _i;
374  OBRing *_ptr;
375  OBMol *_parent;
376  OBRingData *_rings;
377  public:
378 
379  OBMolRingIter() : _ptr(NULL), _parent(NULL), _rings(NULL) { }
380  OBMolRingIter(OBMol *mol);
381  OBMolRingIter(OBMol &mol);
382  OBMolRingIter(const OBMolRingIter &ri);
384 
385  OBMolRingIter& operator=(const OBMolRingIter &ri);
387  operator bool() const { return _ptr != NULL; }
389  OBMolRingIter& operator++();
391  OBMolRingIter operator++(int);
393  OBRing* operator->() const { return _ptr; }
395  OBRing& operator*() const { return *_ptr;}
396  };
397 
398 #define FOR_ATOMS_OF_MOL(a,m) for( OpenBabel::OBMolAtomIter a(m); a; ++a )
399 #define FOR_BONDS_OF_MOL(b,m) for( OpenBabel::OBMolBondIter b(m); b; ++b )
400 #define FOR_NBORS_OF_ATOM(a,p) for( OpenBabel::OBAtomAtomIter a(p); a; ++a )
401 #define FOR_BONDS_OF_ATOM(b,p) for( OpenBabel::OBAtomBondIter b(p); b; ++b )
402 #define FOR_RESIDUES_OF_MOL(r,m) for( OpenBabel::OBResidueIter r(m); r; ++r )
403 #define FOR_ATOMS_OF_RESIDUE(a,r) for( OpenBabel::OBResidueAtomIter a(r); a; ++a )
404 #define FOR_DFS_OF_MOL(a,m) for( OpenBabel::OBMolAtomDFSIter a(m); a; ++a )
405 #define FOR_BFS_OF_MOL(a,m) for( OpenBabel::OBMolAtomBFSIter a(m); a; ++a )
406 #define FOR_BONDBFS_OF_MOL(b,m) for( OpenBabel::OBMolBondBFSIter b(m); b; ++b )
407 #define FOR_RINGS_OF_MOL(r,m) for( OpenBabel::OBMolRingIter r(m); r; ++r )
408 #define FOR_ANGLES_OF_MOL(a,m) for( OpenBabel::OBMolAngleIter a(m); a; ++a )
409 #define FOR_TORSIONS_OF_MOL(t,m) for( OpenBabel::OBMolTorsionIter t(m); t; ++t )
410 #define FOR_PAIRS_OF_MOL(p,m) for( OpenBabel::OBMolPairIter p(m); p; ++p )
411 
412 } // namespace OpenBabel
413 #endif // OB_OBITER_H
414 
Used to store the SSSR set (filled in by OBMol::GetSSSR())
Definition: generic.h:255
~OBAtomBondIter()
Definition: obiter.h:226
OBBond & operator*() const
Definition: obiter.h:186
OBMolAtomDFSIter()
Definition: obiter.h:74
~OBMolBondIter()
Definition: obiter.h:174
~OBAtomAtomIter()
Definition: obiter.h:200
Iterate over all neighboring atoms to an OBAtom.
Definition: obiter.h:190
OBMolRingIter()
Definition: obiter.h:379
std::vector< unsigned int > operator*() const
Definition: obiter.h:314
Stores information on rings in a molecule from SSSR perception.
Definition: ring.h:40
OBRing * operator->() const
Definition: obiter.h:393
OBBond * operator->() const
Definition: obiter.h:184
~OBMolRingIter()
Definition: obiter.h:383
Bond class.
Definition: bond.h:58
Iterate over all atoms in an OBMol.
Definition: obiter.h:41
~OBResidueIter()
Definition: obiter.h:252
Molecule Class.
Definition: mol.h:118
Iterate over all atoms in an OBMol in a breadth-first search (BFS)
Definition: obiter.h:102
Iterate over all rings in an OBMol.
Definition: obiter.h:372
Iterate over all residues in an OBMol.
Definition: obiter.h:242
Iterate over all bonds in an OBMol in a breadth-first search (BFS)
Definition: obiter.h:133
OBAtom & operator*() const
Definition: obiter.h:126
OBMolAtomIter()
Definition: obiter.h:47
Iterate over all pairs of atoms (>1-4) in an OBMol.
Definition: obiter.h:342
OBRing & operator*() const
Definition: obiter.h:395
OBMolAtomBFSIter()
Definition: obiter.h:110
~OBMolAtomDFSIter()
Definition: obiter.h:78
A speed-optimized vector of bits.
Definition: bitvec.h:57
~OBMolAngleIter()
Definition: obiter.h:305
OBResidueIter()
Definition: obiter.h:248
OBBond & operator*() const
Definition: obiter.h:157
OBMolTorsionIter()
Definition: obiter.h:325
~OBResidueAtomIter()
Definition: obiter.h:278
OBAtom & operator*() const
Definition: obiter.h:290
OBBond * operator->() const
Definition: obiter.h:155
OBAtom * operator->() const
Definition: obiter.h:288
std::vector< unsigned int > operator*() const
Definition: obiter.h:338
OBMolBondBFSIter()
Definition: obiter.h:141
Iterate over all bonds on an OBAtom.
Definition: obiter.h:216
OBAtomBondIter()
Definition: obiter.h:222
OBAtom * operator->() const
Definition: obiter.h:210
~OBMolAtomBFSIter()
Definition: obiter.h:114
std::vector< unsigned int > operator*() const
Definition: obiter.h:365
~OBMolBondBFSIter()
Definition: obiter.h:145
Iterate over all bonds in an OBMol.
Definition: obiter.h:164
~OBMolAtomIter()
Definition: obiter.h:51
OBAtom & operator*() const
Definition: obiter.h:212
OBResidue & operator*() const
Definition: obiter.h:264
Iterate over all atoms in an OBResidue.
Definition: obiter.h:268
OBAtom * next()
Definition: obiter.h:92
OBMolAngleIter()
Definition: obiter.h:301
OBBond * operator->() const
Definition: obiter.h:236
Iterate over all torsions in an OBMol.
Definition: obiter.h:318
OBResidueAtomIter()
Definition: obiter.h:274
OBAtom * operator->() const
Definition: obiter.h:124
Fast and efficient bitstring class.
~OBMolPairIter()
Definition: obiter.h:356
OBAtom * operator->() const
Definition: obiter.h:61
OBAtomAtomIter()
Definition: obiter.h:196
OBAtom & operator*() const
Definition: obiter.h:90
OBBond & operator*() const
Definition: obiter.h:238
OBMolBondIter()
Definition: obiter.h:170
OBMolPairIter()
Definition: obiter.h:352
Iterate over all angles in an OBMol.
Definition: obiter.h:294
OBAtom * operator->() const
Definition: obiter.h:88
OBAtom & operator*() const
Definition: obiter.h:63
Residue information.
Definition: residue.h:50
OBResidue * operator->() const
Definition: obiter.h:262
~OBMolTorsionIter()
Definition: obiter.h:329
Iterate over all atoms in an OBMol in a depth-first search (DFS)
Definition: obiter.h:67
Global namespace for all Open Babel code.
Definition: alias.h:22
Atom class.
Definition: atom.h:71