Chemical Data Processing Library C++ API - Version 1.1.1
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 
150  const AccessFunc& getAccessFunc() const;
151 
152  IndexType getIndex() const;
153 
154  private:
155  friend class boost::iterator_core_access;
156 
157  void increment();
158  void decrement();
159  void advance(DifferenceType);
160 
161  template <typename ValueType2, typename AccessFunc2, typename IndexType2>
162  DifferenceType distance_to(const IndexedElementIterator<ValueType2, AccessFunc2, IndexType2>&) const;
163 
164  template <typename ValueType2, typename AccessFunc2, typename IndexType2>
166 
167  ValueType& dereference() const;
168 
169  AccessFunc accessFunc;
170  IndexType index;
171  };
172  } // namespace Util
173 } // namespace CDPL
174 
175 
176 // Implementation
177 
178 template <typename ValueType, typename AccessFunc, typename IndexType>
180 {
181  return index;
182 }
183 
184 template <typename ValueType, typename AccessFunc, typename IndexType>
186 {
187  return accessFunc;
188 }
189 
190 template <typename ValueType, typename AccessFunc, typename IndexType>
192 {
193  index++;
194 }
195 
196 template <typename ValueType, typename AccessFunc, typename IndexType>
198 {
199  index--;
200 }
201 
202 template <typename ValueType, typename AccessFunc, typename IndexType>
204 {
205  index += diff;
206 }
207 
208 template <typename ValueType, typename AccessFunc, typename IndexType>
209 template <typename ValueType2, typename AccessFunc2, typename IndexType2>
210 typename CDPL::Util::IndexedElementIterator<ValueType, AccessFunc, IndexType>::DifferenceType
212 {
213  return (DifferenceType(it.index) - DifferenceType(index));
214 }
215 
216 template <typename ValueType, typename AccessFunc, typename IndexType>
217 template <typename ValueType2, typename AccessFunc2, typename IndexType2>
219 {
220  return (index == it.index && accessFunc == it.accessFunc);
221 }
222 
223 template <typename ValueType, typename AccessFunc, typename IndexType>
225 {
226  return accessFunc(index);
227 }
228 
229 #endif // CDPL_UTIL_INDEXEDELEMENTITERATOR_HPP
CDPL::Util::IndexedElementIterator::getIndex
IndexType getIndex() const
Definition: IndexedElementIterator.hpp:179
CDPL::Util::IndexedElementIterator
A STL compatible random access iterator for container elements accessible by index.
Definition: IndexedElementIterator.hpp:125
CDPL::Util::IndexedElementIterator::IndexedElementIterator
IndexedElementIterator(const IndexedElementIterator< ValueType2, AccessFunc2, IndexType2 > &it)
Constructs and initializes the iterator with another iterator object.
Definition: IndexedElementIterator.hpp:137
CDPL::Util::IndexedElementIterator::getAccessFunc
const AccessFunc & getAccessFunc() const
Definition: IndexedElementIterator.hpp:185
CDPL::Util::IndexedElementIterator::IndexedElementIterator
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
CDPL
The namespace of the Chemical Data Processing Library.