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