1 |
tim |
741 |
/********************************************************************** |
2 |
|
|
obiter.cpp - STL-style iterators for Open Babel |
3 |
|
|
|
4 |
|
|
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc. |
5 |
gezelter |
1081 |
Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison |
6 |
tim |
741 |
|
7 |
|
|
This file is part of the Open Babel project. |
8 |
|
|
For more information, see <http://openbabel.sourceforge.net/> |
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 |
|
|
#include <vector> |
21 |
|
|
|
22 |
|
|
#include "mol.hpp" |
23 |
|
|
#include "obiter.hpp" |
24 |
|
|
|
25 |
|
|
using namespace std; |
26 |
|
|
|
27 |
|
|
namespace OpenBabel |
28 |
|
|
{ |
29 |
|
|
|
30 |
|
|
/** \class OBMolAtomIter |
31 |
|
|
|
32 |
|
|
To facilitate iteration through all atoms in a molecule, without resorting |
33 |
|
|
to atom indexes (which may change in the future) or the OBMol::BeginAtom() |
34 |
gezelter |
1081 |
and OBMol::NextAtom() methods which may be deprecated in the future. |
35 |
tim |
741 |
|
36 |
|
|
This has been made significantly easier by a series of macros in the |
37 |
|
|
obiter.h header file: |
38 |
|
|
|
39 |
|
|
\code |
40 |
|
|
\#define FOR_ATOMS_OF_MOL(a,m) for( OBMolAtomIter a(m); a; a++ ) |
41 |
|
|
\endcode |
42 |
|
|
|
43 |
|
|
Here is an example: |
44 |
|
|
\code |
45 |
|
|
#include "obiter.hpp" |
46 |
|
|
#include "mol.hpp" |
47 |
|
|
|
48 |
|
|
OBMol mol; |
49 |
|
|
double exactMass = 0.0f; |
50 |
|
|
FOR_ATOMS_OF_MOL(a, mol) |
51 |
|
|
{ |
52 |
gezelter |
1081 |
// The variable a behaves like OBAtom* when used with -> and * but |
53 |
|
|
// but needs to be explicitly converted when appearing as a parameter |
54 |
|
|
// in a function call - use &*a |
55 |
|
|
|
56 |
tim |
741 |
exactMass += a->GetExactMass(); |
57 |
|
|
} |
58 |
|
|
\endcode |
59 |
|
|
**/ |
60 |
|
|
|
61 |
|
|
OBMolAtomIter::OBMolAtomIter(OBMol *mol) |
62 |
|
|
{ |
63 |
|
|
_parent = mol; |
64 |
|
|
_ptr = _parent->BeginAtom(_i); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
OBMolAtomIter::OBMolAtomIter(OBMol &mol) |
68 |
|
|
{ |
69 |
|
|
_parent = &mol; |
70 |
|
|
_ptr = _parent->BeginAtom(_i); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
OBMolAtomIter::OBMolAtomIter(const OBMolAtomIter &ai) |
74 |
|
|
{ |
75 |
|
|
_parent = ai._parent; |
76 |
|
|
_ptr = ai._ptr; |
77 |
|
|
_i = ai._i; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
OBMolAtomIter& OBMolAtomIter::operator=(const OBMolAtomIter &ai) |
81 |
|
|
{ |
82 |
|
|
if (this != &ai) |
83 |
|
|
{ |
84 |
|
|
_parent = ai._parent; |
85 |
|
|
_ptr = ai._ptr; |
86 |
|
|
_i = ai._i; |
87 |
|
|
} |
88 |
|
|
return *this; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
OBMolAtomIter OBMolAtomIter::operator++(int) |
92 |
|
|
{ |
93 |
|
|
_ptr = _parent->NextAtom(_i); |
94 |
|
|
return *this; |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
|
98 |
|
|
/** \class OBMolBondIter |
99 |
|
|
|
100 |
|
|
To facilitate iteration through all bonds in a molecule, without resorting |
101 |
|
|
to bond indexes (which may change in the future) or the OBMol::BeginBond() |
102 |
gezelter |
1081 |
and OBMol::NextBond() methods which may be deprecated in the future. |
103 |
tim |
741 |
|
104 |
|
|
This has been made significantly easier by a series of macros in the |
105 |
|
|
obiter.h header file: |
106 |
|
|
|
107 |
|
|
\code |
108 |
|
|
\#define FOR_BONDS_OF_MOL(b,m) for( OBMolBondIter b(m); b; b++ ) |
109 |
|
|
\endcode |
110 |
|
|
|
111 |
|
|
Here is an example: |
112 |
|
|
\code |
113 |
|
|
#include "obiter.hpp" |
114 |
|
|
#include "mol.hpp" |
115 |
|
|
|
116 |
|
|
OBMol mol; |
117 |
|
|
unsigned int bondOrderSum = 0; |
118 |
|
|
FOR_BONDS_OF_MOL(b, mol) |
119 |
|
|
{ |
120 |
gezelter |
1081 |
// The variable b behaves like OBBond* when used with -> and * but |
121 |
|
|
// but needs to be explicitly converted when appearing as a parameter |
122 |
|
|
// in a function call - use &*b |
123 |
tim |
741 |
bondOrderSum += b->GetBO(); |
124 |
|
|
} |
125 |
|
|
\endcode |
126 |
|
|
**/ |
127 |
|
|
|
128 |
|
|
OBMolBondIter::OBMolBondIter(OBMol *mol) |
129 |
|
|
{ |
130 |
|
|
_parent = mol; |
131 |
|
|
_ptr = _parent->BeginBond(_i); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
OBMolBondIter::OBMolBondIter(OBMol &mol) |
135 |
|
|
{ |
136 |
|
|
_parent = &mol; |
137 |
|
|
_ptr = _parent->BeginBond(_i); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
OBMolBondIter::OBMolBondIter(const OBMolBondIter &bi) |
141 |
|
|
{ |
142 |
|
|
_parent = bi._parent; |
143 |
|
|
_ptr = bi._ptr; |
144 |
|
|
_i = bi._i; |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
OBMolBondIter& OBMolBondIter::operator=(const OBMolBondIter &bi) |
148 |
|
|
{ |
149 |
|
|
if (this != &bi) |
150 |
|
|
{ |
151 |
|
|
_parent = bi._parent; |
152 |
|
|
_ptr = bi._ptr; |
153 |
|
|
_i = bi._i; |
154 |
|
|
} |
155 |
|
|
return *this; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
OBMolBondIter OBMolBondIter::operator++(int) |
159 |
|
|
{ |
160 |
|
|
_ptr = _parent->NextBond(_i); |
161 |
|
|
return *this; |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
/** \class OBAtomAtomIter |
165 |
|
|
|
166 |
|
|
To facilitate iteration through all neighbors of an atom, without resorting |
167 |
|
|
to bond indexes (which may change in the future) or the OBAtom::BeginNbr() |
168 |
gezelter |
1081 |
and OBAtom::NextNbr() methods which may be deprecated in the future. |
169 |
tim |
741 |
|
170 |
|
|
This has been made significantly easier by a series of macros in the |
171 |
|
|
obiter.h header file: |
172 |
|
|
|
173 |
|
|
\code |
174 |
|
|
\#define FOR_NBORS_OF_ATOM(a,p) for( OBAtomAtomIter a(p); a; a++ ) |
175 |
|
|
\endcode |
176 |
|
|
|
177 |
|
|
Here is an example: |
178 |
|
|
\code |
179 |
|
|
#include "obiter.hpp" |
180 |
|
|
#include "mol.hpp" |
181 |
|
|
|
182 |
|
|
OBMol mol; |
183 |
|
|
FOR_ATOMS_OF_MOL(a, mol) |
184 |
|
|
{ |
185 |
gezelter |
1081 |
// The variable a behaves like OBAtom* when used with -> and * but |
186 |
|
|
// but needs to be explicitly converted when appearing as a parameter |
187 |
|
|
// in a function call - use &*a |
188 |
|
|
FOR_NBORS_OF_ATOM(b, &*a) |
189 |
tim |
741 |
{ |
190 |
|
|
... |
191 |
|
|
} |
192 |
|
|
} |
193 |
|
|
\endcode |
194 |
|
|
**/ |
195 |
|
|
|
196 |
|
|
OBAtomAtomIter::OBAtomAtomIter(OBAtom *atm) |
197 |
|
|
{ |
198 |
|
|
_parent = atm; |
199 |
|
|
_ptr = _parent->BeginNbrAtom(_i); |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
OBAtomAtomIter::OBAtomAtomIter(OBAtom &atm) |
203 |
|
|
{ |
204 |
|
|
_parent = &atm; |
205 |
|
|
_ptr = _ptr->BeginNbrAtom(_i); |
206 |
|
|
} |
207 |
|
|
|
208 |
|
|
OBAtomAtomIter::OBAtomAtomIter(const OBAtomAtomIter &ai) |
209 |
|
|
{ |
210 |
|
|
_parent = ai._parent; |
211 |
|
|
_ptr = ai._ptr; |
212 |
|
|
_i = ai._i; |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
OBAtomAtomIter& OBAtomAtomIter::operator=(const OBAtomAtomIter &ai) |
216 |
|
|
{ |
217 |
|
|
if (this != &ai) |
218 |
|
|
{ |
219 |
|
|
_parent = ai._parent; |
220 |
|
|
_ptr = ai._ptr; |
221 |
|
|
_i = ai._i; |
222 |
|
|
} |
223 |
|
|
return *this; |
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
OBAtomAtomIter OBAtomAtomIter::operator++(int) |
227 |
|
|
{ |
228 |
|
|
_ptr = _parent->NextNbrAtom(_i); |
229 |
|
|
return *this; |
230 |
|
|
} |
231 |
|
|
|
232 |
|
|
/** \class OBAtomBondIter |
233 |
|
|
|
234 |
|
|
To facilitate iteration through all bonds on an atom, without resorting |
235 |
|
|
to bond indexes (which may change in the future) or the OBAtom::BeginBond() |
236 |
gezelter |
1081 |
and OBAtom::NextBond() methods which may be deprecated in the future |
237 |
tim |
741 |
|
238 |
|
|
This has been made significantly easier by a series of macros in the |
239 |
|
|
obiter.h header file: |
240 |
|
|
|
241 |
|
|
\code |
242 |
|
|
\#define FOR_BONDS_OF_ATOM(b,p) for( OBAtomBondIter b(p); b; b++ ) |
243 |
|
|
\endcode |
244 |
|
|
|
245 |
|
|
Here is an example: |
246 |
|
|
\code |
247 |
|
|
#include "obiter.hpp" |
248 |
|
|
#include "mol.hpp" |
249 |
|
|
|
250 |
|
|
OBAtom atom; |
251 |
|
|
unsigned int tripleBondCount; |
252 |
|
|
FOR_BONDS_OF_ATOM(b, atom) |
253 |
|
|
{ |
254 |
gezelter |
1081 |
// The variable b behaves like OBBond* when used with -> and * but |
255 |
|
|
// but needs to be explicitly converted when appearing as a parameter |
256 |
|
|
// in a function call - use &*b |
257 |
tim |
741 |
if (b->GetBO() == 3) |
258 |
|
|
tripleBondCount++; |
259 |
|
|
} |
260 |
|
|
\endcode |
261 |
|
|
**/ |
262 |
|
|
|
263 |
|
|
OBAtomBondIter::OBAtomBondIter(OBAtom *atm) |
264 |
|
|
{ |
265 |
|
|
_parent = atm; |
266 |
|
|
_ptr = _parent->BeginBond(_i); |
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
OBAtomBondIter::OBAtomBondIter(OBAtom &atm) |
270 |
|
|
{ |
271 |
|
|
_parent = &atm; |
272 |
|
|
_ptr = _parent->BeginBond(_i); |
273 |
|
|
} |
274 |
|
|
|
275 |
|
|
OBAtomBondIter::OBAtomBondIter(const OBAtomBondIter &bi) |
276 |
|
|
{ |
277 |
|
|
_parent = bi._parent; |
278 |
|
|
_ptr = bi._ptr; |
279 |
|
|
_i = bi._i; |
280 |
|
|
} |
281 |
|
|
|
282 |
|
|
OBAtomBondIter& OBAtomBondIter::operator=(const OBAtomBondIter &bi) |
283 |
|
|
{ |
284 |
|
|
if (this != &bi) |
285 |
|
|
{ |
286 |
|
|
_parent = bi._parent; |
287 |
|
|
_ptr = bi._ptr; |
288 |
|
|
_i = bi._i; |
289 |
|
|
} |
290 |
|
|
return *this; |
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
OBAtomBondIter OBAtomBondIter::operator++(int) |
294 |
|
|
{ |
295 |
|
|
_ptr = _parent->NextBond(_i); |
296 |
|
|
return *this; |
297 |
|
|
} |
298 |
|
|
|
299 |
|
|
/** \class OBResidueIter |
300 |
|
|
|
301 |
|
|
To facilitate iteration through all residues in a molecule, without resorting |
302 |
|
|
to residue indexes (which may change in the future) or the OBMol::BeginResidue() |
303 |
gezelter |
1081 |
and OBMol::NextResidue() methods which may be deprecated in the future. |
304 |
tim |
741 |
|
305 |
|
|
This has been made significantly easier by a series of macros in the |
306 |
|
|
obiter.h header file: |
307 |
|
|
|
308 |
|
|
\code |
309 |
|
|
\#define FOR_RESIDUES_OF_MOL(a,m) for( OBResidueIter a(m); a; a++ ) |
310 |
|
|
\endcode |
311 |
|
|
|
312 |
|
|
Here is an example: |
313 |
|
|
\code |
314 |
|
|
#include "obiter.hpp" |
315 |
|
|
#include "mol.hpp" |
316 |
|
|
|
317 |
|
|
OBMol mol; |
318 |
|
|
FOR_RESIDUES_OF_MOL(r, mol) |
319 |
|
|
{ |
320 |
gezelter |
1081 |
// The variable r behaves like OBResidue* when used with -> and * but |
321 |
|
|
// but needs to be explicitly converted when appearing as a parameter |
322 |
|
|
// in a function call - use &*r |
323 |
|
|
|
324 |
tim |
741 |
if (r->GetName() == resname && r->GetNum() == rnum) |
325 |
|
|
{ |
326 |
|
|
// got a match, let's go to work |
327 |
|
|
... |
328 |
|
|
} |
329 |
|
|
} |
330 |
|
|
\endcode |
331 |
|
|
**/ |
332 |
|
|
|
333 |
|
|
OBResidueIter::OBResidueIter(OBMol *mol) |
334 |
|
|
{ |
335 |
|
|
_parent = mol; |
336 |
|
|
_ptr = _parent->BeginResidue(_i); |
337 |
|
|
} |
338 |
|
|
|
339 |
|
|
OBResidueIter::OBResidueIter(OBMol &mol) |
340 |
|
|
{ |
341 |
|
|
_parent = &mol; |
342 |
|
|
_ptr = _parent->BeginResidue(_i); |
343 |
|
|
} |
344 |
|
|
|
345 |
|
|
OBResidueIter::OBResidueIter(const OBResidueIter &ri) |
346 |
|
|
{ |
347 |
|
|
_parent = ri._parent; |
348 |
|
|
_ptr = ri._ptr; |
349 |
|
|
_i = ri._i; |
350 |
|
|
} |
351 |
|
|
|
352 |
|
|
OBResidueIter& OBResidueIter::operator=(const OBResidueIter &ri) |
353 |
|
|
{ |
354 |
|
|
if (this != &ri) |
355 |
|
|
{ |
356 |
|
|
_parent = ri._parent; |
357 |
|
|
_ptr = ri._ptr; |
358 |
|
|
_i = ri._i; |
359 |
|
|
} |
360 |
|
|
return *this; |
361 |
|
|
} |
362 |
|
|
|
363 |
|
|
OBResidueIter OBResidueIter::operator++(int) |
364 |
|
|
{ |
365 |
|
|
_ptr = _parent->NextResidue(_i); |
366 |
|
|
return *this; |
367 |
|
|
} |
368 |
|
|
|
369 |
|
|
/** \class OBResidueAtomIter |
370 |
|
|
|
371 |
|
|
To facilitate iteration through all atoms in a residue, without resorting |
372 |
|
|
to atom indexes (which may change in the future) or the OBResidue::BeginAtom() |
373 |
gezelter |
1081 |
and OBResidue::NextAtom() methods which may be deprecated in the future. |
374 |
tim |
741 |
|
375 |
|
|
This has been made significantly easier by a series of macros in the |
376 |
|
|
obiter.h header file: |
377 |
|
|
|
378 |
|
|
\code |
379 |
|
|
\#define FOR_ATOMS_OF_RESIDUE(a,r) for( OBResidueAtomIter a(r); a; a++ ) |
380 |
|
|
\endcode |
381 |
|
|
|
382 |
|
|
Here is an example: |
383 |
|
|
\code |
384 |
|
|
#include "obiter.hpp" |
385 |
|
|
#include "mol.hpp" |
386 |
|
|
|
387 |
|
|
OBMol mol; |
388 |
|
|
double residueMass = 0.0; |
389 |
|
|
FOR_RESIDUES_OF_MOL(r, mol) |
390 |
|
|
{ |
391 |
gezelter |
1081 |
// The variable r behaves like OBResidue* when used with -> and * but |
392 |
|
|
// but needs to be explicitly converted when appearing as a parameter |
393 |
|
|
// in a function call - use &*r |
394 |
|
|
|
395 |
tim |
741 |
if (r->GetName() == resname && r->GetNum() == rnum) |
396 |
|
|
{ |
397 |
gezelter |
1081 |
FOR_ATOMS_OF_RESIDUE(a, &*r) |
398 |
tim |
741 |
{ |
399 |
|
|
residueMass += a->GetMass(); |
400 |
|
|
} |
401 |
|
|
} |
402 |
|
|
} |
403 |
|
|
\endcode |
404 |
|
|
**/ |
405 |
|
|
|
406 |
|
|
OBResidueAtomIter::OBResidueAtomIter(OBResidue *res) |
407 |
|
|
{ |
408 |
|
|
_parent = res; |
409 |
|
|
_ptr = _parent->BeginAtom(_i); |
410 |
|
|
} |
411 |
|
|
|
412 |
|
|
OBResidueAtomIter::OBResidueAtomIter(OBResidue &res) |
413 |
|
|
{ |
414 |
|
|
_parent = &res; |
415 |
|
|
_ptr = _parent->BeginAtom(_i); |
416 |
|
|
} |
417 |
|
|
|
418 |
|
|
OBResidueAtomIter::OBResidueAtomIter(const OBResidueAtomIter &ri) |
419 |
|
|
{ |
420 |
|
|
_parent = ri._parent; |
421 |
|
|
_ptr = ri._ptr; |
422 |
|
|
_i = ri._i; |
423 |
|
|
} |
424 |
|
|
|
425 |
|
|
OBResidueAtomIter & OBResidueAtomIter::operator = (const OBResidueAtomIter &ri) |
426 |
|
|
{ |
427 |
|
|
if (this != &ri) |
428 |
|
|
{ |
429 |
|
|
_parent = ri._parent; |
430 |
|
|
_ptr = ri._ptr; |
431 |
|
|
_i = ri._i; |
432 |
|
|
} |
433 |
|
|
|
434 |
|
|
return (*this); |
435 |
|
|
} |
436 |
|
|
|
437 |
|
|
OBResidueAtomIter OBResidueAtomIter::operator++ (int) |
438 |
|
|
{ |
439 |
|
|
_ptr = _parent->NextAtom(_i); |
440 |
|
|
return (*this); |
441 |
|
|
} |
442 |
|
|
|
443 |
|
|
} // namespace OpenBabel |
444 |
|
|
|
445 |
|
|
//! \file obiter.cpp |
446 |
|
|
//! \brief STL-style iterators for Open Babel. |