Chemical Data Processing Library C++ API - Version 1.4.0
IndexedElementIterator.hpp
Go to the documentation of this file.
1 /*
2  * IndexedElementIterator.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_UTIL_INDEXEDELEMENTITERATOR_HPP
30 #define CDPL_UTIL_INDEXEDELEMENTITERATOR_HPP
31 
32 #include <cstddef>
33 
34 #include <boost/iterator/iterator_facade.hpp>
35 
36 
37 namespace CDPL
38 {
39 
40  namespace Util
41  {
42 
122  template <typename ValueType, typename AccessFunc, typename IndexType = std::size_t>
123  class IndexedElementIterator : public boost::iterator_facade<IndexedElementIterator<ValueType, AccessFunc, IndexType>,
124  ValueType, boost::random_access_traversal_tag>
125  {
126 
127  typedef typename boost::iterator_facade<IndexedElementIterator<ValueType, AccessFunc, IndexType>,
128  ValueType,
129  boost::random_access_traversal_tag>::difference_type DifferenceType;
130 
131  public:
136  template <typename ValueType2, typename AccessFunc2, typename IndexType2>
138  accessFunc(it.getAccessFunc()), index(it.getIndex())
139  {}
140 
147  IndexedElementIterator(const AccessFunc& access_func, IndexType start_idx):
148  accessFunc(access_func), index(start_idx) {}
149 
154  const AccessFunc& getAccessFunc() const;
155 
160  IndexType getIndex() const;
161 
162  private:
163  friend class boost::iterator_core_access;
164 
165  void increment();
166  void decrement();
167  void advance(DifferenceType);
168 
169  template <typename ValueType2, typename AccessFunc2, typename IndexType2>
170  DifferenceType distance_to(const IndexedElementIterator<ValueType2, AccessFunc2, IndexType2>&) const;
171 
172  template <typename ValueType2, typename AccessFunc2, typename IndexType2>
174 
175  ValueType& dereference() const;
176 
177  AccessFunc accessFunc;
178  IndexType index;
179  };
180  } // namespace Util
181 } // namespace CDPL
182 
183 
184 // Implementation
185 
186 template <typename ValueType, typename AccessFunc, typename IndexType>
188 {
189  return index;
190 }
191 
192 template <typename ValueType, typename AccessFunc, typename IndexType>
194 {
195  return accessFunc;
196 }
197 
198 template <typename ValueType, typename AccessFunc, typename IndexType>
200 {
201  index++;
202 }
203 
204 template <typename ValueType, typename AccessFunc, typename IndexType>
206 {
207  index--;
208 }
209 
210 template <typename ValueType, typename AccessFunc, typename IndexType>
212 {
213  index += diff;
214 }
215 
216 template <typename ValueType, typename AccessFunc, typename IndexType>
217 template <typename ValueType2, typename AccessFunc2, typename IndexType2>
218 typename CDPL::Util::IndexedElementIterator<ValueType, AccessFunc, IndexType>::DifferenceType
220 {
221  return (DifferenceType(it.index) - DifferenceType(index));
222 }
223 
224 template <typename ValueType, typename AccessFunc, typename IndexType>
225 template <typename ValueType2, typename AccessFunc2, typename IndexType2>
227 {
228  return (index == it.index && accessFunc == it.accessFunc);
229 }
230 
231 template <typename ValueType, typename AccessFunc, typename IndexType>
233 {
234  return accessFunc(index);
235 }
236 
237 #endif // CDPL_UTIL_INDEXEDELEMENTITERATOR_HPP
STL compatible random access iterator for container elements accessible by index.
Definition: IndexedElementIterator.hpp:125
const AccessFunc & getAccessFunc() const
Returns the access functor used by the iterator.
Definition: IndexedElementIterator.hpp:193
IndexType getIndex() const
Returns the index that the iterator currently points to.
Definition: IndexedElementIterator.hpp:187
IndexedElementIterator(const IndexedElementIterator< ValueType2, AccessFunc2, IndexType2 > &it)
Constructs and initializes the iterator with another iterator object.
Definition: IndexedElementIterator.hpp:137
IndexedElementIterator(const AccessFunc &access_func, IndexType start_idx)
Constructs and initializes the iterator with the access function access_func and the start element in...
Definition: IndexedElementIterator.hpp:147
The namespace of the Chemical Data Processing Library.