Chemical Data Processing Library C++ API - Version 1.4.0
VectorIterator.hpp
Go to the documentation of this file.
1 /*
2  * VectorIterator.hpp
3  *
4  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
27 #ifndef CDPL_MATH_VECTORITERATOR_HPP
28 #define CDPL_MATH_VECTORITERATOR_HPP
29 
30 #include <functional>
31 
32 #include "CDPL/Math/Expression.hpp"
34 
35 
36 namespace CDPL
37 {
38 
39  namespace Math
40  {
41 
46  template <typename E>
48  {
49 
50  typedef E VectorType;
51  typedef typename E::SizeType SizeType;
52  typedef typename E::Reference Reference;
53 
54  public:
59  VectorElementAccessor(VectorType& e):
60  vec(e) {}
61 
67  Reference operator()(SizeType i) const
68  {
69  return vec.get()(i);
70  }
71 
77  bool operator==(const VectorElementAccessor& accessor) const
78  {
79  return (&vec.get() == &accessor.vec.get());
80  }
81 
82  private:
83  std::reference_wrapper<VectorType> vec;
84  };
85 
90  template <typename E>
91  class VectorElementAccessor<const E>
92  {
93 
94  typedef E VectorType;
95  typedef typename E::SizeType SizeType;
96  typedef typename E::ConstReference Reference;
97 
98  public:
104  vec(accessor.vec) {}
105 
110  VectorElementAccessor(const VectorType& e):
111  vec(e) {}
112 
118  Reference operator()(SizeType i) const
119  {
120  return vec.get()(i);
121  }
122 
128  bool operator==(const VectorElementAccessor& accessor) const
129  {
130  return (&vec.get() == &accessor.vec.get());
131  }
132 
139  {
140  vec = std::reference_wrapper<const VectorType>(accessor.vec);
141  return *this;
142  }
143 
144  private:
145  std::reference_wrapper<const VectorType> vec;
146  };
147 
152  template <typename E>
154  {
155 
157  typedef E VectorType;
159  typedef typename E::SizeType SizeType;
161  typedef typename E::ValueType ValueType;
166 
174  {
175  return IteratorType(AccessorType(e), idx);
176  }
177  };
178 
183  template <typename E>
184  struct VectorIteratorTraits<const E>
185  {
186 
188  typedef E VectorType;
190  typedef typename E::SizeType SizeType;
192  typedef typename E::ValueType ValueType;
197 
205  {
206  return IteratorType(AccessorType(e), idx);
207  }
208  };
209 
216  template <typename E>
218  {
220  }
221 
228  template <typename E>
230  {
231  return VectorIteratorTraits<E>::makeIterator(e(), e().getSize());
232  }
233 
240  template <typename E>
242  {
244  }
245 
252  template <typename E>
254  {
255  return VectorIteratorTraits<const E>::makeIterator(e(), e().getSize());
256  }
257  } // namespace Math
258 } // namespace CDPL
259 
260 #endif // CDPL_MATH_VECTORITERATOR_HPP
Definition of basic expression types.
Definition of class CDPL::Util::IndexedElementIterator.
Specialization of Math::VectorElementAccessor for const vector references (read-only iteration).
Definition: VectorIterator.hpp:92
VectorElementAccessor(const VectorElementAccessor< E > &accessor)
Constructs the accessor from a mutable-vector accessor (relaxing it to read-only access).
Definition: VectorIterator.hpp:103
bool operator==(const VectorElementAccessor &accessor) const
Tells whether this accessor references the same vector as accessor.
Definition: VectorIterator.hpp:128
VectorElementAccessor(const VectorType &e)
Constructs the accessor referencing the constant vector e.
Definition: VectorIterator.hpp:110
VectorElementAccessor & operator=(const VectorElementAccessor< E > &accessor)
Rebinds this accessor to the vector referenced by accessor.
Definition: VectorIterator.hpp:138
Reference operator()(SizeType i) const
Returns a const reference to the element at index i of the referenced vector.
Definition: VectorIterator.hpp:118
Access functor used by Math::VectorIteratorTraits to read mutable vector elements via Util::IndexedEl...
Definition: VectorIterator.hpp:48
bool operator==(const VectorElementAccessor &accessor) const
Tells whether this accessor references the same vector as accessor.
Definition: VectorIterator.hpp:77
VectorElementAccessor(VectorType &e)
Constructs the accessor referencing the (mutable) vector e.
Definition: VectorIterator.hpp:59
Reference operator()(SizeType i) const
Returns a mutable reference to the element at index i of the referenced vector.
Definition: VectorIterator.hpp:67
CRTP base class for all vector expression types.
Definition: Expression.hpp:66
STL compatible random access iterator for container elements accessible by index.
Definition: IndexedElementIterator.hpp:125
constexpr unsigned int E
Specifies that the stereocenter has E configuration.
Definition: CIPDescriptor.hpp:96
VectorIteratorTraits< E >::IteratorType vectorBegin(VectorExpression< E > &e)
Returns a mutable iterator pointing to the first element of the vector expression e.
Definition: VectorIterator.hpp:217
VectorIteratorTraits< E >::IteratorType vectorEnd(VectorExpression< E > &e)
Returns a mutable iterator pointing one past the last element of the vector expression e.
Definition: VectorIterator.hpp:229
The namespace of the Chemical Data Processing Library.
E VectorType
The constant vector expression type being iterated.
Definition: VectorIterator.hpp:188
static IteratorType makeIterator(const VectorType &e, SizeType idx)
Creates a constant iterator pointing to the element at index idx of e.
Definition: VectorIterator.hpp:204
E::SizeType SizeType
The size type used by the iterated vector.
Definition: VectorIterator.hpp:190
E::ValueType ValueType
The element value type of the iterated vector.
Definition: VectorIterator.hpp:192
VectorElementAccessor< const E > AccessorType
The element-access functor type used to materialize iterator references (const-qualified).
Definition: VectorIterator.hpp:194
Util::IndexedElementIterator< const ValueType, AccessorType, SizeType > IteratorType
The constructed constant iterator type (a Util::IndexedElementIterator specialization).
Definition: VectorIterator.hpp:196
Traits selecting the element accessor and Util::IndexedElementIterator specialization for mutable ite...
Definition: VectorIterator.hpp:154
E VectorType
The (mutable) vector expression type being iterated.
Definition: VectorIterator.hpp:157
E::ValueType ValueType
The element value type of the iterated vector.
Definition: VectorIterator.hpp:161
Util::IndexedElementIterator< ValueType, AccessorType, SizeType > IteratorType
The constructed iterator type (a Util::IndexedElementIterator specialization).
Definition: VectorIterator.hpp:165
VectorElementAccessor< E > AccessorType
The element-access functor type used to materialize iterator references.
Definition: VectorIterator.hpp:163
static IteratorType makeIterator(VectorType &e, SizeType idx)
Creates an iterator pointing to the element at index idx of e.
Definition: VectorIterator.hpp:173
E::SizeType SizeType
The size type used by the iterated vector.
Definition: VectorIterator.hpp:159