OpenStructure
iterator.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef OST_ITERATOR_HH
20 #define OST_ITERATOR_HH
21 
22 #include <map>
23 
24 
25 #include <ost/mol/chain_handle.hh>
26 #include <ost/mol/chain_view.hh>
28 #include <ost/mol/residue_view.hh>
29 #include <ost/mol/atom_handle.hh>
30 #include <ost/mol/atom_view.hh>
31 #include <ost/mol/entity_handle.hh>
32 #include <ost/mol/entity_view.hh>
37 
38 namespace ost { namespace mol {
39 
50 template <typename I>
51 class IterRange {
52 public:
53  typedef I iterator_type;
54  typedef typename I::value_type value_type;
55  IterRange() {}
56  IterRange(I beg, I end)
57  : end_(end), cur_(beg) {
58  }
60  bool AtEnd() const {
61  return cur_==end_;
62  }
65  ++cur_;
66  return *this;
67  }
68  typename I::value_type& Get() {
69  return *cur_;
70  }
72  ++cur_;
73  return *this;
74  }
75  typename I::value_type& operator*() {
76  return this->Get();
77  }
78 private:
79  I end_;
80  I cur_;
81 };
82 
83 
84 namespace impl {
85  // forward declearation of chain impl list.
86  typedef std::vector<ChainImplPtr> ChainImplList;
87 }
88 
89 class DLLEXPORT_OST_MOL ChainHandleIter : public std::iterator<std::forward_iterator_tag,
90  ChainHandle> {
91 public: // internally used constructors
92  ChainHandleIter(impl::ChainImplList::iterator chain_it)
93  : cur_(chain_it) {
94  }
95 public:
97  return ChainHandle(*cur_);
98  }
99 
101  ++cur_;
102  return *this;
103  }
104 
105  bool operator==(const ChainHandleIter& rhs) const {
106  return cur_==rhs.cur_;
107  }
108 
109  bool operator!=(const ChainHandleIter& rhs) const {
110  return !this->operator==(rhs);
111  }
112 private:
113  impl::ChainImplList::iterator cur_;
114 };
115 
116 class DLLEXPORT_OST_MOL ResidueHandleIter : public std::iterator<std::forward_iterator_tag,
117  ResidueHandle> {
118 public:
119  ResidueHandleIter(): cur_chain_(NULL), cur_res_(NULL) { }
122  impl::EntityImplPtr ent, bool skip_empty);
123 
124  bool operator==(const ResidueHandleIter& rhs) const
125  {
126  return cur_res_==rhs.cur_res_;
127  }
128 
129  void SkipEmpty();
130 
131  bool operator!=(const ResidueHandleIter& rhs) const
132  {
133  return !this->operator==(rhs);
134  }
135  ResidueHandleIter& operator++();
136 
138 
139 private:
142  impl::EntityImplPtr ent_;
143 };
144 
145 class DLLEXPORT_OST_MOL ResidueViewIter : public std::iterator<std::forward_iterator_tag,
146  ResidueView> {
147 public:
148  ResidueViewIter(): cur_chain_(NULL), cur_res_(NULL) { }
149 
152  EntityView ent, bool skip_empty);
153 
154  bool operator==(const ResidueViewIter& rhs) const {
155  return cur_res_==rhs.cur_res_;
156  }
157  void SkipEmpty();
158 
159  bool operator!=(const ResidueViewIter& rhs) const {
160  return !this->operator==(rhs);
161  }
162  ResidueViewIter& operator++();
163 
165 
166 private:
167  impl::pointer_it<ChainView> cur_chain_;
169  EntityView ent_;
170 };
171 
172 class DLLEXPORT_OST_MOL AtomHandleIter : public std::iterator<std::forward_iterator_tag,
173  AtomHandle> {
174 public:
175  AtomHandleIter(): cur_chain_(NULL), cur_res_(NULL), cur_atom_(NULL) { }
176 
180  impl::EntityImplPtr ent, bool skip_empty);
181 
182  bool operator==(const AtomHandleIter& rhs) const
183  {
184  return cur_atom_==rhs.cur_atom_;
185  }
186 
187  bool operator!=(const AtomHandleIter& rhs) const
188  {
189  return !this->operator==(rhs);
190  }
191 
192  AtomHandleIter& operator++();
193 
195 private:
196  void SkipEmpty();
200  impl::EntityImplPtr ent_;
201 };
202 
203 class DLLEXPORT_OST_MOL AtomViewIter : public std::iterator<std::forward_iterator_tag,
204  AtomView> {
205 public:
206  AtomViewIter(): cur_chain_(NULL), cur_res_(NULL), cur_atom_(NULL) { }
207 
211  EntityView ent, bool skip_empty);
212 
213  bool operator==(const AtomViewIter& rhs) const
214  {
215  return cur_atom_==rhs.cur_atom_;
216  }
217 
218  bool operator!=(const AtomViewIter& rhs) const
219  {
220  return !this->operator==(rhs);
221  }
222 
223  AtomViewIter& operator++();
224 
226 private:
227  void SkipEmpty();
228  impl::pointer_it<ChainView> cur_chain_;
230  impl::pointer_it<AtomView> cur_atom_;
231  EntityView ent_;
232 };
233 
234 }} //ns
235 
236 
237 #endif