Chemical Data Processing Library C++ API - Version 1.2.0
VectorProxy.hpp
Go to the documentation of this file.
1 /*
2  * VectorProxy.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_VECTORPROXY_HPP
28 #define CDPL_MATH_VECTORPROXY_HPP
29 
30 #include <type_traits>
31 
32 #include "CDPL/Math/Expression.hpp"
34 #include "CDPL/Math/TypeTraits.hpp"
35 #include "CDPL/Math/Functional.hpp"
36 #include "CDPL/Math/Range.hpp"
37 #include "CDPL/Math/Slice.hpp"
38 
39 
40 namespace CDPL
41 {
42 
43  namespace Math
44  {
45 
46  template <typename V>
47  class VectorRange : public VectorExpression<VectorRange<V> >
48  {
49 
50  typedef VectorRange<V> SelfType;
51 
52  public:
53  typedef V VectorType;
54  typedef typename V::SizeType SizeType;
55  typedef typename V::DifferenceType DifferenceType;
56  typedef typename V::ValueType ValueType;
57  typedef typename V::ConstReference ConstReference;
58  typedef typename std::conditional<std::is_const<V>::value,
59  typename V::ConstReference,
60  typename V::Reference>::type Reference;
61  typedef typename std::conditional<std::is_const<V>::value,
62  typename V::ConstClosureType,
63  typename V::ClosureType>::type VectorClosureType;
64  typedef const SelfType ConstClosureType;
67 
69  data(v), range(r) {}
70 
72  {
73  return data(range(i));
74  }
75 
77  {
78  return data(range(i));
79  }
80 
82  {
83  return data[range(i)];
84  }
85 
87  {
88  return data[range(i)];
89  }
90 
92  {
93  return range.getStart();
94  }
95 
96  SizeType getSize() const
97  {
98  return range.getSize();
99  }
100 
101  bool isEmpty() const
102  {
103  return range.isEmpty();
104  }
105 
107  {
108  return data;
109  }
110 
111  const VectorClosureType& getData() const
112  {
113  return data;
114  }
115 
117  {
118  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(r));
119  return *this;
120  }
121 
122  template <typename E>
124  {
125  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(e));
126  return *this;
127  }
128 
129  template <typename E>
131  {
132  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this + e));
133  return *this;
134  }
135 
136  template <typename E>
138  {
139  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this - e));
140  return *this;
141  }
142 
143  template <typename T>
144  typename std::enable_if<IsScalar<T>::value, VectorRange>::type& operator*=(const T& t)
145  {
146  vectorAssignScalar<ScalarMultiplicationAssignment>(*this, t);
147  return *this;
148  }
149 
150  template <typename T>
151  typename std::enable_if<IsScalar<T>::value, VectorRange>::type& operator/=(const T& t)
152  {
153  vectorAssignScalar<ScalarDivisionAssignment>(*this, t);
154  return *this;
155  }
156 
157  template <typename E>
159  {
160  vectorAssignVector<ScalarAssignment>(*this, e);
161  return *this;
162  }
163 
164  template <typename E>
166  {
167  vectorAssignVector<ScalarAdditionAssignment>(*this, e);
168  return *this;
169  }
170 
171  template <typename E>
173  {
174  vectorAssignVector<ScalarSubtractionAssignment>(*this, e);
175  return *this;
176  }
177 
179  {
180  if (this != &r)
181  vectorSwap(*this, r);
182  }
183 
184  friend void swap(VectorRange& r1, VectorRange& r2)
185  {
186  r1.swap(r2);
187  }
188 
189  private:
190  VectorClosureType data;
191  RangeType range;
192  };
193 
194  template <typename V>
195  class VectorSlice : public VectorExpression<VectorSlice<V> >
196  {
197 
198  typedef VectorSlice<V> SelfType;
199 
200  public:
201  typedef V VectorType;
202  typedef typename V::SizeType SizeType;
203  typedef typename V::DifferenceType DifferenceType;
204  typedef typename V::ValueType ValueType;
205  typedef typename V::ConstReference ConstReference;
206  typedef typename std::conditional<std::is_const<V>::value,
207  typename V::ConstReference,
208  typename V::Reference>::type Reference;
209  typedef typename std::conditional<std::is_const<V>::value,
210  typename V::ConstClosureType,
211  typename V::ClosureType>::type VectorClosureType;
212  typedef const SelfType ConstClosureType;
215 
217  data(v), slice(s) {}
218 
220  {
221  return data(slice(i));
222  }
223 
225  {
226  return data(slice(i));
227  }
228 
230  {
231  return data[slice(i)];
232  }
233 
235  {
236  return data[slice(i)];
237  }
238 
240  {
241  return slice.getStart();
242  }
243 
245  {
246  return slice.getStride();
247  }
248 
250  {
251  return slice.getSize();
252  }
253 
254  bool isEmpty() const
255  {
256  return slice.isEmpty();
257  }
258 
260  {
261  return data;
262  }
263 
264  const VectorClosureType& getData() const
265  {
266  return data;
267  }
268 
270  {
271  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(s));
272  return *this;
273  }
274 
275  template <typename E>
277  {
278  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(e));
279  return *this;
280  }
281 
282  template <typename E>
284  {
285  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this + e));
286  return *this;
287  }
288 
289  template <typename E>
291  {
292  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this - e));
293  return *this;
294  }
295 
296  template <typename T>
297  typename std::enable_if<IsScalar<T>::value, VectorSlice>::type& operator*=(const T& t)
298  {
299  vectorAssignScalar<ScalarMultiplicationAssignment>(*this, t);
300  return *this;
301  }
302 
303  template <typename T>
304  typename std::enable_if<IsScalar<T>::value, VectorSlice>::type& operator/=(const T& t)
305  {
306  vectorAssignScalar<ScalarDivisionAssignment>(*this, t);
307  return *this;
308  }
309 
310  template <typename E>
312  {
313  vectorAssignVector<ScalarAssignment>(*this, e);
314  return *this;
315  }
316 
317  template <typename E>
319  {
320  vectorAssignVector<ScalarAdditionAssignment>(*this, e);
321  return *this;
322  }
323 
324  template <typename E>
326  {
327  vectorAssignVector<ScalarSubtractionAssignment>(*this, e);
328  return *this;
329  }
330 
332  {
333  if (this != &s)
334  vectorSwap(*this, s);
335  }
336 
337  friend void swap(VectorSlice& s1, VectorSlice& s2)
338  {
339  s1.swap(s2);
340  }
341 
342  private:
343  VectorClosureType data;
344  SliceType slice;
345  };
346 
347  template <typename V>
349  {};
350 
351  template <typename V>
353  {};
354 
355  template <typename V>
357  {};
358 
359  template <typename V>
361  {};
362 
363  template <typename E>
366  {
367  return VectorRange<E>(e(), r);
368  }
369 
370  template <typename E>
371  VectorRange<const E>
373  {
374  return VectorRange<const E>(e(), r);
375  }
376 
377  template <typename E>
378  VectorRange<E>
382  {
383  typedef typename VectorRange<E>::RangeType RangeType;
384 
385  return VectorRange<E>(e(), RangeType(start, stop));
386  }
387 
388  template <typename E>
389  VectorRange<const E>
393  {
394  typedef typename VectorRange<const E>::RangeType RangeType;
395 
396  return VectorRange<const E>(e(), RangeType(start, stop));
397  }
398 
399  template <typename E>
400  VectorSlice<E>
402  {
403  return VectorSlice<E>(e(), s);
404  }
405 
406  template <typename E>
407  VectorSlice<const E>
409  {
410  return VectorSlice<const E>(e(), s);
411  }
412 
413  template <typename E>
414  VectorSlice<E>
419  {
420  typedef typename VectorSlice<E>::SliceType SliceType;
421 
422  return VectorSlice<E>(e(), SliceType(start, stride, size));
423  }
424 
425  template <typename E>
426  VectorSlice<const E>
431  {
432  typedef typename VectorSlice<const E>::SliceType SliceType;
433 
434  return VectorSlice<const E>(e(), SliceType(start, stride, size));
435  }
436  } // namespace Math
437 } // namespace CDPL
438 
439 #endif // CDPL_MATH_VECTORPROXY_HPP
Definition of basic expression types.
Definition of various functors.
Definition of a data type for describing index ranges.
Definition of a data type for describing index slices.
Definition of type traits.
Implementation of vector assignment routines.
SizeType getStart() const
Definition: Range.hpp:67
bool isEmpty() const
Definition: Range.hpp:82
SizeType getSize() const
Definition: Range.hpp:77
SizeType SizeType
Definition: Range.hpp:50
SizeType SizeType
Definition: Slice.hpp:50
SizeType getStart() const
Definition: Slice.hpp:68
SizeType getSize() const
Definition: Slice.hpp:78
DifferenceType getStride() const
Definition: Slice.hpp:73
DifferenceType DifferenceType
Definition: Slice.hpp:51
bool isEmpty() const
Definition: Slice.hpp:83
Definition: Expression.hpp:54
Definition: VectorProxy.hpp:48
bool isEmpty() const
Definition: VectorProxy.hpp:101
SizeType getStart() const
Definition: VectorProxy.hpp:91
Reference operator[](SizeType i)
Definition: VectorProxy.hpp:81
VectorRange & assign(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:158
VectorClosureType & getData()
Definition: VectorProxy.hpp:106
Reference operator()(SizeType i)
Definition: VectorProxy.hpp:71
friend void swap(VectorRange &r1, VectorRange &r2)
Definition: VectorProxy.hpp:184
ConstReference operator[](SizeType i) const
Definition: VectorProxy.hpp:86
VectorRange & operator-=(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:137
V VectorType
Definition: VectorProxy.hpp:53
std::enable_if< IsScalar< T >::value, VectorRange >::type & operator/=(const T &t)
Definition: VectorProxy.hpp:151
SizeType getSize() const
Definition: VectorProxy.hpp:96
VectorRange(VectorType &v, const RangeType &r)
Definition: VectorProxy.hpp:68
SelfType ClosureType
Definition: VectorProxy.hpp:65
std::conditional< std::is_const< V >::value, typename V::ConstClosureType, typename V::ClosureType >::type VectorClosureType
Definition: VectorProxy.hpp:63
const VectorClosureType & getData() const
Definition: VectorProxy.hpp:111
VectorRange & operator=(const VectorRange &r)
Definition: VectorProxy.hpp:116
V::ConstReference ConstReference
Definition: VectorProxy.hpp:57
V::ValueType ValueType
Definition: VectorProxy.hpp:56
VectorRange & operator=(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:123
VectorRange & plusAssign(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:165
V::DifferenceType DifferenceType
Definition: VectorProxy.hpp:55
VectorRange & minusAssign(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:172
void swap(VectorRange &r)
Definition: VectorProxy.hpp:178
std::conditional< std::is_const< V >::value, typename V::ConstReference, typename V::Reference >::type Reference
Definition: VectorProxy.hpp:60
Range< SizeType > RangeType
Definition: VectorProxy.hpp:66
const SelfType ConstClosureType
Definition: VectorProxy.hpp:64
V::SizeType SizeType
Definition: VectorProxy.hpp:54
VectorRange & operator+=(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:130
std::enable_if< IsScalar< T >::value, VectorRange >::type & operator*=(const T &t)
Definition: VectorProxy.hpp:144
ConstReference operator()(SizeType i) const
Definition: VectorProxy.hpp:76
Definition: VectorProxy.hpp:196
VectorSlice & operator=(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:276
ConstReference operator()(SizeType i) const
Definition: VectorProxy.hpp:224
V::DifferenceType DifferenceType
Definition: VectorProxy.hpp:203
Slice< SizeType, DifferenceType > SliceType
Definition: VectorProxy.hpp:214
ConstReference operator[](SizeType i) const
Definition: VectorProxy.hpp:234
const VectorClosureType & getData() const
Definition: VectorProxy.hpp:264
Reference operator[](SizeType i)
Definition: VectorProxy.hpp:229
V::ValueType ValueType
Definition: VectorProxy.hpp:204
VectorSlice & plusAssign(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:318
const SelfType ConstClosureType
Definition: VectorProxy.hpp:212
VectorSlice & operator+=(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:283
std::enable_if< IsScalar< T >::value, VectorSlice >::type & operator*=(const T &t)
Definition: VectorProxy.hpp:297
SizeType getSize() const
Definition: VectorProxy.hpp:249
Reference operator()(SizeType i)
Definition: VectorProxy.hpp:219
std::conditional< std::is_const< V >::value, typename V::ConstReference, typename V::Reference >::type Reference
Definition: VectorProxy.hpp:208
bool isEmpty() const
Definition: VectorProxy.hpp:254
SizeType getStart() const
Definition: VectorProxy.hpp:239
DifferenceType getStride() const
Definition: VectorProxy.hpp:244
V VectorType
Definition: VectorProxy.hpp:201
std::enable_if< IsScalar< T >::value, VectorSlice >::type & operator/=(const T &t)
Definition: VectorProxy.hpp:304
friend void swap(VectorSlice &s1, VectorSlice &s2)
Definition: VectorProxy.hpp:337
void swap(VectorSlice &s)
Definition: VectorProxy.hpp:331
VectorClosureType & getData()
Definition: VectorProxy.hpp:259
V::SizeType SizeType
Definition: VectorProxy.hpp:202
V::ConstReference ConstReference
Definition: VectorProxy.hpp:205
VectorSlice & minusAssign(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:325
VectorSlice(VectorType &v, const SliceType &s)
Definition: VectorProxy.hpp:216
VectorSlice & operator=(const VectorSlice &s)
Definition: VectorProxy.hpp:269
std::conditional< std::is_const< V >::value, typename V::ConstClosureType, typename V::ClosureType >::type VectorClosureType
Definition: VectorProxy.hpp:211
VectorSlice & assign(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:311
VectorSlice & operator-=(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:290
SelfType ClosureType
Definition: VectorProxy.hpp:213
constexpr unsigned int V
Specifies Vanadium.
Definition: AtomType.hpp:177
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
constexpr unsigned int r
Specifies that the stereocenter has r configuration.
Definition: CIPDescriptor.hpp:76
constexpr unsigned int s
Specifies that the stereocenter has s configuration.
Definition: CIPDescriptor.hpp:81
MatrixSlice< E > slice(MatrixExpression< E > &e, const typename MatrixSlice< E >::SliceType &s1, const typename MatrixSlice< E >::SliceType &s2)
Definition: MatrixProxy.hpp:788
void vectorSwap(V &v, VectorExpression< E > &e)
Definition: VectorAssignment.hpp:72
MatrixRange< E > range(MatrixExpression< E > &e, const typename MatrixRange< E >::RangeType &r1, const typename MatrixRange< E >::RangeType &r2)
Definition: MatrixProxy.hpp:744
The namespace of the Chemical Data Processing Library.
Definition: TypeTraits.hpp:179
V::VectorTemporaryType Type
Definition: TypeTraits.hpp:181