Chemical Data Processing Library C++ API - Version 1.1.1
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
CDPL::Math::VectorTemporaryTraits
Definition: TypeTraits.hpp:179
CDPL::Math::VectorSlice::operator[]
Reference operator[](SizeType i)
Definition: VectorProxy.hpp:229
CDPL::Math::VectorSlice::operator*=
std::enable_if< IsScalar< T >::value, VectorSlice >::type & operator*=(const T &t)
Definition: VectorProxy.hpp:297
CDPL::Math::vectorSwap
void vectorSwap(V &v, VectorExpression< E > &e)
Definition: VectorAssignment.hpp:72
CDPL::Math::VectorSlice::operator=
VectorSlice & operator=(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:276
VectorAssignment.hpp
Implementation of vector assignment routines.
CDPL::Math::VectorSlice::isEmpty
bool isEmpty() const
Definition: VectorProxy.hpp:254
CDPL::Math::VectorRange::ConstReference
V::ConstReference ConstReference
Definition: VectorProxy.hpp:57
CDPL::Math::VectorRange::swap
friend void swap(VectorRange &r1, VectorRange &r2)
Definition: VectorProxy.hpp:184
CDPL::Math::VectorSlice::minusAssign
VectorSlice & minusAssign(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:325
CDPL::Math::VectorSlice::ClosureType
SelfType ClosureType
Definition: VectorProxy.hpp:213
CDPL::Math::VectorSlice::operator/=
std::enable_if< IsScalar< T >::value, VectorSlice >::type & operator/=(const T &t)
Definition: VectorProxy.hpp:304
CDPL::Math::VectorSlice::operator()
ConstReference operator()(SizeType i) const
Definition: VectorProxy.hpp:224
CDPL::Math::VectorExpression
Definition: Expression.hpp:54
CDPL::Math::VectorSlice::VectorClosureType
std::conditional< std::is_const< V >::value, typename V::ConstClosureType, typename V::ClosureType >::type VectorClosureType
Definition: VectorProxy.hpp:211
CDPL::Math::VectorRange::operator()
Reference operator()(SizeType i)
Definition: VectorProxy.hpp:71
CDPL::Math::VectorRange::isEmpty
bool isEmpty() const
Definition: VectorProxy.hpp:101
CDPL::Math::VectorRange::operator-=
VectorRange & operator-=(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:137
CDPL::Math::VectorRange::RangeType
Range< SizeType > RangeType
Definition: VectorProxy.hpp:66
CDPL::Math::VectorSlice::operator-=
VectorSlice & operator-=(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:290
CDPL::Math::VectorRange::VectorType
V VectorType
Definition: VectorProxy.hpp:53
CDPL::Math::VectorSlice::operator+=
VectorSlice & operator+=(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:283
CDPL::Math::Range< SizeType >
CDPL::Math::VectorRange::SizeType
V::SizeType SizeType
Definition: VectorProxy.hpp:54
CDPL::Math::VectorRange::operator[]
Reference operator[](SizeType i)
Definition: VectorProxy.hpp:81
CDPL::Math::slice
MatrixSlice< E > slice(MatrixExpression< E > &e, const typename MatrixSlice< E >::SliceType &s1, const typename MatrixSlice< E >::SliceType &s2)
Definition: MatrixProxy.hpp:788
CDPL::Math::VectorSlice::plusAssign
VectorSlice & plusAssign(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:318
CDPL::Math::Slice::isEmpty
bool isEmpty() const
Definition: Slice.hpp:83
CDPL::Math::VectorRange::assign
VectorRange & assign(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:158
CDPL::Math::VectorSlice::ConstReference
V::ConstReference ConstReference
Definition: VectorProxy.hpp:205
CDPL::Math::Slice< SizeType, DifferenceType >
CDPL::Math::VectorRange::ValueType
V::ValueType ValueType
Definition: VectorProxy.hpp:56
CDPL::Math::VectorSlice::VectorType
V VectorType
Definition: VectorProxy.hpp:201
CDPL::Math::VectorRange::ConstClosureType
const SelfType ConstClosureType
Definition: VectorProxy.hpp:64
CDPL::Math::Range< SizeType >::SizeType
SizeType SizeType
Definition: Range.hpp:50
CDPL::Math::VectorRange::operator=
VectorRange & operator=(const VectorRange &r)
Definition: VectorProxy.hpp:116
CDPL::Math::VectorRange::VectorClosureType
std::conditional< std::is_const< V >::value, typename V::ConstClosureType, typename V::ClosureType >::type VectorClosureType
Definition: VectorProxy.hpp:63
TypeTraits.hpp
Definition of type traits.
CDPL::Math::VectorSlice::operator()
Reference operator()(SizeType i)
Definition: VectorProxy.hpp:219
CDPL::Math::VectorSlice::Reference
std::conditional< std::is_const< V >::value, typename V::ConstReference, typename V::Reference >::type Reference
Definition: VectorProxy.hpp:208
CDPL::Math::VectorRange::operator+=
VectorRange & operator+=(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:130
CDPL::Math::VectorRange::minusAssign
VectorRange & minusAssign(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:172
V
Functional.hpp
Definition of various functors.
CDPL::Math::Range::isEmpty
bool isEmpty() const
Definition: Range.hpp:82
CDPL::Math::VectorRange::VectorRange
VectorRange(VectorType &v, const RangeType &r)
Definition: VectorProxy.hpp:68
CDPL::Math::VectorRange::DifferenceType
V::DifferenceType DifferenceType
Definition: VectorProxy.hpp:55
CDPL::Math::VectorSlice::getData
VectorClosureType & getData()
Definition: VectorProxy.hpp:259
CDPL::Math::Slice< SizeType, DifferenceType >::SizeType
SizeType SizeType
Definition: Slice.hpp:50
CDPL::Chem::AtomType::T
const unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
CDPL::Math::VectorRange::getStart
SizeType getStart() const
Definition: VectorProxy.hpp:91
CDPL::Chem::CIPDescriptor::s
const unsigned int s
Specifies that the stereocenter has s configuration.
Definition: CIPDescriptor.hpp:81
CDPL::Chem::CIPDescriptor::r
const unsigned int r
Specifies that the stereocenter has r configuration.
Definition: CIPDescriptor.hpp:76
CDPL::Math::VectorRange::operator()
ConstReference operator()(SizeType i) const
Definition: VectorProxy.hpp:76
CDPL::Math::VectorSlice::SliceType
Slice< SizeType, DifferenceType > SliceType
Definition: VectorProxy.hpp:214
CDPL::Math::Slice::getStride
DifferenceType getStride() const
Definition: Slice.hpp:73
CDPL::Math::VectorSlice::VectorSlice
VectorSlice(VectorType &v, const SliceType &s)
Definition: VectorProxy.hpp:216
CDPL::Math::Slice::getStart
SizeType getStart() const
Definition: Slice.hpp:68
CDPL::Math::VectorRange::getData
const VectorClosureType & getData() const
Definition: VectorProxy.hpp:111
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Math::VectorSlice::getStart
SizeType getStart() const
Definition: VectorProxy.hpp:239
CDPL::Math::Slice< SizeType, DifferenceType >::DifferenceType
DifferenceType DifferenceType
Definition: Slice.hpp:51
CDPL::Math::VectorRange::operator=
VectorRange & operator=(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:123
CDPL::Math::VectorRange::getData
VectorClosureType & getData()
Definition: VectorProxy.hpp:106
CDPL::Math::VectorSlice::SizeType
V::SizeType SizeType
Definition: VectorProxy.hpp:202
CDPL::Math::VectorSlice::ValueType
V::ValueType ValueType
Definition: VectorProxy.hpp:204
CDPL::Math::VectorSlice::assign
VectorSlice & assign(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:311
CDPL::Math::VectorSlice::ConstClosureType
const SelfType ConstClosureType
Definition: VectorProxy.hpp:212
CDPL::Math::VectorRange::ClosureType
SelfType ClosureType
Definition: VectorProxy.hpp:65
CDPL::Math::VectorRange::Reference
std::conditional< std::is_const< V >::value, typename V::ConstReference, typename V::Reference >::type Reference
Definition: VectorProxy.hpp:60
CDPL::Math::VectorRange::operator[]
ConstReference operator[](SizeType i) const
Definition: VectorProxy.hpp:86
CDPL::Math::VectorSlice::swap
void swap(VectorSlice &s)
Definition: VectorProxy.hpp:331
CDPL::Math::VectorSlice::swap
friend void swap(VectorSlice &s1, VectorSlice &s2)
Definition: VectorProxy.hpp:337
CDPL::Math::VectorSlice::DifferenceType
V::DifferenceType DifferenceType
Definition: VectorProxy.hpp:203
CDPL::Math::VectorRange::plusAssign
VectorRange & plusAssign(const VectorExpression< E > &e)
Definition: VectorProxy.hpp:165
CDPL::Math::VectorSlice::operator[]
ConstReference operator[](SizeType i) const
Definition: VectorProxy.hpp:234
CDPL::Math::VectorRange::operator/=
std::enable_if< IsScalar< T >::value, VectorRange >::type & operator/=(const T &t)
Definition: VectorProxy.hpp:151
CDPL::Math::VectorRange::getSize
SizeType getSize() const
Definition: VectorProxy.hpp:96
CDPL::Math::VectorRange::swap
void swap(VectorRange &r)
Definition: VectorProxy.hpp:178
CDPL::Math::VectorRange
Definition: VectorProxy.hpp:48
CDPL::Math::Range::getSize
SizeType getSize() const
Definition: Range.hpp:77
Slice.hpp
Definition of a data type for describing index slices.
Expression.hpp
Definition of basic expression types.
CDPL::Math::range
MatrixRange< E > range(MatrixExpression< E > &e, const typename MatrixRange< E >::RangeType &r1, const typename MatrixRange< E >::RangeType &r2)
Definition: MatrixProxy.hpp:744
CDPL::Math::VectorSlice::getStride
DifferenceType getStride() const
Definition: VectorProxy.hpp:244
CDPL::Math::VectorSlice::operator=
VectorSlice & operator=(const VectorSlice &s)
Definition: VectorProxy.hpp:269
Range.hpp
Definition of a data type for describing index ranges.
CDPL::Math::VectorSlice::getData
const VectorClosureType & getData() const
Definition: VectorProxy.hpp:264
CDPL::Math::Slice::getSize
SizeType getSize() const
Definition: Slice.hpp:78
CDPL::Math::VectorSlice
Definition: VectorProxy.hpp:196
CDPL::Math::VectorTemporaryTraits::Type
V::VectorTemporaryType Type
Definition: TypeTraits.hpp:181
CDPL::Math::Range::getStart
SizeType getStart() const
Definition: Range.hpp:67
CDPL::Math::VectorRange::operator*=
std::enable_if< IsScalar< T >::value, VectorRange >::type & operator*=(const T &t)
Definition: VectorProxy.hpp:144
CDPL::Math::VectorSlice::getSize
SizeType getSize() const
Definition: VectorProxy.hpp:249