1 //
2 //  Copyright (C) 2002-2006 Greg Landrum and Rational Discovery LLC
3 //
4 //   @@ All Rights Reserved @@
5 //  This file is part of the RDKit.
6 //  The contents are covered by the terms of the BSD license
7 //  which is included in the file license.txt, found at the root
8 //  of the RDKit source tree.
9 //
10 /*! \file AtomIterators.h
11 
12   \brief various tools for iterating over a molecule's Atoms.
13 
14    <b>WARNING:</b> If you go changing the molecule underneath one of
15    these iterators you will be sad...
16 */
17 #include <RDGeneral/export.h>
18 #ifndef __RD_ATOM_ITERATORS_H__
19 #define __RD_ATOM_ITERATORS_H__
20 
21 #ifdef _MSC_VER
22 #pragma warning(disable : 4661)  // no suitable definition provided for explicit
23                                  // template instantiation request
24 #endif
25 
26 namespace RDKit {
27 class QueryAtom;
28 
29 //! A general random access iterator
30 template <class Atom_, class Mol_>
31 class RDKIT_GRAPHMOL_EXPORT AtomIterator_ {
32  public:
33   typedef AtomIterator_<Atom_, Mol_> ThisType;
AtomIterator_()34   AtomIterator_() : _mol(nullptr){};
35   AtomIterator_(Mol_ *mol);
36   AtomIterator_(Mol_ *mol, int pos);
37   AtomIterator_(const ThisType &other);
38   AtomIterator_ &operator=(const ThisType &other);
39   AtomIterator_ &operator+=(int val);
40   AtomIterator_ &operator-=(int val);
41   AtomIterator_ operator+(int val) const;
42   AtomIterator_ operator-(int val) const;
43 
44   // iterator subtraction
45   int operator-(ThisType &other) const;
46 
47   // dereference
48   Atom_ *operator*() const;
49   // random access
50   Atom_ *operator[](const int which) const;
51   bool operator==(const ThisType &other) const;
52   bool operator!=(const ThisType &other) const;
53   bool operator<(const ThisType &other) const;
54   bool operator<=(const ThisType &other) const;
55   bool operator>(const ThisType &other) const;
56   bool operator>=(const ThisType &other) const;
57 
58   // pre-increment
59   ThisType &operator++();
60   ThisType operator++(int);
61 
62   // pre-decrement
63   ThisType &operator--();
64   ThisType operator--(int);
65 
66  private:
67   int _pos{-1};
68   int _max{-1};
69   Mol_ *_mol;
70 };
71 
72 //! Iterate over heteroatoms, this is bidirectional
73 template <class Atom_, class Mol_>
74 class RDKIT_GRAPHMOL_EXPORT HeteroatomIterator_ {
75  public:
76   typedef HeteroatomIterator_<Atom_, Mol_> ThisType;
HeteroatomIterator_()77   HeteroatomIterator_() : _mol(nullptr){};
78   HeteroatomIterator_(Mol_ *mol);
79   HeteroatomIterator_(Mol_ *mol, int pos);
80   ~HeteroatomIterator_();
81   HeteroatomIterator_(const ThisType &other);
82   HeteroatomIterator_ &operator=(const ThisType &other);
83   bool operator==(const ThisType &other) const;
84   bool operator!=(const ThisType &other) const;
85 
86   Atom_ *operator*() const;
87 
88   // pre-increment
89   ThisType &operator++();
90   ThisType operator++(int);
91 
92   // pre-decrement
93   ThisType &operator--();
94   ThisType operator--(int);
95 
96  private:
97   int _end{-1};
98   int _pos{-1};
99   Mol_ *_mol;
100   // FIX: somehow changing the following to a pointer make the regression test
101   // pass
102   // QueryAtom _qA;
103   QueryAtom *_qA;
104 
105   int _findNext(int from);
106   int _findPrev(int from);
107 };
108 
109 //! Iterate over aromatic atoms, this is bidirectional
110 template <class Atom_, class Mol_>
111 class RDKIT_GRAPHMOL_EXPORT AromaticAtomIterator_ {
112  public:
113   typedef AromaticAtomIterator_<Atom_, Mol_> ThisType;
AromaticAtomIterator_()114   AromaticAtomIterator_() : _mol(nullptr){};
115   AromaticAtomIterator_(Mol_ *mol);
116   AromaticAtomIterator_(Mol_ *mol, int pos);
117   ~AromaticAtomIterator_();
118   AromaticAtomIterator_(const ThisType &other);
119   AromaticAtomIterator_ &operator=(const ThisType &other);
120   bool operator==(const ThisType &other) const;
121   bool operator!=(const ThisType &other) const;
122 
123   Atom_ *operator*() const;
124 
125   // pre-increment
126   ThisType &operator++();
127   ThisType operator++(int);
128 
129   // pre-decrement
130   ThisType &operator--();
131   ThisType operator--(int);
132 
133  private:
134   int _end{-1};
135   int _pos{-1};
136   Mol_ *_mol;
137 
138   int _findNext(int from);
139   int _findPrev(int from);
140 };
141 
142 //! Iterate over atoms matching a query. This is bidirectional.
143 template <class Atom_, class Mol_>
144 class RDKIT_GRAPHMOL_EXPORT QueryAtomIterator_ {
145  public:
146   typedef QueryAtomIterator_<Atom_, Mol_> ThisType;
QueryAtomIterator_()147   QueryAtomIterator_() : _mol(nullptr){};
148   QueryAtomIterator_(Mol_ *mol, QueryAtom const *what);
149   QueryAtomIterator_(Mol_ *mol, int pos);
150   ~QueryAtomIterator_();
151   QueryAtomIterator_(const ThisType &other);
152   QueryAtomIterator_ &operator=(const ThisType &other);
153   bool operator==(const ThisType &other) const;
154   bool operator!=(const ThisType &other) const;
155 
156   Atom_ *operator*() const;
157 
158   // pre-increment
159   ThisType &operator++();
160   ThisType operator++(int);
161 
162   // pre-decrement
163   ThisType &operator--();
164   ThisType operator--(int);
165 
166  private:
167   int _end{-1};
168   int _pos{-1};
169   Mol_ *_mol;
170   QueryAtom *_qA{nullptr};
171 
172   int _findNext(int from);
173   int _findPrev(int from);
174 };
175 
176 //! Iterate over atoms matching a query function. This is bidirectional.
177 template <class Atom_, class Mol_>
178 class RDKIT_GRAPHMOL_EXPORT MatchingAtomIterator_ {
179  public:
180   typedef MatchingAtomIterator_<Atom_, Mol_> ThisType;
MatchingAtomIterator_()181   MatchingAtomIterator_() : _mol(nullptr), _qF(nullptr){};
182   MatchingAtomIterator_(Mol_ *mol, bool (*fn)(Atom_ *));
183   MatchingAtomIterator_(Mol_ *mol, int pos);
184   ~MatchingAtomIterator_();
185   MatchingAtomIterator_(const ThisType &other);
186   MatchingAtomIterator_ &operator=(const ThisType &other);
187   bool operator==(const ThisType &other) const;
188   bool operator!=(const ThisType &other) const;
189 
190   Atom_ *operator*() const;
191 
192   // pre-increment
193   ThisType &operator++();
194   ThisType operator++(int);
195 
196   // pre-decrement
197   ThisType &operator--();
198   ThisType operator--(int);
199 
200  private:
201   int _end{-1};
202   int _pos{-1};
203   Mol_ *_mol;
204   bool (*_qF)(Atom_ *);
205 
206   int _findNext(int from);
207   int _findPrev(int from);
208 };
209 
210 }  // namespace RDKit
211 
212 #endif
213